Full-windowed Maxgui with Max2d

Started by markcwm, January 20, 2018, 08:27:58

Previous topic - Next topic

markcwm

I'm not sure where this should go so it's going in Miscellaneous, obviously this is PD code. It's just some basic window code for initializing Max2d in an auto-maximized window using Maxgui. Similar code surely exists but I've never found it so had to figure it out myself.

I think this is better than a fullscreen window as you can minimize it. I also prefer this to games that leave the maximizing to the user (since you don't know if it uses a virtual resolution you may just leave it be). You would use SetVirtualResolution for Max2d and design for 16:8 ratio as desktops and laptops are 16:9 or 16:10 and have a statusbar or dock at the bottom. You can remove the titlebar flag and it will go fullscreen but this doesn't work in linux. I did a stress test to see if there is any slowdown compared to fullscreen and they were both the same speed. I tested in ubuntu which is very slow in windowed modes of a smaller resolution.

So this is what I'll be using if I ever get a game out there, maybe one day!
Code (blitzmax) Select
' fullwindowed.bmx
' full-windowed (not fullscreen) Maxgui with Max2d

SuperStrict

Framework brl.glmax2d
?Not linux
Import maxgui.drivers
?linux
Import bah.gtkmaxgui
?Not bmxng
Import brl.timer
?bmxng
Import brl.timerdefault
?
Import brl.eventqueue

' find maximum window area by maximizing
Local initW%=320,initH%=240
Global AppW%=initW, AppH%=initH
Global AppX%=(DesktopWidth()-AppW)/2, AppY%=(DesktopHeight()-AppH)/2
Local windowflags:Int=WINDOW_RESIZABLE|WINDOW_CLIENTCOORDS
windowflags:|WINDOW_TITLEBAR
Global timer:TTimer=CreateTimer(60)
Local oldms:Int=MilliSecs()

Global window:TGadget=CreateWindow(AppTitle, AppX, AppY, initW, initH, Null, windowflags)
MaximizeWindow(window)
Repeat
WaitEvent()
Select EventID()
Case EVENT_WINDOWCLOSE
Exit
Case EVENT_TIMERTICK
AppW=window.ClientWidth() ' client size needs a few ticks to initialize
AppH=window.ClientHeight()
If AppW<>initW And AppH<>initH Then Exit
If Abs(MilliSecs()-oldms)>1000 Then Exit ' if something goes wrong exit after 1 second
End Select
Forever

' now init canvas
Global canvas:TGadget=CreateCanvas(0,0, AppW, AppH ,window)
SetGadgetLayout canvas, EDGE_ALIGNED, EDGE_ALIGNED, EDGE_ALIGNED, EDGE_ALIGNED
ActivateGadget(canvas)
EnablePolledInput(canvas)
SetGraphics CanvasGraphics(canvas) ' use a canvas context, as opposed to GLMax2DDriver() which uses Graphics()
SetVirtualResolution ClientWidth(canvas), ClientHeight(canvas)
SetViewport 0, 0, ClientWidth(canvas), ClientHeight(canvas)

' main loop
Local fpscounter%, fpstime%, fpscurrent% ' FPS counter
Local scrollup%, scrolly%

While Not KeyDown(KEY_ESCAPE)
WaitEvent()
Select EventID()
Case EVENT_WINDOWCLOSE, EVENT_APPTERMINATE
End
Case EVENT_TIMERTICK
RedrawGadget canvas
Case EVENT_GADGETPAINT
SetColor 255,255,255
Cls()
fpscounter:+1
If Abs(MilliSecs()-fpstime)>1000
fpstime=MilliSecs()
fpscurrent=fpscounter
fpscounter=0
EndIf
If scrollup=0 Then scrolly:+1 Else scrolly:-1
If scrolly>=ClientHeight(canvas)-TextHeight("A") Then scrollup=1
If scrolly<=0 Then scrollup=0
DrawText "FPS = "+fpscurrent+", MemAlloced = "+GCMemAlloced()+", width = "+ClientWidth(canvas)+", height = "+ClientHeight(canvas), 20, scrolly
Flip()
GCCollect()
EndSelect
Wend

FreeGadget window
FreeGadget canvas
End