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 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 */
267extern SDLMAIN_DECLSPEC int SDLCALL SDL_AppIterate(void);
268
269/**
270 * App-implemented event entry point for SDL_MAIN_USE_CALLBACKS apps.
271 *
272 * Apps implement this function when using SDL_MAIN_USE_CALLBACKS. If
273 * using a standard "main" function, you should not supply this.
274 *
275 * This function is called as needed by SDL after SDL_AppInit returns 0;
276 * It is called once for each new event.
277 *
278 * There is (currently) no guarantee about what thread this will be called
279 * from; whatever thread pushes an event onto SDL's queue will trigger this
280 * function. SDL is responsible for pumping the event queue between
281 * each call to SDL_AppIterate, so in normal operation one should only
282 * get events in a serial fashion, but be careful if you have a thread that
283 * explicitly calls SDL_PushEvent.
284 *
285 * Events sent to this function are not owned by the app; if you need to
286 * save the data, you should copy it.
287 *
288 * You do not need to free event data (such as the `file` string in
289 * SDL_EVENT_DROP_FILE), as SDL will free it once this function returns.
290 * Note that this is different than one might expect when using a standard
291 * "main" function!
292 *
293 * This function should not go into an infinite mainloop; it should handle
294 * the provided event appropriately and return.
295 *
296 * If this function returns 0, the app will continue normal operation,
297 * receiving repeated calls to SDL_AppIterate and SDL_AppEvent for the life
298 * of the program. If this function returns < 0, SDL will call SDL_AppQuit
299 * and terminate the process with an exit code that reports an error to the
300 * platform. If it returns > 0, the SDL calls SDL_AppQuit and terminates with
301 * an exit code that reports success to the platform.
302 *
303 * \returns -1 to terminate with an error, 1 to terminate with success, 0 to continue.
304 *
305 * \threadsafety This function is not thread safe.
306 *
307 * \since This function is available since SDL 3.0.0.
308 *
309 * \sa SDL_AppInit
310 * \sa SDL_AppIterate
311 */
312extern SDLMAIN_DECLSPEC int SDLCALL SDL_AppEvent(const SDL_Event *event);
313
314/**
315 * App-implemented deinit entry point for SDL_MAIN_USE_CALLBACKS apps.
316 *
317 * Apps implement this function when using SDL_MAIN_USE_CALLBACKS. If
318 * using a standard "main" function, you should not supply this.
319 *
320 * This function is called once by SDL before terminating the program.
321 *
322 * This function will be called no matter what, even if SDL_AppInit
323 * requests termination.
324 *
325 * This function should not go into an infinite mainloop; it should
326 * deinitialize any resources necessary, perform whatever shutdown
327 * activities, and return.
328 *
329 * You do not need to call SDL_Quit() in this function, as SDL will call
330 * it after this function returns and before the process terminates, but
331 * it is safe to do so.
332 *
333 * \threadsafety This function is not thread safe.
334 *
335 * \since This function is available since SDL 3.0.0.
336 *
337 * \sa SDL_AppInit
338 */
339extern SDLMAIN_DECLSPEC void SDLCALL SDL_AppQuit(void);
340
341#endif /* SDL_MAIN_USE_CALLBACKS */
342
343
344/**
345 * The prototype for the application's main() function
346 */
347typedef int (SDLCALL *SDL_main_func)(int argc, char *argv[]);
348extern SDLMAIN_DECLSPEC int SDLCALL SDL_main(int argc, char *argv[]);
349
350
351/**
352 * Circumvent failure of SDL_Init() when not using SDL_main() as an entry
353 * point.
354 *
355 * This function is defined in SDL_main.h, along with the preprocessor rule to
356 * redefine main() as SDL_main(). Thus to ensure that your main() function
357 * will not be changed it is necessary to define SDL_MAIN_HANDLED before
358 * including SDL.h.
359 *
360 * \since This function is available since SDL 3.0.0.
361 *
362 * \sa SDL_Init
363 */
364extern DECLSPEC void SDLCALL SDL_SetMainReady(void);
365
366/**
367 * Initializes and launches an SDL application, by doing platform-specific
368 * initialization before calling your mainFunction and cleanups after it
369 * returns, if that is needed for a specific platform, otherwise it just calls
370 * mainFunction.
371 *
372 * You can use this if you want to use your own main() implementation without
373 * using SDL_main (like when using SDL_MAIN_HANDLED). When using this, you do
374 * *not* need SDL_SetMainReady().
375 *
376 * \param argc The argc parameter from the application's main() function, or 0
377 * if the platform's main-equivalent has no argc
378 * \param argv The argv parameter from the application's main() function, or
379 * NULL if the platform's main-equivalent has no argv
380 * \param mainFunction Your SDL app's C-style main(), an SDL_main_func. NOT
381 * the function you're calling this from! Its name doesn't
382 * matter, but its signature must be like int my_main(int
383 * argc, char* argv[])
384 * \param reserved should be NULL (reserved for future use, will probably be
385 * platform-specific then)
386 * \returns the return value from mainFunction: 0 on success, -1 on failure;
387 * SDL_GetError() might have more information on the failure
388 *
389 * \since This function is available since SDL 3.0.0.
390 */
391extern DECLSPEC int SDLCALL SDL_RunApp(int argc, char* argv[], SDL_main_func mainFunction, void * reserved);
392
393/**
394 * An entry point for SDL's use in SDL_MAIN_USE_CALLBACKS.
395 *
396 * Generally, you should not call this function directly. This only exists to
397 * hand off work into SDL as soon as possible, where it has a lot more control
398 * and functionality available, and make the inline code in SDL_main.h as
399 * small as possible.
400 *
401 * Not all platforms use this, it's actual use is hidden in a magic
402 * header-only library, and you should not call this directly unless you
403 * _really_ know what you're doing.
404 *
405 * \param argc standard Unix main argc
406 * \param argv standard Unix main argv
407 * \param appinit The application's SDL_AppInit function
408 * \param appiter The application's SDL_AppIterate function
409 * \param appevent The application's SDL_AppEvent function
410 * \param appquit The application's SDL_AppQuit function
411 * \returns standard Unix main return value
412 *
413 * \threadsafety It is not safe to call this anywhere except as the only
414 * function call in SDL_main.
415 *
416 * \since This function is available since SDL 3.0.0.
417 */
418extern 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);
419
420
421#if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK)
422
423/**
424 * Register a win32 window class for SDL's use.
425 *
426 * This can be called to set the application window class at startup. It is
427 * safe to call this multiple times, as long as every call is eventually
428 * paired with a call to SDL_UnregisterApp, but a second registration attempt
429 * while a previous registration is still active will be ignored, other than
430 * to increment a counter.
431 *
432 * Most applications do not need to, and should not, call this directly; SDL
433 * will call it when initializing the video subsystem.
434 *
435 * \param name the window class name, in UTF-8 encoding. If NULL, SDL
436 * currently uses "SDL_app" but this isn't guaranteed.
437 * \param style the value to use in WNDCLASSEX::style. If `name` is NULL, SDL
438 * currently uses `(CS_BYTEALIGNCLIENT | CS_OWNDC)` regardless of
439 * what is specified here.
440 * \param hInst the HINSTANCE to use in WNDCLASSEX::hInstance. If zero, SDL
441 * will use `GetModuleHandle(NULL)` instead.
442 * \returns 0 on success or a negative error code on failure; call
443 * SDL_GetError() for more information.
444 *
445 * \since This function is available since SDL 3.0.0.
446 */
447extern DECLSPEC int SDLCALL SDL_RegisterApp(const char *name, Uint32 style, void *hInst);
448
449/**
450 * Deregister the win32 window class from an SDL_RegisterApp call.
451 *
452 * This can be called to undo the effects of SDL_RegisterApp.
453 *
454 * Most applications do not need to, and should not, call this directly; SDL
455 * will call it when deinitializing the video subsystem.
456 *
457 * It is safe to call this multiple times, as long as every call is eventually
458 * paired with a prior call to SDL_RegisterApp. The window class will only be
459 * deregistered when the registration counter in SDL_RegisterApp decrements to
460 * zero through calls to this function.
461 *
462 * \since This function is available since SDL 3.0.0.
463 */
464extern DECLSPEC void SDLCALL SDL_UnregisterApp(void);
465
466#endif /* defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK) */
467
468#ifdef SDL_PLATFORM_GDK
469
470/**
471 * Callback from the application to let the suspend continue.
472 *
473 * \since This function is available since SDL 3.0.0.
474 */
475extern DECLSPEC void SDLCALL SDL_GDKSuspendComplete(void);
476
477#endif /* SDL_PLATFORM_GDK */
478
479#ifdef __cplusplus
480}
481#endif
482
483#include <SDL3/SDL_close_code.h>
484
485#if !defined(SDL_MAIN_HANDLED) && !defined(SDL_MAIN_NOIMPL)
486 /* include header-only SDL_main implementations */
487 #if defined(SDL_MAIN_USE_CALLBACKS) \
488 || defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK) || defined(SDL_PLATFORM_IOS) || defined(SDL_PLATFORM_TVOS) \
489 || defined(SDL_PLATFORM_3DS) || defined(SDL_PLATFORM_NGAGE) || defined(SDL_PLATFORM_PS2) || defined(SDL_PLATFORM_PSP)
490
491 /* platforms which main (-equivalent) can be implemented in plain C */
492 #include <SDL3/SDL_main_impl.h>
493
494 #elif defined(SDL_PLATFORM_WINRT) /* C++ platforms */
495 #ifdef __cplusplus
496 #include <SDL3/SDL_main_impl.h>
497 #else
498 /* Note: to get rid of the following warning, you can #define SDL_MAIN_NOIMPL before including SDL_main.h
499 * 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()!
500 */
501 #ifdef _MSC_VER
502 #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>")
503 #elif defined(__GNUC__) /* gcc, clang, mingw and compatible are matched by this and have #warning */
504 #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>"
505 #endif /* __GNUC__ */
506 #endif /* __cplusplus */
507
508 #endif /* C++ platforms like SDL_PLATFORM_WINRT etc */
509#endif
510
511#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:347
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