main: Indented SDL_main headers for readability, removed SDL2 compat macros.

Specifically, SDL_WinRTRunApp, SDL_UIKitRunApp, and SDL_GDKRunApp macros were
removed, as likely unnecessary to SDL3 users. A note was added to the
migration doc about how to roll replacements. These are not going into
SDL_oldnames.h.

Fixes #8245.
This commit is contained in:
Ryan C. Gordon
2024-01-25 17:39:38 -05:00
parent 22ea59425d
commit 8814095aa8
3 changed files with 236 additions and 250 deletions

View File

@@ -861,7 +861,15 @@ SDL3 doesn't have a static libSDLmain to link against anymore.
Instead SDL_main.h is now a header-only library **and not included by SDL.h anymore**. Instead SDL_main.h is now a header-only library **and not included by SDL.h anymore**.
Using it is really simple: Just `#include <SDL3/SDL_main.h>` in the source file with your standard Using it is really simple: Just `#include <SDL3/SDL_main.h>` in the source file with your standard
`int main(int argc, char* argv[])` function. `int main(int argc, char* argv[])` function. See docs/README-main-functions.md for details.
Several platform-specific entry point functions have been removed as unnecessary. If for some reason you explicitly need them, here are easy replacements:
```c
#define SDL_WinRTRunApp(MAIN_FUNC, RESERVED) SDL_RunApp(0, NULL, MAIN_FUNC, RESERVED)
#define SDL_UIKitRunApp(ARGC, ARGV, MAIN_FUNC) SDL_RunApp(ARGC, ARGV, MAIN_FUNC, NULL)
#define SDL_GDKRunApp(MAIN_FUNC, RESERVED) SDL_RunApp(0, NULL, MAIN_FUNC, RESERVED)
```
## SDL_metal.h ## SDL_metal.h

View File

