Author Topic: Scrollable output in QB64  (Read 5103 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
Scrollable output in QB64
« on: August 06, 2019, 09:42:46 pm »
As per a question from chrisowen09 from the irc channel, here's a few brief demos of how you can create scrollable output for a  program:


First, and often easiest way is to simply dump the output to the $CONSOLE -- it's scrollable.
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(640, 480, 32)
  2. PRINT "Stuff to screen"
  3. LINE (100, 100)-(200, 200), &HFFF0F000, BF
  4. PRINT "And now I have stuff which I might need to scroll, such as for diagnostics...."
  5. FOR i = 1 TO 100 'Use _DEST to print to console
  6.     _DEST _CONSOLE: PRINT "Diagnose this line #"; i 'Print to the console
  7.     _DEST 0: PRINT "Diagnose this line #"; i 'and the main screen
  8. PRINT "If you notice, you can scroll from the console automatically..."
  9. PRINT "It's the easiest way to handle scroll events, as it's native for us."
  10. FOR i = 1 TO 100 'And a second method to print to console, using ECHO
  11.     _ECHO "Also Diagnose this line #" + STR$(i) 'ECHO to the console
  12.     PRINT "Also Diagnose this line #"; i 'and the main screen



A second method is to write a simple scroll routine yourself to display the data -- here's one which uses U and D to scroll up and down:

Code: QB64: [Select]
  1. DIM array(100) AS STRING
  2.  
  3. OPEN "temp.txt" FOR OUTPUT AS #1
  4. FOR i = 1 TO 100
  5.     PRINT #1, "Junk Line of Input #"; i
  6.  
  7. OPEN "temp.txt" FOR BINARY AS #1 'Load the data
  8. l = LOF(1) 'all of it at once
  9. junk$ = SPACE$(l)
  10. GET #1, 1, junk$ 'The file is now in junk$
  11.  
  12.  
  13. ParseData array(), junk$ 'And we parse junk$ into our array
  14.  
  15. ScrollableDisplay 1, 100, array()
  16.  
  17. SUB ScrollableDisplay (start, finish, array() AS STRING)
  18.     IF start < LBOUND(array) THEN start = LBOUND(array)
  19.     IF finish > UBOUND(array) THEN finish = UBOUND(array)
  20.     maxcount = _HEIGHT - 2
  21.     DO
  22.         CLS
  23.         FOR i = start TO finish
  24.             PRINT i, array(i)
  25.             IF i - start >= maxcount THEN EXIT FOR
  26.         NEXT
  27.         i$ = UCASE$(INKEY$)
  28.         SELECT CASE i$
  29.             CASE "U" 'scroll up
  30.                 IF start > LBOUND(array) THEN start = start - 1
  31.             CASE "D"
  32.                 IF start < finish - maxcount THEN start = start + 1
  33.         END SELECT
  34.         _LIMIT 30
  35.         _DISPLAY
  36.     LOOP
  37.  
  38. SUB ParseData (array() AS STRING, text$)
  39.     temp$ = text$
  40.     IF INSTR(text$, CHR$(13)) THEN CRLF = 2 ELSE CRLF = 1
  41.     DO
  42.         IF l >= LEN(temp$) THEN EXIT DO
  43.         l = INSTR(temp$, CHR$(10))
  44.         count = count + 1
  45.         array(count) = LEFT$(temp$, l - CRLF)
  46.         temp$ = MID$(temp$, l + 1)
  47.     LOOP

Neither are really that difficult to code, but $CONSOLE and _ECHO are definitely the quickest and simplest way to toss data to a program, for diagnostic purposes usually.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Scrollable output in QB64
« Reply #1 on: August 07, 2019, 12:02:13 am »
Pretty interesting! You also can output your data to a .txt file and use Notepad to scroll it, like I've done a few times. The trick though is to count how many words you want per-line and then add a print #1, " "   after that to move the data to the next line. This makes an Example.txt file and counts to 1000 numbers on the file with 10 per line. Notice how I also used INT (integer) with some math to make it go to the next line when the number is divisible by 10.

Code: QB64: [Select]
  1. OPEN "Example.txt" FOR OUTPUT AS #1
  2. FOR NUMBERS = 1 TO 1000
  3.     PRINT NUMBERS; " ";
  4.     PRINT #1, NUMBERS; " ";
  5.     IF NUMBERS / 10 = INT(NUMBERS / 10) THEN PRINT #1, " ": PRINT
  6. NEXT NUMBERS
  7.  
  8.  
« Last Edit: August 07, 2019, 12:16:50 am by SierraKen »

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: Scrollable output in QB64
« Reply #2 on: August 07, 2019, 06:12:15 am »
Ah, now I see what the console is for. I always wondered what was so great about a console. If I had a console for every time I wanted to examine output that went off screen while I was blinking...

While on the subject, here's how I generally handle large data arrays with a mouse wheel scroll.

Code: QB64: [Select]
  1. TYPE nmlst
  2.     a AS INTEGER
  3.     b AS INTEGER
  4.     c AS INTEGER
  5.     d AS STRING * 4
  6.  
  7. DIM SHARED Datr(100) AS nmlst
  8. FOR x = 1 TO 100
  9.     Datr(x).a = x: Datr(x).b = x * 2: Datr(x).c = x * 5
  10.     IF Datr(x).a MOD 2 = 0 THEN
  11.         Datr(x).d = "even"
  12.     ELSE
  13.         Datr(x).d = "odd"
  14.     END IF
  15.  
  16. IF UBOUND(Datr) > 20 THEN
  17.     oi = 1: maxoff = UBOUND(Datr) - 20: offset = 0
  18.  
  19. SCREEN 0 '25x40
  20.     CLS
  21.     LOCATE 1, 1
  22.     PRINT " Scroll with mouse wheel. ESC to quit"
  23.  
  24.     FOR a = 1 TO 20
  25.         LOCATE a + 1, 1
  26.         PRINT Datr(a + offset).a; " is "; Datr(a + offset).d; ",  x 2 = "; Datr(a + offset).b; ",  x 5 = "; Datr(a + offset).c
  27.     NEXT a
  28.     Mouse_Loop oi, maxoff, offset
  29.     _DISPLAY
  30.     _LIMIT 50
  31.  
  32.  
  33. SUB Mouse_Loop (var AS INTEGER, var2 AS INTEGER, var3 AS INTEGER)
  34.  
  35.     '-------------------------ALGORITHM--------------------------------------------------CLEARED
  36.     ' SUB: Mouse_Loop
  37.     '
  38.     ' Purpose:
  39.     ' Primary mouse input loop. Controls mousewheel scrolling and conditions
  40.     ' x,y position data.
  41.     '
  42.     ' Passed Variables:
  43.     ' var sends whether data list is extensive enough for using offset printing values
  44.     ' var2 sends maximum allowed offset value
  45.     '
  46.     '------------------------------------------------------------------------------------
  47.  
  48.     DO WHILE _MOUSEINPUT '                                      scan for changes in mouse position and save to global variables
  49.         IF var = 1 THEN
  50.             var3 = var3 + _MOUSEWHEEL '                     mousewheel offset determines starting element to print
  51.             IF var3 < 0 THEN var3 = 0 '                     don't go beyond bottom of array
  52.             IF var3 > var2 THEN var3 = var2 '               don't go beyond the end of the array
  53.         END IF
  54.         'mouse_y = _MOUSEY ' globals used where mouse positions are important.
  55.         'mouse_x = _MOUSEX
  56.     LOOP
  57.  
  58. END SUB 'Mouse_Loop
  59.  
  60.  
« Last Edit: August 07, 2019, 07:31:08 am by OldMoses »

Offline SierraKen

  • Forum Resident
  • Posts: 1454
    • View Profile
Re: Scrollable output in QB64
« Reply #3 on: August 07, 2019, 12:27:40 pm »
That's awesome OldMoses! I never seen that kind of thing before in any BASIC language. I saved it to learn by later sometime, thanks.

Offline OldMoses

  • Seasoned Forum Regular
  • Posts: 469
    • View Profile
Re: Scrollable output in QB64
« Reply #4 on: August 07, 2019, 01:02:27 pm »
That's awesome OldMoses! I never seen that kind of thing before in any BASIC language. I saved it to learn by later sometime, thanks.

I couldn't do it before QB64. It used to be the stuff of assembly routines and interrupt calls, which I never did learn to fool with.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Scrollable output in QB64
« Reply #5 on: August 07, 2019, 03:59:09 pm »
And here's a direct example for scrolling text which suits Chris's long data structure:

Code: QB64: [Select]
  1. TYPE InventoryDataStructure
  2.     EmployeeScan AS STRING * 4
  3.     Employeename AS STRING * 60
  4.     Brand AS STRING * 60
  5.     Number AS STRING * 40
  6.     UnitName AS STRING * 60
  7.     WeightBegin AS SINGLE
  8.     UPCCode AS STRING * 20
  9.     Cost AS SINGLE
  10.     PurchaseDate AS STRING * 10
  11.     ExpirationDate AS STRING * 10
  12.     WeightCheckOut AS SINGLE
  13.     WeightCheckIn AS SINGLE
  14.     'STOCKUPC = 9, more corrupts the barcode print out.
  15.     StockUPC AS STRING * 10 'Serial number
  16.     TypeofProductInventory AS STRING * 20
  17.     BatchNumber AS STRING * 4
  18.     BoxNumber AS STRING * 4
  19.     GallonorPound AS STRING * 8
  20.     INorOut AS STRING * 3
  21.     Room AS STRING * 10
  22.     Rack AS STRING * 4
  23.     Shelf AS STRING * 4
  24.     Bin AS STRING * 4
  25.     DT AS STRING * 10
  26.     TM AS STRING * 8
  27. DIM SHARED ProductInventory AS InventoryDataStructure
  28. DIM SHARED RecordCount AS LONG
  29. ProductInventoryDBase$ = "Product Inventory Stock.RND"
  30.  
  31. REDIM array(100000) AS STRING 'large enough to hold all the records
  32.  
  33.  
  34. OPEN ProductInventoryDBase$ FOR BINARY AS #1 'Load the data
  35. temp$ = SPACE$(LEN(ProductInventory)) 'the length of the data, as a single string so we can assign it to our array
  36.     GET #1, , temp$ 'load one record at a time
  37.     RecordCount = RecordCount + 1 'count the records as we go so we can put them in our array for display
  38.     array(RecordCount) = temp$ 'assign the value to the array for display
  39. CLOSE ' Close the file
  40.  
  41. WIDTH 200, 30 'Set to as large a value as possible to see more on the screen at once.
  42. ScrollableDisplay 1, RecordCount, array()
  43.  
  44. SUB ScrollableDisplay (start, finish, array() AS STRING)
  45.     IF start < LBOUND(array) THEN start = LBOUND(array)
  46.     IF finish > UBOUND(array) THEN finish = UBOUND(array)
  47.     DO
  48.         CLS
  49.         FOR i = start TO finish
  50.             PRINT "RECORD" + STR$(i) + ":" + array(i)
  51.             PRINT 'A space between records for readability
  52.             IF CSRLIN > 25 THEN EXIT FOR
  53.         NEXT
  54.         i$ = UCASE$(INKEY$)
  55.         SELECT CASE i$
  56.             CASE CHR$(0) + CHR$(72) 'scroll up
  57.                 IF start > 1 THEN start = start - 1
  58.             CASE CHR$(0) + CHR$(80) 'scroll down
  59.                 IF start < finish THEN start = start + 1
  60.             CASE CHR$(27) 'Exit
  61.                 EXIT SUB
  62.         END SELECT
  63.         _LIMIT 30
  64.         _DISPLAY
  65.     LOOP
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Chris80194

  • Newbie
  • Posts: 42
    • View Profile
Re: Scrollable output in QB64
« Reply #6 on: August 08, 2019, 11:03:30 pm »
Thank you ill bang it into the compiler.

I have this bad habit of falling asleep from time to time, I just can not seem to get over it. 
Not sure if there is a support group that can help with it either.

Offline Chris80194

  • Newbie
  • Posts: 42
    • View Profile
Re: Scrollable output in QB64
« Reply #7 on: August 08, 2019, 11:13:13 pm »
OldMoses  Ok I hate you.... Now I have another idea to try in my program ughhhhh and if I only DID NOT KNOW how to do that I would not have even worried about but NOW I have to make it work.  LOL

SMcNeill That works great in the test window the data looks a bit corrupted which could be the issue I am having with some searching...
Of course I have also made changes and did not clear that main data file too but that is what I was looking for.
Thanks.