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
This commit is contained in:
Daniel Gibson 2022-12-04 03:29:22 +01:00 committed by Sam Lantinga
parent ffaf451558
commit ca2fe7be1a
10 changed files with 214 additions and 105 deletions

View file

@ -207,6 +207,21 @@ extern DECLSPEC void SDLCALL SDL_UnregisterApp(void);
#endif /* defined(__WIN32__) || defined(__GDK__) */
#ifdef __WIN32__
/**
* Initialize and launch an SDL/Win32 (classic WinAPI) application.
*
* \param mainFunction the SDL app's C-style main(), an SDL_main_func
* \param reserved reserved for future use; should be NULL
* \returns 0 on success or -1 on failure; call SDL_GetError() to retrieve
* more information on the failure.
*
* \since This function is available since SDL 3.0.0.
*/
extern DECLSPEC int SDLCALL SDL_Win32RunApp(SDL_main_func mainFunction, void * reserved);
#endif /* __WIN32__ */
#ifdef __WINRT__
@ -266,8 +281,16 @@ extern DECLSPEC void SDLCALL SDL_GDKSuspendComplete(void);
#ifdef __cplusplus
}
#endif
#include <SDL3/close_code.h>
#if !defined(SDL_MAIN_HANDLED) && !defined(_SDL_MAIN_NOIMPL)
/* include header-only SDL_main implementations */
#if defined(__WIN32__) || defined(__GDK__) /* TODO: other platforms */
#include <SDL3/SDL_main_impl.h>
#endif
#endif /* SDL_MAIN_HANDLED */
#endif /* SDL_main_h_ */
/* vi: set ts=4 sw=4 expandtab: */