@@ -41,16 +41,16 @@
*/ */
#ifndef SDL_MAIN_HANDLED #ifndef SDL_MAIN_HANDLED
#ifdef SDL_PLATFORM_WIN32 #ifdef SDL_PLATFORM_WIN32
/* On Windows SDL provides WinMain(), which parses the command line and passes /* On Windows SDL provides WinMain(), which parses the command line and passes
the arguments to your main function. the arguments to your main function.
If you provide your own WinMain(), you may define SDL_MAIN_HANDLED If you provide your own WinMain(), you may define SDL_MAIN_HANDLED
*/ */
#define SDL_MAIN_AVAILABLE #define SDL_MAIN_AVAILABLE
#elif defined(SDL_PLATFORM_WINRT) #elif defined(SDL_PLATFORM_WINRT)
/* On WinRT, SDL provides a main function that initializes CoreApplication, /* On WinRT, SDL provides a main function that initializes CoreApplication,
creating an instance of IFrameworkView in the process. creating an instance of IFrameworkView in the process.
Ideally, #include'ing SDL_main.h is enough to get a main() function working. Ideally, #include'ing SDL_main.h is enough to get a main() function working.
@@ -60,21 +60,21 @@
and build that with /ZW (still include SDL_main.h in your other file with main()!). and build that with /ZW (still include SDL_main.h in your other file with main()!).
In XAML apps, instead the function SDL_RunApp() must be called with a pointer In XAML apps, instead the function SDL_RunApp() must be called with a pointer
to the Direct3D-hosted XAML control passed in as the "reserved" argument. to the Direct3D-hosted XAML control passed in as the "reserved" argument.
*/ */
#define SDL_MAIN_NEEDED #define SDL_MAIN_NEEDED
#elif defined(SDL_PLATFORM_GDK) #elif defined(SDL_PLATFORM_GDK)
/* On GDK, SDL provides a main function that initializes the game runtime. /* On GDK, SDL provides a main function that initializes the game runtime.
If you prefer to write your own WinMain-function instead of having SDL If you prefer to write your own WinMain-function instead of having SDL
provide one that calls your main() function, provide one that calls your main() function,
#define SDL_MAIN_HANDLED before #include'ing SDL_main.h #define SDL_MAIN_HANDLED before #include'ing SDL_main.h
and call the SDL_RunApp function from your entry point. and call the SDL_RunApp function from your entry point.
*/ */
#define SDL_MAIN_NEEDED #define SDL_MAIN_NEEDED
#elif defined(SDL_PLATFORM_IOS) #elif defined(SDL_PLATFORM_IOS)
/* On iOS SDL provides a main function that creates an application delegate /* On iOS SDL provides a main function that creates an application delegate
and starts the iOS application run loop. and starts the iOS application run loop.
To use it, just #include SDL_main.h in the source file that contains your To use it, just #include SDL_main.h in the source file that contains your
@@ -82,53 +82,52 @@
See src/video/uikit/SDL_uikitappdelegate.m for more details. See src/video/uikit/SDL_uikitappdelegate.m for more details.
*/ */
#define SDL_MAIN_NEEDED #define SDL_MAIN_NEEDED
#elif defined(SDL_PLATFORM_ANDROID) #elif defined(SDL_PLATFORM_ANDROID)
/* On Android SDL provides a Java class in SDLActivity.java that is the /* On Android SDL provides a Java class in SDLActivity.java that is the
main activity entry point. main activity entry point.
See docs/README-android.md for more details on extending that class. See docs/README-android.md for more details on extending that class.
*/ */
#define SDL_MAIN_NEEDED #define SDL_MAIN_NEEDED
/* We need to export SDL_main so it can be launched from Java */ /* We need to export SDL_main so it can be launched from Java */
#define SDLMAIN_DECLSPEC DECLSPEC #define SDLMAIN_DECLSPEC DECLSPEC
#elif defined(SDL_PLATFORM_PSP) #elif defined(SDL_PLATFORM_PSP)
/* On PSP SDL provides a main function that sets the module info, /* On PSP SDL provides a main function that sets the module info,
activates the GPU and starts the thread required to be able to exit activates the GPU and starts the thread required to be able to exit
the software. the software.
If you provide this yourself, you may define SDL_MAIN_HANDLED If you provide this yourself, you may define SDL_MAIN_HANDLED
*/ */
#define SDL_MAIN_AVAILABLE #define SDL_MAIN_AVAILABLE
#elif defined(SDL_PLATFORM_PS2) #elif defined(SDL_PLATFORM_PS2)
#define SDL_MAIN_AVAILABLE #define SDL_MAIN_AVAILABLE
#define SDL_PS2_SKIP_IOP_RESET() \ #define SDL_PS2_SKIP_IOP_RESET() \
void reset_IOP(); \ void reset_IOP(); \
void reset_IOP() {} void reset_IOP() {}
#elif defined(SDL_PLATFORM_3DS) #elif defined(SDL_PLATFORM_3DS)
/* /*
On N3DS, SDL provides a main function that sets up the screens On N3DS, SDL provides a main function that sets up the screens
and storage. and storage.
If you provide this yourself, you may define SDL_MAIN_HANDLED If you provide this yourself, you may define SDL_MAIN_HANDLED
*/ */
#define SDL_MAIN_AVAILABLE #define SDL_MAIN_AVAILABLE
#elif defined(SDL_PLATFORM_NGAGE) #elif defined(SDL_PLATFORM_NGAGE)
/*
/*
TODO: not sure if it should be SDL_MAIN_NEEDED, in SDL2 ngage had a TODO: not sure if it should be SDL_MAIN_NEEDED, in SDL2 ngage had a
main implementation, but wasn't mentioned in SDL_main.h main implementation, but wasn't mentioned in SDL_main.h
*/ */
#define SDL_MAIN_AVAILABLE #define SDL_MAIN_AVAILABLE
#endif #endif
#endif /* SDL_MAIN_HANDLED */ #endif /* SDL_MAIN_HANDLED */
#ifndef SDLMAIN_DECLSPEC #ifndef SDLMAIN_DECLSPEC
@@ -470,26 +469,8 @@ extern DECLSPEC void SDLCALL SDL_UnregisterApp(void);
#endif /* defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK) */ #endif /* defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK) */
#ifdef SDL_PLATFORM_WINRT
/* for compatibility with SDL2's function of this name */
#define SDL_WinRTRunApp(MAIN_FUNC, RESERVED) SDL_RunApp(0, NULL, MAIN_FUNC, RESERVED)
#endif /* SDL_PLATFORM_WINRT */
#ifdef SDL_PLATFORM_IOS
/* for compatibility with SDL2's function of this name */
#define SDL_UIKitRunApp(ARGC, ARGV, MAIN_FUNC) SDL_RunApp(ARGC, ARGV, MAIN_FUNC, NULL)
#endif /* SDL_PLATFORM_IOS */
#ifdef SDL_PLATFORM_GDK #ifdef SDL_PLATFORM_GDK
/* for compatibility with SDL2's function of this name */
#define SDL_GDKRunApp(MAIN_FUNC, RESERVED) SDL_RunApp(0, NULL, MAIN_FUNC, RESERVED)
/** /**
* Callback from the application to let the suspend continue. * Callback from the application to let the suspend continue.
* *
@@ -506,31 +487,29 @@ extern DECLSPEC void SDLCALL SDL_GDKSuspendComplete(void);
#include <SDL3/SDL_close_code.h> #include <SDL3/SDL_close_code.h>
#if !defined(SDL_MAIN_HANDLED) && !defined(SDL_MAIN_NOIMPL) #if !defined(SDL_MAIN_HANDLED) && !defined(SDL_MAIN_NOIMPL)
/* include header-only SDL_main implementations */ /* include header-only SDL_main implementations */
#if defined(SDL_MAIN_USE_CALLBACKS) \ #if defined(SDL_MAIN_USE_CALLBACKS) \
|| defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK) || defined(SDL_PLATFORM_IOS) || defined(SDL_PLATFORM_TVOS) \ || defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK) || defined(SDL_PLATFORM_IOS) || defined(SDL_PLATFORM_TVOS) \
|| defined(SDL_PLATFORM_3DS) || defined(SDL_PLATFORM_NGAGE) || defined(SDL_PLATFORM_PS2) || defined(SDL_PLATFORM_PSP) || defined(SDL_PLATFORM_3DS) || defined(SDL_PLATFORM_NGAGE) || defined(SDL_PLATFORM_PS2) || defined(SDL_PLATFORM_PSP)
/* platforms which main (-equivalent) can be implemented in plain C */ /* platforms which main (-equivalent) can be implemented in plain C */
#include <SDL3/SDL_main_impl.h> #include <SDL3/SDL_main_impl.h>
#elif defined(SDL_PLATFORM_WINRT) /* C++ platforms */ #elif defined(SDL_PLATFORM_WINRT) /* C++ platforms */
#ifdef __cplusplus
#ifdef __cplusplus #include <SDL3/SDL_main_impl.h>
#include <SDL3/SDL_main_impl.h> #else
#else /* Note: to get rid of the following warning, you can #define SDL_MAIN_NOIMPL before including SDL_main.h
/* Note: to get rid of the following warning, you can #define SDL_MAIN_NOIMPL before including SDL_main.h
* 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()! * 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()!
*/ */
#ifdef _MSC_VER #ifdef _MSC_VER
#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>") #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>")
#elif defined(__GNUC__) /* gcc, clang, mingw and compatible are matched by this and have #warning */ #elif defined(__GNUC__) /* gcc, clang, mingw and compatible are matched by this and have #warning */
#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>" #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>"
#endif /* __GNUC__ */ #endif /* __GNUC__ */
#endif /* __cplusplus */ #endif /* __cplusplus */
#endif /* C++ platforms like SDL_PLATFORM_WINRT etc */ #endif /* C++ platforms like SDL_PLATFORM_WINRT etc */
#endif
#endif /* SDL_MAIN_HANDLED */
#endif /* SDL_main_h_ */ #endif /* SDL_main_h_ */

