SDL 3.0
SDL_main.h
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21
22#ifndef SDL_main_h_
23#define SDL_main_h_
24
26#include <SDL3/SDL_stdinc.h>
27#include <SDL3/SDL_error.h>
28#include <SDL3/SDL_events.h>
29
30/*
31 * For details on how SDL_main works, and how to use it, please refer to:
32 *
33 * https://wiki.libsdl.org/SDL3/README/main-functions
34 *
35 * (or docs/README-main-functions.md in the SDL source tree)
36 */
37
38/**
39 * \file SDL_main.h
40 *
41 * Redefine main() on some platforms so that it is called by SDL.
42 */
43
44#ifndef SDL_MAIN_HANDLED
45 #ifdef SDL_PLATFORM_WIN32
46 /* On Windows SDL provides WinMain(), which parses the command line and passes
47 the arguments to your main function.
48
49 If you provide your own WinMain(), you may define SDL_MAIN_HANDLED
50 */
51 #define SDL_MAIN_AVAILABLE
52
53 #elif defined(SDL_PLATFORM_WINRT)
54 /* On WinRT, SDL provides a main function that initializes CoreApplication,
55 creating an instance of IFrameworkView in the process.
56
57 Ideally, #include'ing SDL_main.h is enough to get a main() function working.
58 However, that requires the source file your main() is in to be compiled
59 as C++ *and* with the /ZW compiler flag. If that's not feasible, add an
60 otherwise empty .cpp file that only contains `#include <SDL3/SDL_main.h>`
61 and build that with /ZW (still include SDL_main.h in your other file with main()!).
62 In XAML apps, instead the function SDL_RunApp() must be called with a pointer
63 to the Direct3D-hosted XAML control passed in as the "reserved" argument.
64 */
65 #define SDL_MAIN_NEEDED
66
67 #elif defined(SDL_PLATFORM_GDK)
68 /* On GDK, SDL provides a main function that initializes the game runtime.
69
70 If you prefer to write your own WinMain-function instead of having SDL
71 provide one that calls your main() function,
72 #define SDL_MAIN_HANDLED before #include'ing SDL_main.h
73 and call the SDL_RunApp function from your entry point.
74 */
75 #define SDL_MAIN_NEEDED
76
77 #elif defined(SDL_PLATFORM_IOS)
78 /* On iOS SDL provides a main function that creates an application delegate
79 and starts the iOS application run loop.
80
81 To use it, just #include SDL_main.h in the source file that contains your
82 main() function.
83
84 See src/video/uikit/SDL_uikitappdelegate.m for more details.
85 */
86 #define SDL_MAIN_NEEDED
87
88 #elif defined(SDL_PLATFORM_ANDROID)
89 /* On Android SDL provides a Java class in SDLActivity.java that is the
90 main activity entry point.
91
92 See docs/README-android.md for more details on extending that class.
93 */
94 #define SDL_MAIN_NEEDED
95
96 /* We need to export SDL_main so it can be launched from Java */
97 #define SDLMAIN_DECLSPEC DECLSPEC
98
99 #elif defined(SDL_PLATFORM_PSP)
100 /* On PSP SDL provides a main function that sets the module info,
101 activates the GPU and starts the thread required to be able to exit
102 the software.
103
104 If you provide this yourself, you may define SDL_MAIN_HANDLED
105 */
106 #define SDL_MAIN_AVAILABLE
107
108 #elif defined(SDL_PLATFORM_PS2)
109 #define SDL_MAIN_AVAILABLE
110
111 #define SDL_PS2_SKIP_IOP_RESET() \
112 void reset_IOP(); \
113 void reset_IOP() {}
114
115 #elif defined(SDL_PLATFORM_3DS)
116 /*
117 On N3DS, SDL provides a main function that sets up the screens
118 and storage.
119
120 If you provide this yourself, you may define SDL_MAIN_HANDLED
121 */
122 #define SDL_MAIN_AVAILABLE
123
124 #elif defined(SDL_PLATFORM_NGAGE)
125 /*
126 TODO: not sure if it should be SDL_MAIN_NEEDED, in SDL2 ngage had a
127 main implementation, but wasn't mentioned in SDL_main.h
128 */
129 #define SDL_MAIN_AVAILABLE
130
131 #endif
132#endif /* SDL_MAIN_HANDLED */
133
134#ifndef SDLMAIN_DECLSPEC
135#define SDLMAIN_DECLSPEC
136#endif
137
138/**
139 * \file SDL_main.h
140 *
141 * The application's main() function must be called with C linkage,
142 * and should be declared like this:
143 *
144 * ```c
145 * #ifdef __cplusplus
146 * extern "C"
147 * #endif
148 * int main(int argc, char *argv[])
149 * {
150 * }
151 * ```
152 */
153
154#if defined(SDL_MAIN_NEEDED) || defined(SDL_MAIN_AVAILABLE) || defined(SDL_MAIN_USE_CALLBACKS)
155#define main SDL_main
156#endif
157
158#include <SDL3/SDL_begin_code.h>
159#ifdef __cplusplus
160extern "C" {
161#endif
162
163typedef int (SDLCALL *SDL_AppInit_func)(void **appstate, int argc, char *argv[]);
164typedef int (SDLCALL *SDL_AppIterate_func)(void *appstate);
165typedef int (SDLCALL *SDL_AppEvent_func)(void *appstate, const SDL_Event *event);
166typedef void (SDLCALL *SDL_AppQuit_func)(void *appstate);
167
168/**
169 * You can (optionally!) define SDL_MAIN_USE_CALLBACKS before including
170 * SDL_main.h, and then your application will _not_ have a standard
171 * "main" entry point. Instead, it will operate as a collection of
172 * functions that are called as necessary by the system. On some
173 * platforms, this is just a layer where SDL drives your program
174 * instead of your program driving SDL, on other platforms this might
175 * hook into the OS to manage the lifecycle. Programs on most platforms
176 * can use whichever approach they prefer, but the decision boils down
177 * to:
178 *
179 * - Using a standard "main" function: this works like it always has for
180 * the past 50+ years in C programming, and your app is in control.
181 * - Using the callback functions: this might clean up some code,
182 * avoid some #ifdef blocks in your program for some platforms, be more
183 * resource-friendly to the system, and possibly be the primary way to
184 * access some future platforms (but none require this at the moment).
185 *
186 * This is up to the app; both approaches are considered valid and supported
187 * ways to write SDL apps.
188 *
189 * If using the callbacks, don't define a "main" function. Instead, implement
190 * the functions listed below in your program.
191 */
192#ifdef SDL_MAIN_USE_CALLBACKS
193
194/**
195 * App-implemented initial entry point for SDL_MAIN_USE_CALLBACKS apps.
196 *
197 * Apps implement this function when using SDL_MAIN_USE_CALLBACKS. If
198 * using a standard "main" function, you should not supply this.
199 *
200 * This function is called by SDL once, at startup. The function should
201 * initialize whatever is necessary, possibly create windows and open
202 * audio devices, etc. The `argc` and `argv` parameters work like they would
203 * with a standard "main" function.
204 *
205 * This function should not go into an infinite mainloop; it should do any
206 * one-time setup it requires and then return.
207 *
208 * The app may optionally assign a pointer to `*appstate`. This pointer will
209 * be provided on every future call to the other entry points, to allow
210 * application state to be preserved between functions without the app
211 * needing to use a global variable. If this isn't set, the pointer will
212 * be NULL in future entry points.
213 *
214 * If this function returns 0, the app will proceed to normal operation,
215 * and will begin receiving repeated calls to SDL_AppIterate and SDL_AppEvent
216 * for the life of the program. If this function returns < 0, SDL will
217 * call SDL_AppQuit and terminate the process with an exit code that reports
218 * an error to the platform. If it returns > 0, the SDL calls SDL_AppQuit
219 * and terminates with an exit code that reports success to the platform.
220 *
221 * \param appstate a place where the app can optionally store a pointer for future use.
222 * \param argc The standard ANSI C main's argc; number of elements in `argv`
223 * \param argv The standard ANSI C main's argv; array of command line arguments.
224 * \returns -1 to terminate with an error, 1 to terminate with success, 0 to continue.
225 *
226 * \threadsafety This function is not thread safe.
227 *
228 * \since This function is available since SDL 3.0.0.
229 *
230 * \sa SDL_AppIterate
231 * \sa SDL_AppEvent
232 * \sa SDL_AppQuit
233 */
234extern SDLMAIN_DECLSPEC int SDLCALL SDL_AppInit(void **appstate, int argc, char *argv[]);
235
236/**
237 * App-implemented iteration entry point for SDL_MAIN_USE_CALLBACKS apps.
238 *
239 * Apps implement this function when using SDL_MAIN_USE_CALLBACKS. If
240 * using a standard "main" function, you should not supply this.
241 *
242 * This function is called repeatedly by SDL after SDL_AppInit returns 0.
243 * The function should operate as a single iteration the program's primary
244 * loop; it should update whatever state it needs and draw a new frame of
245 * video, usually.
246 *
247 * On some platforms, this function will be called at the refresh rate of
248 * the display (which might change during the life of your app!). There are
249 * no promises made about what frequency this function might run at. You
250 * should use SDL's timer functions if you need to see how much time has
251 * passed since the last iteration.
252 *
253 * There is no need to process the SDL event queue during this function;
254 * SDL will send events as they arrive in SDL_AppEvent, and in most cases
255 * the event queue will be empty when this function runs anyhow.
256 *
257 * This function should not go into an infinite mainloop; it should do one
258 * iteration of whatever the program does and return.
259 *
260 * The `appstate` parameter is an optional pointer provided by the app during
261 * SDL_AppInit(). If the app never provided a pointer, this will be NULL.
262 *
263 * If this function returns 0, the app will continue normal operation,
264 * receiving repeated calls to SDL_AppIterate and SDL_AppEvent for the life
265 * of the program. If this function returns < 0, SDL will call SDL_AppQuit
266 * and terminate the process with an exit code that reports an error to the
267 * platform. If it returns > 0, the SDL calls SDL_AppQuit and terminates with
268 * an exit code that reports success to the platform.
269 *
270 * \param appstate an optional pointer, provided by the app in SDL_AppInit.
271 * \returns -1 to terminate with an error, 1 to terminate with success, 0 to continue.
272 *
273 * \threadsafety This function is not thread safe.
274 *
275 * \since This function is available since SDL 3.0.0.
276 *
277 * \sa SDL_AppInit
278 * \sa SDL_AppEvent
279 */
280extern SDLMAIN_DECLSPEC int SDLCALL SDL_AppIterate(void *appstate);
281
282/**
283 * App-implemented event entry point for SDL_MAIN_USE_CALLBACKS apps.
284 *
285 * Apps implement this function when using SDL_MAIN_USE_CALLBACKS. If
286 * using a standard "main" function, you should not supply this.
287 *
288 * This function is called as needed by SDL after SDL_AppInit returns 0;
289 * It is called once for each new event.
290 *
291 * There is (currently) no guarantee about what thread this will be called
292 * from; whatever thread pushes an event onto SDL's queue will trigger this
293 * function. SDL is responsible for pumping the event queue between
294 * each call to SDL_AppIterate, so in normal operation one should only
295 * get events in a serial fashion, but be careful if you have a thread that
296 * explicitly calls SDL_PushEvent.
297 *
298 * Events sent to this function are not owned by the app; if you need to
299 * save the data, you should copy it.
300 *
301 * You do not need to free event data (such as the `file` string in
302 * SDL_EVENT_DROP_FILE), as SDL will free it once this function returns.
303 * Note that this is different than one might expect when using a standard
304 * "main" function!
305 *
306 * This function should not go into an infinite mainloop; it should handle
307 * the provided event appropriately and return.
308 *
309 * The `appstate` parameter is an optional pointer provided by the app during
310 * SDL_AppInit(). If the app never provided a pointer, this will be NULL.
311 *
312 * If this function returns 0, the app will continue normal operation,
313 * receiving repeated calls to SDL_AppIterate and SDL_AppEvent for the life
314 * of the program. If this function returns < 0, SDL will call SDL_AppQuit
315 * and terminate the process with an exit code that reports an error to the
316 * platform. If it returns > 0, the SDL calls SDL_AppQuit and terminates with
317 * an exit code that reports success to the platform.
318 *
319 * \param appstate an optional pointer, provided by the app in SDL_AppInit.
320 * \param event the new event for the app to examine.
321 * \returns -1 to terminate with an error, 1 to terminate with success, 0 to continue.
322 *
323 * \threadsafety This function is not thread safe.
324 *
325 * \since This function is available since SDL 3.0.0.
326 *
327 * \sa SDL_AppInit
328 * \sa SDL_AppIterate
329 */
330extern SDLMAIN_DECLSPEC int SDLCALL SDL_AppEvent(void *appstate, const SDL_Event *event);
331
332/**
333 * App-implemented deinit entry point for SDL_MAIN_USE_CALLBACKS apps.
334 *
335 * Apps implement this function when using SDL_MAIN_USE_CALLBACKS. If
336 * using a standard "main" function, you should not supply this.
337 *
338 * This function is called once by SDL before terminating the program.
339 *
340 * This function will be called no matter what, even if SDL_AppInit
341 * requests termination.
342 *
343 * This function should not go into an infinite mainloop; it should
344 * deinitialize any resources necessary, perform whatever shutdown
345 * activities, and return.
346 *
347 * You do not need to call SDL_Quit() in this function, as SDL will call
348 * it after this function returns and before the process terminates, but
349 * it is safe to do so.
350 *
351 * The `appstate` parameter is an optional pointer provided by the app during
352 * SDL_AppInit(). If the app never provided a pointer, this will be NULL.
353 * This function call is the last time this pointer will be provided, so
354 * any resources to it should be cleaned up here.
355 *
356 * \param appstate an optional pointer, provided by the app in SDL_AppInit.
357 *
358 * \threadsafety This function is not thread safe.
359 *
360 * \since This function is available since SDL 3.0.0.
361 *
362 * \sa SDL_AppInit
363 */
364extern SDLMAIN_DECLSPEC void SDLCALL SDL_AppQuit(void *appstate);
365
366#endif /* SDL_MAIN_USE_CALLBACKS */
367
368
369/**
370 * The prototype for the application's main() function
371 *
372 * \since This datatype is available since SDL 3.0.0.
373 */
374typedef int (SDLCALL *SDL_main_func)(int argc, char *argv[]);
375extern SDLMAIN_DECLSPEC int SDLCALL SDL_main(int argc, char *argv[]);
376
377
378/**
379 * Circumvent failure of SDL_Init() when not using SDL_main() as an entry
380 * point.
381 *
382 * This function is defined in SDL_main.h, along with the preprocessor rule to
383 * redefine main() as SDL_main(). Thus to ensure that your main() function
384 * will not be changed it is necessary to define SDL_MAIN_HANDLED before
385 * including SDL.h.
386 *
387 * \since This function is available since SDL 3.0.0.
388 *
389 * \sa SDL_Init
390 */
391extern DECLSPEC void SDLCALL SDL_SetMainReady(void);
392
393/**
394 * Initializes and launches an SDL application, by doing platform-specific
395 * initialization before calling your mainFunction and cleanups after it
396 * returns, if that is needed for a specific platform, otherwise it just calls
397 * mainFunction.
398 *
399 * You can use this if you want to use your own main() implementation without
400 * using SDL_main (like when using SDL_MAIN_HANDLED). When using this, you do
401 * *not* need SDL_SetMainReady().
402 *
403 * \param argc The argc parameter from the application's main() function, or 0
404 * if the platform's main-equivalent has no argc
405 * \param argv The argv parameter from the application's main() function, or
406 * NULL if the platform's main-equivalent has no argv
407 * \param mainFunction Your SDL app's C-style main(), an SDL_main_func. NOT
408 * the function you're calling this from! Its name doesn't
409 * matter, but its signature must be like int my_main(int
410 * argc, char* argv[])
411 * \param reserved should be NULL (reserved for future use, will probably be
412 * platform-specific then)
413 * \returns the return value from mainFunction: 0 on success, -1 on failure;
414 * SDL_GetError() might have more information on the failure
415 *
416 * \since This function is available since SDL 3.0.0.
417 */
418extern DECLSPEC int SDLCALL SDL_RunApp(int argc, char* argv[], SDL_main_func mainFunction, void * reserved);
419
420/**
421 * An entry point for SDL's use in SDL_MAIN_USE_CALLBACKS.
422 *
423 * Generally, you should not call this function directly. This only exists to
424 * hand off work into SDL as soon as possible, where it has a lot more control
425 * and functionality available, and make the inline code in SDL_main.h as
426 * small as possible.
427 *
428 * Not all platforms use this, it's actual use is hidden in a magic
429 * header-only library, and you should not call this directly unless you
430 * _really_ know what you're doing.
431 *
432 * \param argc standard Unix main argc
433 * \param argv standard Unix main argv
434 * \param appinit The application's SDL_AppInit function
435 * \param appiter The application's SDL_AppIterate function
436 * \param appevent The application's SDL_AppEvent function
437 * \param appquit The application's SDL_AppQuit function
438 * \returns standard Unix main return value
439 *
440 * \threadsafety It is not safe to call this anywhere except as the only
441 * function call in SDL_main.
442 *
443 * \since This function is available since SDL 3.0.0.
444 */
445extern DECLSPEC int SDLCALL SDL_EnterAppMainCallbacks(int argc, char* argv[], SDL_AppInit_func appinit, SDL_AppIterate_func appiter, SDL_AppEvent_func appevent, SDL_AppQuit_func appquit);
446
447
448#if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK)
449
450/**
451 * Register a win32 window class for SDL's use.
452 *
453 * This can be called to set the application window class at startup. It is
454 * safe to call this multiple times, as long as every call is eventually
455 * paired with a call to SDL_UnregisterApp, but a second registration attempt
456 * while a previous registration is still active will be ignored, other than
457 * to increment a counter.
458 *
459 * Most applications do not need to, and should not, call this directly; SDL
460 * will call it when initializing the video subsystem.
461 *
462 * \param name the window class name, in UTF-8 encoding. If NULL, SDL
463 * currently uses "SDL_app" but this isn't guaranteed.
464 * \param style the value to use in WNDCLASSEX::style. If `name` is NULL, SDL
465 * currently uses `(CS_BYTEALIGNCLIENT | CS_OWNDC)` regardless of
466 * what is specified here.
467 * \param hInst the HINSTANCE to use in WNDCLASSEX::hInstance. If zero, SDL
468 * will use `GetModuleHandle(NULL)` instead.
469 * \returns 0 on success or a negative error code on failure; call
470 * SDL_GetError() for more information.
471 *
472 * \since This function is available since SDL 3.0.0.
473 */
474extern DECLSPEC int SDLCALL SDL_RegisterApp(const char *name, Uint32 style, void *hInst);
475
476/**
477 * Deregister the win32 window class from an SDL_RegisterApp call.
478 *
479 * This can be called to undo the effects of SDL_RegisterApp.
480 *
481 * Most applications do not need to, and should not, call this directly; SDL
482 * will call it when deinitializing the video subsystem.
483 *
484 * It is safe to call this multiple times, as long as every call is eventually
485 * paired with a prior call to SDL_RegisterApp. The window class will only be
486 * deregistered when the registration counter in SDL_RegisterApp decrements to
487 * zero through calls to this function.
488 *
489 * \since This function is available since SDL 3.0.0.
490 */
491extern DECLSPEC void SDLCALL SDL_UnregisterApp(void);
492
493#endif /* defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK) */
494
495#ifdef SDL_PLATFORM_GDK
496
497/**
498 * Callback from the application to let the suspend continue.
499 *
500 * \since This function is available since SDL 3.0.0.
501 */
502extern DECLSPEC void SDLCALL SDL_GDKSuspendComplete(void);
503
504#endif /* SDL_PLATFORM_GDK */
505
506#ifdef __cplusplus
507}
508#endif
509
510#include <SDL3/SDL_close_code.h>
511
512#if !defined(SDL_MAIN_HANDLED) && !defined(SDL_MAIN_NOIMPL)
513 /* include header-only SDL_main implementations */
514 #if defined(SDL_MAIN_USE_CALLBACKS) \
515 || defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK) || defined(SDL_PLATFORM_IOS) || defined(SDL_PLATFORM_TVOS) \
516 || defined(SDL_PLATFORM_3DS) || defined(SDL_PLATFORM_NGAGE) || defined(SDL_PLATFORM_PS2) || defined(SDL_PLATFORM_PSP)
517
518 /* platforms which main (-equivalent) can be implemented in plain C */
519 #include <SDL3/SDL_main_impl.h>
520
521 #elif defined(SDL_PLATFORM_WINRT) /* C++ platforms */
522 #ifdef __cplusplus
523 #include <SDL3/SDL_main_impl.h>
524 #else
525 /* Note: to get rid of the following warning, you can #define SDL_MAIN_NOIMPL before including SDL_main.h
526 * in your C sourcefile that contains the standard main. Do *not* use SDL_MAIN_HANDLED for that, then SDL_main won't find your main()!
527 */
528 #ifdef _MSC_VER
529 #pragma message("Note: Your platform needs the SDL_main implementation in a C++ source file. You can keep your main() in plain C (then continue including SDL_main.h there!) and create a fresh .cpp file that only contains #include <SDL3/SDL_main.h>")
530 #elif defined(__GNUC__) /* gcc, clang, mingw and compatible are matched by this and have #warning */
531 #warning "Note: Your platform needs the SDL_main implementation in a C++ source file. You can keep your main() in plain C and create a fresh .cpp file that only contains #include <SDL3/SDL_main.h>"
532 #endif /* __GNUC__ */
533 #endif /* __cplusplus */
534
535 #endif /* C++ platforms like SDL_PLATFORM_WINRT etc */
536#endif
537
538#endif /* SDL_main_h_ */
int(* SDL_AppEvent_func)(void *appstate, const SDL_Event *event)
Definition SDL_main.h:165
void SDL_SetMainReady(void)
SDLMAIN_DECLSPEC int SDL_main(int argc, char *argv[])
#define SDLMAIN_DECLSPEC
Definition SDL_main.h:135
int(* SDL_main_func)(int argc, char *argv[])
Definition SDL_main.h:374
int(* SDL_AppIterate_func)(void *appstate)
Definition SDL_main.h:164
int SDL_EnterAppMainCallbacks(int argc, char *argv[], SDL_AppInit_func appinit, SDL_AppIterate_func appiter, SDL_AppEvent_func appevent, SDL_AppQuit_func appquit)
int SDL_RunApp(int argc, char *argv[], SDL_main_func mainFunction, void *reserved)
void(* SDL_AppQuit_func)(void *appstate)
Definition SDL_main.h:166
int(* SDL_AppInit_func)(void **appstate, int argc, char *argv[])
Definition SDL_main.h:163
uint32_t Uint32
Definition SDL_stdinc.h:224