Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Screen Library
#1
This little snippet of code can be easily ripped out, saved as a single BM file and included into your programs to give lots of useful information and capabilities that we just don't normally have for use in QB64PE, by making use of glut and windows system calls.

Code: (Select All)
$Resize:Stretch
$ScreenHide
Screen.DPIaware -2 'turn on awareness
Screen _NewImage(800, 600, 32)
_ScreenShow
Screen.Move.Middle

f = _LoadFont("courbd.ttf", 32, "monospace")
_Font f

Do
    Cls
    Print "Desktop size: "; _DesktopWidth; _DesktopHeight
    Print "Screen  size: "; _Width; _Height
    Print "Scaled  size: "; _ScaledWidth; _ScaledHeight
    Print "Window  size: "; Screen.Window.Width; Screen.Window.Height 'these let us see the window size after resizing
    Print "Our border size in pixels: "; Screen.Border.Width
    Print "Our title bar height in pixels: "; Screen.TitleBar.Height
    Screen.Snapback 320, 240, 1024, 720 'this limits our program to stay inside these dimensions
    _Limit 10
    _Display
Loop Until _KeyHit


'*****************************************************************
'***  Cut from here down to make a SCREEN LIBRARY.BM library  ***
'*****************************************************************

Declare Library
    Function glutGet& (ByVal what&)
    Sub glutReshapeWindow (ByVal width&, ByVal height&)
End Declare

Function Screen.Border.Width&
    Screen.Border.Width = glutGet(506)
End Function

Sub Screen.DPIaware (mode As Long)
    'Const UNAWARE = -1, AWARE = -2, PER_MONITOR_AWARE = -3
    'Const PER_MONITOR_AWARE_V2 = -4, UNAWARE_GDISCALED = -5
    $If WIN Then
        Static CalledOnce As Long
        Declare Dynamic Library "user32"
            Function DPI& Alias SetProcessDpiAwarenessContext (ByVal dpiContext As _Offset)
        End Declare
        If mode > 0 Or mode < -5 Then Exit Sub
        If CalledOnce Then Exit Sub Else CalledOnce = -1
        result = DPI(mode)
        If result = 0 Then
            _MessageBox "Failed to set DPI Awareness", "Notice -- failed to set DPI Awareness. Defaulting to System settings.", "info"
        Else
            _Delay .25 'always nice for a set delay after system calls like this, if they work.
        End If
    $End If
End Sub

Sub Screen.Move (x, y)
    'Moves to the absolute coordinates of the desktop, ignoring border and title, so the program window is
    ' positioned with the program window at the desired position, without taking them into consideration.
    _ScreenMove x - Screen.Border.Width, y - Screen.Border.Width - Screen.TitleBar.Height
End Sub

Sub Screen.Move.Middle
    'Moves to the absolute middle of the desktop, ignoring border and title, so the program window is centered without
    'taking them into consideration.
    _ScreenMove (_DesktopWidth - _Width - BorderWidth) / 2 + 1, (_DesktopHeight - _Height - BorderWidth) / 2 - TitleBarHeight + 1
End Sub

Sub Screen.Snapback (xMin As Long, yMin As Long, xMax As Long, yMax As Long)
    xsize = Screen.Window.Width
    ysize = Screen.Window.Height
    If xsize < xMin Then resize = -1: xsize = xMin
    If xsize > xMax Then resize = -1: xsize = xMax
    If ysize < yMin Then resize = -1: ysize = yMin
    If ysize > yMax Then resize = -1: ysize = yMax
    If resize Then
        glutReshapeWindow xsize, ysize
        _Delay .25 'give time for resize effects to take place
        nothing = _Resize 'clear any resize flag as this is a system call and not one that the program needs to change
    End If
End Sub

Function Screen.TitleBar.Height&
    Screen.TitleBar.Height = glutGet(507)
End Function

Function Screen.Window.Height& '103 is the const value of GLUT_WINDOW_HEIGHT
    'Note that these values won't change until the window size changes.
    'if $RESIZE is disabled, these aren't helpful at all.
    'These are just for telling us the window size that our screen exists within
    Screen.Window.Height = glutGet(103)
End Function

Function Screen.Window.Width& '102 is the const value of GLUT_WINDOW_WIDTH
    'Note that these values won't change until the window size changes.
    'if $RESIZE is disabled, these aren't helpful at all.
    'These are just for telling us the window size that our screen exists within
    Screen.Window.Width = glutGet(102)
End Function

Normally I'd save this as a BM file to download, but it's small and easy to just copy/paste into the demo above so folks can see it at work.  

The Screen.Snapback was something that took me *forever* to sort out how to do, and is personally one of my proudest little routines to get working.  At first glance, it doesn't seem anything fancy, but it was just a PITA to find all the pieces and put them together to set the size limits on a window to keep it from being stretched too small or two large.  (Which might be real important to track if you have pop-up windows and such, and want to know a minimum size to make them so they're visible and don't end up off screen.)

Run it.  Test it out.  Drag the edge of the screen and resize it if you like.  Watch how the values change, update, and the additional information you now have to work with about your program that you didn't have before.  

Hopefully someone else can get some use out of this, as it's some of my personally more useful stuff for me.  Wink
Reply




Users browsing this thread: 1 Guest(s)