@@ -31,75 +31,12 @@ extern "C" {
31
31
#include < shellapi.h> // CommandLineToArgvW()
32
32
#include < appnotify.h>
33
33
34
- static int OutOfMemory (void )
35
- {
36
- SDL_ShowSimpleMessageBox (SDL_MESSAGEBOX_ERROR, " Fatal Error" , " Out of memory - aborting" , NULL );
37
- return -1 ;
38
- }
39
-
40
- static int ErrorProcessingCommandLine (void )
41
- {
42
- SDL_ShowSimpleMessageBox (SDL_MESSAGEBOX_ERROR, " Fatal Error" , " Error processing command line arguments" , NULL );
43
- return -1 ;
44
- }
45
-
46
34
extern " C"
47
- int SDL_RunApp (int caller_argc , char * caller_argv [], SDL_main_func mainFunction, void *)
35
+ int SDL_RunApp (int argc , char * argv [], SDL_main_func mainFunction, void *)
48
36
{
49
- int result, argc;
50
- LPWSTR *argvw = NULL ;
51
- char **argv = NULL ;
52
37
HRESULT hr;
53
38
XTaskQueueHandle taskQueue;
54
39
55
- // Note that we need to be careful about how we allocate/free memory in this function. If the application calls
56
- // SDL_SetMemoryFunctions(), we can't rely on SDL_free() to use the same allocator after SDL_main() returns.
57
-
58
- if (!caller_argv || caller_argc < 0 ) {
59
- // If the passed argv is NULL or argc is negative, the user expects SDL to get the command line arguments
60
- // using GetCommandLineW() and convert them to argc and argv before calling mainFunction().
61
-
62
- // Because of how the Windows command line works, we know for sure that the buffer size required to store all
63
- // argument strings converted to UTF-8 (with null terminators) is guaranteed to be less than or equal to the
64
- // size of the original command line string converted to UTF-8.
65
- const int argdata_size = WideCharToMultiByte (CP_UTF8, 0 , GetCommandLineW (), -1 , NULL , 0 , NULL , NULL ); // Includes the null terminator
66
- if (!argdata_size) {
67
- result = ErrorProcessingCommandLine ();
68
- goto cleanup;
69
- }
70
-
71
- argvw = CommandLineToArgvW (GetCommandLineW (), &argc);
72
- if (!argvw || argc < 0 ) {
73
- result = OutOfMemory ();
74
- goto cleanup;
75
- }
76
-
77
- // Allocate argv followed by the argument string buffer as one contiguous allocation.
78
- argv = (char **)HeapAlloc (GetProcessHeap (), HEAP_ZERO_MEMORY, (argc + 1 ) * sizeof (*argv) + argdata_size);
79
- if (!argv) {
80
- result = OutOfMemory ();
81
- goto cleanup;
82
- }
83
- char *argdata = ((char *)argv) + (argc + 1 ) * sizeof (*argv);
84
- int argdata_index = 0 ;
85
-
86
- for (int i = 0 ; i < argc; ++i) {
87
- const int bytes_written = WideCharToMultiByte (CP_UTF8, 0 , argvw[i], -1 , argdata + argdata_index, argdata_size - argdata_index, NULL , NULL );
88
- if (!bytes_written) {
89
- result = ErrorProcessingCommandLine ();
90
- goto cleanup;
91
- }
92
- argv[i] = argdata + argdata_index;
93
- argdata_index += bytes_written;
94
- }
95
- argv[argc] = NULL ;
96
-
97
- argvw = NULL ;
98
-
99
- caller_argc = argc;
100
- caller_argv = argv;
101
- }
102
-
103
40
// !!! FIXME: This function does not currently properly deinitialize GDK resources on failure.
104
41
105
42
hr = XGameRuntimeInitialize ();
@@ -130,7 +67,9 @@ int SDL_RunApp(int caller_argc, char* caller_argv[], SDL_main_func mainFunction,
130
67
return -1 ;
131
68
}
132
69
133
- result = mainFunction (caller_argc, caller_argv); // No need for SDL_CallMain(); we already know that we have a valid argv
70
+ // The common Windows SDL_CallMain() implementation is responsible for handling the argv
71
+ // and substituting it with a new argv parsed from the command-line string, if needed.
72
+ result = SDL_CallMain (argc, argv, mainFunction);
134
73
135
74
GDK_UnregisterChangeNotifications ();
136
75
@@ -156,10 +95,5 @@ int SDL_RunApp(int caller_argc, char* caller_argv[], SDL_main_func mainFunction,
156
95
result = -1 ;
157
96
}
158
97
159
- cleanup:
160
-
161
- HeapFree (GetProcessHeap (), 0 , argv);
162
- LocalFree (argvw);
163
-
164
98
return result;
165
99
}
0 commit comments