Author Topic: Question about text attributes in SCREEN 0  (Read 3512 times)

0 Members and 1 Guest are viewing this topic.

Offline LechU

  • Newbie
  • Posts: 2
    • View Profile
Question about text attributes in SCREEN 0
« on: March 04, 2019, 07:07:10 am »
Two simple(?) questions:

1. Is it possible to print something on the screen (SCREEN 0) WITHOUT altering old attribytes at cursor position?
2. Is it possible to change attributes of text already displayed at screen (SCREEN 0)?



Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Question about text attributes in SCREEN 0
« Reply #1 on: March 04, 2019, 10:11:25 am »
I suppose the attribute of color comes to mind. You could change that attribute and then rewrite the text to the screen, but a long time ago, I recall doing something with PEEK/POKE to change those attributes, and, if I remember correctly, once the new values were poked into memory, the text already on the screen changed color. Well, it has been over 20 years, so I may be mistaken. Maybe some sort of screen refresh procedure was needed as well. Anyway, it was an interesting concept, which I never needed. IS there something like it in QB64? Maybe. Again, I've never needed a way to change text attributes without rewriting the text, text line, or screen, so I wouldn't know. Rewrites are so fast, it doesn't seem necessary to do it any other way. It will be interesting to see if there is a Qb64, GL, Windows API call, or PEKK/POKE method that is still valid to do what I think you are inquiring about. If this isn't what you mean, please provide a more detailed explanation of what you are trying to achieve, or wait for additional responses.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Question about text attributes in SCREEN 0
« Reply #2 on: March 04, 2019, 10:55:22 am »
Sounds to me as if you're looking for the _MEM commands:

