Ooops
October 28, 2020, 11:43:41 PM

Author Topic: Full-windowed Maxgui with Max2d  (Read 1303 times)

Offline markcwm

  • Sr. Member
  • ****
  • Posts: 473
Full-windowed Maxgui with Max2d
« on: January 20, 2018, 08:27:58 AM »
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
  1. ' fullwindowed.bmx
  2. ' full-windowed (not fullscreen) Maxgui with Max2d
  3.  
  4. SuperStrict
  5.  
  6. Framework brl.glmax2d
  7. ?Not linux
  8. Import maxgui.drivers
  9. ?linux
  10. Import bah.gtkmaxgui
  11. ?Not bmxng
  12. Import brl.timer
  13. ?bmxng
  14. Import brl.timerdefault
  15. ?
  16. Import brl.eventqueue
  17.  
  18. ' find maximum window area by maximizing
  19. Local initW%=320,initH%=240
  20. Global AppW%=initW, AppH%=initH
  21. Global AppX%=(DesktopWidth()-AppW)/2, AppY%=(DesktopHeight()-AppH)/2
  22. Local windowflags:Int=WINDOW_RESIZABLE|WINDOW_CLIENTCOORDS
  23. windowflags:|WINDOW_TITLEBAR
  24. Global timer:TTimer=CreateTimer(60)
  25. Local oldms:Int=MilliSecs()
  26.  
  27. Global window:TGadget=CreateWindow(AppTitle, AppX, AppY, initW, initH, Null, windowflags)
  28. MaximizeWindow(window)
  29. Repeat
  30.         WaitEvent()
  31.         Select EventID()
  32.                 Case EVENT_WINDOWCLOSE
  33.                         Exit
  34.                 Case EVENT_TIMERTICK
  35.                         AppW=window.ClientWidth() ' client size needs a few ticks to initialize
  36.                         AppH=window.ClientHeight()
  37.                         If AppW<>initW And AppH<>initH Then Exit
  38.                         If Abs(MilliSecs()-oldms)>1000 Then Exit ' if something goes wrong exit after 1 second
  39.         End Select
  40. Forever
  41.  
  42. ' now init canvas
  43. Global canvas:TGadget=CreateCanvas(0,0, AppW, AppH ,window)
  44. SetGadgetLayout canvas, EDGE_ALIGNED, EDGE_ALIGNED, EDGE_ALIGNED, EDGE_ALIGNED
  45. ActivateGadget(canvas)
  46. EnablePolledInput(canvas)
  47. SetGraphics CanvasGraphics(canvas) ' use a canvas context, as opposed to GLMax2DDriver() which uses Graphics()
  48. SetVirtualResolution ClientWidth(canvas), ClientHeight(canvas)
  49. SetViewport 0, 0, ClientWidth(canvas), ClientHeight(canvas)
  50.  
  51. ' main loop
  52. Local fpscounter%, fpstime%, fpscurrent% ' FPS counter
  53. Local scrollup%, scrolly%
  54.  
  55. While Not KeyDown(KEY_ESCAPE)
  56.         WaitEvent()
  57.         Select EventID()
  58.                 Case EVENT_WINDOWCLOSE, EVENT_APPTERMINATE
  59.                         End
  60.                 Case EVENT_TIMERTICK
  61.                         RedrawGadget canvas
  62.                 Case EVENT_GADGETPAINT
  63.                         SetColor 255,255,255
  64.                         Cls()
  65.                         fpscounter:+1
  66.                         If Abs(MilliSecs()-fpstime)>1000
  67.                                 fpstime=MilliSecs()
  68.                                 fpscurrent=fpscounter
  69.                                 fpscounter=0
  70.                         EndIf
  71.                         If scrollup=0 Then scrolly:+1 Else scrolly:-1
  72.                         If scrolly>=ClientHeight(canvas)-TextHeight("A") Then scrollup=1
  73.                         If scrolly<=0 Then scrollup=0
  74.                         DrawText "FPS = "+fpscurrent+", MemAlloced = "+GCMemAlloced()+", width = "+ClientWidth(canvas)+", height = "+ClientHeight(canvas), 20, scrolly
  75.                         Flip()
  76.                         GCCollect()
  77.         EndSelect
  78. Wend
  79.  
  80. FreeGadget window
  81. FreeGadget canvas
  82. End
  83.  

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal