Author Topic: A nice console demo  (Read 930 times)

0 Members and 1 Guest are viewing this topic.

Arnold

  • Hero Member
  • *****
  • Posts: 977
A nice console demo
« on: May 01, 2019, 11:31:37 pm »
Although I prefer the clean syntax of Oxygenbasic, I am aware that it is possible to adapt code from other Basic dialects with only minor changes. The downside is that you have to work a little more carefully to avoid disappointment, e.g. in the declaration of variables and in the specification of types in the parameters of functions / subs. By using console.inc and sysutil.inc almost all functions are there to create all kind of console apps.

This is an adapted BCX console example (S119) which almost uses the same code (line 35 - line 87). It can be run directly from an editor and be compiled to 32-bit or 64-bit executable. By applying #console and %TryAttachConsole the executable can be started in a Dos window without opening an extra console window. It behaves like the original.

Only small adaptions are necessary. Sub locate builds on sub SetPos in console.inc. Sub color is a function which is simalar to the color macro of console.inc. Instat is a macro definition for function inkey of console.inc.

This demo was very instructive for me. When I tried to wrap the function instat, I experimented with GetAsyncKeyState, GetKeyState, ReadConsole, FlushConsoleInputBuffer, and I suddenly realized that there already exists the function inkey() in console.inc which is called inkey$ in QBasic. Apparently, I have not yet discovered all the gems contained in the include files.

Code: OxygenBasic
  1. $ filename "s119.exe"
  2. 'uses rtl32
  3. 'uses rtl64
  4.  
  5. #console
  6. % TryAttachConsole
  7.  
  8. uses console
  9.  
  10. def instat inkey
  11.  
  12. type CONSOLE_CURSOR_INFO
  13.     dword dwSize
  14.     bool  bVisible
  15. end type
  16.  
  17. ! SetConsoleCursorInfo lib "kernel32.dll" (sys hConsoleOutput, CONSOLE_CURSOR_INFO *lpConsoleCursorInfo) as bool
  18.  
  19. sub locate (int row,int col,optional int showCursor=1,int shape=12)
  20.   CONSOLE_CURSOR_INFO cci
  21.   SetPos(col-1,row-1)
  22.   cci.bVisible = showCursor
  23.   cci.dwSize   = shape
  24.   SetConsoleCursorInfo(ConsOut, cci)
  25. end sub
  26.  
  27. sub color(int fg, bg)
  28.   SetConsoleTextAttribute (ConsOut, fg+bg*16)
  29. end sub
  30.  
  31. #lookahead
  32.  
  33. =============================================================================
  34.  
  35. cls
  36.  
  37. DrawBox (20,5,60,20,14,4,11,9,3,"Oxygenbasic >< ROCKS!")
  38.  
  39.  
  40. While Not Instat
  41.   Locate 12,29,0
  42.   Color 4,9
  43.   Print "Oxygenbasic >< ROCKS!"
  44.   Sleep(200)
  45.   Locate 12,29,0
  46.   Color 15,9
  47.   Print "Oxygenbasic >< ROCKS!"
  48.   Sleep(200)
  49. Wend
  50. Color 7,0
  51.  
  52.  
  53. SUB DrawBox (int X1,Y1,X2,Y2,Bdrfg,Bdrbg,Winfg,Winbg,Style,string Title$)
  54. dim Tb,Sid,Tlc,Trc,Blc,Brc,Tmp
  55. dim Temp$ as string
  56.  
  57. Select Case Style
  58. '-----------------------------------------------------------------
  59.  Case 1 :         'Single Sides,Top And Bottom
  60. '-----------------------------------------------------------------
  61.  Tb=196  :  Sid=179  :  Tlc=218  :  Trc=191  :  Blc=192  :  Brc=217
  62. '-----------------------------------------------------------------
  63.  Case 2 :         'Double Sides,Top And Bottom
  64. '-----------------------------------------------------------------
  65.  Tb=205  :  Sid=186  :  Tlc=201  :  Trc=187  :  Blc=200  :  Brc=188
  66. '-----------------------------------------------------------------
  67.  Case 3 :         'Single Sides,Double Top And Bottom"
  68. '-----------------------------------------------------------------
  69.  Tb=205  :  Sid=179  :  Tlc=213  :  Trc=184  :  Blc=212  :  Brc=190
  70. '-----------------------------------------------------------------
  71. End Select
  72.  
  73. Temp$ = Chr$(Tlc) & String$(X2-X1-2,chr(Tb)) & Chr$(Trc)
  74. Locate Y1,X1,0  :  Color(Bdrfg,Bdrbg)  :  Print Temp$
  75. Locate Y1,((X1+X2)/2) - (Len(Title$)/2),0  :  Print Title$
  76.  
  77. For Tmp = Y1+1 To Y2-1
  78.   Color Bdrfg,Bdrbg
  79.   Locate Tmp,X1,0    :  Print Chr$(Sid)
  80.   Color Winfg,Winbg  :  Print Space$(X2-X1-2)
  81.   Color Bdrfg,Bdrbg  :  Print Chr$(Sid)
  82. Next
  83.  
  84. Temp$ = Chr$(Blc) & String$(X2-X1-2,chr(Tb)) & Chr$(Brc)
  85. Locate Y2,X1,0  :  Print Temp$
  86.  
  87. End Sub
  88.  
  89. ===============================================================
  90.  
  91. locate 24,1
  92. color 7,0
  93. print "Enter a key..."
  94. waitkey
  95.