Author Topic: Read character unter cursor  (Read 3024 times)

0 Members and 1 Guest are viewing this topic.

Offline Spacecaptain

  • Newbie
  • Posts: 2
  • What Kind of Man Owns his Own Computer.
    • View Profile
Read character unter cursor
« on: May 19, 2020, 06:14:01 am »
Is there any option to read the ascii-character at a specific screen location?
I would like to read and save a specific area of the text screen into an array before placing another text/window (a progress bar, for example) over it so I am able to restore this area afterwards.
Any help is appreciated!


Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: Read character unter cursor
« Reply #1 on: May 19, 2020, 07:09:44 am »
Long time ago, when I still used SCREEN 0 regulary, I made this two SUBs to save/restore screen areas. Note these SUBs are especially made for mode 0 (text only) screens, on other modes it probably won't respect the blinking colors.

see also http://www.qb64.org/wiki/SCREEN_(function)

Code: QB64: [Select]
  1. '---------------------------------------------------------------------
  2. 'Function:  Saving any text screen area for later restore, eg. any
  3. '           area which is temporary covered/overwritten by other things.
  4. '
  5. 'Synopsis:  GetRefresh dat$, lin%, col%, hei%, wid%
  6. '
  7. 'Inputs:    dat$ --> this variable will become the ID for the specified
  8. '                    area, don't change its content until the area was
  9. '                    restored via PutRefresh() and you no longer need it
  10. '                    for further refresh calls
  11. '           lin% --> text line of top/left edge of the area
  12. '           col% --> text column of top/left edge of the area
  13. '           hei% --> height of the area as text lines
  14. '           wid% --> width of the area as text columns
  15. '
  16. 'Notes:     Note the intended side effect on the argument "dat$".
  17. '---------------------------------------------------------------------
  18. SUB GetRefresh (dat$, lin%, col%, hei%, wid%)
  19. '--- save dimensions ---
  20. dat$ = CHR$(lin%) + CHR$(col%) + CHR$(hei%) + CHR$(wid%)
  21. '--- save screen data ---
  22. FOR y% = lin% TO lin% + hei% - 1
  23.     FOR x% = col% TO col% + wid% - 1
  24.         dat$ = dat$ + CHR$(SCREEN(y%, x%)) + CHR$(SCREEN(y%, x%, 1))
  25.     NEXT x%
  26. NEXT y%
  27.  
  28. '---------------------------------------------------------------------
  29. 'Function:  Restore a text screen area saved earlier by GetRefresh().
  30. '           Note that the actual PRINT (cursor) position and COLOR is
  31. '           changed after this call, so you have to use LOCATE and
  32. '           COLOR to designate your next PRINT position and color.
  33. '
  34. 'Synopsis:  PutRefresh dat$
  35. '
  36. 'Inputs:    dat$ --> the same (and still unchanged) variable you did use
  37. '                    as "dat$" for the respective GetRefresh() call.
  38. '---------------------------------------------------------------------
  39. SUB PutRefresh (dat$)
  40. '--- get dimensions ---
  41. lin% = ASC(MID$(dat$, 1, 1))
  42. col% = ASC(MID$(dat$, 2, 1))
  43. hei% = ASC(MID$(dat$, 3, 1))
  44. wid% = ASC(MID$(dat$, 4, 1))
  45. wc& = 5
  46. '--- restore screen data ---
  47. FOR y% = lin% TO lin% + hei% - 1
  48.     FOR x% = col% TO col% + wid% - 1
  49.         co% = ASC(MID$(dat$, wc& + 1, 1))
  50.         fg% = (co% AND &HF)
  51.         bg% = (co% AND &HF0) / 16
  52.         IF bg% > 7 THEN 'MSB set in the bg% nibble indicates blinking fg%
  53.             fg% = fg% + 16
  54.             bg% = bg% - 8
  55.         END IF
  56.         LOCATE y%, x%, 0
  57.         COLOR fg%, bg%
  58.         PRINT MID$(dat$, wc&, 1);
  59.         wc& = wc& + 2
  60.     NEXT x%
  61. NEXT y%
  62.  
  63.  

P.S. - Und Grüße aus'm Harz nach Berlin :)
« Last Edit: May 19, 2020, 09:23:44 am by RhoSigma »
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Read character unter cursor
« Reply #2 on: May 19, 2020, 09:12:39 am »
Easiest solution to what it sounds like you're doing is PCOPY.


PRINT "Some junk on the screen"
PRINT "And some more junk"
PCOPY 0, 1
PRINT "PRESS <ANY KEY> TO ERASE THE SCREEN"
SLEEP
CLS
PRINT "It's all erased!  Press <ANY KEY> to restore the original>
SLEEP
PCOPY 1, 0
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Spacecaptain

  • Newbie
  • Posts: 2
  • What Kind of Man Owns his Own Computer.
    • View Profile
Re: Read character unter cursor
« Reply #3 on: May 19, 2020, 09:24:22 am »
Excellent - thanks a lot for the quick reply!

Und Grüße zurück in den Harz ;-)