SDL_RunApp(3)

πŸ’– SDL_RunApp(3) πŸ’–

okay bestie, we're in the SDL3 FUNCTIONS section of the manual, and today's main character is SDL_RunApp. pull up a chair, grab a boba, this one's actually so useful 🫢


✨ NAME ✨

SDL_RunApp β€” initializes and launches an SDL application, by doing platform-specific initialization before calling your mainFunction and cleanups after it returns, if that is needed for a specific platform, otherwise it just calls mainFunction. literally that simple. it's giving "responsible adult who handles the boring stuff so you don't have to" πŸ’…


🌈 SYNOPSIS 🌈

#include <SDL3/SDL_main.h>

int SDL_RunApp(int argc, char *argv[], SDL_main_func mainFunction, void *reserved);

yes bestie, that's the whole thing. four params, one int coming back. slay.


πŸ¦‹ DESCRIPTION πŸ¦‹

okay so. you can use this if you want to use your own main() implementation without using SDL_main (like when using SDL_MAIN_HANDLED). so if you're out here being a DIY queen with your own entry point, this is your girl. πŸ’–

and the best part? when you use this, you do not need SDL_SetMainReady(). not me saving you a whole function call, we love a shortcut ✨

also super important: if argv is NULL, SDL will provide command line arguments, either by querying the OS for them if possible, or supplying a filler array if not. so even if your platform is being weird about args, SDL's got you. no cap. 🧚


πŸŽ€ FUNCTION PARAMETERS πŸŽ€

  • argc β€” the argc parameter from the application's main() function, or 0 if the platform's main-equivalent has no argc. like, zero, nada, nothing. don't stress it.
  • argv β€” the argv parameter from the application's main() function, or NULL if the platform's main-equivalent has no argv.
  • mainFunction β€” your SDL app's C-style main(). NOT the function you're calling this from!! (okay this is the part where everyone gets confused, so let me say it louder for the people in the back πŸ“’) its name doesn't matter; it doesn't literally have to be main. call it goGirlGo for all SDL cares, the function pointer is what counts. πŸ’…
  • reserved β€” should be NULL (reserved for future use, will probably be platform-specific then). it's the mysterious locked door of parameters. leave it alone. πŸ™ˆ

πŸ’€ RETURN VALUE πŸ’€

Returns the return value from mainFunction: 0 on success, otherwise failure; SDL_GetError() might have more information on the failure.

so like, 0 = we ate, anything else = something's wrong and SDL_GetError() is the group chat where the drama gets explained. 😭


πŸ”₯ THREAD SAFETY πŸ”₯

Generally this is called once, near startup, from the process's initial thread.

translation: call it one time, at the beginning, from the thread you started with. don't be out here calling it from random threads, that's not the vibe. ⭐


πŸ‘‘ AVAILABILITY πŸ‘‘

This function is available since SDL 3.2.0. so if you're on anything older... bestie, upgrade. πŸ’œ


that's the whole page!! now go write your own main() and let SDL_RunApp do the platform-specific heavy lifting for you. periodt. πŸ’–βœ¨

β€” cute sign-off from the Simple Directmedia Layer girlies, shipping SDL 3.4.18 πŸŽ€πŸ¦‹πŸŒΈ

πŸ“¦ where this page comes from