How to implement SDL3 callbacks in Odin

Is there a way to implement the new SDL3 callback functionality in Odin, and if yes then where can I get more information?

1 Like

You will set the callbacks through calling certain procedures found in the sdl3 vendor binding (and the official documentation). Here is a quick example highlighting the setup for one of the four callbacks found in the EnterAppMainCallback procedure:

EnterAppMainCallbacks :: proc(
	argc: c.int, 
	argv: [^]cstring, 
	appinit: AppInit_func, 
	appiter: AppIterate_func, 
	appevent: AppEvent_func,  // <--- we will focus on this callback
	appquit: AppQuit_func
) -> c.int ---

Your callback procedure’s signature should match the corresponding library procedure’s signature.

// sdl3 procedure (https://pkg.odin-lang.org/vendor/sdl3/#AppEvent_func)
AppEvent_func :: #type proc "c" (appstate: rawptr, event: ^Event) -> AppResult

// user defined callback procedure
my_app_event_callback :: proc "c" (appstate: rawptr, event: ^sdl3.Event) -> sdl3.AppResult {
	// process event and return appropriate AppResult value... 
}

When you call EnterAppMainCallbacks, or any other procedure that registers one or more callbacks, pass your callback procedure.

_ := sdl3.EnterAppMainCallbacks(
	// ...
	appevent = my_app_event_callback,
	// ...
)
1 Like

I have an example on my GitHub learningOdin/sdlAppWithCallbacks/sdlAppWithCallbacks.odin at main · thegreatpissant/learningOdin · GitHub

Also found this example on Reddit that scopes the functions differently. https://www.reddit.com/r/odinlang/comments/1itfqoc/odin_w_sdl3_callbacks/

1 Like