October 28, 2020, 05:51:59 AM

Author Topic: [bb] Gprint/Ginput/locate by Alaric [ 1+ years ago ]  (Read 1213 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Gprint/Ginput/locate by Alaric [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : Gprint/Ginput/locate
Author : Alaric
Posted : 1+ years ago

Description : Uses the graphics screen to display text rather than a new console window.  Be sure to globablize printx and printy.  Please use the code that I have recently posted here as opposed to the original code.  There are some major bugs which I believe that I have managed to stamp out.

Code :
Code: BlitzBasic
  1. ;Note: other than the globals (and the functions), the rest of this program is just an example.  
  2.  
  3. Graphics 800,600,16,2
  4. SetBuffer(BackBuffer())
  5. Global printx,printy
  6. Color 255,0,0
  7. Line 0,0,GraphicsHeight(),GraphicsWidth()
  8.  
  9. Strng$=ginput$("Hello?  ")
  10. Color 0,255,0
  11. gprint "Hi!"
  12. Color 0,0,255
  13. locate(100,100)
  14. Color 255,255,255
  15. gprint(strng)
  16. locate 0,0
  17.  
  18. For loop = 1 To 100
  19.         Color Rand(255),Rand(255),Rand(255)
  20.         gprint("Can you see me? "+loop)
  21.         Delay(50)
  22. Next
  23.  
  24. WaitKey()
  25.  
  26. Function gInput$(PrintSTR$)
  27.         Local graphicsshiftbuffer=CreateImage(GraphicsWidth(),GraphicsHeight())
  28.         Local InputSTR$ = ""
  29.         Local NextKey = 0
  30.         Local NextLet$
  31.        
  32.         If GraphicsHeight() <= printy;this entire "If" is just a way to shift the graphics buffer up when
  33.                                                                         ;the bottom of the "graphics" is reached.  This is (pretty much) the
  34.                                                                         ;same as the old input command. See gprint for entire commentation.  
  35.                 CopyRect 0,0,GraphicsWidth(),GraphicsHeight(),0,0,FrontBuffer(),ImageBuffer(graphicsshiftbuffer)
  36.                 CopyRect 0,0,GraphicsWidth(),GraphicsHeight(),0,-StringHeight(printstring),ImageBuffer(graphicsshiftbuffer),FrontBuffer()
  37.                 Viewport 0,GraphicsHeight()-StringHeight(printstring),GraphicsWidth(),GraphicsHeight()
  38.                 Cls
  39.                 Viewport 0,0,GraphicsWidth(),GraphicsHeight()
  40.                 Printy = GraphicsHeight()-StringHeight(printstring)
  41.         End If
  42.         Viewport printx,printy,GraphicsWidth()-printx,StringHeight(printstr$);I use this command to ensure
  43.                                                                                                                                   ;that when I use the CLS command I don't
  44.                                                                                                                                   ;clear the ENTIRE screen, just the
  45.                                                                                                                                   ;Input line.
  46.         Cls ;clear the line where the Input will be placed.  
  47.         Text printx,printy, printstr$ + inputstr$ ;draw the string.  
  48.         Flip;flip the viewport area
  49.         Repeat
  50.                 nextlet$ = ""
  51.                 Nextkey = WaitKey() ;returns the ASCII value of whichever key was pressed.  
  52.                 If nextkey = 13 Then Exit ;check to see if return was hit.  
  53.                 If Nextkey <> 0 Then nextlet$ = Chr$(nextkey) ;If the key was actually pressed then make nextlet
  54.                                                                                                           ;equal to the letter signified by the ASCII value
  55.                                                                                                           ;inside of nextkey.  
  56.                 If NextLet$ <> "" And nextkey <> 8                        ;check to see if nextlet is a letter or
  57.                         InputSTR$ = InputSTR$ + NextLet$;add nextlet to the output
  58.                 ElseIf nextkey = 8;check to see if delete was pressed.  
  59.                         InputSTR$ = Left$(InputSTR$,Len(InputSTR$) - 1) ;remove a letter from inputstr
  60.                 End If
  61.                 Cls ;clear the input line
  62.                 Text printx,printy, printstr$ + inputstr$;draw the text to the screen
  63.                 Flip ;flip the viewport buffer
  64.         Forever ;loop
  65.         Viewport 0,0,GraphicsWidth(),GraphicsHeight() ;return the viewport to normal.  
  66.         printy=printy+15 ;increase the printy variable so that the next "gprint" or "ginput" command will
  67.                                          ;not be right on top of this one.  
  68.         Return inputstr ;return whatever the user typed in.  
  69. End Function
  70.  
  71. Function gPrint(printstring$)
  72.         Local graphicsshiftbuffer=CreateImage(GraphicsWidth(),GraphicsHeight())
  73.         If GraphicsHeight() <= printy+StringHeight(printstring);this entire "If" is just a way to shift the graphics buffer up when the
  74.                                                                 ;bottom of the "graphics" is reached.  This is (pretty much) the same
  75.                                                                 ;as the old input command.  
  76.                 CopyRect 0,0,GraphicsWidth(),GraphicsHeight(),0,0,FrontBuffer(),ImageBuffer(graphicsshiftbuffer)
  77.                 ;this line copies the entire frontbuffer to the image graphicsshiftbuffer.  
  78.                
  79.                 CopyRect 0,0,GraphicsWidth(),GraphicsHeight(),0,-StringHeight(printstring),ImageBuffer(graphicsshiftbuffer),FrontBuffer()
  80.                 ;this line copies the image graphicsshiftbuffer back to the frontbuffer, but down one
  81.                 ;line of text.  
  82.                
  83.                 Viewport 0,GraphicsHeight()-StringHeight(printstring),GraphicsWidth(),GraphicsHeight()
  84.                 ;this command resets the origin to a different area and allows me to CLS without clearing
  85.                 ;the entire screen.  
  86.                
  87.                 Cls
  88.                 ;clear the area specified by the Viewport command.  
  89.                 Flip
  90.                
  91.                 Viewport 0,0,GraphicsWidth(),GraphicsHeight()
  92.                 ;reset the viewport to the entire screen.  
  93.  
  94.                 Text printx,printy, printstring$
  95.                 ;draw the text to the screen.  
  96.                 printy=printy-StringHeight(printstring)
  97.         Else
  98.                 Text printx,printy, printstring
  99.                 printy = printy + StringHeight(printstring)
  100.                 ;since the entire screen (including the above text) has been moved up, there is no need
  101.                 ;to move the next line of text down.  
  102.         End If
  103.         Flip
  104. End Function
  105.  
  106. Function locate(NewPrintX,NewPrintY)
  107.         printx = newprintx
  108.         printy = newprinty
  109. End Function


Comments :


Kuron(Posted 1+ years ago)

 Hmm, i might could use this.  Thanks a lot =cD


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal