@@ -31,75 +31,13 @@ extern "C" {
3131#include < shellapi.h> // CommandLineToArgvW()
3232#include < appnotify.h>
3333
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-
4634extern " 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 *)
4836{
49- int result, argc;
50- LPWSTR *argvw = NULL ;
51- char **argv = NULL ;
37+ int result;
5238 HRESULT hr;
5339 XTaskQueueHandle taskQueue;
5440
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-
10341 // !!! FIXME: This function does not currently properly deinitialize GDK resources on failure.
10442
10543 hr = XGameRuntimeInitialize ();
@@ -130,7 +68,9 @@ int SDL_RunApp(int caller_argc, char* caller_argv[], SDL_main_func mainFunction,
13068 return -1 ;
13169 }
13270
133- result = mainFunction (caller_argc, caller_argv); // No need for SDL_CallMain(); we already know that we have a valid argv
71+ // The common Windows SDL_CallMain() implementation is responsible for handling the argv
72+ // and substituting it with a new argv parsed from the command-line string, if needed.
73+ result = SDL_CallMain (argc, argv, mainFunction);
13474
13575 GDK_UnregisterChangeNotifications ();
13676
@@ -156,10 +96,5 @@ int SDL_RunApp(int caller_argc, char* caller_argv[], SDL_main_func mainFunction,
15696 result = -1 ;
15797 }
15898
159- cleanup:
160-
161- HeapFree (GetProcessHeap (), 0 , argv);
162- LocalFree (argvw);
163-
16499 return result;
165100}
0 commit comments