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