Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Events/Hook - how to efficiently listen to events (eg. EVENT_WINDOWSIZE) #335

Open
GWRon opened this issue Oct 25, 2024 · 0 comments
Open

Comments

@GWRon
Copy link
Contributor

GWRon commented Oct 25, 2024

Heya,

am just experimenting a bit with the resizeable windows (sdlrendermax2d offers them conveniently). For now the graphics context gets already automatically updated by SDL .. so it scales (keeping aspect ratio) and all this.
But stuff like GraphicsWidth() and others simply return the values of some variables which are not updated when the windows resizes.
Yes, one method would be to have custom callbacks (onWindowResize ...) added or similar stuff. But I am still experimenting what and how to approach all this (eg you can have multiple contexts/canvases in a window etc).

So I wanted to add a hook to listen to EVENT_WINDOWSIZE (which is BBEVENT_WINDOWSIZE which itself listens to the SDL-equivalent). You cannot simply attach a callback to a given eventID (eg EVENT_WINDOWSIZE). You have to add a "hook" (up to 256 hooks at all are allowed as brl.hooks has an artificial limit of 256). So when using brl.event' (and also brl.hooks`) you can only add a hook which itself is a function then receiving ALL events (unfiltered).

So a way to have a callback for this event would be to act similar to brl.eventqueue which itself adds to the EmitEventHook-Event and then has to cast the object into an TEvent, then check the ID of the event if it is of interest.

SuperStrict
Framework Brl.StandardIO
Import SDL.SDLRenderMax2D

'react to window resizes
AddHook(EmitEventHook, WindowResizedHook, Null, -10000)


Graphics 800,600,0,0, SDL_WINDOW_RESIZABLE

Repeat
	SetClsColor 100,100,0
	Cls
	DrawText "Window: " + GraphicsWidth()+"x"+GraphicsHeight(),5,1 * 12
	DrawText "Mouse: Virtual=" + TrimFloat(VirtualMouseX(),2)+", "+TrimFloat(VirtualMouseY(),3),5,2 * 12
	DrawText "       Normal =" + TrimFloat(MouseX(),2)+", "+TrimFloat(MouseY(),3),5,3 * 12

	Flip
Until AppTerminate() or KeyHit(KEY_ESCAPE)


'This function will be called as soon as the window resizes
Function WindowResizedHook:Object( id:Int, data:Object,context:Object )
	Local ev:TEvent=TEvent( data )
	If Not ev Return Null
	
	If ev.id <> EVENT_WINDOWSIZE Then Return Null
	
	print "resize"
End Function


Function TrimFloat:String(value:Float, precision:Int = 2)
	Local c:Int = string( int(value) ).length + 1 + precision
	Return string(value)[.. c]
End Function

As you see this means if somehow the application triggers 1000 events per second then eg the eventqueue will check all 1000, my custom WindowResizedHook will also check all 1000...

So it is a bit like a "broadcast" system - but unfiltered. If each hook could define what it is interested in, then hooks could be run "by emitted event x" instead of "for all emitted events".

As said - this aside a "callback" in this very situation might be the better approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant