SDL_windows_main.c (2410B)
1 /* 2 SDL_windows_main.c, placed in the public domain by Sam Lantinga 4/13/98 3 4 The WinMain function -- calls your program's main() function 5 */ 6 #include "SDL_config.h" 7 8 #ifdef __WIN32__ 9 10 /* Include this so we define UNICODE properly */ 11 #include "../../core/windows/SDL_windows.h" 12 #include <shellapi.h> /* CommandLineToArgvW() */ 13 14 /* Include the SDL main definition header */ 15 #include "SDL.h" 16 #include "SDL_main.h" 17 18 #ifdef main 19 # undef main 20 #endif /* main */ 21 22 #define WIN_WStringToUTF8(S) SDL_iconv_string("UTF-8", "UTF-16LE", (char *)(S), (SDL_wcslen(S)+1)*sizeof(WCHAR)) 23 24 /* Pop up an out of memory message, returns to Windows */ 25 static BOOL 26 OutOfMemory(void) 27 { 28 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal Error", "Out of memory - aborting", NULL); 29 return FALSE; 30 } 31 32 #if defined(_MSC_VER) 33 /* The VC++ compiler needs main/wmain defined */ 34 # define console_ansi_main main 35 # if UNICODE 36 # define console_wmain wmain 37 # endif 38 #endif 39 40 /* Gets the arguments with GetCommandLine, converts them to argc and argv 41 and calls SDL_main */ 42 static int 43 main_getcmdline(void) 44 { 45 LPWSTR *argvw; 46 char **argv; 47 int i, argc, result; 48 49 argvw = CommandLineToArgvW(GetCommandLineW(), &argc); 50 if (argvw == NULL) { 51 return OutOfMemory(); 52 } 53 54 /* Parse it into argv and argc */ 55 argv = (char **)SDL_calloc(argc + 1, sizeof(*argv)); 56 if (!argv) { 57 return OutOfMemory(); 58 } 59 for (i = 0; i < argc; ++i) { 60 argv[i] = WIN_WStringToUTF8(argvw[i]); 61 if (!argv[i]) { 62 return OutOfMemory(); 63 } 64 } 65 argv[i] = NULL; 66 LocalFree(argvw); 67 68 SDL_SetMainReady(); 69 70 /* Run the application main() code */ 71 result = SDL_main(argc, argv); 72 73 /* Free argv, to avoid memory leak */ 74 for (i = 0; i < argc; ++i) { 75 SDL_free(argv[i]); 76 } 77 SDL_free(argv); 78 79 return result; 80 } 81 82 /* This is where execution begins [console apps, ansi] */ 83 int 84 console_ansi_main(int argc, char *argv[]) 85 { 86 return main_getcmdline(); 87 } 88 89 90 #if UNICODE 91 /* This is where execution begins [console apps, unicode] */ 92 int 93 console_wmain(int argc, wchar_t *wargv[], wchar_t *wenvp) 94 { 95 return main_getcmdline(); 96 } 97 #endif 98 99 /* This is where execution begins [windowed apps] */ 100 int WINAPI 101 WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw) 102 { 103 return main_getcmdline(); 104 } 105 106 #endif /* __WIN32__ */ 107 108 /* vi: set ts=4 sw=4 expandtab: */