SDL 3.0
SDL_main_impl.h
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2026 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_impl_h_
23#define SDL_main_impl_h_
24
25#ifndef SDL_main_h_
26#error "This header should not be included directly, but only via SDL_main.h!"
27#endif
28
29/* if someone wants to include SDL_main.h but doesn't want the main handing magic,
30 (maybe to call SDL_RegisterApp()) they can #define SDL_MAIN_HANDLED first.
31 SDL_MAIN_NOIMPL is for SDL-internal usage (only affects implementation,
32 not definition of SDL_MAIN_AVAILABLE etc in SDL_main.h) and if the user wants
33 to have the SDL_main implementation (from this header) in another source file
34 than their main() function, for example if SDL_main requires C++
35 and main() is implemented in plain C */
36#if !defined(SDL_MAIN_HANDLED) && !defined(SDL_MAIN_NOIMPL)
37
38 /* the implementations below must be able to use the implement real main(), nothing renamed
39 (the user's main() will be renamed to SDL_main so it can be called from here) */
40 #ifdef main
41 #undef main
42 #endif
43
44 #ifdef SDL_MAIN_USE_CALLBACKS
45
46 #if 0
47 /* currently there are no platforms that _need_ a magic entry point here
48 for callbacks, but if one shows up, implement it here. */
49
50 #elif defined(SDL_PLATFORM_OPENHARMONY)
51 /* HarmonyOS/OpenHarmony: we look up SDL_AppInit/SDL_AppIterate/etc in libmain.so and call them directly.
52 Since we don't need a `main` entry point, we don't generate an SDL_main implementation here _at all_. */
53 #ifndef SDL_MAIN_USE_CALLBACKS /* !!! FIXME, remove this later? */
54 #error SDL3 on OpenHarmony currently requires the SDL Main Callbacks to operate.
55 #endif
56
57 #else /* use a standard SDL_main, which the app SHOULD NOT ALSO SUPPLY. */
58
59 /* this define makes the normal SDL_main entry point stuff work...we just provide SDL_main() instead of the app. */
60 #define SDL_MAIN_CALLBACK_STANDARD 1
61
62 int SDL_main(int argc, char **argv)
63 {
65 }
66
67 #endif /* platform-specific tests */
68
69 #endif /* SDL_MAIN_USE_CALLBACKS */
70
71
72 /* set up the usual SDL_main stuff if we're not using callbacks or if we are but need the normal entry point,
73 unless the real entry point needs to be somewhere else entirely, like Android where it's in Java code */
74 #if (!defined(SDL_MAIN_USE_CALLBACKS) || defined(SDL_MAIN_CALLBACK_STANDARD)) && !defined(SDL_MAIN_EXPORTED)
75
76 #if defined(SDL_PLATFORM_PRIVATE_MAIN)
77 /* Private platforms may have their own ideas about entry points. */
78 #include "SDL_main_impl_private.h"
79
80 #elif defined(SDL_PLATFORM_WINDOWS)
81
82 /* these defines/typedefs are needed for the WinMain() definition */
83 #ifndef WINAPI
84 #define WINAPI __stdcall
85 #endif
86
87 typedef struct HINSTANCE__ * HINSTANCE;
88 typedef char *LPSTR;
89 typedef wchar_t *PWSTR;
90
91 /* The VC++ compiler needs main/wmain defined, but not for GDK */
92 #if defined(_MSC_VER) && !defined(SDL_PLATFORM_GDK)
93
94 /* This is where execution begins [console apps] */
95 #if defined(UNICODE) && UNICODE
96 int wmain(int argc, wchar_t *wargv[], wchar_t *wenvp)
97 {
98 (void)argc;
99 (void)wargv;
100 (void)wenvp;
101 return SDL_RunApp(0, NULL, SDL_main, NULL);
102 }
103 #else /* ANSI */
104 int main(int argc, char *argv[])
105 {
106 (void)argc;
107 (void)argv;
108 return SDL_RunApp(0, NULL, SDL_main, NULL);
109 }
110 #endif /* UNICODE */
111
112 #endif /* _MSC_VER && ! SDL_PLATFORM_GDK */
113
114 /* This is where execution begins [windowed apps and GDK] */
115
116 #ifdef __cplusplus
117 extern "C" {
118 #endif
119
120 #if defined(UNICODE) && UNICODE
121 int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE hPrev, PWSTR szCmdLine, int sw)
122 #else /* ANSI */
123 int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
124 #endif
125 {
126 (void)hInst;
127 (void)hPrev;
128 (void)szCmdLine;
129 (void)sw;
130 return SDL_RunApp(0, NULL, SDL_main, NULL);
131 }
132
133 #ifdef __cplusplus
134 } /* extern "C" */
135 #endif
136
137 /* end of SDL_PLATFORM_WINDOWS impls */
138
139 #else /* platforms that use a standard main() and just call SDL_RunApp(), like iOS and 3DS */
140 int main(int argc, char *argv[])
141 {
142 return SDL_RunApp(argc, argv, SDL_main, NULL);
143 }
144
145 /* end of impls for standard-conforming platforms */
146
147 #endif /* SDL_PLATFORM_WIN32 etc */
148
149 #endif /* !defined(SDL_MAIN_USE_CALLBACKS) || defined(SDL_MAIN_CALLBACK_STANDARD) */
150
151 /* rename users main() function to SDL_main() so it can be called from the wrappers above */
152 #define main SDL_main
153
154#endif /* SDL_MAIN_HANDLED */
155
156#endif /* SDL_main_impl_h_ */
#define NULL
SDLMAIN_DECLSPEC int SDL_main(int argc, char *argv[])
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)
#define main
void SDL_AppQuit(void *appstate, SDL_AppResult result)
Definition hello.c:65
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
Definition hello.c:20
SDL_AppResult SDL_AppIterate(void *appstate)
Definition hello.c:41
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
Definition hello.c:31