SDL 3.0
SDL_main_impl.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_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 /* main */
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#else /* use a standard SDL_main, which the app SHOULD NOT ALSO SUPPLY. */
51
52/* this define makes the normal SDL_main entry point stuff work...we just provide SDL_main() instead of the app. */
53#define SDL_MAIN_CALLBACK_STANDARD 1
54
55int SDL_main(int argc, char **argv)
56{
57 return SDL_EnterAppMainCallbacks(argc, argv, SDL_AppInit, SDL_AppIterate, SDL_AppEvent, SDL_AppQuit);
58}
59
60#endif /* platform-specific tests */
61
62#endif /* SDL_MAIN_USE_CALLBACKS */
63
64
65/* set up the usual SDL_main stuff if we're not using callbacks or if we are but need the normal entry point. */
66#if !defined(SDL_MAIN_USE_CALLBACKS) || defined(SDL_MAIN_CALLBACK_STANDARD)
67
68#if defined(__WIN32__) || defined(__GDK__)
69
70/* these defines/typedefs are needed for the WinMain() definition */
71#ifndef WINAPI
72#define WINAPI __stdcall
73#endif
74
75#include <SDL3/SDL_begin_code.h>
76
77#ifdef __cplusplus
78extern "C" {
79#endif
80
81typedef struct HINSTANCE__ * HINSTANCE;
82typedef char* LPSTR;
83typedef wchar_t* PWSTR;
84
85/* The VC++ compiler needs main/wmain defined, but not for GDK */
86#if defined(_MSC_VER) && !defined(__GDK__)
87
88/* This is where execution begins [console apps] */
89#if defined( UNICODE ) && UNICODE
90int wmain(int argc, wchar_t *wargv[], wchar_t *wenvp)
91{
92 (void)argc;
93 (void)wargv;
94 (void)wenvp;
95 return SDL_RunApp(0, NULL, SDL_main, NULL);
96}
97#else /* ANSI */
98int main(int argc, char *argv[])
99{
100 (void)argc;
101 (void)argv;
102 return SDL_RunApp(0, NULL, SDL_main, NULL);
103}
104#endif /* UNICODE */
105
106#endif /* _MSC_VER && ! __GDK__ */
107
108/* This is where execution begins [windowed apps and GDK] */
109#if defined( UNICODE ) && UNICODE
110int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE hPrev, PWSTR szCmdLine, int sw)
111#else /* ANSI */
112int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
113#endif
114{
115 (void)hInst;
116 (void)hPrev;
117 (void)szCmdLine;
118 (void)sw;
119 return SDL_RunApp(0, NULL, SDL_main, NULL);
120}
121
122#ifdef __cplusplus
123} /* extern "C" */
124#endif
125
126#include <SDL3/SDL_close_code.h>
127
128/* end of __WIN32__ and __GDK__ impls */
129#elif defined(__WINRT__)
130
131/* WinRT main based on SDL_winrt_main_NonXAML.cpp, placed in the public domain by David Ludwig 3/13/14 */
132
133#include <wrl.h>
134
135/* At least one file in any SDL/WinRT app appears to require compilation
136 with C++/CX, otherwise a Windows Metadata file won't get created, and
137 an APPX0702 build error can appear shortly after linking.
138
139 The following set of preprocessor code forces this file to be compiled
140 as C++/CX, which appears to cause Visual C++ 2012's build tools to
141 create this .winmd file, and will help allow builds of SDL/WinRT apps
142 to proceed without error.
143
144 If other files in an app's project enable C++/CX compilation, then it might
145 be possible for the .cpp file including SDL_main.h to be compiled without /ZW,
146 for Visual C++'s build tools to create a winmd file, and for the app to
147 build without APPX0702 errors. In this case, if
148 SDL_WINRT_METADATA_FILE_AVAILABLE is defined as a C/C++ macro, then
149 the #error (to force C++/CX compilation) will be disabled.
150
151 Please note that /ZW can be specified on a file-by-file basis. To do this,
152 right click on the file in Visual C++, click Properties, then change the
153 setting through the dialog that comes up.
154*/
155#ifndef SDL_WINRT_METADATA_FILE_AVAILABLE
156#if !defined(__cplusplus) || !defined(__cplusplus_winrt)
157#error The C++ file that includes SDL_main.h must be compiled as C++ code with /ZW, otherwise build errors due to missing .winmd files can occur.
158#endif
159#endif
160
161/* Prevent MSVC++ from warning about threading models when defining our
162 custom WinMain. The threading model will instead be set via a direct
163 call to Windows::Foundation::Initialize (rather than via an attributed
164 function).
165
166 To note, this warning (C4447) does not seem to come up unless this file
167 is compiled with C++/CX enabled (via the /ZW compiler flag).
168*/
169#ifdef _MSC_VER
170#pragma warning(disable : 4447)
171#endif
172
173/* Make sure the function to initialize the Windows Runtime gets linked in. */
174#ifdef _MSC_VER
175#pragma comment(lib, "runtimeobject.lib")
176#endif
177
178int CALLBACK WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
179{
180 return SDL_RunApp(0, NULL, SDL_main, NULL);
181}
182
183/* end of WinRT impl */
184#elif defined(__NGAGE__)
185
186/* same typedef as in ngage SDKs e32def.h */
187typedef signed int TInt;
188/* TODO: if it turns out that this only works when built as C++,
189 move __NGAGE__ into the C++ section in SDL_main.h */
190TInt E32Main()
191{
192 return SDL_RunApp(0, NULL, SDL_main, NULL);
193}
194
195/* end of __NGAGE__ impl */
196
197#else /* platforms that use a standard main() and just call SDL_RunApp(), like iOS and 3DS */
198
199#include <SDL3/SDL_begin_code.h>
200
201#ifdef __cplusplus
202extern "C" {
203#endif
204
205int main(int argc, char *argv[])
206{
207 return SDL_RunApp(argc, argv, SDL_main, NULL);
208}
209
210#ifdef __cplusplus
211} /* extern "C" */
212#endif
213
214#include <SDL3/SDL_close_code.h>
215
216/* end of impls for standard-conforming platforms */
217
218#endif /* __WIN32__ etc */
219
220#endif /* !defined(SDL_MAIN_USE_CALLBACKS) || defined(SDL_MAIN_CALLBACK_STANDARD) */
221
222/* rename users main() function to SDL_main() so it can be called from the wrappers above */
223#define main SDL_main
224
225#endif /* SDL_MAIN_HANDLED */
226
227#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