Code: QB64: [Select]
  1.  
  2. PRINT "Hello World"
  3. SLEEP 'so we can see the original text
  4.  
  5. m = _MEMIMAGE(0)
  6.  
  7. _MEMPUT m, m.OFFSET, 4 AS _UNSIGNED _BYTE 'first, to change the character at 1,1 (Top Left of screen; it should be "H")
  8. SLEEP 'so we can see the change, as we changed the "H" to CHR$(4) --- ""
  9.  
  10. _MEMPUT m, m.OFFSET + 3 * 2 + 1, 4 AS _UNSIGNED _BYTE 'now let's try to change the "3rd" character's color to RED (color value 4)
  11. 'If you notice, we actually changed the FOURTH characcter's color to red (the second "l" in "Hello World".
  12. 'This makes sense once you realize that memory is mapped starting at 0,0; whereas LOCATE starts at 1,1 in SCREEN 0.
  13. 'To work with any particular point for SCREEN 0 memory, we need to remember:
  14.  
  15. '1) Screen Information is stored in 2 bytes -- First Byte is the character.
  16. '                                              Second Byte is the color code.
  17.  
  18. '2) To convert the screen coordinates to memory coordinates, use the simple formula:
  19. '                                              CharacterMemoryPosition = ((Column -1) * _WIDTH + Row -1)) * 2
  20. '                                              ColorMemoryPosition =     ((Column -1) * _WIDTH + Row -1)) * 2 + 1
  21.  
  22. '3) Next tip is to always remember to add the value to m.Offset which is QB64's way to track where the screen information starts in memory
  23.  
  24.  
  25. 'So to make Row 2, Column 1 become a "W", we'd do the following:
  26. CharacterMemoryPosition = ((2 - 1) * _WIDTH + 1 - 1) * 2
  27. SLEEP 'so we can wait and see the W appear where we want it
  28. _MEMPUT m, m.OFFSET + CharacterMemoryPosition, "W"
  29. 'And to change that W to being blue, we'd do the following:
  30. ColorMemoryPosition = ((2 - 1) * _WIDTH + 1 - 1) * 2 + 1
  31. SLEEP 'to see the change one step at a time
  32. _MEMPUT m, m.OFFSET + ColorMemoryPosition, 1 AS _UNSIGNED _BYTE
  33.  
  34.  

https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Question about text attributes in SCREEN 0
« Reply #3 on: March 04, 2019, 11:23:34 am »
I think if I ever decide to look into more QB64 or GL commands, I'd start with _MEM. It's like it's not your grandma's PEEK and POKE. Careful, Steve! :D

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline LechU

  • Newbie
  • Posts: 2
    • View Profile
Re: Question about text attributes in SCREEN 0
« Reply #4 on: March 04, 2019, 02:53:09 pm »
Sounds to me as if you're looking for the _MEM commands:

Code: QB64: [Select]
  1.  
  2. PRINT "Hello World"
  3. SLEEP 'so we can see the original text
  4.  
  5. m = _MEMIMAGE(0)
  6.  
  7. _MEMPUT m, m.OFFSET, 4 AS _UNSIGNED _BYTE 'first, to change the character at 1,1 (Top Left of screen; it should be "H")
  8. SLEEP 'so we can see the change, as we changed the "H" to CHR$(4) --- ""
  9.  
  10. _MEMPUT m, m.OFFSET + 3 * 2 + 1, 4 AS _UNSIGNED _BYTE 'now let's try to change the "3rd" character's color to RED (color value 4)
  11. 'If you notice, we actually changed the FOURTH characcter's color to red (the second "l" in "Hello World".
  12. 'This makes sense once you realize that memory is mapped starting at 0,0; whereas LOCATE starts at 1,1 in SCREEN 0.
  13. 'To work with any particular point for SCREEN 0 memory, we need to remember:
  14.  
  15. '1) Screen Information is stored in 2 bytes -- First Byte is the character.
  16. '                                              Second Byte is the color code.
  17.  
  18. '2) To convert the screen coordinates to memory coordinates, use the simple formula:
  19. '                                              CharacterMemoryPosition = ((Column -1) * _WIDTH + Row -1)) * 2
  20. '                                              ColorMemoryPosition =     ((Column -1) * _WIDTH + Row -1)) * 2 + 1
  21.  
  22. '3) Next tip is to always remember to add the value to m.Offset which is QB64's way to track where the screen information starts in memory
  23.  
  24.  
  25. 'So to make Row 2, Column 1 become a "W", we'd do the following:
  26. CharacterMemoryPosition = ((2 - 1) * _WIDTH + 1 - 1) * 2
  27. SLEEP 'so we can wait and see the W appear where we want it
  28. _MEMPUT m, m.OFFSET + CharacterMemoryPosition, "W"
  29. 'And to change that W to being blue, we'd do the following:
  30. ColorMemoryPosition = ((2 - 1) * _WIDTH + 1 - 1) * 2 + 1
  31. SLEEP 'to see the change one step at a time
  32. _MEMPUT m, m.OFFSET + ColorMemoryPosition, 1 AS _UNSIGNED _BYTE
  33.  
  34.  

VERY BIG THANKS.
This is exactly what I'm looking for.
THANKS AGAIN.


But. Are You raelly sure that:

        CharacterMemoryPosition = ((Column - 1) * _WIDTH + Row - 1) * 2
        ColorMemoryPosition =     ((Column - 1) * _WIDTH + Row - 1) * 2 + 1


not:

        CharacterMemoryPosition = (Column - 1 + _WIDTH * (Row - 1)) * 2
        ColorMemoryPosition =     (Column - 1 * _WIDTH * (Row - 1)) * 2 + 1


Row and Column swapped?

« Last Edit: March 04, 2019, 04:23:41 pm by LechU »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Question about text attributes in SCREEN 0
« Reply #5 on: March 04, 2019, 08:51:26 pm »
In graphics, it’s ((y * _WIDTH) + x), so I’ve probably reversed column/row, as I usually work and think in graphical mode coordinates.

LOCATE (row, column) translates to graphical (y, x), so it’d actually be:

((Row * _WIDTH) + Column) * m.ELEMENTSIZE

So you’re right, I got it backwards.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Question about text attributes in SCREEN 0
« Reply #6 on: March 04, 2019, 09:38:33 pm »

But. Are You raelly sure that:


Woe, speaking of backwards... Raelly? Oh well, it's not as funny as a clock typo, but I'll take it!

You know before _MEM came along, I had a graphics text screen program that mapped all color and style attributes with a screen reading multi-dimensional array function. _MEM reminds me of that. Steve, I'll have a look at your code sometime soon. It looks like fun.

Pete :D
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline Cobalt

  • QB64 Developer
  • Forum Resident
  • Posts: 878
  • At 60 I become highly radioactive!
    • View Profile
Re: Question about text attributes in SCREEN 0
« Reply #7 on: March 05, 2019, 11:09:58 am »
yeah its a very very simple POKE setup;
 DEF SEG = &HB800
  POKE 0 + xloc% + (yloc% * 160), Char%
  POKE 1 + xloc% + (yloc% * 160), colr%
 DEF SEG

or a little more involved, and I mean a little;
Code: QB64: [Select]
  1. SUB reaprint (Layer%, x%, y%, text$, col%)
  2.  DEF SEG = &HB800
  3.  l% = Layer% * 4096
  4.  xloc% = x% * 2
  5.  yloc% = y%
  6.  IF LEN(LTRIM$(RTRIM$(text$))) > 1 THEN
  7.   FOR I% = 1 TO LEN(text$)
  8.    t$ = MID$(text$, I%, 1)
  9.    t% = ASC(t$)
  10.    POKE l% + 0 + xloc% + (yloc% * 160), t%
  11.    POKE l% + 1 + xloc% + (yloc% * 160), col%
  12.    xloc% = xloc% + 2
  13.    IF xloc% = 160 THEN xloc% = 0: yloc% = yloc% + 1
  14.   NEXT I%
  15.  ELSEIF LEN(text$) = 1 THEN
  16.   t% = ASC(text$)
  17.   POKE l% + 0 + xloc% + (yloc% * 160), t%
  18.   POKE l% + 1 + xloc% + (yloc% * 160), col%
Granted after becoming radioactive I only have a half-life!

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: Question about text attributes in SCREEN 0
« Reply #8 on: March 05, 2019, 06:55:02 pm »
Hi Cobalt
fine sub but without documentation... so I have tried it and now can I write this syntax

Quote
REAPRINT (Layer%,X%,Y%, text$, col%)

this sub PRINT on the screen 0 a text with the declared color passed with col%
Parameters:
Layer% is 0 for simple print on the screen
X% starts from 0(column to start to print) , Y% starts from 0 (row to start to print) so you must substract 1 from these cohordinates
Col% is the number of the color used in Screen 0 (range 0-255)

and as a good help must be here and example code

Code: QB64: [Select]
  1. PRINT "Hi I try reaprint,I imagine that first layer% is 0"
  2. reaprint 0, 4, 1, "I", 14
  3. reaprint 0, 6, 1, "t", 4
  4. reaprint 0, 8, 1, "u", 29
  5.  
  6. PRINT "Hi I try reaprint with parameter -1 for x% and y%"
  7. reaprint 0, (4 - 1), (1 - 1), "I", 14
  8. reaprint 0, (6 - 1), (1 - 1), "t", 4
  9. reaprint 0, (8 - 1), (1 - 1), "u", 245
  10. FOR a% = 0 TO 255
  11.     LOCATE 10, 5: PRINT "Col%"; a%
  12.     reaprint 0, 4, 10, "W E", a%
  13.     _DELAY .3
  14.  
Programming isn't difficult, only it's  consuming time and coffee