Author Topic: PutText  (Read 3136 times)

0 Members and 1 Guest are viewing this topic.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
PutText
« on: November 23, 2021, 12:21:00 pm »
For all you SCREEN 0 herpes out there like Pete, here's PutText -- it's like PutImage, but for TextScreens!

Code: QB64: [Select]
  1. FooBar = _NewImage(10, 5, 0)
  2. _Dest FooBar
  3. Color 4, 15: Locate 1, 1: Print "**********";
  4. Color 15, 4: Locate 2, 1: Print "*-Foobar-*";
  5. Color 4, 15: Locate 3, 1: Print "**********";
  6. Color 2, 0: Locate 4, 1: Print "**********";
  7. Locate 5, 1: Print "**********";
  8.  
  9. PutText "(10,1)", FooBar, 0, "(1,1) to (10,3)"
  10.  
  11.  
  12. 'let's try to put the text at an off image coordinate
  13. PutText "(0,0)", FooBar, 0, "(1,1) to  (10,5)"
  14.  
  15.  
  16. 'or off the right of the screen
  17. PutText "(75,20)", FooBar, 0, "(1,1) to step (9,2)"
  18.  
  19. _Delay .25
  20.  
  21. x = 80
  22.     Cls
  23.     PutText Str$(x) + ",3", FooBar, 0, "(1,1) to step (9,2)"
  24.     _Limit 30
  25.     _Display
  26.     x = x - 1
  27.     If x < -10 Then x = 80
  28.  
  29.  
  30. Sub PutText (dest$, source, dest, source$)
  31.     CurrentDest = _Dest: CurrentSource = _Source
  32.     Type PutType
  33.         x1 As Integer
  34.         y1 As Integer
  35.         x2 As Integer
  36.         y2 As Integer
  37.     End Type
  38.     Dim As PutType d, s
  39.  
  40.  
  41.     ParsePutParam dest$, d
  42.     ParsePutParam source$, s
  43.  
  44.     _Source source: _Dest dest
  45.     For x = s.x1 To s.x2 'Step Sgn(s.x2 - s.x1)
  46.         For y = s.y1 To s.y2 'Step Sgn(s.y2 - s.y1)
  47.             If x > 0 And x <= _Width(source) And y > 0 And y <= _Height(source) Then
  48.                 p = Screen(y, x, 0)
  49.                 c = Screen(y, x, 1)
  50.                 x1 = d.x1 + x - s.x1: y1 = d.y1 + y - s.y1
  51.                 If x1 > 0 And x1 <= _Width(dest) And y1 > 0 And y1 <= _Height(dest) Then
  52.                     Color c Mod 16, c \ 16
  53.                     _PrintString (x1, y1), Chr$(p)
  54.                 End If
  55.             End If
  56.         Next
  57.     Next
  58.     _Dest CurrentDest: _Source CurrentSource
  59.     Color DC, BG
  60.  
  61. Sub ParsePutParam (param$, o As PutType)
  62.     p$ = UCase$(_Trim$(param$))
  63.     strip$ = "() "
  64.     For j = 1 To Len(strip$)
  65.         Do
  66.             i = InStr(p$, Mid$(strip$, j, 1))
  67.             If i Then p$ = Left$(p$, i - 1) + Mid$(p$, i + 1)
  68.         Loop Until i = 0
  69.     Next
  70.     o.x1 = Val(p$)
  71.     comma = InStr(p$, ",")
  72.     If comma Then p$ = Mid$(p$, comma + 1) Else Exit Sub
  73.  
  74.     o.y1 = Val(p$)
  75.     p$ = Mid$(p$, Len(_Trim$(Str$(o.y1))) + 1)
  76.  
  77.     If Left$(p$, 2) = "TO" Then p$ = Mid$(p$, 3) Else Exit Sub
  78.  
  79.     If Left$(p$, 4) = "STEP" Then
  80.         o.x2 = o.x1: o.y2 = o.y1: p$ = Mid$(p$, 5)
  81.     Else
  82.         o.x2 = 0: o.y2 = 0
  83.     End If
  84.  
  85.     o.x2 = o.x2 + Val(p$)
  86.     comma = InStr(p$, ",")
  87.  
  88.     If comma Then p$ = Mid$(p$, comma + 1) Else Exit Sub
  89.     o.y2 = o.y2 + Val(p$)
  90.  

I was going to build this to  work more like PutImage, but then I realized that wouldn't be possible with a text screen.  For instance, there's no way to stretch or scale the "image" here, since all text screens are stuck with a single font in use at a time, so I figured all I really needed was a singular destination point (x,y) rather than (x,y) to (x2,y2).   

Get the portion of text you want, put it where you want it.  It's that simple.

Another little change is that I tweaked the syntax to use "TO" instead of a dash ("-") to separate points. You never know -- somebody, somewhere, might be using WINDOW to work with negative screen coordinates, so I didn't want to run into issues with that dash getting screwed up with the parsing of a negative location.   "(10,10) TO STEP (-10,0)", for an example.

