Author Topic: Resizing Screen Demo  (Read 3833 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
Resizing Screen Demo
« on: November 09, 2021, 04:29:04 pm »
I had someone send me a message asking about how to use $RESIZE to set a screen to reflow text (not stretch or scale it), so I thought I'd write up a quick little demo and share with everyone.

Code: QB64: [Select]
  1. Dim Shared As Long DW, DH, DisplayScreen, WorkScreen, Font, FontSize
  2. Dim Shared As Long ScreenWidth, ScreenHeight
  3.  
  4.  
  5.  
  6. _Title "X-Mas Adventure"
  7. '$Dynamic
  8. DefLng A-Z
  9.  
  10.  
  11.  
  12. If DW > 1920 Then 'I'm going to assume it's a super highres screen
  13.     DisplayScreen = _NewImage(1280, 960, 32)
  14.     FontSize = 40
  15.     DisplayScreen = _NewImage(640, 480, 32)
  16.     FontSize = 20
  17.  
  18. Screen DisplayScreen
  19. Font = _LoadFont("courbd.ttf", FontSize, "monospace")
  20. _Font Font
  21.  
  22.  
  23.  
  24.  
  25.     Cls , 0
  26.     k = _KeyHit
  27.     If _Resize Then resize
  28.  
  29.     Select Case k
  30.         Case 27: System
  31.     End Select
  32.  
  33.  
  34.     Select Case Room
  35.         Case 0: TitleScreen
  36.         Case 1: Room1
  37.     End Select
  38.  
  39.  
  40.     _Limit 30
  41.     _Display
  42.  
  43.  
  44.  
  45. Sub Room1
  46.     Print "This is just a demo of a resizable screen and how we'd adjust our text to give us more words per row with a wide screen and fewer words with a narrower screen.  This will also change font size automatically to a much larger font, if your screen resolution is greater than 1920 pizels wide, and still look proper for you.  This is just a demo and really does nothing beyond this point..."
  47.     Print
  48.     Print "Note, there is *NO* word-wrap built into this demo."
  49.  
  50.  
  51. Sub TitleScreen
  52.     Static Delay#
  53.     If Delay# = 0 Then Delay# = ExtendedTimer + 10.0 '10 seconds to view and resize my pretty little title screen
  54.     If Delay# < ExtendedTimer Then
  55.         Room = 1
  56.     End If
  57.     Text1$ = "Resizing Screen Demo"
  58.     Text2$ = "By. SMcNeill"
  59.     Text3$ = "2021"
  60.     Top = (_Height - _FontHeight * 3) / 2
  61.     CenterText Text1$, Top
  62.     CenterText Text2$, Top + _FontHeight
  63.     CenterText Text3$, Top + _FontHeight * 2
  64.  
  65. Sub CenterText (text$, Ypos)
  66.     CenterX = (_Width - _PrintWidth(text$)) / 2
  67.     _PrintString (CenterX, Ypos), text$
  68.  
  69.  
  70. Sub resize
  71.     RX = _ResizeWidth: RY = _ResizeHeight
  72.     _Font 16
  73.     tempscreen = _NewImage(RX, RY, 32)
  74.     Screen tempscreen
  75.     _FreeImage DisplayScreen
  76.     DisplayScreen = tempscreen
  77.     _Font Font
  78.     ScreenWidth = _Width / _FontWidth
  79.     ScreenHeight = _Height / _FontHeight
  80.  
  81. Function ExtendedTimer##
  82.     d$ = Date$
  83.     l = InStr(d$, "-")
  84.     l1 = InStr(l + 1, d$, "-")
  85.     m = Val(Left$(d$, l))
  86.     d = Val(Mid$(d$, l + 1))
  87.     y = Val(Mid$(d$, l1 + 1)) - 1970
  88.     For i = 1 To m
  89.         Select Case i 'Add the number of days for each previous month passed
  90.             Case 1: d = d 'January doestn't have any carry over days.
  91.             Case 2, 4, 6, 8, 9, 11: d = d + 31
  92.             Case 3: d = d + 28
  93.             Case 5, 7, 10, 12: d = d + 30
  94.         End Select
  95.     Next
  96.     For i = 1 To y
  97.         d = d + 365
  98.     Next
  99.     For i = 2 To y Step 4
  100.         If m > 2 Then d = d + 1 'add an extra day for leap year every 4 years, starting in 1970
  101.     Next
  102.     d = d - 1 'for year 2000
  103.     s~&& = d * 24 * 60 * 60 'Seconds are days * 24 hours * 60 minutes * 60 seconds
  104.     ExtendedTimer## = (s~&& + Timer)
  105.  

Now this makes a simple little program for us with a resizable screen.  The first screen is nothing but some text that centers itself for us as a simple title screen.  It's configured to remain visible for 10 seconds while you play with the size of the screen and watch the text move to reposition itself always into the center of your screen, regardless of the dimensions you set.  (Unless you scrunch the screen too small, of course.  I'm keeping the demo simple here, so there's no limit to how large/small you can make your screen, and if it's too small, the text won't have room to display properly, so be aware of that.  :P )

Note the resize SUB in particular -- it's the one which is in charge of the screen dimensions:

Code: QB64: [Select]
  1. Sub resize
  2.     RX = _ResizeWidth: RY = _ResizeHeight
  3.     _Font 16
  4.     tempscreen = _NewImage(RX, RY, 32)
  5.     Screen tempscreen
  6.     _FreeImage DisplayScreen
  7.     DisplayScreen = tempscreen
  8.     _Font Font
  9.     ScreenWidth = _Width / _FontWidth
  10.     ScreenHeight = _Height / _FontHeight

    RX = _ResizeWidth: RY = _ResizeHeight -- read the resized dimensions
    _Font 16  -- get ready to free the screen by making certain to swap to a default font for the display
    tempscreen = _NewImage(RX, RY, 32) -- make the new screen
    Screen tempscreen -- swap to the new screen
    _FreeImage DisplayScreen -- free the old screen
    DisplayScreen = tempscreen -- make the new screen the old screen
    _Font Font -- set the proper font back for the new screen

And that's basically the whole process in a nutshell!

If anyone has questions about this little resizing technique, feel free to ask, and I'll answer all I can for you guys until the process makes sense to everybody.  It's really not as difficult as some people make it out to be.  ;)
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!