So this windows thing..


New member
3rd Party Module Dev
Tutorial Author
Jan 2, 2020
Here I try to skip between fullscreen, normal windowed mode and floating windowed mode.
It kind of works but lots of unpolished things happens, for instance :

1) When resizing it updates everything as long as you are dragging the window but if you hold and stand still, the content does to?!

2) Going from windowed to fullscreen forgets the mouseposition, I try to go around this problem but it's not a perfect solve

3) Going from floating window and normal windowed mode creates a window in the centre of the screen for a short time
because how the SetDeviceWindow command work.

It's all just small things but together they make everything the game very unpolished so I would be grateful to get any tip on how to fix them one by one..

A nice side-discovery while I was solving other problems II found a way to tell if you are resizing a window from the top, bottom, left or right. Feel free to use this if you feel there's a use for it!

Also, there's a nice routine that creates a very polished splash screen, so I keep it i here, if anyone wants it.

F toggles Floating mode
A toggles fullscreen mode

(try hold A or F continuously to see the effect, or try to resize the window and hold the size to see the content stop)


Import mojo2
Import brl.databuffer
Import "commands.cpp"

Extern ' bind contents of commands.cpp
Function GetWindowPosition:Int[]() = "commands::getWindowPosition"

' ---------------------------------------------------------------------------------------------------------

Function Main:Int()
    New myApp()
    Return 0

Class myApp Extends App

     Field gameCanvas:Canvas
     Field isLoading:Bool = True
     Field fullscreen:Bool = True
     Field xd:Int = 4

    ' Structure holding window
    Field xx:Int
    Field yy:Int
     Field ow:Int
     Field oh:Int
     Field cw:Int
     Field ch:Int
     Field ox:Int
     Field oy:Int
     Field cx:Int
     Field cy:Int
     Field oldcx:Int
     Field oldcy:Int
     Field coords:Int[]

     Field startup:Int = True
        Field x:Float,y:Int,d:Int
    Field w:Int,h:Int
    Field init:Bool = False
    Field canvas:Canvas
    Field screen2:Image
    Field screen2canvas:Canvas
    Field offset:Float
    Field floating:Int = 0
    Field mx:Float, my:Float

    Method OnCreate:Int()
          gameCanvas = New Canvas()
          ow = 512-16 ; oh = 256+32
        gameCanvas.SetViewport 0,0,ow,oh  
        gameCanvas.SetProjection2d 0,ow,0,oh
        gameCanvas.SetScissor 0,0,ow,oh
         SetSwapInterval 1 ; SetUpdateRate 0
         w = 1280
         h = 720

         SetDeviceWindowTitle "test"
         Return 0

    Method OnUpdate:Int()
          If KeyHit(KEY_ESCAPE) Then OnClose()
          If KeyHit(KEY_F) Then floating = 1 - floating ; fullscreen = Not fullscreen ;  oldcx = cx ; oldcy = cy ; ToggleFullscreen
         If KeyHit(KEY_A) Then ToggleFullscreen
        Return 0

    Method OnRender:Int()

        ' Startup-screen
        If startup
             gameCanvas.Clear 0,0,1
              gameCanvas.DrawText "Level 1 - namehere...",162,128
              gameCanvas.SetColor 1,1,1
             If Millisecs() > 4000
                 Local desktop:DisplayMode = DesktopMode()
                 ow = desktop.Width
                 oh = desktop.Height
                 ow = w
                 oh = h
                 fullscreen = False
                 gameCanvas.SetColor 1,1,1
                 gameCanvas.Clear 0,0,0
                 SetDeviceWindowPosition 78,66
             gameCanvas.SetColor 1,1,1

        ' Normal main
         If Not startup
              gameCanvas.Clear 0,1,0
              ' Get window position
            coords = GetWindowPosition()
            cx = coords[0]
            cy = coords[1]
            ' Get window size
             cw = DeviceWindowWidth()
             ch = DeviceWindowHeight()
             ' Have some animation on the screen
            x = x + xd
            If x > (cw- 64) Then x = -xd
            gameCanvas.DrawOval x,512,64,64
            gameCanvas.DrawOval x,96,64,64
          Return 0

    Method OnResize:Int()
        Local oldcw := cw
        Local oldch := ch
        cw = DeviceWindowWidth()
         ch = DeviceWindowHeight()
        gameCanvas.SetViewport 0,0,cw,ch
        gameCanvas.SetProjection2d 0,cw,0,ch
        gameCanvas.SetScissor 0,0,cw,ch

         ' Find out from what direction we're resizing the window
          coords = GetWindowPosition()
          If coords[0] <> cx
              Print "resizing from leftside"
          ElseIf (oldch <> ch) And (coords[1] = cy)
              Print "resizing from bottom"
         Elseif (coords[1] <> cy)
              Print "resizing from top"
              Print "resizing from rightside"
         ' corner
        Return 0

    Method ToggleFullscreen:Void()
        If ow = 0 Then ow = 800
        If oh = 0 Then oh = 600
        ' Set windowed mode, floating or not
        If Not fullscreen
            SetDeviceWindow ow, oh, 4 + 2 + (floating * 8)
            SetDeviceWindowPosition oldcx,oldcy
            gameCanvas.SetViewport 0,0,ow,oh  
            gameCanvas.SetProjection2d 0,ow,0,oh
            gameCanvas.SetScissor 0,0,ow,oh
            ' Save Windowsize
            ow = cw    
            oh = ch
            ' Go fullscreen
            ' Save window position
            coords = GetWindowPosition()
            cx = coords[0]
            cy = coords[1]
            oldcx = cx
            oldcy = cy
            ' Save mouse position and read desktopsize
             Local oldmx:Int = MouseX()
             Local oldmy:Int = MouseY()
            Local desktop:DisplayMode = DesktopMode()
            Local awidth : Int = desktop.Width
            Local aheight : Int = desktop.Height
            ' try not mess up the mousepointer
            ox = cx
            oy = cy
            SetDeviceWindow awidth,aheight, 1
            gameCanvas.SetViewport 0,0,awidth,aheight
            gameCanvas.SetProjection2d 0,awidth,0,aheight
            gameCanvas.SetScissor 0,0,awidth,aheight
            SetMousePos oldmx+cx,oldmy+cy ' from WINDOW to GLOBAL coordinates, fails at least some of the time
        fullscreen = Not fullscreen


namespace commands
    // ------------------------------------------------------------------
    Array<int> getWindowPosition()
        Array<int> output = Array<int>(2);
        int xpos, ypos;
        glfwGetWindowPos(BBGlfwGame::GlfwGame()->GetGLFWwindow(), &xpos, &ypos);
        output[0] = xpos;
        output[1] = ypos;
        return output;
    // ------------------------------------------------------------------
Last edited:
Top Bottom