Implement SDL_main as header-only lib for Win32
(remaining platforms will follow)
SDL_main.h is *not* included by SDL.h anymore, users are supposed to
include it directly now, usually only in the file they implement main() in.
If they need the header elsewhere or don't want SDL_main to implement
main() (but only call SDL_SetMainReady() or whatever), they
can #define SDL_MAIN_HANDLED first, same as before.
For SDL-internal usage, I added _SDL_MAIN_NOIMPL, which *also* skips the
implementation and `#define main SDL_main`, but still defines
SDL_MAIN_AVAILABLE and SDL_MAIN_NEEDED in SDL_main.h, as before.
To make the implementaion in the header shorter and avoid including windows.h,
I moved most of the Win32 SDL_main code into SDL3.dll via SDL_Win32RunApp(),
so the header-only part is just the different main functions calling
SDL_Win32RunApp(SDL_main, NULL)
Note that I changed changed the return value and type of OutOfMemory()
to return -1 instead of FALSE, so main() (or WinMain() or whatever)
returns -1 instead of 0 in case of an out-of-memory error
Compared to original Win32 SDL_main, I tweaked the part of the
implementation in SDL_main_impl.h a bit to avoid linker warnings
and conflicts with stuff from windows.h:
- replaced windows.h with own define of WINAPI
and typedef-ing HINSTANCE and LPSTR.
This prevents conflicts between all the generically-named #defines and
types in windows.h and user code (like DrawState in some SDL tests)
- only using one of main() or wmain() gets rid of a MSVC linker error
("warning LNK4067: ambiguous entry point")
If this still causes problems, we might try getting rid of wmain(),
seemed to me like MSVC can use regular main() in UNICODE mode as well
- simplified the UNICODE logic for that - while this is not exactly
equivalent to the old, it should make sense and Works For Me
2022-12-03 19:29:22 -07:00
|
|
|
/*
|
|
|
|
Simple DirectMedia Layer
|
2023-01-09 10:41:41 -07:00
|
|
|
Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
|
Implement SDL_main as header-only lib for Win32
(remaining platforms will follow)
SDL_main.h is *not* included by SDL.h anymore, users are supposed to
include it directly now, usually only in the file they implement main() in.
If they need the header elsewhere or don't want SDL_main to implement
main() (but only call SDL_SetMainReady() or whatever), they
can #define SDL_MAIN_HANDLED first, same as before.
For SDL-internal usage, I added _SDL_MAIN_NOIMPL, which *also* skips the
implementation and `#define main SDL_main`, but still defines
SDL_MAIN_AVAILABLE and SDL_MAIN_NEEDED in SDL_main.h, as before.
To make the implementaion in the header shorter and avoid including windows.h,
I moved most of the Win32 SDL_main code into SDL3.dll via SDL_Win32RunApp(),
so the header-only part is just the different main functions calling
SDL_Win32RunApp(SDL_main, NULL)
Note that I changed changed the return value and type of OutOfMemory()
to return -1 instead of FALSE, so main() (or WinMain() or whatever)
returns -1 instead of 0 in case of an out-of-memory error
Compared to original Win32 SDL_main, I tweaked the part of the
implementation in SDL_main_impl.h a bit to avoid linker warnings
and conflicts with stuff from windows.h:
- replaced windows.h with own define of WINAPI
and typedef-ing HINSTANCE and LPSTR.
This prevents conflicts between all the generically-named #defines and
types in windows.h and user code (like DrawState in some SDL tests)
- only using one of main() or wmain() gets rid of a MSVC linker error
("warning LNK4067: ambiguous entry point")
If this still causes problems, we might try getting rid of wmain(),
seemed to me like MSVC can use regular main() in UNICODE mode as well
- simplified the UNICODE logic for that - while this is not exactly
equivalent to the old, it should make sense and Works For Me
2022-12-03 19:29:22 -07:00
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely, subject to the following restrictions:
|
|
|
|
|
|
|
|
1. The origin of this software must not be misrepresented; you must not
|
|
|
|
claim that you wrote the original software. If you use this software
|
|
|
|
in a product, an acknowledgment in the product documentation would be
|
|
|
|
appreciated but is not required.
|
|
|
|
2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
misrepresented as being the original software.
|
|
|
|
3. This notice may not be removed or altered from any source distribution.
|
|
|
|
*/
|
|
|
|
|
2022-12-17 22:13:25 -07:00
|
|
|
#ifndef SDL_main_impl_h_
|
|
|
|
#define SDL_main_impl_h_
|
Implement SDL_main as header-only lib for Win32
(remaining platforms will follow)
SDL_main.h is *not* included by SDL.h anymore, users are supposed to
include it directly now, usually only in the file they implement main() in.
If they need the header elsewhere or don't want SDL_main to implement
main() (but only call SDL_SetMainReady() or whatever), they
can #define SDL_MAIN_HANDLED first, same as before.
For SDL-internal usage, I added _SDL_MAIN_NOIMPL, which *also* skips the
implementation and `#define main SDL_main`, but still defines
SDL_MAIN_AVAILABLE and SDL_MAIN_NEEDED in SDL_main.h, as before.
To make the implementaion in the header shorter and avoid including windows.h,
I moved most of the Win32 SDL_main code into SDL3.dll via SDL_Win32RunApp(),
so the header-only part is just the different main functions calling
SDL_Win32RunApp(SDL_main, NULL)
Note that I changed changed the return value and type of OutOfMemory()
to return -1 instead of FALSE, so main() (or WinMain() or whatever)
returns -1 instead of 0 in case of an out-of-memory error
Compared to original Win32 SDL_main, I tweaked the part of the
implementation in SDL_main_impl.h a bit to avoid linker warnings
and conflicts with stuff from windows.h:
- replaced windows.h with own define of WINAPI
and typedef-ing HINSTANCE and LPSTR.
This prevents conflicts between all the generically-named #defines and
types in windows.h and user code (like DrawState in some SDL tests)
- only using one of main() or wmain() gets rid of a MSVC linker error
("warning LNK4067: ambiguous entry point")
If this still causes problems, we might try getting rid of wmain(),
seemed to me like MSVC can use regular main() in UNICODE mode as well
- simplified the UNICODE logic for that - while this is not exactly
equivalent to the old, it should make sense and Works For Me
2022-12-03 19:29:22 -07:00
|
|
|
|
2023-03-30 12:26:31 -06:00
|
|
|
#ifndef SDL_main_h_
|
Implement SDL_main as header-only lib for Win32
(remaining platforms will follow)
SDL_main.h is *not* included by SDL.h anymore, users are supposed to
include it directly now, usually only in the file they implement main() in.
If they need the header elsewhere or don't want SDL_main to implement
main() (but only call SDL_SetMainReady() or whatever), they
can #define SDL_MAIN_HANDLED first, same as before.
For SDL-internal usage, I added _SDL_MAIN_NOIMPL, which *also* skips the
implementation and `#define main SDL_main`, but still defines
SDL_MAIN_AVAILABLE and SDL_MAIN_NEEDED in SDL_main.h, as before.
To make the implementaion in the header shorter and avoid including windows.h,
I moved most of the Win32 SDL_main code into SDL3.dll via SDL_Win32RunApp(),
so the header-only part is just the different main functions calling
SDL_Win32RunApp(SDL_main, NULL)
Note that I changed changed the return value and type of OutOfMemory()
to return -1 instead of FALSE, so main() (or WinMain() or whatever)
returns -1 instead of 0 in case of an out-of-memory error
Compared to original Win32 SDL_main, I tweaked the part of the
implementation in SDL_main_impl.h a bit to avoid linker warnings
and conflicts with stuff from windows.h:
- replaced windows.h with own define of WINAPI
and typedef-ing HINSTANCE and LPSTR.
This prevents conflicts between all the generically-named #defines and
types in windows.h and user code (like DrawState in some SDL tests)
- only using one of main() or wmain() gets rid of a MSVC linker error
("warning LNK4067: ambiguous entry point")
If this still causes problems, we might try getting rid of wmain(),
seemed to me like MSVC can use regular main() in UNICODE mode as well
- simplified the UNICODE logic for that - while this is not exactly
equivalent to the old, it should make sense and Works For Me
2022-12-03 19:29:22 -07:00
|
|
|
#error "This header should not be included directly, but only via SDL_main.h!"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* if someone wants to include SDL_main.h but doesn't want the main handing magic,
|
|
|
|
(maybe to call SDL_RegisterApp()) they can #define SDL_MAIN_HANDLED first
|
2022-12-11 17:22:43 -07:00
|
|
|
SDL_MAIN_NOIMPL is for SDL-internal usage (only affects implementation,
|
|
|
|
not definition of SDL_MAIN_AVAILABLE etc in SDL_main.h) and if the user wants
|
|
|
|
to have the SDL_main implementation (from this header) in another source file
|
|
|
|
than their main() function, for example if SDL_main requires C++
|
|
|
|
and main() is implemented in plain C */
|
|
|
|
#if !defined(SDL_MAIN_HANDLED) && !defined(SDL_MAIN_NOIMPL)
|
Implement SDL_main as header-only lib for Win32
(remaining platforms will follow)
SDL_main.h is *not* included by SDL.h anymore, users are supposed to
include it directly now, usually only in the file they implement main() in.
If they need the header elsewhere or don't want SDL_main to implement
main() (but only call SDL_SetMainReady() or whatever), they
can #define SDL_MAIN_HANDLED first, same as before.
For SDL-internal usage, I added _SDL_MAIN_NOIMPL, which *also* skips the
implementation and `#define main SDL_main`, but still defines
SDL_MAIN_AVAILABLE and SDL_MAIN_NEEDED in SDL_main.h, as before.
To make the implementaion in the header shorter and avoid including windows.h,
I moved most of the Win32 SDL_main code into SDL3.dll via SDL_Win32RunApp(),
so the header-only part is just the different main functions calling
SDL_Win32RunApp(SDL_main, NULL)
Note that I changed changed the return value and type of OutOfMemory()
to return -1 instead of FALSE, so main() (or WinMain() or whatever)
returns -1 instead of 0 in case of an out-of-memory error
Compared to original Win32 SDL_main, I tweaked the part of the
implementation in SDL_main_impl.h a bit to avoid linker warnings
and conflicts with stuff from windows.h:
- replaced windows.h with own define of WINAPI
and typedef-ing HINSTANCE and LPSTR.
This prevents conflicts between all the generically-named #defines and
types in windows.h and user code (like DrawState in some SDL tests)
- only using one of main() or wmain() gets rid of a MSVC linker error
("warning LNK4067: ambiguous entry point")
If this still causes problems, we might try getting rid of wmain(),
seemed to me like MSVC can use regular main() in UNICODE mode as well
- simplified the UNICODE logic for that - while this is not exactly
equivalent to the old, it should make sense and Works For Me
2022-12-03 19:29:22 -07:00
|
|
|
|
2022-12-11 20:38:06 -07:00
|
|
|
/* 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) */
|
|
|
|
#ifdef main
|
|
|
|
# undef main
|
|
|
|
#endif /* main */
|
|
|
|
|
main: Added _optional_ callback entry points.
This lets apps optionally have a handful of callbacks for their entry points instead of a single main function. If used, the actual main/SDL_main/whatever entry point will be implemented in the single-header library SDL_main.h and the app will implement four separate functions:
First:
int SDL_AppInit(int argc, char **argv);
This will be called once before anything else. argc/argv work like they always do. If this returns 0, the app runs. If it returns < 0, the app calls SDL_AppQuit and terminates with an exit code that reports an error to the platform. If it returns > 0, the app calls SDL_AppQuit and terminates with an exit code that reports success to the platform. This function should not go into an infinite mainloop; it should do any one-time startup it requires and then return.
Then:
int SDL_AppIterate(void);
This is called over and over, possibly at the refresh rate of the display or some other metric that the platform dictates. This is where the heart of your app runs. It should return as quickly as reasonably possible, but it's not a "run one memcpy and that's all the time you have" sort of thing. The app should do any game updates, and render a frame of video. If it returns < 0, SDL will call SDL_AppQuit and terminate the process with an exit code that reports an error to the platform. If it returns > 0, the app calls SDL_AppQuit and terminates with an exit code that reports success to the platform. If it returns 0, then SDL_AppIterate will be called again at some regular frequency. The platform may choose to run this more or less (perhaps less in the background, etc), or it might just call this function in a loop as fast as possible. You do not check the event queue in this function (SDL_AppEvent exists for that).
Next:
int SDL_AppEvent(const SDL_Event *event);
This will be called once for each event pushed into the SDL queue. This may be called from any thread, and possibly in parallel to SDL_AppIterate. The fields in event do not need to be free'd (as you would normally need to do for SDL_EVENT_DROP_FILE, etc), and your app should not call SDL_PollEvent, SDL_PumpEvent, etc, as SDL will manage this for you. Return values are the same as from SDL_AppIterate(), so you can terminate in response to SDL_EVENT_QUIT, etc.
Finally:
void SDL_AppQuit(void);
This is called once before terminating the app--assuming the app isn't being forcibly killed or crashed--as a last chance to clean up. After this returns, SDL will call SDL_Quit so the app doesn't have to (but it's safe for the app to call it, too). Process termination proceeds as if the app returned normally from main(), so atexit handles will run, if your platform supports that.
The app does not implement SDL_main if using this. To turn this on, define SDL_MAIN_USE_CALLBACKS before including SDL_main.h. Defines like SDL_MAIN_HANDLED and SDL_MAIN_NOIMPL are also respected for callbacks, if the app wants to do some sort of magic main implementation thing.
In theory, on most platforms these can be implemented in the app itself, but this saves some #ifdefs in the app and lets everyone struggle less against some platforms, and might be more efficient in the long run, too.
On some platforms, it's possible this is the only reasonable way to go, but we haven't actually hit one that 100% requires it yet (but we will, if we want to write a RetroArch backend, for example).
Using the callback entry points works on every platform, because on platforms that don't require them, we can fake them with a simple loop in an internal implementation of the usual SDL_main.
The primary way we expect people to write SDL apps is with SDL_main, and this is not intended to replace it. If the app chooses to use this, it just removes some platform-specific details they might have to otherwise manage, and maybe removes a barrier to entry on some future platform.
Fixes #6785.
Reference PR #8247.
2023-11-01 16:40:41 -06:00
|
|
|
#ifdef SDL_MAIN_USE_CALLBACKS
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* currently there are no platforms that _need_ a magic entry point here
|
|
|
|
for callbacks, but if one shows up, implement it here. */
|
|
|
|
|
|
|
|
#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. */
|
|
|
|
#define SDL_MAIN_CALLBACK_STANDARD 1
|
|
|
|
|
|
|
|
int SDL_main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
return SDL_EnterAppMainCallbacks(argc, argv, SDL_AppInit, SDL_AppIterate, SDL_AppEvent, SDL_AppQuit);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* platform-specific tests */
|
|
|
|
|
|
|
|
#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. */
|
|
|
|
#if !defined(SDL_MAIN_USE_CALLBACKS) || defined(SDL_MAIN_CALLBACK_STANDARD)
|
|
|
|
|
2022-12-10 20:45:38 -07:00
|
|
|
#if defined(__WIN32__) || defined(__GDK__)
|
Implement SDL_main as header-only lib for Win32
(remaining platforms will follow)
SDL_main.h is *not* included by SDL.h anymore, users are supposed to
include it directly now, usually only in the file they implement main() in.
If they need the header elsewhere or don't want SDL_main to implement
main() (but only call SDL_SetMainReady() or whatever), they
can #define SDL_MAIN_HANDLED first, same as before.
For SDL-internal usage, I added _SDL_MAIN_NOIMPL, which *also* skips the
implementation and `#define main SDL_main`, but still defines
SDL_MAIN_AVAILABLE and SDL_MAIN_NEEDED in SDL_main.h, as before.
To make the implementaion in the header shorter and avoid including windows.h,
I moved most of the Win32 SDL_main code into SDL3.dll via SDL_Win32RunApp(),
so the header-only part is just the different main functions calling
SDL_Win32RunApp(SDL_main, NULL)
Note that I changed changed the return value and type of OutOfMemory()
to return -1 instead of FALSE, so main() (or WinMain() or whatever)
returns -1 instead of 0 in case of an out-of-memory error
Compared to original Win32 SDL_main, I tweaked the part of the
implementation in SDL_main_impl.h a bit to avoid linker warnings
and conflicts with stuff from windows.h:
- replaced windows.h with own define of WINAPI
and typedef-ing HINSTANCE and LPSTR.
This prevents conflicts between all the generically-named #defines and
types in windows.h and user code (like DrawState in some SDL tests)
- only using one of main() or wmain() gets rid of a MSVC linker error
("warning LNK4067: ambiguous entry point")
If this still causes problems, we might try getting rid of wmain(),
seemed to me like MSVC can use regular main() in UNICODE mode as well
- simplified the UNICODE logic for that - while this is not exactly
equivalent to the old, it should make sense and Works For Me
2022-12-03 19:29:22 -07:00
|
|
|
|
|
|
|
/* these defines/typedefs are needed for the WinMain() definition */
|
|
|
|
#ifndef WINAPI
|
|
|
|
#define WINAPI __stdcall
|
|
|
|
#endif
|
|
|
|
|
2022-12-10 20:45:38 -07:00
|
|
|
typedef struct HINSTANCE__ * HINSTANCE;
|
|
|
|
typedef char* LPSTR;
|
2022-12-29 22:16:22 -07:00
|
|
|
typedef wchar_t* PWSTR;
|
2022-12-10 20:45:38 -07:00
|
|
|
|
2022-12-29 22:16:22 -07:00
|
|
|
/* The VC++ compiler needs main/wmain defined, but not for GDK */
|
|
|
|
#if defined(_MSC_VER) && !defined(__GDK__)
|
Implement SDL_main as header-only lib for Win32
(remaining platforms will follow)
SDL_main.h is *not* included by SDL.h anymore, users are supposed to
include it directly now, usually only in the file they implement main() in.
If they need the header elsewhere or don't want SDL_main to implement
main() (but only call SDL_SetMainReady() or whatever), they
can #define SDL_MAIN_HANDLED first, same as before.
For SDL-internal usage, I added _SDL_MAIN_NOIMPL, which *also* skips the
implementation and `#define main SDL_main`, but still defines
SDL_MAIN_AVAILABLE and SDL_MAIN_NEEDED in SDL_main.h, as before.
To make the implementaion in the header shorter and avoid including windows.h,
I moved most of the Win32 SDL_main code into SDL3.dll via SDL_Win32RunApp(),
so the header-only part is just the different main functions calling
SDL_Win32RunApp(SDL_main, NULL)
Note that I changed changed the return value and type of OutOfMemory()
to return -1 instead of FALSE, so main() (or WinMain() or whatever)
returns -1 instead of 0 in case of an out-of-memory error
Compared to original Win32 SDL_main, I tweaked the part of the
implementation in SDL_main_impl.h a bit to avoid linker warnings
and conflicts with stuff from windows.h:
- replaced windows.h with own define of WINAPI
and typedef-ing HINSTANCE and LPSTR.
This prevents conflicts between all the generically-named #defines and
types in windows.h and user code (like DrawState in some SDL tests)
- only using one of main() or wmain() gets rid of a MSVC linker error
("warning LNK4067: ambiguous entry point")
If this still causes problems, we might try getting rid of wmain(),
seemed to me like MSVC can use regular main() in UNICODE mode as well
- simplified the UNICODE logic for that - while this is not exactly
equivalent to the old, it should make sense and Works For Me
2022-12-03 19:29:22 -07:00
|
|
|
|
2022-12-29 22:16:22 -07:00
|
|
|
/* This is where execution begins [console apps] */
|
Implement SDL_main as header-only lib for Win32
(remaining platforms will follow)
SDL_main.h is *not* included by SDL.h anymore, users are supposed to
include it directly now, usually only in the file they implement main() in.
If they need the header elsewhere or don't want SDL_main to implement
main() (but only call SDL_SetMainReady() or whatever), they
can #define SDL_MAIN_HANDLED first, same as before.
For SDL-internal usage, I added _SDL_MAIN_NOIMPL, which *also* skips the
implementation and `#define main SDL_main`, but still defines
SDL_MAIN_AVAILABLE and SDL_MAIN_NEEDED in SDL_main.h, as before.
To make the implementaion in the header shorter and avoid including windows.h,
I moved most of the Win32 SDL_main code into SDL3.dll via SDL_Win32RunApp(),
so the header-only part is just the different main functions calling
SDL_Win32RunApp(SDL_main, NULL)
Note that I changed changed the return value and type of OutOfMemory()
to return -1 instead of FALSE, so main() (or WinMain() or whatever)
returns -1 instead of 0 in case of an out-of-memory error
Compared to original Win32 SDL_main, I tweaked the part of the
implementation in SDL_main_impl.h a bit to avoid linker warnings
and conflicts with stuff from windows.h:
- replaced windows.h with own define of WINAPI
and typedef-ing HINSTANCE and LPSTR.
This prevents conflicts between all the generically-named #defines and
types in windows.h and user code (like DrawState in some SDL tests)
- only using one of main() or wmain() gets rid of a MSVC linker error
("warning LNK4067: ambiguous entry point")
If this still causes problems, we might try getting rid of wmain(),
seemed to me like MSVC can use regular main() in UNICODE mode as well
- simplified the UNICODE logic for that - while this is not exactly
equivalent to the old, it should make sense and Works For Me
2022-12-03 19:29:22 -07:00
|
|
|
#if defined( UNICODE ) && UNICODE
|
2022-12-29 22:16:22 -07:00
|
|
|
int wmain(int argc, wchar_t *wargv[], wchar_t *wenvp)
|
2023-01-07 09:28:07 -07:00
|
|
|
{
|
|
|
|
(void)argc;
|
|
|
|
(void)wargv;
|
|
|
|
(void)wenvp;
|
|
|
|
return SDL_RunApp(0, NULL, SDL_main, NULL);
|
|
|
|
}
|
Implement SDL_main as header-only lib for Win32
(remaining platforms will follow)
SDL_main.h is *not* included by SDL.h anymore, users are supposed to
include it directly now, usually only in the file they implement main() in.
If they need the header elsewhere or don't want SDL_main to implement
main() (but only call SDL_SetMainReady() or whatever), they
can #define SDL_MAIN_HANDLED first, same as before.
For SDL-internal usage, I added _SDL_MAIN_NOIMPL, which *also* skips the
implementation and `#define main SDL_main`, but still defines
SDL_MAIN_AVAILABLE and SDL_MAIN_NEEDED in SDL_main.h, as before.
To make the implementaion in the header shorter and avoid including windows.h,
I moved most of the Win32 SDL_main code into SDL3.dll via SDL_Win32RunApp(),
so the header-only part is just the different main functions calling
SDL_Win32RunApp(SDL_main, NULL)
Note that I changed changed the return value and type of OutOfMemory()
to return -1 instead of FALSE, so main() (or WinMain() or whatever)
returns -1 instead of 0 in case of an out-of-memory error
Compared to original Win32 SDL_main, I tweaked the part of the
implementation in SDL_main_impl.h a bit to avoid linker warnings
and conflicts with stuff from windows.h:
- replaced windows.h with own define of WINAPI
and typedef-ing HINSTANCE and LPSTR.
This prevents conflicts between all the generically-named #defines and
types in windows.h and user code (like DrawState in some SDL tests)
- only using one of main() or wmain() gets rid of a MSVC linker error
("warning LNK4067: ambiguous entry point")
If this still causes problems, we might try getting rid of wmain(),
seemed to me like MSVC can use regular main() in UNICODE mode as well
- simplified the UNICODE logic for that - while this is not exactly
equivalent to the old, it should make sense and Works For Me
2022-12-03 19:29:22 -07:00
|
|
|
#else /* ANSI */
|
2022-12-29 22:16:22 -07:00
|
|
|
int main(int argc, char *argv[])
|
Implement SDL_main as header-only lib for Win32
(remaining platforms will follow)
SDL_main.h is *not* included by SDL.h anymore, users are supposed to
include it directly now, usually only in the file they implement main() in.
If they need the header elsewhere or don't want SDL_main to implement
main() (but only call SDL_SetMainReady() or whatever), they
can #define SDL_MAIN_HANDLED first, same as before.
For SDL-internal usage, I added _SDL_MAIN_NOIMPL, which *also* skips the
implementation and `#define main SDL_main`, but still defines
SDL_MAIN_AVAILABLE and SDL_MAIN_NEEDED in SDL_main.h, as before.
To make the implementaion in the header shorter and avoid including windows.h,
I moved most of the Win32 SDL_main code into SDL3.dll via SDL_Win32RunApp(),
so the header-only part is just the different main functions calling
SDL_Win32RunApp(SDL_main, NULL)
Note that I changed changed the return value and type of OutOfMemory()
to return -1 instead of FALSE, so main() (or WinMain() or whatever)
returns -1 instead of 0 in case of an out-of-memory error
Compared to original Win32 SDL_main, I tweaked the part of the
implementation in SDL_main_impl.h a bit to avoid linker warnings
and conflicts with stuff from windows.h:
- replaced windows.h with own define of WINAPI
and typedef-ing HINSTANCE and LPSTR.
This prevents conflicts between all the generically-named #defines and
types in windows.h and user code (like DrawState in some SDL tests)
- only using one of main() or wmain() gets rid of a MSVC linker error
("warning LNK4067: ambiguous entry point")
If this still causes problems, we might try getting rid of wmain(),
seemed to me like MSVC can use regular main() in UNICODE mode as well
- simplified the UNICODE logic for that - while this is not exactly
equivalent to the old, it should make sense and Works For Me
2022-12-03 19:29:22 -07:00
|
|
|
{
|
2023-01-06 17:24:20 -07:00
|
|
|
(void)argc;
|
|
|
|
(void)argv;
|
2022-12-14 22:01:34 -07:00
|
|
|
return SDL_RunApp(0, NULL, SDL_main, NULL);
|
Implement SDL_main as header-only lib for Win32
(remaining platforms will follow)
SDL_main.h is *not* included by SDL.h anymore, users are supposed to
include it directly now, usually only in the file they implement main() in.
If they need the header elsewhere or don't want SDL_main to implement
main() (but only call SDL_SetMainReady() or whatever), they
can #define SDL_MAIN_HANDLED first, same as before.
For SDL-internal usage, I added _SDL_MAIN_NOIMPL, which *also* skips the
implementation and `#define main SDL_main`, but still defines
SDL_MAIN_AVAILABLE and SDL_MAIN_NEEDED in SDL_main.h, as before.
To make the implementaion in the header shorter and avoid including windows.h,
I moved most of the Win32 SDL_main code into SDL3.dll via SDL_Win32RunApp(),
so the header-only part is just the different main functions calling
SDL_Win32RunApp(SDL_main, NULL)
Note that I changed changed the return value and type of OutOfMemory()
to return -1 instead of FALSE, so main() (or WinMain() or whatever)
returns -1 instead of 0 in case of an out-of-memory error
Compared to original Win32 SDL_main, I tweaked the part of the
implementation in SDL_main_impl.h a bit to avoid linker warnings
and conflicts with stuff from windows.h:
- replaced windows.h with own define of WINAPI
and typedef-ing HINSTANCE and LPSTR.
This prevents conflicts between all the generically-named #defines and
types in windows.h and user code (like DrawState in some SDL tests)
- only using one of main() or wmain() gets rid of a MSVC linker error
("warning LNK4067: ambiguous entry point")
If this still causes problems, we might try getting rid of wmain(),
seemed to me like MSVC can use regular main() in UNICODE mode as well
- simplified the UNICODE logic for that - while this is not exactly
equivalent to the old, it should make sense and Works For Me
2022-12-03 19:29:22 -07:00
|
|
|
}
|
2023-01-07 09:28:07 -07:00
|
|
|
#endif /* UNICODE */
|
Implement SDL_main as header-only lib for Win32
(remaining platforms will follow)
SDL_main.h is *not* included by SDL.h anymore, users are supposed to
include it directly now, usually only in the file they implement main() in.
If they need the header elsewhere or don't want SDL_main to implement
main() (but only call SDL_SetMainReady() or whatever), they
can #define SDL_MAIN_HANDLED first, same as before.
For SDL-internal usage, I added _SDL_MAIN_NOIMPL, which *also* skips the
implementation and `#define main SDL_main`, but still defines
SDL_MAIN_AVAILABLE and SDL_MAIN_NEEDED in SDL_main.h, as before.
To make the implementaion in the header shorter and avoid including windows.h,
I moved most of the Win32 SDL_main code into SDL3.dll via SDL_Win32RunApp(),
so the header-only part is just the different main functions calling
SDL_Win32RunApp(SDL_main, NULL)
Note that I changed changed the return value and type of OutOfMemory()
to return -1 instead of FALSE, so main() (or WinMain() or whatever)
returns -1 instead of 0 in case of an out-of-memory error
Compared to original Win32 SDL_main, I tweaked the part of the
implementation in SDL_main_impl.h a bit to avoid linker warnings
and conflicts with stuff from windows.h:
- replaced windows.h with own define of WINAPI
and typedef-ing HINSTANCE and LPSTR.
This prevents conflicts between all the generically-named #defines and
types in windows.h and user code (like DrawState in some SDL tests)
- only using one of main() or wmain() gets rid of a MSVC linker error
("warning LNK4067: ambiguous entry point")
If this still causes problems, we might try getting rid of wmain(),
seemed to me like MSVC can use regular main() in UNICODE mode as well
- simplified the UNICODE logic for that - while this is not exactly
equivalent to the old, it should make sense and Works For Me
2022-12-03 19:29:22 -07:00
|
|
|
|
2022-12-29 22:16:22 -07:00
|
|
|
#endif /* _MSC_VER && ! __GDK__ */
|
2022-12-10 20:45:38 -07:00
|
|
|
|
|
|
|
/* This is where execution begins [windowed apps and GDK] */
|
2022-12-29 22:16:22 -07:00
|
|
|
#if defined( UNICODE ) && UNICODE
|
|
|
|
int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE hPrev, PWSTR szCmdLine, int sw)
|
|
|
|
#else /* ANSI */
|
|
|
|
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
|
|
|
|
#endif
|
Implement SDL_main as header-only lib for Win32
(remaining platforms will follow)
SDL_main.h is *not* included by SDL.h anymore, users are supposed to
include it directly now, usually only in the file they implement main() in.
If they need the header elsewhere or don't want SDL_main to implement
main() (but only call SDL_SetMainReady() or whatever), they
can #define SDL_MAIN_HANDLED first, same as before.
For SDL-internal usage, I added _SDL_MAIN_NOIMPL, which *also* skips the
implementation and `#define main SDL_main`, but still defines
SDL_MAIN_AVAILABLE and SDL_MAIN_NEEDED in SDL_main.h, as before.
To make the implementaion in the header shorter and avoid including windows.h,
I moved most of the Win32 SDL_main code into SDL3.dll via SDL_Win32RunApp(),
so the header-only part is just the different main functions calling
SDL_Win32RunApp(SDL_main, NULL)
Note that I changed changed the return value and type of OutOfMemory()
to return -1 instead of FALSE, so main() (or WinMain() or whatever)
returns -1 instead of 0 in case of an out-of-memory error
Compared to original Win32 SDL_main, I tweaked the part of the
implementation in SDL_main_impl.h a bit to avoid linker warnings
and conflicts with stuff from windows.h:
- replaced windows.h with own define of WINAPI
and typedef-ing HINSTANCE and LPSTR.
This prevents conflicts between all the generically-named #defines and
types in windows.h and user code (like DrawState in some SDL tests)
- only using one of main() or wmain() gets rid of a MSVC linker error
("warning LNK4067: ambiguous entry point")
If this still causes problems, we might try getting rid of wmain(),
seemed to me like MSVC can use regular main() in UNICODE mode as well
- simplified the UNICODE logic for that - while this is not exactly
equivalent to the old, it should make sense and Works For Me
2022-12-03 19:29:22 -07:00
|
|
|
{
|
2023-01-06 17:24:20 -07:00
|
|
|
(void)hInst;
|
|
|
|
(void)hPrev;
|
|
|
|
(void)szCmdLine;
|
|
|
|
(void)sw;
|
2022-12-14 22:01:34 -07:00
|
|
|
return SDL_RunApp(0, NULL, SDL_main, NULL);
|
Implement SDL_main as header-only lib for Win32
(remaining platforms will follow)
SDL_main.h is *not* included by SDL.h anymore, users are supposed to
include it directly now, usually only in the file they implement main() in.
If they need the header elsewhere or don't want SDL_main to implement
main() (but only call SDL_SetMainReady() or whatever), they
can #define SDL_MAIN_HANDLED first, same as before.
For SDL-internal usage, I added _SDL_MAIN_NOIMPL, which *also* skips the
implementation and `#define main SDL_main`, but still defines
SDL_MAIN_AVAILABLE and SDL_MAIN_NEEDED in SDL_main.h, as before.
To make the implementaion in the header shorter and avoid including windows.h,
I moved most of the Win32 SDL_main code into SDL3.dll via SDL_Win32RunApp(),
so the header-only part is just the different main functions calling
SDL_Win32RunApp(SDL_main, NULL)
Note that I changed changed the return value and type of OutOfMemory()
to return -1 instead of FALSE, so main() (or WinMain() or whatever)
returns -1 instead of 0 in case of an out-of-memory error
Compared to original Win32 SDL_main, I tweaked the part of the
implementation in SDL_main_impl.h a bit to avoid linker warnings
and conflicts with stuff from windows.h:
- replaced windows.h with own define of WINAPI
and typedef-ing HINSTANCE and LPSTR.
This prevents conflicts between all the generically-named #defines and
types in windows.h and user code (like DrawState in some SDL tests)
- only using one of main() or wmain() gets rid of a MSVC linker error
("warning LNK4067: ambiguous entry point")
If this still causes problems, we might try getting rid of wmain(),
seemed to me like MSVC can use regular main() in UNICODE mode as well
- simplified the UNICODE logic for that - while this is not exactly
equivalent to the old, it should make sense and Works For Me
2022-12-03 19:29:22 -07:00
|
|
|
}
|
|
|
|
|
2022-12-10 20:45:38 -07:00
|
|
|
/* end of __WIN32__ and __GDK__ impls */
|
2022-12-11 17:22:43 -07:00
|
|
|
#elif defined(__WINRT__)
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
an APPX0702 build error can appear shortly after linking.
|
|
|
|
|
|
|
|
The following set of preprocessor code forces this file to be compiled
|
|
|
|
as C++/CX, which appears to cause Visual C++ 2012's build tools to
|
|
|
|
create this .winmd file, and will help allow builds of SDL/WinRT apps
|
|
|
|
to proceed without error.
|
|
|
|
|
|
|
|
If other files in an app's project enable C++/CX compilation, then it might
|
|
|
|
be possible for the .cpp file including SDL_main.h to be compiled without /ZW,
|
|
|
|
for Visual C++'s build tools to create a winmd file, and for the app to
|
|
|
|
build without APPX0702 errors. In this case, if
|
|
|
|
SDL_WINRT_METADATA_FILE_AVAILABLE is defined as a C/C++ macro, then
|
|
|
|
the #error (to force C++/CX compilation) will be disabled.
|
|
|
|
|
|
|
|
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
|
|
|
|
setting through the dialog that comes up.
|
|
|
|
*/
|
|
|
|
#ifndef SDL_WINRT_METADATA_FILE_AVAILABLE
|
|
|
|
#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.
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Prevent MSVC++ from warning about threading models when defining our
|
|
|
|
custom WinMain. The threading model will instead be set via a direct
|
|
|
|
call to Windows::Foundation::Initialize (rather than via an attributed
|
|
|
|
function).
|
|
|
|
|
|
|
|
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).
|
|
|
|
*/
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(disable : 4447)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Make sure the function to initialize the Windows Runtime gets linked in. */
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma comment(lib, "runtimeobject.lib")
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int CALLBACK WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
|
|
|
|
{
|
2022-12-14 22:01:34 -07:00
|
|
|
return SDL_RunApp(0, NULL, SDL_main, NULL);
|
2022-12-11 17:22:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* end of WinRT impl */
|
2022-12-12 10:30:05 -07:00
|
|
|
#elif defined(__NGAGE__)
|
|
|
|
|
|
|
|
/* same typedef as in ngage SDKs e32def.h */
|
|
|
|
typedef signed int TInt;
|
|
|
|
/* TODO: if it turns out that this only works when built as C++,
|
|
|
|
move __NGAGE__ into the C++ section in SDL_main.h */
|
|
|
|
TInt E32Main()
|
|
|
|
{
|
|
|
|
return SDL_RunApp(0, NULL, SDL_main, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* end of __NGAGE__ impl */
|
|
|
|
|
|
|
|
#else /* platforms that use a standard main() and just call SDL_RunApp(), like iOS and 3DS */
|
2022-12-11 20:38:06 -07:00
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2022-12-14 22:01:34 -07:00
|
|
|
return SDL_RunApp(argc, argv, SDL_main, NULL);
|
2022-12-11 20:38:06 -07:00
|
|
|
}
|
|
|
|
|
2022-12-12 10:30:05 -07:00
|
|
|
/* end of impls for standard-conforming platforms */
|
Implement SDL_main as header-only lib for Win32
(remaining platforms will follow)
SDL_main.h is *not* included by SDL.h anymore, users are supposed to
include it directly now, usually only in the file they implement main() in.
If they need the header elsewhere or don't want SDL_main to implement
main() (but only call SDL_SetMainReady() or whatever), they
can #define SDL_MAIN_HANDLED first, same as before.
For SDL-internal usage, I added _SDL_MAIN_NOIMPL, which *also* skips the
implementation and `#define main SDL_main`, but still defines
SDL_MAIN_AVAILABLE and SDL_MAIN_NEEDED in SDL_main.h, as before.
To make the implementaion in the header shorter and avoid including windows.h,
I moved most of the Win32 SDL_main code into SDL3.dll via SDL_Win32RunApp(),
so the header-only part is just the different main functions calling
SDL_Win32RunApp(SDL_main, NULL)
Note that I changed changed the return value and type of OutOfMemory()
to return -1 instead of FALSE, so main() (or WinMain() or whatever)
returns -1 instead of 0 in case of an out-of-memory error
Compared to original Win32 SDL_main, I tweaked the part of the
implementation in SDL_main_impl.h a bit to avoid linker warnings
and conflicts with stuff from windows.h:
- replaced windows.h with own define of WINAPI
and typedef-ing HINSTANCE and LPSTR.
This prevents conflicts between all the generically-named #defines and
types in windows.h and user code (like DrawState in some SDL tests)
- only using one of main() or wmain() gets rid of a MSVC linker error
("warning LNK4067: ambiguous entry point")
If this still causes problems, we might try getting rid of wmain(),
seemed to me like MSVC can use regular main() in UNICODE mode as well
- simplified the UNICODE logic for that - while this is not exactly
equivalent to the old, it should make sense and Works For Me
2022-12-03 19:29:22 -07:00
|
|
|
|
|
|
|
#endif /* __WIN32__ etc */
|
|
|
|
|
main: Added _optional_ callback entry points.
This lets apps optionally have a handful of callbacks for their entry points instead of a single main function. If used, the actual main/SDL_main/whatever entry point will be implemented in the single-header library SDL_main.h and the app will implement four separate functions:
First:
int SDL_AppInit(int argc, char **argv);
This will be called once before anything else. argc/argv work like they always do. If this returns 0, the app runs. If it returns < 0, the app calls SDL_AppQuit and terminates with an exit code that reports an error to the platform. If it returns > 0, the app calls SDL_AppQuit and terminates with an exit code that reports success to the platform. This function should not go into an infinite mainloop; it should do any one-time startup it requires and then return.
Then:
int SDL_AppIterate(void);
This is called over and over, possibly at the refresh rate of the display or some other metric that the platform dictates. This is where the heart of your app runs. It should return as quickly as reasonably possible, but it's not a "run one memcpy and that's all the time you have" sort of thing. The app should do any game updates, and render a frame of video. If it returns < 0, SDL will call SDL_AppQuit and terminate the process with an exit code that reports an error to the platform. If it returns > 0, the app calls SDL_AppQuit and terminates with an exit code that reports success to the platform. If it returns 0, then SDL_AppIterate will be called again at some regular frequency. The platform may choose to run this more or less (perhaps less in the background, etc), or it might just call this function in a loop as fast as possible. You do not check the event queue in this function (SDL_AppEvent exists for that).
Next:
int SDL_AppEvent(const SDL_Event *event);
This will be called once for each event pushed into the SDL queue. This may be called from any thread, and possibly in parallel to SDL_AppIterate. The fields in event do not need to be free'd (as you would normally need to do for SDL_EVENT_DROP_FILE, etc), and your app should not call SDL_PollEvent, SDL_PumpEvent, etc, as SDL will manage this for you. Return values are the same as from SDL_AppIterate(), so you can terminate in response to SDL_EVENT_QUIT, etc.
Finally:
void SDL_AppQuit(void);
This is called once before terminating the app--assuming the app isn't being forcibly killed or crashed--as a last chance to clean up. After this returns, SDL will call SDL_Quit so the app doesn't have to (but it's safe for the app to call it, too). Process termination proceeds as if the app returned normally from main(), so atexit handles will run, if your platform supports that.
The app does not implement SDL_main if using this. To turn this on, define SDL_MAIN_USE_CALLBACKS before including SDL_main.h. Defines like SDL_MAIN_HANDLED and SDL_MAIN_NOIMPL are also respected for callbacks, if the app wants to do some sort of magic main implementation thing.
In theory, on most platforms these can be implemented in the app itself, but this saves some #ifdefs in the app and lets everyone struggle less against some platforms, and might be more efficient in the long run, too.
On some platforms, it's possible this is the only reasonable way to go, but we haven't actually hit one that 100% requires it yet (but we will, if we want to write a RetroArch backend, for example).
Using the callback entry points works on every platform, because on platforms that don't require them, we can fake them with a simple loop in an internal implementation of the usual SDL_main.
The primary way we expect people to write SDL apps is with SDL_main, and this is not intended to replace it. If the app chooses to use this, it just removes some platform-specific details they might have to otherwise manage, and maybe removes a barrier to entry on some future platform.
Fixes #6785.
Reference PR #8247.
2023-11-01 16:40:41 -06:00
|
|
|
#endif /* !defined(SDL_MAIN_USE_CALLBACKS) || defined(SDL_MAIN_CALLBACK_STANDARD) */
|
|
|
|
|
2022-12-11 20:38:06 -07:00
|
|
|
/* rename users main() function to SDL_main() so it can be called from the wrappers above */
|
|
|
|
#define main SDL_main
|
|
|
|
|
Implement SDL_main as header-only lib for Win32
(remaining platforms will follow)
SDL_main.h is *not* included by SDL.h anymore, users are supposed to
include it directly now, usually only in the file they implement main() in.
If they need the header elsewhere or don't want SDL_main to implement
main() (but only call SDL_SetMainReady() or whatever), they
can #define SDL_MAIN_HANDLED first, same as before.
For SDL-internal usage, I added _SDL_MAIN_NOIMPL, which *also* skips the
implementation and `#define main SDL_main`, but still defines
SDL_MAIN_AVAILABLE and SDL_MAIN_NEEDED in SDL_main.h, as before.
To make the implementaion in the header shorter and avoid including windows.h,
I moved most of the Win32 SDL_main code into SDL3.dll via SDL_Win32RunApp(),
so the header-only part is just the different main functions calling
SDL_Win32RunApp(SDL_main, NULL)
Note that I changed changed the return value and type of OutOfMemory()
to return -1 instead of FALSE, so main() (or WinMain() or whatever)
returns -1 instead of 0 in case of an out-of-memory error
Compared to original Win32 SDL_main, I tweaked the part of the
implementation in SDL_main_impl.h a bit to avoid linker warnings
and conflicts with stuff from windows.h:
- replaced windows.h with own define of WINAPI
and typedef-ing HINSTANCE and LPSTR.
This prevents conflicts between all the generically-named #defines and
types in windows.h and user code (like DrawState in some SDL tests)
- only using one of main() or wmain() gets rid of a MSVC linker error
("warning LNK4067: ambiguous entry point")
If this still causes problems, we might try getting rid of wmain(),
seemed to me like MSVC can use regular main() in UNICODE mode as well
- simplified the UNICODE logic for that - while this is not exactly
equivalent to the old, it should make sense and Works For Me
2022-12-03 19:29:22 -07:00
|
|
|
#endif /* SDL_MAIN_HANDLED */
|
|
|
|
|
2022-12-17 22:13:25 -07:00
|
|
|
#endif /* SDL_main_impl_h_ */
|