Skip to content

Commit 12f2d21

Browse files
committed
main: Document SDL_RunApp() argv handling
1 parent e9250fc commit 12f2d21

File tree

1 file changed

+38
-20
lines changed

1 file changed

+38
-20
lines changed

include/SDL3/SDL_main.h

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@
4141
* This is also where an app can be configured to use the main callbacks, via
4242
* the SDL_MAIN_USE_CALLBACKS macro.
4343
*
44-
* SDL_main.h is a "single-header library," which is to say that including
44+
* `SDL_main.h` is a "single-header library," which is to say that including
4545
* this header inserts code into your program, and you should only include it
46-
* once in most cases. SDL.h does not include this header automatically.
46+
* once in most cases. `SDL.h` does not include this header automatically.
47+
*
48+
* If you want to include `SDL_main.h` but don't want SDL to redefine main(),
49+
* you can define SDL_MAIN_HANDLED before including `SDL_main.h`.
4750
*
4851
* For more information, see:
4952
*
@@ -64,9 +67,10 @@
6467
* Inform SDL that the app is providing an entry point instead of SDL.
6568
*
6669
* SDL does not define this macro, but will check if it is defined when
67-
* including `SDL_main.h`. If defined, SDL will expect the app to provide the
68-
* proper entry point for the platform, and all the other magic details
69-
* needed, like manually calling SDL_SetMainReady.
70+
* including `SDL_main.h`. If defined, SDL expects the app to provide a proper
71+
* entry point for the platform that either calls SDL_RunApp() to let SDL
72+
* initialize all platform-specific details, or initializes everything itself
73+
* outside of SDL before finally calling SDL_SetMainReady().
7074
*
7175
* Please see [README-main-functions](README-main-functions), (or
7276
* docs/README-main-functions.md in the source tree) for a more detailed
@@ -115,7 +119,7 @@
115119
*
116120
* \since This macro is available since SDL 3.2.0.
117121
*/
118-
#define SDL_MAIN_AVAILABLE
122+
#define SDL_MAIN_AVAILABLE 1
119123

120124
/**
121125
* Defined if the target platform _requires_ a special mainline through SDL.
@@ -134,7 +138,7 @@
134138
*
135139
* \since This macro is available since SDL 3.2.0.
136140
*/
137-
#define SDL_MAIN_NEEDED
141+
#define SDL_MAIN_NEEDED 1
138142

139143
#endif
140144

@@ -250,12 +254,12 @@
250254
*
251255
* \sa SDL_DECLSPEC
252256
*/
253-
#define SDLMAIN_DECLSPEC
257+
#define SDLMAIN_DECLSPEC SDL_DECLSPEC
254258

255259
#elif defined(SDL_MAIN_EXPORTED)
256260
/* We need to export SDL_main so it can be launched from external code,
257261
like SDLActivity.java on Android */
258-
#define SDLMAIN_DECLSPEC SDL_DECLSPEC
262+
#define SDLMAIN_DECLSPEC SDL_DECLSPEC
259263
#else
260264
/* usually this is empty */
261265
#define SDLMAIN_DECLSPEC
@@ -531,34 +535,46 @@ typedef int (SDLCALL *SDL_main_func)(int argc, char *argv[]);
531535
extern SDLMAIN_DECLSPEC int SDLCALL SDL_main(int argc, char *argv[]);
532536

533537
/**
534-
* Circumvent failure of SDL_Init() when not using SDL_main() as an entry
535-
* point.
538+
* Informs SDL that an app that provides its own entry point instead of using
539+
* SDL_main() has initialized all platform-specific details outside of SDL.
540+
*
541+
* Apps that don't use SDL_main() should call SDL_SetMainReady() before
542+
* calling SDL_Init(), otherwise SDL_Init() may fail on some platforms.
536543
*
537-
* This function is defined in SDL_main.h, along with the preprocessor rule to
538-
* redefine main() as SDL_main(). Thus to ensure that your main() function
539-
* will not be changed it is necessary to define SDL_MAIN_HANDLED before
540-
* including SDL.h.
544+
* If your app provides its own entry point, consider instead using
545+
* SDL_RunApp(), which automatically initializes all platform-specific details
546+
* for you and is generally more portable and reliable than initializing
547+
* everything on your own. When using SDL_RunApp(), you do *not* need to call
548+
* SDL_SetMainReady().
541549
*
542550
* \since This function is available since SDL 3.2.0.
543551
*
552+
* \sa SDL_RunApp
544553
* \sa SDL_Init
545554
*/
546555
extern SDL_DECLSPEC void SDLCALL SDL_SetMainReady(void);
547556

548557
/**
549558
* Initializes and launches an SDL application, by doing platform-specific
550-
* initialization before calling your mainFunction and cleanups after it
559+
* initialization before calling your `mainFunction` and cleanups after it
551560
* returns, if that is needed for a specific platform, otherwise it just calls
552-
* mainFunction.
561+
* `mainFunction`.
553562
*
554563
* You can use this if you want to use your own main() implementation without
555-
* using SDL_main (like when using SDL_MAIN_HANDLED). When using this, you do
556-
* *not* need SDL_SetMainReady().
564+
* using SDL_main() (like when using SDL_MAIN_HANDLED). When using this, you
565+
* do *not* need to call SDL_SetMainReady().
566+
*
567+
* If `argv` is non-NULL, SDL will pass it forward to `mainFunction` as-is.
568+
* Otherwise, SDL will override `argv` in a platform-specific way. On some
569+
* platforms like Windows, SDL may try to get the command line string and
570+
* parse it into an argv. On others, it might just fall back to a dummy argv.
571+
* Either way, SDL ensures that the argv passed to `mainFunction` is not NULL.
557572
*
558573
* \param argc the argc parameter from the application's main() function, or 0
559574
* if the platform's main-equivalent has no argc.
560575
* \param argv the argv parameter from the application's main() function, or
561-
* NULL if the platform's main-equivalent has no argv.
576+
* NULL if the platform's main-equivalent has no argv (in which
577+
* case SDL will override it in a platform-specific way).
562578
* \param mainFunction your SDL app's C-style main(). NOT the function you're
563579
* calling this from! Its name doesn't matter; it doesn't
564580
* literally have to be `main`.
@@ -572,6 +588,8 @@ extern SDL_DECLSPEC void SDLCALL SDL_SetMainReady(void);
572588
* process's initial thread.
573589
*
574590
* \since This function is available since SDL 3.2.0.
591+
*
592+
* \sa SDL_SetMainReady
575593
*/
576594
extern SDL_DECLSPEC int SDLCALL SDL_RunApp(int argc, char *argv[], SDL_main_func mainFunction, void *reserved);
577595

0 commit comments

Comments
 (0)