Using MaxGUI, I am trying to have a window with a canvas on it which will obviously display the screen from my game ... and my window is resizeable. I have set up a hook and put most of the event detecting in the hook so that it is called even while the user is clicking things like dragging the title bar or resizing the window. For the most part this works, but there are some `gaps` in what normally would be full-time refreshing of the canvas as in a regular game.
The first problem is this: Window resize events only get created when you move the mouse away from the current coordinates while holding down the mouse button. When the mouse is stationary there is no resize event generated, so the hook is not called and the display doesn't update. So as you sit there holding down the mouse on the resize gadget, the game doesn't update.
The second problem is this: Almost the inverse of the first problem, when you click on the title bar and drag the window, window move events are generated only when the window has finished moving, at which point the game seems to update properly even if you're still holding the mouse down, but when the window is actually moving around with different mouse coords it stops updating.
How can I address these two issues so that the game screen is constantly being redrawn at 60hz or whatever rate, even when it is being moved or resized?
The first problem is this: Window resize events only get created when you move the mouse away from the current coordinates while holding down the mouse button. When the mouse is stationary there is no resize event generated, so the hook is not called and the display doesn't update. So as you sit there holding down the mouse on the resize gadget, the game doesn't update.
The second problem is this: Almost the inverse of the first problem, when you click on the title bar and drag the window, window move events are generated only when the window has finished moving, at which point the game seems to update properly even if you're still holding the mouse down, but when the window is actually moving around with different mouse coords it stops updating.
How can I address these two issues so that the game screen is constantly being redrawn at 60hz or whatever rate, even when it is being moved or resized?
Strict Global GAME_WIDTH=320'ClientWidth(Desktop()) Global GAME_HEIGHT=240'ClientHeight(Desktop()) ' create a centered window with client size GAME_WIDTH,GAME_HEIGHT Local wx=(GadgetWidth(Desktop())-GAME_WIDTH)/2 Local wy=(GadgetHeight(Desktop())-GAME_HEIGHT)/2 Global lastdw:Int=GadgetWidth(Desktop()) Global lastdh:Int=GadgetHeight(Desktop()) 'make a window Global window:TGadget=CreateWindow("My Canvas",wx,wy,GAME_WIDTH,GAME_HEIGHT,Null,WINDOW_TITLEBAR|WINDOW_CLIENTCOORDS|WINDOW_RESIZABLE|WINDOW_STATUS) SetMinWindowSize window,150,100 ' create a canvas for our game Global canvas:TGadget=CreateCanvas(0,0,GAME_WIDTH,GAME_HEIGHT,window) SetGraphics CanvasGraphics(canvas) ' create an update timer CreateTimer 60 Function MyHook:Object(id:Int,tData:Object,tContext:Object) Local Event:TEvent=TEvent(tData) If Event.source=window Select Event.ID Case EVENT_WINDOWSIZE 'WHILE the user is still resizing the window, 'make a new canvas with the new dynamic size and refresh 'at the new size (ie update the game) Print "ReSize Hook! "+Event.ToString() 'Update canvas size based on sized window FreeGadget canvas canvas=CreateCanvas(0,0,GadgetWidth(window),GadgetHeight(window),window) 'redraw game SetGraphics CanvasGraphics(canvas) SetOrigin GadgetWidth(window)/2,GadgetHeight(window)/2 SetLineWidth 5*(GadgetWidth(window)/Float(GadgetWidth(Desktop()))) Cls Local t=MilliSecs() DrawLine 0,0,520*Cos(t),520*Sin(t) DrawLine 0,0,580*Cos(t/60),580*Sin(t/60) SetOrigin 0,0 DrawLine 0,GadgetHeight(canvas)-50,GadgetWidth(canvas),GadgetHeight(canvas)-50 Flip 1 RedrawGadget canvas Return Null Case EVENT_WINDOWMOVE 'WHILE the user is still moving the window, 'update the game Print "Move Hook! "+Event.ToString() 'redraw game SetGraphics CanvasGraphics(canvas) SetOrigin GadgetWidth(window)/2,GadgetHeight(window)/2 SetLineWidth 5*(GadgetWidth(window)/Float(GadgetWidth(Desktop()))) Cls Local t=MilliSecs() DrawLine 0,0,520*Cos(t),520*Sin(t) DrawLine 0,0,580*Cos(t/60),580*Sin(t/60) SetOrigin 0,0 DrawLine 0,GadgetHeight(canvas)-50,GadgetWidth(canvas),GadgetHeight(canvas)-50 Flip 1 RedrawGadget canvas Return Null End Select EndIf If Event.ID=EVENT_TIMERTICK If GadgetWidth(Desktop())<>lastdw Or GadgetHeight(Desktop())<>lastdh 'Desktop resized lastdw=GadgetWidth(Desktop()) lastdh=GadgetHeight(Desktop()) GAME_WIDTH=lastdw GAME_HEIGHT=lastdh SetGadgetShape(window,0,0,lastdw,lastdh) 'resize FreeGadget canvas canvas=CreateCanvas(0,0,GadgetWidth(window),GadgetHeight(window),window) ActivateGadget(canvas) SetGraphics CanvasGraphics(canvas) EndIf RedrawGadget canvas Delay 1 'friendly to o/s EndIf If Event.ID=EVENT_GADGETPAINT And Event.Source=canvas SetGraphics CanvasGraphics(canvas) SetOrigin GadgetWidth(window)/2,GadgetHeight(window)/2 SetLineWidth 5*(GadgetWidth(window)/Float(GadgetWidth(Desktop()))) Cls Local t=MilliSecs() DrawLine 0,0,520*Cos(t),520*Sin(t) DrawLine 0,0,580*Cos(t/60),580*Sin(t/60) SetOrigin 0,0 DrawLine 0,GadgetHeight(canvas)-50,GadgetWidth(canvas),GadgetHeight(canvas)-50 Flip 1 EndIf Return tData End Function 'Add our hook to the system AddHook EmitEventHook,MyHook While WaitEvent() Select EventID() 'This is never executed, trapped by the hook function ' Case EVENT_WINDOWSIZE ' FreeGadget canvas ' canvas=CreateCanvas(0,0,GadgetWidth(window),GadgetHeight(window),window) ' 'SetGraphics CanvasGraphics(canvas) Case EVENT_MOUSEMOVE Print "Mouse MOVED!" Case EVENT_WINDOWCLOSE FreeGadget canvas End Case EVENT_APPTERMINATE End End Select Wend