SDL 3.0
SDL_main.h
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2023 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
27/**
28 * \file SDL_main.h
29 *
30 * Redefine main() on some platforms so that it is called by SDL.
31 */
32
33#ifndef SDL_MAIN_HANDLED
34#if defined(__WIN32__)
35/* On Windows SDL provides WinMain(), which parses the command line and passes
36 the arguments to your main function.
37
38 If you provide your own WinMain(), you may define SDL_MAIN_HANDLED
39 */
40#define SDL_MAIN_AVAILABLE
41
42#elif defined(__WINRT__)
43/* On WinRT, SDL provides a main function that initializes CoreApplication,
44 creating an instance of IFrameworkView in the process.
45
46 Ideally, #include'ing SDL_main.h is enough to get a main() function working.
47 However, that requires the source file your main() is in to be compiled
48 as C++ *and* with the /ZW compiler flag. If that's not feasible, add an
49 otherwise empty .cpp file that only contains `#include <SDL3/SDL_main.h>`
50 and build that with /ZW (still include SDL_main.h in your other file with main()!).
51 In XAML apps, instead the function SDL_RunApp() must be called with a pointer
52 to the Direct3D-hosted XAML control passed in as the "reserved" argument.
53*/
54#define SDL_MAIN_NEEDED
55
56#elif defined(__GDK__)
57/* On GDK, SDL provides a main function that initializes the game runtime.
58
59 If you prefer to write your own WinMain-function instead of having SDL
60 provide one that calls your main() function,
61 #define SDL_MAIN_HANDLED before #include'ing SDL_main.h
62 and call the SDL_RunApp function from your entry point.
63*/
64#define SDL_MAIN_NEEDED
65
66#elif defined(__IOS__)
67/* On iOS SDL provides a main function that creates an application delegate
68 and starts the iOS application run loop.
69
70 To use it, just #include SDL_main.h in the source file that contains your
71 main() function.
72
73 See src/video/uikit/SDL_uikitappdelegate.m for more details.
74 */
75#define SDL_MAIN_NEEDED
76
77#elif defined(__ANDROID__)
78/* On Android SDL provides a Java class in SDLActivity.java that is the
79 main activity entry point.
80
81 See docs/README-android.md for more details on extending that class.
82 */
83#define SDL_MAIN_NEEDED
84
85/* We need to export SDL_main so it can be launched from Java */
86#define SDLMAIN_DECLSPEC DECLSPEC
87
88#elif defined(__PSP__)
89/* On PSP SDL provides a main function that sets the module info,
90 activates the GPU and starts the thread required to be able to exit
91 the software.
92
93 If you provide this yourself, you may define SDL_MAIN_HANDLED
94 */
95#define SDL_MAIN_AVAILABLE
96
97#elif defined(__PS2__)
98#define SDL_MAIN_AVAILABLE
99
100#define SDL_PS2_SKIP_IOP_RESET() \
101 void reset_IOP(); \
102 void reset_IOP() {}
103
104#elif defined(__3DS__)
105/*
106 On N3DS, SDL provides a main function that sets up the screens
107 and storage.
108
109 If you provide this yourself, you may define SDL_MAIN_HANDLED
110*/
111#define SDL_MAIN_AVAILABLE
112
113#elif defined(__NGAGE__)
114
115/*
116 TODO: not sure if it should be SDL_MAIN_NEEDED, in SDL2 ngage had a
117 main implementation, but wasn't mentioned in SDL_main.h
118 */
119#define SDL_MAIN_AVAILABLE
120
121#endif
122#endif /* SDL_MAIN_HANDLED */
123
124#ifndef SDLMAIN_DECLSPEC
125#define SDLMAIN_DECLSPEC
126#endif
127
128/**
129 * \file SDL_main.h
130 *
131 * The application's main() function must be called with C linkage,
132 * and should be declared like this:
133 * \code
134 * #ifdef __cplusplus
135 * extern "C"
136 * #endif
137 * int main(int argc, char *argv[])
138 * {
139 * }
140 * \endcode
141 */
142
143#if defined(SDL_MAIN_NEEDED) || defined(SDL_MAIN_AVAILABLE)
144#define main SDL_main
145#endif
146
147#include <SDL3/SDL_begin_code.h>
148#ifdef __cplusplus
149extern "C" {
150#endif
151
152/**
153 * The prototype for the application's main() function
154 */
155typedef int (*SDL_main_func)(int argc, char *argv[]);
156extern SDLMAIN_DECLSPEC int SDL_main(int argc, char *argv[]);
157
158
159/**
160 * Circumvent failure of SDL_Init() when not using SDL_main() as an entry
161 * point.
162 *
163 * This function is defined in SDL_main.h, along with the preprocessor rule to
164 * redefine main() as SDL_main(). Thus to ensure that your main() function
165 * will not be changed it is necessary to define SDL_MAIN_HANDLED before
166 * including SDL.h.
167 *
168 * \since This function is available since SDL 3.0.0.
169 *
170 * \sa SDL_Init
171 */
172extern DECLSPEC void SDLCALL SDL_SetMainReady(void);
173
174/**
175 * Initializes and launches an SDL application, by doing platform-specific
176 * initialization before calling your mainFunction and cleanups after it
177 * returns, if that is needed for a specific platform, otherwise it just calls
178 * mainFunction.
179 *
180 * You can use this if you want to use your own main() implementation without
181 * using SDL_main (like when using SDL_MAIN_HANDLED). When using this, you do
182 * *not* need SDL_SetMainReady().
183 *
184 * \param argc The argc parameter from the application's main() function, or 0
185 * if the platform's main-equivalent has no argc
186 * \param argv The argv parameter from the application's main() function, or
187 * NULL if the platform's main-equivalent has no argv
188 * \param mainFunction Your SDL app's C-style main(), an SDL_main_func. NOT
189 * the function you're calling this from! Its name doesn't
190 * matter, but its signature must be like int my_main(int
191 * argc, char* argv[])
192 * \param reserved should be NULL (reserved for future use, will probably be
193 * platform-specific then)
194 * \return the return value from mainFunction: 0 on success, -1 on failure;
195 * SDL_GetError() might have more information on the failure
196 *
197 * \since This function is available since SDL 3.0.0.
198 */
199extern DECLSPEC int SDLCALL SDL_RunApp(int argc, char* argv[], SDL_main_func mainFunction, void * reserved);
200
201#if defined(__WIN32__) || defined(__GDK__)
202
203/**
204 * Register a win32 window class for SDL's use.
205 *
206 * This can be called to set the application window class at startup. It is
207 * safe to call this multiple times, as long as every call is eventually
208 * paired with a call to SDL_UnregisterApp, but a second registration attempt
209 * while a previous registration is still active will be ignored, other than
210 * to increment a counter.
211 *
212 * Most applications do not need to, and should not, call this directly; SDL
213 * will call it when initializing the video subsystem.
214 *
215 * \param name the window class name, in UTF-8 encoding. If NULL, SDL
216 * currently uses "SDL_app" but this isn't guaranteed.
217 * \param style the value to use in WNDCLASSEX::style. If `name` is NULL, SDL
218 * currently uses `(CS_BYTEALIGNCLIENT | CS_OWNDC)` regardless of
219 * what is specified here.
220 * \param hInst the HINSTANCE to use in WNDCLASSEX::hInstance. If zero, SDL
221 * will use `GetModuleHandle(NULL)` instead.
222 * \returns 0 on success, -1 on error. SDL_GetError() may have details.
223 *
224 * \since This function is available since SDL 3.0.0.
225 */
226extern DECLSPEC int SDLCALL SDL_RegisterApp(const char *name, Uint32 style, void *hInst);
227
228/**
229 * Deregister the win32 window class from an SDL_RegisterApp call.
230 *
231 * This can be called to undo the effects of SDL_RegisterApp.
232 *
233 * Most applications do not need to, and should not, call this directly; SDL
234 * will call it when deinitializing the video subsystem.
235 *
236 * It is safe to call this multiple times, as long as every call is eventually
237 * paired with a prior call to SDL_RegisterApp. The window class will only be
238 * deregistered when the registration counter in SDL_RegisterApp decrements to
239 * zero through calls to this function.
240 *
241 * \since This function is available since SDL 3.0.0.
242 */
243extern DECLSPEC void SDLCALL SDL_UnregisterApp(void);
244
245#endif /* defined(__WIN32__) || defined(__GDK__) */
246
247
248#ifdef __WINRT__
249
250/* for compatibility with SDL2's function of this name */
251#define SDL_WinRTRunApp(MAIN_FUNC, RESERVED) SDL_RunApp(0, NULL, MAIN_FUNC, RESERVED)
252
253#endif /* __WINRT__ */
254
255#if defined(__IOS__)
256
257/* for compatibility with SDL2's function of this name */
258#define SDL_UIKitRunApp(ARGC, ARGV, MAIN_FUNC) SDL_RunApp(ARGC, ARGV, MAIN_FUNC, NULL)
259
260#endif /* __IOS__ */
261
262#ifdef __GDK__
263
264/* for compatibility with SDL2's function of this name */
265#define SDL_GDKRunApp(MAIN_FUNC, RESERVED) SDL_RunApp(0, NULL, MAIN_FUNC, RESERVED)
266
267/**
268 * Callback from the application to let the suspend continue.
269 *
270 * \since This function is available since SDL 3.0.0.
271 */
272extern DECLSPEC void SDLCALL SDL_GDKSuspendComplete(void);
273
274#endif /* __GDK__ */
275
276#ifdef __cplusplus
277}
278#endif
279
280#include <SDL3/SDL_close_code.h>
281
282#if !defined(SDL_MAIN_HANDLED) && !defined(SDL_MAIN_NOIMPL)
283/* include header-only SDL_main implementations */
284#if defined(__WIN32__) || defined(__GDK__) || defined(__IOS__) || defined(__TVOS__) \
285 || defined(__3DS__) || defined(__NGAGE__) || defined(__PS2__) || defined(__PSP__)
286
287/* platforms whichs main (-equivalent) can be implemented in plain C */
288#include <SDL3/SDL_main_impl.h>
289
290#elif defined(__WINRT__) /* C++ platforms */
291
292#ifdef __cplusplus
293#include <SDL3/SDL_main_impl.h>
294#else
295/* Note: to get rid of the following warning, you can #define SDL_MAIN_NOIMPL before including SDL_main.h
296 * 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()!
297 */
298#ifdef _MSC_VER
299#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>")
300#elif defined(__GNUC__) /* gcc, clang, mingw and compatible are matched by this and have #warning */
301#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>"
302#endif /* __GNUC__ */
303#endif /* __cplusplus */
304
305#endif /* C++ platforms like __WINRT__ etc */
306
307#endif /* SDL_MAIN_HANDLED */
308
309#endif /* SDL_main_h_ */
void SDL_SetMainReady(void)
SDLMAIN_DECLSPEC int SDL_main(int argc, char *argv[])
#define SDLMAIN_DECLSPEC
Definition: SDL_main.h:125
int(* SDL_main_func)(int argc, char *argv[])
Definition: SDL_main.h:155
int SDL_RunApp(int argc, char *argv[], SDL_main_func mainFunction, void *reserved)
GLuint const GLchar * name
uint32_t Uint32
Definition: SDL_stdinc.h:171