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