summaryrefslogtreecommitdiff
path: root/contrib/SDL-3.2.8/src/main/windows
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2025-12-27 12:03:39 -0800
committer3gg <3gg@shellblade.net>2025-12-27 12:03:39 -0800
commit5a079a2d114f96d4847d1ee305d5b7c16eeec50e (patch)
tree8926ab44f168acf787d8e19608857b3af0f82758 /contrib/SDL-3.2.8/src/main/windows
Initial commit
Diffstat (limited to 'contrib/SDL-3.2.8/src/main/windows')
-rw-r--r--contrib/SDL-3.2.8/src/main/windows/SDL_sysmain_runapp.c99
1 files changed, 99 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/main/windows/SDL_sysmain_runapp.c b/contrib/SDL-3.2.8/src/main/windows/SDL_sysmain_runapp.c
new file mode 100644
index 0000000..d8c3fac
--- /dev/null
+++ b/contrib/SDL-3.2.8/src/main/windows/SDL_sysmain_runapp.c
@@ -0,0 +1,99 @@
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2025 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#include "SDL_internal.h"
22
23#ifdef SDL_PLATFORM_WIN32
24
25#include "../../core/windows/SDL_windows.h"
26
27/* Win32-specific SDL_RunApp(), which does most of the SDL_main work,
28 based on SDL_windows_main.c, placed in the public domain by Sam Lantinga 4/13/98 */
29
30#include <shellapi.h> // CommandLineToArgvW()
31
32// Pop up an out of memory message, returns to Windows
33static int OutOfMemory(void)
34{
35 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal Error", "Out of memory - aborting", NULL);
36 return -1;
37}
38
39int MINGW32_FORCEALIGN SDL_RunApp(int _argc, char* _argv[], SDL_main_func mainFunction, void * reserved)
40{
41 /* Gets the arguments with GetCommandLine, converts them to argc and argv
42 and calls SDL_main */
43
44 LPWSTR *argvw;
45 char **argv;
46 int i, argc, result;
47
48 (void)_argc; (void)_argv; (void)reserved;
49
50 argvw = CommandLineToArgvW(GetCommandLineW(), &argc);
51 if (!argvw) {
52 return OutOfMemory();
53 }
54
55 /* Note that we need to be careful about how we allocate/free memory here.
56 * If the application calls SDL_SetMemoryFunctions(), we can't rely on
57 * SDL_free() to use the same allocator after SDL_main() returns.
58 */
59
60 // Parse it into argv and argc
61 argv = (char **)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (argc + 1) * sizeof(*argv));
62 if (!argv) {
63 return OutOfMemory();
64 }
65 for (i = 0; i < argc; ++i) {
66 const int utf8size = WideCharToMultiByte(CP_UTF8, 0, argvw[i], -1, NULL, 0, NULL, NULL);
67 if (!utf8size) { // uhoh?
68 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal Error", "Error processing command line arguments", NULL);
69 return -1;
70 }
71
72 argv[i] = (char *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, utf8size); // this size includes the null-terminator character.
73 if (!argv[i]) {
74 return OutOfMemory();
75 }
76
77 if (WideCharToMultiByte(CP_UTF8, 0, argvw[i], -1, argv[i], utf8size, NULL, NULL) == 0) { // failed? uhoh!
78 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal Error", "Error processing command line arguments", NULL);
79 return -1;
80 }
81 }
82 argv[i] = NULL;
83 LocalFree(argvw);
84
85 SDL_SetMainReady();
86
87 // Run the application main() code
88 result = mainFunction(argc, argv);
89
90 // Free argv, to avoid memory leak
91 for (i = 0; i < argc; ++i) {
92 HeapFree(GetProcessHeap(), 0, argv[i]);
93 }
94 HeapFree(GetProcessHeap(), 0, argv);
95
96 return result;
97}
98
99#endif // SDL_PLATFORM_WIN32