View File

@@ -35,99 +35,102 @@
and main() is implemented in plain C */ and main() is implemented in plain C */
#if !defined(SDL_MAIN_HANDLED) && !defined(SDL_MAIN_NOIMPL) #if !defined(SDL_MAIN_HANDLED) && !defined(SDL_MAIN_NOIMPL)
/* the implementations below must be able to use the implement real main(), nothing renamed /* the implementations below must be able to use the implement real main(), nothing renamed
(the user's main() will be renamed to SDL_main so it can be called from here) */ (the user's main() will be renamed to SDL_main so it can be called from here) */
#ifdef main #ifdef main
# undef main #undef main
#endif /* main */ #endif
#ifdef SDL_MAIN_USE_CALLBACKS #ifdef SDL_MAIN_USE_CALLBACKS
#if 0 #if 0
/* currently there are no platforms that _need_ a magic entry point here /* currently there are no platforms that _need_ a magic entry point here
for callbacks, but if one shows up, implement it here. */ for callbacks, but if one shows up, implement it here. */
#else /* use a standard SDL_main, which the app SHOULD NOT ALSO SUPPLY. */ #else /* use a standard SDL_main, which the app SHOULD NOT ALSO SUPPLY. */
/* this define makes the normal SDL_main entry point stuff work...we just provide SDL_main() instead of the app. */ /* this define makes the normal SDL_main entry point stuff work...we just provide SDL_main() instead of the app. */
#define SDL_MAIN_CALLBACK_STANDARD 1 #define SDL_MAIN_CALLBACK_STANDARD 1
int SDL_main(int argc, char **argv) int SDL_main(int argc, char **argv)
{ {
return SDL_EnterAppMainCallbacks(argc, argv, SDL_AppInit, SDL_AppIterate, SDL_AppEvent, SDL_AppQuit); return SDL_EnterAppMainCallbacks(argc, argv, SDL_AppInit, SDL_AppIterate, SDL_AppEvent, SDL_AppQuit);
} }
#endif /* platform-specific tests */ #endif /* platform-specific tests */
#endif /* SDL_MAIN_USE_CALLBACKS */ #endif /* SDL_MAIN_USE_CALLBACKS */
/* set up the usual SDL_main stuff if we're not using callbacks or if we are but need the normal entry point. */ /* set up the usual SDL_main stuff if we're not using callbacks or if we are but need the normal entry point. */
#if !defined(SDL_MAIN_USE_CALLBACKS) || defined(SDL_MAIN_CALLBACK_STANDARD) #if !defined(SDL_MAIN_USE_CALLBACKS) || defined(SDL_MAIN_CALLBACK_STANDARD)
#if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK) #if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK)
/* these defines/typedefs are needed for the WinMain() definition */ /* these defines/typedefs are needed for the WinMain() definition */
#ifndef WINAPI #ifndef WINAPI
#define WINAPI __stdcall #define WINAPI __stdcall
#endif #endif
typedef struct HINSTANCE__ * HINSTANCE; typedef struct HINSTANCE__ * HINSTANCE;
typedef char* LPSTR; typedef char* LPSTR;
typedef wchar_t* PWSTR; typedef wchar_t* PWSTR;
/* The VC++ compiler needs main/wmain defined, but not for GDK */ /* The VC++ compiler needs main/wmain defined, but not for GDK */
#if defined(_MSC_VER) && !defined(SDL_PLATFORM_GDK) #if defined(_MSC_VER) && !defined(SDL_PLATFORM_GDK)
/* This is where execution begins [console apps] */ /* This is where execution begins [console apps] */
#if defined( UNICODE ) && UNICODE #if defined( UNICODE ) && UNICODE
int wmain(int argc, wchar_t *wargv[], wchar_t *wenvp) int wmain(int argc, wchar_t *wargv[], wchar_t *wenvp)
{ {
(void)argc; (void)argc;
(void)wargv; (void)wargv;
(void)wenvp; (void)wenvp;
return SDL_RunApp(0, NULL, SDL_main, NULL); return SDL_RunApp(0, NULL, SDL_main, NULL);
} }
#else /* ANSI */ #else /* ANSI */
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
(void)argc; (void)argc;
(void)argv; (void)argv;
return SDL_RunApp(0, NULL, SDL_main, NULL); return SDL_RunApp(0, NULL, SDL_main, NULL);
} }
#endif /* UNICODE */ #endif /* UNICODE */
#endif /* _MSC_VER && ! SDL_PLATFORM_GDK */ #endif /* _MSC_VER && ! SDL_PLATFORM_GDK */
/* This is where execution begins [windowed apps and GDK] */ /* This is where execution begins [windowed apps and GDK] */
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#if defined( UNICODE ) && UNICODE
int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE hPrev, PWSTR szCmdLine, int sw) #if defined( UNICODE ) && UNICODE
#else /* ANSI */ int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE hPrev, PWSTR szCmdLine, int sw)
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw) #else /* ANSI */
#endif int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
{ #endif
{
(void)hInst; (void)hInst;
(void)hPrev; (void)hPrev;
(void)szCmdLine; (void)szCmdLine;
(void)sw; (void)sw;
return SDL_RunApp(0, NULL, SDL_main, NULL); return SDL_RunApp(0, NULL, SDL_main, NULL);
} }
#ifdef __cplusplus
} /* extern "C" */
#endif
/* end of SDL_PLATFORM_WIN32 and SDL_PLATFORM_GDK impls */ #ifdef __cplusplus
#elif defined(SDL_PLATFORM_WINRT) } /* extern "C" */
#endif
/* WinRT main based on SDL_winrt_main_NonXAML.cpp, placed in the public domain by David Ludwig 3/13/14 */ /* end of SDL_PLATFORM_WIN32 and SDL_PLATFORM_GDK impls */
#include <wrl.h> #elif defined(SDL_PLATFORM_WINRT)
/* At least one file in any SDL/WinRT app appears to require compilation /* WinRT main based on SDL_winrt_main_NonXAML.cpp, placed in the public domain by David Ludwig 3/13/14 */
#include <wrl.h>
/* At least one file in any SDL/WinRT app appears to require compilation
with C++/CX, otherwise a Windows Metadata file won't get created, and with C++/CX, otherwise a Windows Metadata file won't get created, and
an APPX0702 build error can appear shortly after linking. an APPX0702 build error can appear shortly after linking.
@@ -146,70 +149,66 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
Please note that /ZW can be specified on a file-by-file basis. To do this, Please note that /ZW can be specified on a file-by-file basis. To do this,
right click on the file in Visual C++, click Properties, then change the right click on the file in Visual C++, click Properties, then change the
setting through the dialog that comes up. setting through the dialog that comes up.
*/ */
#ifndef SDL_WINRT_METADATA_FILE_AVAILABLE #ifndef SDL_WINRT_METADATA_FILE_AVAILABLE
#if !defined(__cplusplus) || !defined(__cplusplus_winrt) #if !defined(__cplusplus) || !defined(__cplusplus_winrt)
#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. #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.
#endif #endif
#endif #endif
/* Prevent MSVC++ from warning about threading models when defining our /* Prevent MSVC++ from warning about threading models when defining our
custom WinMain. The threading model will instead be set via a direct custom WinMain. The threading model will instead be set via a direct
call to Windows::Foundation::Initialize (rather than via an attributed call to Windows::Foundation::Initialize (rather than via an attributed
function). function).
To note, this warning (C4447) does not seem to come up unless this file To note, this warning (C4447) does not seem to come up unless this file
is compiled with C++/CX enabled (via the /ZW compiler flag). is compiled with C++/CX enabled (via the /ZW compiler flag).
*/ */
#ifdef _MSC_VER #ifdef _MSC_VER
#pragma warning(disable : 4447) #pragma warning(disable : 4447)
#endif /* Make sure the function to initialize the Windows Runtime gets linked in. */
#pragma comment(lib, "runtimeobject.lib")
#endif
/* Make sure the function to initialize the Windows Runtime gets linked in. */ #ifdef __cplusplus
#ifdef _MSC_VER extern "C" {
#pragma comment(lib, "runtimeobject.lib") #endif
#endif int CALLBACK WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
#ifdef __cplusplus
extern "C" {
#endif
int CALLBACK WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
return SDL_RunApp(0, NULL, SDL_main, NULL); return SDL_RunApp(0, NULL, SDL_main, NULL);
} }
#ifdef __cplusplus #ifdef __cplusplus
} /* extern "C" */ } /* extern "C" */
#endif #endif
/* end of WinRT impl */ /* end of WinRT impl */
#elif defined(SDL_PLATFORM_NGAGE)
/* same typedef as in ngage SDKs e32def.h */ #elif defined(SDL_PLATFORM_NGAGE)
typedef signed int TInt; /* same typedef as in ngage SDKs e32def.h */
/* TODO: if it turns out that this only works when built as C++, typedef signed int TInt;
/* TODO: if it turns out that this only works when built as C++,
move SDL_PLATFORM_NGAGE into the C++ section in SDL_main.h */ move SDL_PLATFORM_NGAGE into the C++ section in SDL_main.h */
TInt E32Main() TInt E32Main()
{ {
return SDL_RunApp(0, NULL, SDL_main, NULL); return SDL_RunApp(0, NULL, SDL_main, NULL);
} }
/* end of SDL_PLATFORM_NGAGE impl */ /* end of SDL_PLATFORM_NGAGE impl */
#else /* platforms that use a standard main() and just call SDL_RunApp(), like iOS and 3DS */ #else /* platforms that use a standard main() and just call SDL_RunApp(), like iOS and 3DS */
int main(int argc, char *argv[])
int main(int argc, char *argv[]) {
{
return SDL_RunApp(argc, argv, SDL_main, NULL); return SDL_RunApp(argc, argv, SDL_main, NULL);
} }
/* end of impls for standard-conforming platforms */ /* end of impls for standard-conforming platforms */
#endif /* SDL_PLATFORM_WIN32 etc */ #endif /* SDL_PLATFORM_WIN32 etc */
#endif /* !defined(SDL_MAIN_USE_CALLBACKS) || defined(SDL_MAIN_CALLBACK_STANDARD) */ #endif /* !defined(SDL_MAIN_USE_CALLBACKS) || defined(SDL_MAIN_CALLBACK_STANDARD) */
/* rename users main() function to SDL_main() so it can be called from the wrappers above */ /* rename users main() function to SDL_main() so it can be called from the wrappers above */
#define main SDL_main #define main SDL_main
#endif /* SDL_MAIN_HANDLED */ #endif /* SDL_MAIN_HANDLED */