Currently I'm only printing text in an incremental fashion (from position 0 to 10, for instance rather than from 10 to 0), but I do plan on adding in the optionality later for bidirectional putting of text so we can reverse and flip text as wanted with one little command.  (I'll just need to wake up enough to sort out how to do it first!  LOL!)
 
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: PutText
« Reply #1 on: November 23, 2021, 04:42:04 pm »
We now can flip and reverse text, just like we can with _PUTIMAGE:

Code: QB64: [Select]
  1. FooBar = _NewImage(10, 5, 0)
  2. _Dest FooBar
  3. Color 4, 15: Locate 1, 1: Print "1********1";
  4. Color 15, 4: Locate 2, 1: Print "2-Foobar-2";
  5. Color 4, 15: Locate 3, 1: Print "3********3";
  6. Color 2, 0: Locate 4, 1: Print "4********4";
  7. Locate 5, 1: Print "*5*******5";
  8.  
  9. PutText "(10,1)", FooBar, 0, "(1,1) to (10,3)"
  10.  
  11.  
  12. 'let's try to put the text backwards
  13. PutText "(30,1)", FooBar, 0, "(10,1) to  (1,3)"
  14.  
  15.  
  16. 'next test is flipping from bottom to top
  17. PutText "(50,1)", FooBar, 0, "(1,3) to (10,1)"
  18.  
  19. _Delay .25
  20.  
  21. x = 80
  22.     Cls
  23.     PutText Str$(x) + ",3", FooBar, 0, "(10,1) to step (-9,2)"
  24.     _Limit 30
  25.     _Display
  26.     x = x - 1
  27.     If x < -10 Then x = 80
  28.  
  29.  
  30. Sub PutText (dest$, source, dest, source$)
  31.     CurrentDest = _Dest: CurrentSource = _Source
  32.     Type PutType
  33.         x1 As Integer
  34.         y1 As Integer
  35.         x2 As Integer
  36.         y2 As Integer
  37.     End Type
  38.     Dim As PutType d, s
  39.  
  40.  
  41.     ParsePutParam dest$, d
  42.     ParsePutParam source$, s
  43.  
  44.     _Source source: _Dest dest
  45.     For x = s.x1 To s.x2 Step Sgn(s.x2 - s.x1)
  46.         y0 = 0
  47.         For y = s.y1 To s.y2 Step Sgn(s.y2 - s.y1)
  48.             If x > 0 And x <= _Width(source) And y > 0 And y <= _Height(source) Then
  49.                 p = Screen(y, x, 0)
  50.                 c = Screen(y, x, 1)
  51.                 x1 = d.x1 + x0: y1 = d.y1 + y0
  52.                 If x1 > 0 And x1 <= _Width(dest) And y1 > 0 And y1 <= _Height(dest) Then
  53.                     Color c Mod 16, c \ 16
  54.                     _PrintString (x1, y1), Chr$(p)
  55.                 End If
  56.             End If
  57.             y0 = y0 + 1
  58.         Next
  59.         x0 = x0 + 1
  60.     Next
  61.     _Dest CurrentDest: _Source CurrentSource
  62.     Color DC, BG
  63.  
  64. Sub ParsePutParam (param$, o As PutType)
  65.     p$ = UCase$(_Trim$(param$))
  66.     strip$ = "() "
  67.     For j = 1 To Len(strip$)
  68.         Do
  69.             i = InStr(p$, Mid$(strip$, j, 1))
  70.             If i Then p$ = Left$(p$, i - 1) + Mid$(p$, i + 1)
  71.         Loop Until i = 0
  72.     Next
  73.     o.x1 = Val(p$)
  74.     comma = InStr(p$, ",")
  75.     If comma Then p$ = Mid$(p$, comma + 1) Else Exit Sub
  76.  
  77.     o.y1 = Val(p$)
  78.     p$ = Mid$(p$, Len(_Trim$(Str$(o.y1))) + 1)
  79.  
  80.     If Left$(p$, 2) = "TO" Then p$ = Mid$(p$, 3) Else Exit Sub
  81.  
  82.     If Left$(p$, 4) = "STEP" Then
  83.         o.x2 = o.x1: o.y2 = o.y1: p$ = Mid$(p$, 5)
  84.     Else
  85.         o.x2 = 0: o.y2 = 0
  86.     End If
  87.  
  88.     o.x2 = o.x2 + Val(p$)
  89.     comma = InStr(p$, ",")
  90.  
  91.     If comma Then p$ = Mid$(p$, comma + 1) Else Exit Sub
  92.     o.y2 = o.y2 + Val(p$)
  93.  

  [ You are not allowed to view this attachment ]  

Is there anything I'm overlooking for a text-style routine to mimic _PUTIMAGE?  It seems to me that this handles everything I can think to toss at it.  Selectable images/screens to copy/paste to; forward, backward, reverse, and bottom to top text; with safety built in for off-screen coordinates so it doesn't toss errors...   What am I missing?  If anything?
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Dav

  • Forum Resident
  • Posts: 792
    • View Profile
Re: PutText
« Reply #2 on: November 23, 2021, 06:51:17 pm »
That's pretty slick!  I keep forgetting TYPE can be used inside a SUB.

Works fine here.

- Dav