Atlas - SDL_windows_main.c

Home / ext / SDL2 / src / main / windows Lines: 1 | Size: 4582 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
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 13/* Include the SDL main definition header */ 14#include "SDL.h" 15#include "SDL_main.h" 16 17#ifdef main 18# undef main 19#endif /* main */ 20 21static void 22UnEscapeQuotes(char *arg) 23{ 24 char *last = NULL; 25 26 while (*arg) { 27 if (*arg == '"' && (last != NULL && *last == '\\')) { 28 char *c_curr = arg; 29 char *c_last = last; 30 31 while (*c_curr) { 32 *c_last = *c_curr; 33 c_last = c_curr; 34 c_curr++; 35 } 36 *c_last = '\0'; 37 } 38 last = arg; 39 arg++; 40 } 41} 42 43/* Parse a command line buffer into arguments */ 44static int 45ParseCommandLine(char *cmdline, char **argv) 46{ 47 char *bufp; 48 char *lastp = NULL; 49 int argc, last_argc; 50 51 argc = last_argc = 0; 52 for (bufp = cmdline; *bufp;) { 53 /* Skip leading whitespace */ 54 while (*bufp == ' ' || *bufp == '\t') { 55 ++bufp; 56 } 57 /* Skip over argument */ 58 if (*bufp == '"') { 59 ++bufp; 60 if (*bufp) { 61 if (argv) { 62 argv[argc] = bufp; 63 } 64 ++argc; 65 } 66 /* Skip over word */ 67 lastp = bufp; 68 while (*bufp && (*bufp != '"' || *lastp == '\\')) { 69 lastp = bufp; 70 ++bufp; 71 } 72 } else { 73 if (*bufp) { 74 if (argv) { 75 argv[argc] = bufp; 76 } 77 ++argc; 78 } 79 /* Skip over word */ 80 while (*bufp && (*bufp != ' ' && *bufp != '\t')) { 81 ++bufp; 82 } 83 } 84 if (*bufp) { 85 if (argv) { 86 *bufp = '\0'; 87 } 88 ++bufp; 89 } 90 91 /* Strip out \ from \" sequences */ 92 if (argv && last_argc != argc) { 93 UnEscapeQuotes(argv[last_argc]); 94 } 95 last_argc = argc; 96 } 97 if (argv) { 98 argv[argc] = NULL; 99 } 100 return (argc); 101} 102 103/* Pop up an out of memory message, returns to Windows */ 104static BOOL 105OutOfMemory(void) 106{ 107 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal Error", "Out of memory - aborting", NULL); 108 return FALSE; 109} 110 111#if defined(_MSC_VER) 112/* The VC++ compiler needs main/wmain defined */ 113# define console_ansi_main main 114# if UNICODE 115# define console_wmain wmain 116# endif 117#endif 118 119/* WinMain, main, and wmain eventually call into here. */ 120static int 121main_utf8(int argc, char *argv[]) 122{ 123 SDL_SetMainReady(); 124 125 /* Run the application main() code */ 126 return SDL_main(argc, argv); 127} 128 129/* Gets the arguments with GetCommandLine, converts them to argc and argv 130 and calls main_utf8 */ 131static int 132main_getcmdline() 133{ 134 char **argv; 135 int argc; 136 char *cmdline; 137 int retval = 0; 138 139 /* Grab the command line */ 140 TCHAR *text = GetCommandLine(); 141#if UNICODE 142 cmdline = WIN_StringToUTF8(text); 143#else 144 /* !!! FIXME: are these in the system codepage? We need to convert to UTF-8. */ 145 cmdline = SDL_strdup(text); 146#endif 147 if (cmdline == NULL) { 148 return OutOfMemory(); 149 } 150 151 /* Parse it into argv and argc */ 152 argc = ParseCommandLine(cmdline, NULL); 153 argv = SDL_stack_alloc(char *, argc + 1); 154 if (argv == NULL) { 155 return OutOfMemory(); 156 } 157 ParseCommandLine(cmdline, argv); 158 159 retval = main_utf8(argc, argv); 160 161 SDL_stack_free(argv); 162 SDL_free(cmdline); 163 164 return retval; 165} 166 167/* This is where execution begins [console apps, ansi] */ 168int 169console_ansi_main(int argc, char *argv[]) 170{ 171 return main_getcmdline(); 172} 173 174 175#if UNICODE 176/* This is where execution begins [console apps, unicode] */ 177int 178console_wmain(int argc, wchar_t *wargv[], wchar_t *wenvp) 179{ 180 int retval = 0; 181 char **argv = SDL_stack_alloc(char*, argc + 1); 182 int i; 183 184 for (i = 0; i < argc; ++i) { 185 argv[i] = WIN_StringToUTF8(wargv[i]); 186 } 187 argv[argc] = NULL; 188 189 retval = main_utf8(argc, argv); 190 191 /* !!! FIXME: we are leaking all the elements of argv we allocated. */ 192 SDL_stack_free(argv); 193 194 return retval; 195} 196#endif 197 198/* This is where execution begins [windowed apps] */ 199int WINAPI 200WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw) 201{ 202 return main_getcmdline(); 203} 204 205#endif /* __WIN32__ */ 206 207/* vi: set ts=4 sw=4 expandtab: */ 208
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.