Mercurial > sdl-ios-xcode
annotate src/main/win32/SDL_main.c @ 112:9ef74357a5fb
Incorporated slightly modified version of Rainer's WinCE patch
author | Sam Lantinga <slouken@lokigames.com> |
---|---|
date | Wed, 18 Jul 2001 20:04:23 +0000 |
parents | 13ee9f4834ea |
children | 29a638dc26db |
rev | line source |
---|---|
0 | 1 /* |
2 SDL_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 | |
7 #include <stdio.h> | |
8 #include <string.h> | |
9 #include <ctype.h> | |
10 #include <stdlib.h> | |
11 | |
12 #include <windows.h> | |
13 #include <malloc.h> /* For _alloca() */ | |
14 | |
15 /* Include the SDL main definition header */ | |
16 #include "SDL.h" | |
17 #include "SDL_main.h" | |
18 #ifdef main | |
112
9ef74357a5fb
Incorporated slightly modified version of Rainer's WinCE patch
Sam Lantinga <slouken@lokigames.com>
parents:
36
diff
changeset
|
19 #ifndef _WIN32_WCE_EMULATION |
0 | 20 #undef main |
21 #endif | |
112
9ef74357a5fb
Incorporated slightly modified version of Rainer's WinCE patch
Sam Lantinga <slouken@lokigames.com>
parents:
36
diff
changeset
|
22 #endif |
0 | 23 |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
24 /* Do we really not want stdio redirection with Windows CE? */ |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
25 #ifdef _WIN32_WCE |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
26 #define NO_STDIO_REDIRECT |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
27 #endif |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
28 |
0 | 29 /* The standard output files */ |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
30 #define STDOUT_FILE TEXT("stdout.txt") |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
31 #define STDERR_FILE TEXT("stderr.txt") |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
32 |
112
9ef74357a5fb
Incorporated slightly modified version of Rainer's WinCE patch
Sam Lantinga <slouken@lokigames.com>
parents:
36
diff
changeset
|
33 #if defined(_WIN32_WCE) && _WIN32_WCE < 300 |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
34 /* seems to be undefined in Win CE although in online help */ |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
35 #define isspace(a) (((CHAR)a == ' ') || ((CHAR)a == '\t')) |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
36 |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
37 /* seems to be undefined in Win CE although in online help */ |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
38 char *strrchr(char *str, int c) |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
39 { |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
40 char *p; |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
41 |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
42 /* Skip to the end of the string */ |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
43 p=str; |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
44 while (*p) |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
45 p++; |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
46 |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
47 /* Look for the given character */ |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
48 while ( (p >= str) && (*p != (CHAR)c) ) |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
49 p--; |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
50 |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
51 /* Return NULL if character not found */ |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
52 if ( p < str ) { |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
53 p = NULL; |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
54 } |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
55 return p; |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
56 } |
112
9ef74357a5fb
Incorporated slightly modified version of Rainer's WinCE patch
Sam Lantinga <slouken@lokigames.com>
parents:
36
diff
changeset
|
57 #endif /* _WIN32_WCE < 300 */ |
0 | 58 |
59 /* Parse a command line buffer into arguments */ | |
60 static int ParseCommandLine(char *cmdline, char **argv) | |
61 { | |
62 char *bufp; | |
63 int argc; | |
64 | |
65 argc = 0; | |
66 for ( bufp = cmdline; *bufp; ) { | |
67 /* Skip leading whitespace */ | |
68 while ( isspace(*bufp) ) { | |
69 ++bufp; | |
70 } | |
71 /* Skip over argument */ | |
72 if ( *bufp == '"' ) { | |
73 ++bufp; | |
74 if ( *bufp ) { | |
75 if ( argv ) { | |
76 argv[argc] = bufp; | |
77 } | |
78 ++argc; | |
79 } | |
80 /* Skip over word */ | |
81 while ( *bufp && (*bufp != '"') ) { | |
82 ++bufp; | |
83 } | |
84 } else { | |
85 if ( *bufp ) { | |
86 if ( argv ) { | |
87 argv[argc] = bufp; | |
88 } | |
89 ++argc; | |
90 } | |
91 /* Skip over word */ | |
92 while ( *bufp && ! isspace(*bufp) ) { | |
93 ++bufp; | |
94 } | |
95 } | |
96 if ( *bufp ) { | |
97 if ( argv ) { | |
98 *bufp = '\0'; | |
99 } | |
100 ++bufp; | |
101 } | |
102 } | |
103 if ( argv ) { | |
104 argv[argc] = NULL; | |
105 } | |
106 return(argc); | |
107 } | |
108 | |
109 /* Show an error message */ | |
110 static void ShowError(const char *title, const char *message) | |
111 { | |
112 /* If USE_MESSAGEBOX is defined, you need to link with user32.lib */ | |
113 #ifdef USE_MESSAGEBOX | |
114 MessageBox(NULL, message, title, MB_ICONEXCLAMATION|MB_OK); | |
115 #else | |
116 fprintf(stderr, "%s: %s\n", title, message); | |
117 #endif | |
118 } | |
119 | |
120 /* Pop up an out of memory message, returns to Windows */ | |
121 static BOOL OutOfMemory(void) | |
122 { | |
123 ShowError("Fatal Error", "Out of memory - aborting"); | |
124 return FALSE; | |
125 } | |
126 | |
127 /* Remove the output files if there was no output written */ | |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
128 static void __cdecl cleanup_output(void) |
0 | 129 { |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
130 #ifndef NO_STDIO_REDIRECT |
0 | 131 FILE *file; |
132 int empty; | |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
133 #endif |
0 | 134 |
135 /* Flush the output in case anything is queued */ | |
136 fclose(stdout); | |
137 fclose(stderr); | |
138 | |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
139 #ifndef NO_STDIO_REDIRECT |
0 | 140 /* See if the files have any output in them */ |
141 file = fopen(STDOUT_FILE, "rb"); | |
142 if ( file ) { | |
143 empty = (fgetc(file) == EOF) ? 1 : 0; | |
144 fclose(file); | |
145 if ( empty ) { | |
146 remove(STDOUT_FILE); | |
147 } | |
148 } | |
149 file = fopen(STDERR_FILE, "rb"); | |
150 if ( file ) { | |
151 empty = (fgetc(file) == EOF) ? 1 : 0; | |
152 fclose(file); | |
153 if ( empty ) { | |
154 remove(STDERR_FILE); | |
155 } | |
156 } | |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
157 #endif |
0 | 158 } |
159 | |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
160 #if defined(_MSC_VER) && !defined(_WIN32_WCE) |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
161 /* The VC++ compiler needs main defined */ |
0 | 162 #define console_main main |
163 #endif | |
164 | |
165 /* This is where execution begins [console apps] */ | |
166 int console_main(int argc, char *argv[]) | |
167 { | |
168 int n; | |
169 char *bufp, *appname; | |
170 | |
171 /* Get the class name from argv[0] */ | |
172 appname = argv[0]; | |
173 if ( (bufp=strrchr(argv[0], '\\')) != NULL ) { | |
174 appname = bufp+1; | |
175 } else | |
176 if ( (bufp=strrchr(argv[0], '/')) != NULL ) { | |
177 appname = bufp+1; | |
178 } | |
179 | |
180 if ( (bufp=strrchr(appname, '.')) == NULL ) | |
181 n = strlen(appname); | |
182 else | |
183 n = (bufp-appname); | |
184 | |
185 bufp = (char *)alloca(n+1); | |
186 if ( bufp == NULL ) { | |
187 return OutOfMemory(); | |
188 } | |
189 strncpy(bufp, appname, n); | |
190 bufp[n] = '\0'; | |
191 appname = bufp; | |
192 | |
193 /* Load SDL dynamic link library */ | |
194 if ( SDL_Init(SDL_INIT_NOPARACHUTE) < 0 ) { | |
195 ShowError("WinMain() error", SDL_GetError()); | |
196 return(FALSE); | |
197 } | |
198 atexit(cleanup_output); | |
199 atexit(SDL_Quit); | |
200 | |
15
ac67f6758d63
Don't try to register the app if video code is disabled
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
201 #ifndef DISABLE_VIDEO |
ac67f6758d63
Don't try to register the app if video code is disabled
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
202 /* Create and register our class */ |
0 | 203 if ( SDL_RegisterApp(appname, CS_BYTEALIGNCLIENT, |
204 GetModuleHandle(NULL)) < 0 ) { | |
205 ShowError("WinMain() error", SDL_GetError()); | |
206 exit(1); | |
207 } | |
15
ac67f6758d63
Don't try to register the app if video code is disabled
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
208 #endif /* !DISABLE_VIDEO */ |
ac67f6758d63
Don't try to register the app if video code is disabled
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
209 |
ac67f6758d63
Don't try to register the app if video code is disabled
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
210 /* Run the application main() code */ |
0 | 211 SDL_main(argc, argv); |
212 | |
213 /* Exit cleanly, calling atexit() functions */ | |
214 exit(0); | |
215 } | |
216 | |
217 /* This is where execution begins [windowed apps] */ | |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
218 #ifdef _WIN32_WCE |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
219 int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPWSTR szCmdLine, int sw) |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
220 #else |
0 | 221 int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw) |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
222 #endif |
0 | 223 { |
224 HINSTANCE handle; | |
225 char **argv; | |
226 int argc; | |
227 char *cmdline; | |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
228 #ifdef _WIN32_WCE |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
229 wchar_t *bufp; |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
230 int nLen; |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
231 #else |
0 | 232 char *bufp; |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
233 #endif |
0 | 234 #ifndef NO_STDIO_REDIRECT |
235 FILE *newfp; | |
236 #endif | |
237 | |
238 /* Start up DDHELP.EXE before opening any files, so DDHELP doesn't | |
239 keep them open. This is a hack.. hopefully it will be fixed | |
240 someday. DDHELP.EXE starts up the first time DDRAW.DLL is loaded. | |
241 */ | |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
242 handle = LoadLibrary(TEXT("DDRAW.DLL")); |
0 | 243 if ( handle != NULL ) { |
244 FreeLibrary(handle); | |
245 } | |
246 | |
247 #ifndef NO_STDIO_REDIRECT | |
248 /* Redirect standard input and standard output */ | |
249 newfp = freopen(STDOUT_FILE, "w", stdout); | |
250 if ( newfp == NULL ) { /* This happens on NT */ | |
251 #if !defined(stdout) | |
252 stdout = fopen(STDOUT_FILE, "w"); | |
253 #else | |
254 newfp = fopen(STDOUT_FILE, "w"); | |
255 if ( newfp ) { | |
256 *stdout = *newfp; | |
257 } | |
258 #endif | |
259 } | |
260 newfp = freopen(STDERR_FILE, "w", stderr); | |
261 if ( newfp == NULL ) { /* This happens on NT */ | |
262 #if !defined(stderr) | |
263 stderr = fopen(STDERR_FILE, "w"); | |
264 #else | |
265 newfp = fopen(STDERR_FILE, "w"); | |
266 if ( newfp ) { | |
267 *stderr = *newfp; | |
268 } | |
269 #endif | |
270 } | |
271 setvbuf(stdout, NULL, _IOLBF, BUFSIZ); /* Line buffered */ | |
272 setbuf(stderr, NULL); /* No buffering */ | |
273 #endif /* !NO_STDIO_REDIRECT */ | |
274 | |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
275 #ifdef _WIN32_WCE |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
276 nLen = wcslen(szCmdLine)+128+1; |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
277 bufp = (wchar_t *)alloca(nLen*2); |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
278 GetModuleFileName(NULL, bufp, 128); |
112
9ef74357a5fb
Incorporated slightly modified version of Rainer's WinCE patch
Sam Lantinga <slouken@lokigames.com>
parents:
36
diff
changeset
|
279 wcsncpy(bufp+wcslen(bufp), szCmdLine,nLen-wcslen(bufp)); |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
280 nLen = wcslen(bufp)+1; |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
281 cmdline = (char *)alloca(nLen); |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
282 if ( cmdline == NULL ) { |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
283 return OutOfMemory(); |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
284 } |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
285 WideCharToMultiByte(CP_ACP, 0, bufp, -1, cmdline, nLen, NULL, NULL); |
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
286 #else |
0 | 287 /* Grab the command line (use alloca() on Windows) */ |
288 bufp = GetCommandLine(); | |
289 cmdline = (char *)alloca(strlen(bufp)+1); | |
290 if ( cmdline == NULL ) { | |
291 return OutOfMemory(); | |
292 } | |
293 strcpy(cmdline, bufp); | |
36
13ee9f4834ea
Windows CE patches contributed by Rainer Loritz
Sam Lantinga <slouken@lokigames.com>
parents:
15
diff
changeset
|
294 #endif |
0 | 295 |
296 /* Parse it into argv and argc */ | |
297 argc = ParseCommandLine(cmdline, NULL); | |
298 argv = (char **)alloca((argc+1)*(sizeof *argv)); | |
299 if ( argv == NULL ) { | |
300 return OutOfMemory(); | |
301 } | |
302 ParseCommandLine(cmdline, argv); | |
303 | |
304 /* Run the main program (after a little SDL initialization) */ | |
305 return(console_main(argc, argv)); | |
306 } |