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