mirror of
https://github.com/libsdl-org/SDL.git
synced 2025-10-04 00:46:25 +00:00
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:

committed by
Sam Lantinga

parent
ffaf451558
commit
ca2fe7be1a
@@ -181,6 +181,8 @@
|
||||
#endif
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#define _SDL_MAIN_NOIMPL /* don't drag in header-only implementation of SDL_main */
|
||||
#include <SDL3/SDL_main.h>
|
||||
|
||||
/* The internal implementations of these functions have up to nanosecond precision.
|
||||
We can expose these functions as part of the API if we want to later.
|
||||
|
@@ -331,6 +331,78 @@ void WIN_RectToRECT(const SDL_Rect *sdlrect, RECT *winrect)
|
||||
winrect->bottom = sdlrect->y + sdlrect->h - 1;
|
||||
}
|
||||
|
||||
/* SDL_Win32RunApp(), which does most of the SDL_main work for Win32 */
|
||||
#ifdef __WIN32__
|
||||
|
||||
#include <shellapi.h> /* CommandLineToArgvW() */
|
||||
|
||||
/* Pop up an out of memory message, returns to Windows */
|
||||
static int
|
||||
OutOfMemory(void)
|
||||
{
|
||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal Error", "Out of memory - aborting", NULL);
|
||||
return -1;
|
||||
}
|
||||
|
||||
DECLSPEC int
|
||||
SDL_Win32RunApp(SDL_main_func mainFunction, void * xamlBackgroundPanel)
|
||||
{
|
||||
|
||||
/* Gets the arguments with GetCommandLine, converts them to argc and argv
|
||||
and calls SDL_main */
|
||||
|
||||
LPWSTR *argvw;
|
||||
char **argv;
|
||||
int i, argc, result;
|
||||
|
||||
argvw = CommandLineToArgvW(GetCommandLineW(), &argc);
|
||||
if (argvw == NULL) {
|
||||
return OutOfMemory();
|
||||
}
|
||||
|
||||
/* Note that we need to be careful about how we allocate/free memory here.
|
||||
* If the application calls SDL_SetMemoryFunctions(), we can't rely on
|
||||
* SDL_free() to use the same allocator after SDL_main() returns.
|
||||
*/
|
||||
|
||||
/* Parse it into argv and argc */
|
||||
argv = (char **)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (argc + 1) * sizeof(*argv));
|
||||
if (argv == NULL) {
|
||||
return OutOfMemory();
|
||||
}
|
||||
for (i = 0; i < argc; ++i) {
|
||||
DWORD len;
|
||||
char *arg = WIN_StringToUTF8W(argvw[i]);
|
||||
if (arg == NULL) {
|
||||
return OutOfMemory();
|
||||
}
|
||||
len = (DWORD)SDL_strlen(arg);
|
||||
argv[i] = (char *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len + 1);
|
||||
if (!argv[i]) {
|
||||
return OutOfMemory();
|
||||
}
|
||||
SDL_memcpy(argv[i], arg, len);
|
||||
SDL_free(arg);
|
||||
}
|
||||
argv[i] = NULL;
|
||||
LocalFree(argvw);
|
||||
|
||||
SDL_SetMainReady();
|
||||
|
||||
/* Run the application main() code */
|
||||
result = mainFunction(argc, argv);
|
||||
|
||||
/* Free argv, to avoid memory leak */
|
||||
for (i = 0; i < argc; ++i) {
|
||||
HeapFree(GetProcessHeap(), 0, argv[i]);
|
||||
}
|
||||
HeapFree(GetProcessHeap(), 0, argv);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#endif /* __WIN32__ */
|
||||
|
||||
#endif /* defined(__WIN32__) || defined(__WINRT__) || defined(__GDK__) */
|
||||
|
||||
/*
|
||||
|
@@ -31,6 +31,9 @@
|
||||
#endif
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#define _SDL_MAIN_NOIMPL /* don't drag in header-only implementation of SDL_main */
|
||||
#include <SDL3/SDL_main.h>
|
||||
|
||||
|
||||
/* These headers have system specific definitions, so aren't included above */
|
||||
#include <SDL3/SDL_syswm.h>
|
||||
|
@@ -861,6 +861,7 @@ SDL3_0.0.0 {
|
||||
SDL_DelayNS;
|
||||
SDL_GetEventState;
|
||||
SDL_GetRenderDriver;
|
||||
SDL_Win32RunApp;
|
||||
# extra symbols go here (don't modify this line)
|
||||
local: *;
|
||||
};
|
||||
|
@@ -885,3 +885,4 @@
|
||||
#define SDL_DelayNS SDL_DelayNS_REAL
|
||||
#define SDL_GetEventState SDL_GetEventState_REAL
|
||||
#define SDL_GetRenderDriver SDL_GetRenderDriver_REAL
|
||||
#define SDL_Win32RunApp SDL_Win32RunApp_REAL
|
||||
|
@@ -961,3 +961,6 @@ SDL_DYNAPI_PROC(Uint64,SDL_GetTicksNS,(void),(),return)
|
||||
SDL_DYNAPI_PROC(void,SDL_DelayNS,(Uint64 a),(a),)
|
||||
SDL_DYNAPI_PROC(Uint8,SDL_GetEventState,(Uint32 a),(a),return)
|
||||
SDL_DYNAPI_PROC(const char*,SDL_GetRenderDriver,(int a),(a),return)
|
||||
#if defined(__WIN32__)
|
||||
SDL_DYNAPI_PROC(int,SDL_Win32RunApp,(SDL_main_func a, void *b),(a,b),return)
|
||||
#endif
|
||||
|
@@ -1,110 +1,8 @@
|
||||
/*
|
||||
SDL_windows_main.c, placed in the public domain by Sam Lantinga 4/13/98
|
||||
|
||||
The WinMain function -- calls your program's main() function
|
||||
Nothing to do here, the code moved into SDL_main_impl.h and SDL_windows.c (SDL_Win32RunApp())
|
||||
TODO: remove this file
|
||||
*/
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#ifdef __WIN32__
|
||||
|
||||
/* Include this so we define UNICODE properly */
|
||||
#include "../../core/windows/SDL_windows.h"
|
||||
#include <shellapi.h> /* CommandLineToArgvW() */
|
||||
|
||||
#ifdef main
|
||||
#undef main
|
||||
#endif /* main */
|
||||
|
||||
/* Pop up an out of memory message, returns to Windows */
|
||||
static BOOL OutOfMemory(void)
|
||||
{
|
||||
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal Error", "Out of memory - aborting", NULL);
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
/* The VC++ compiler needs main/wmain defined */
|
||||
#define console_ansi_main main
|
||||
#if UNICODE
|
||||
#define console_wmain wmain
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* Gets the arguments with GetCommandLine, converts them to argc and argv
|
||||
and calls SDL_main */
|
||||
static int main_getcmdline(void)
|
||||
{
|
||||
LPWSTR *argvw;
|
||||
char **argv;
|
||||
int i, argc, result;
|
||||
|
||||
argvw = CommandLineToArgvW(GetCommandLineW(), &argc);
|
||||
if (argvw == NULL) {
|
||||
return OutOfMemory();
|
||||
}
|
||||
|
||||
/* Note that we need to be careful about how we allocate/free memory here.
|
||||
* If the application calls SDL_SetMemoryFunctions(), we can't rely on
|
||||
* SDL_free() to use the same allocator after SDL_main() returns.
|
||||
*/
|
||||
|
||||
/* Parse it into argv and argc */
|
||||
argv = (char **)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (argc + 1) * sizeof(*argv));
|
||||
if (argv == NULL) {
|
||||
return OutOfMemory();
|
||||
}
|
||||
for (i = 0; i < argc; ++i) {
|
||||
DWORD len;
|
||||
char *arg = WIN_StringToUTF8W(argvw[i]);
|
||||
if (arg == NULL) {
|
||||
return OutOfMemory();
|
||||
}
|
||||
len = (DWORD)SDL_strlen(arg);
|
||||
argv[i] = (char *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (size_t)len + 1);
|
||||
if (!argv[i]) {
|
||||
return OutOfMemory();
|
||||
}
|
||||
SDL_memcpy(argv[i], arg, len);
|
||||
SDL_free(arg);
|
||||
}
|
||||
argv[i] = NULL;
|
||||
LocalFree(argvw);
|
||||
|
||||
SDL_SetMainReady();
|
||||
|
||||
/* Run the application main() code */
|
||||
result = SDL_main(argc, argv);
|
||||
|
||||
/* Free argv, to avoid memory leak */
|
||||
for (i = 0; i < argc; ++i) {
|
||||
HeapFree(GetProcessHeap(), 0, argv[i]);
|
||||
}
|
||||
HeapFree(GetProcessHeap(), 0, argv);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/* This is where execution begins [console apps, ansi] */
|
||||
int console_ansi_main(int argc, char *argv[])
|
||||
{
|
||||
return main_getcmdline();
|
||||
}
|
||||
|
||||
#if UNICODE
|
||||
/* This is where execution begins [console apps, unicode] */
|
||||
int console_wmain(int argc, wchar_t *wargv[], wchar_t *wenvp)
|
||||
{
|
||||
return main_getcmdline();
|
||||
}
|
||||
#endif
|
||||
|
||||
/* This is where execution begins [windowed apps] */
|
||||
int WINAPI
|
||||
WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw) /* NOLINT(readability-inconsistent-declaration-parameter-name) */
|
||||
{
|
||||
return main_getcmdline();
|
||||
}
|
||||
|
||||
#endif /* __WIN32__ */
|
||||
|
||||
/* vi: set ts=4 sw=4 expandtab: */
|
||||
|
Reference in New Issue
Block a user