Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
8557 | maxcodehac | 1 | #ifdef _WIN32 |
2 | |||
3 | /* |
||
4 | SDL_main.c, placed in the public domain by Sam Lantinga 4/13/98 |
||
5 | |||
6 | Modified to write stdout/stderr to a message box at shutdown by Ripper 2007-12-27 |
||
7 | |||
8 | The WinMain function -- calls your program's main() function |
||
9 | */ |
||
10 | |||
11 | #include |
||
12 | #include |
||
13 | |||
14 | #define WIN32_LEAN_AND_MEAN |
||
15 | #include |
||
16 | |||
17 | #ifdef _WIN32_WCE |
||
18 | # define DIR_SEPERATOR TEXT("\\") |
||
19 | # undef _getcwd |
||
20 | # define _getcwd(str,len) wcscpy(str,TEXT("")) |
||
21 | # define setbuf(f,b) |
||
22 | # define setvbuf(w,x,y,z) |
||
23 | # define fopen _wfopen |
||
24 | # define freopen _wfreopen |
||
25 | # define remove(x) DeleteFile(x) |
||
26 | #else |
||
27 | # define DIR_SEPERATOR TEXT("/") |
||
28 | # include |
||
29 | #endif |
||
30 | |||
31 | /* Include the SDL main definition header */ |
||
32 | #include "SDL.h" |
||
33 | #include "SDL_main.h" |
||
34 | |||
35 | #ifdef main |
||
36 | # ifndef _WIN32_WCE_EMULATION |
||
37 | # undef main |
||
38 | # endif /* _WIN32_WCE_EMULATION */ |
||
39 | #endif /* main */ |
||
40 | |||
41 | /* The standard output files */ |
||
42 | #define STDOUT_FILE TEXT("stdout.txt") |
||
43 | #define STDERR_FILE TEXT("stderr.txt") |
||
44 | |||
45 | #ifndef NO_STDIO_REDIRECT |
||
46 | # ifdef _WIN32_WCE |
||
47 | static wchar_t stdoutPath[MAX_PATH]; |
||
48 | static wchar_t stderrPath[MAX_PATH]; |
||
49 | # else |
||
50 | static char stdoutPath[MAX_PATH]; |
||
51 | static char stderrPath[MAX_PATH]; |
||
52 | # endif |
||
53 | #endif |
||
54 | |||
55 | #if defined(_WIN32_WCE) && _WIN32_WCE < 300 |
||
56 | /* seems to be undefined in Win CE although in online help */ |
||
57 | #define isspace(a) (((CHAR)a == ' ') || ((CHAR)a == '\t')) |
||
58 | #endif /* _WIN32_WCE < 300 */ |
||
59 | |||
60 | /* Parse a command line buffer into arguments */ |
||
61 | static int ParseCommandLine(char *cmdline, char **argv) |
||
62 | { |
||
63 | char *bufp; |
||
64 | int argc; |
||
65 | |||
66 | argc = 0; |
||
67 | for ( bufp = cmdline; *bufp; ) { |
||
68 | /* Skip leading whitespace */ |
||
69 | while ( isspace(*bufp) ) { |
||
70 | ++bufp; |
||
71 | } |
||
72 | /* Skip over argument */ |
||
73 | if ( *bufp == '"' ) { |
||
74 | ++bufp; |
||
75 | if ( *bufp ) { |
||
76 | if ( argv ) { |
||
77 | argv[argc] = bufp; |
||
78 | } |
||
79 | ++argc; |
||
80 | } |
||
81 | /* Skip over word */ |
||
82 | while ( *bufp && (*bufp != '"') ) { |
||
83 | ++bufp; |
||
84 | } |
||
85 | } else { |
||
86 | if ( *bufp ) { |
||
87 | if ( argv ) { |
||
88 | argv[argc] = bufp; |
||
89 | } |
||
90 | ++argc; |
||
91 | } |
||
92 | /* Skip over word */ |
||
93 | while ( *bufp && ! isspace(*bufp) ) { |
||
94 | ++bufp; |
||
95 | } |
||
96 | } |
||
97 | if ( *bufp ) { |
||
98 | if ( argv ) { |
||
99 | *bufp = '\0'; |
||
100 | } |
||
101 | ++bufp; |
||
102 | } |
||
103 | } |
||
104 | if ( argv ) { |
||
105 | argv[argc] = NULL; |
||
106 | } |
||
107 | return(argc); |
||
108 | } |
||
109 | |||
110 | /* Show an error message */ |
||
111 | static void ShowError(const char *title, const char *message) |
||
112 | { |
||
113 | /* If USE_MESSAGEBOX is defined, you need to link with user32.lib */ |
||
114 | #ifdef USE_MESSAGEBOX |
||
115 | MessageBox(NULL, message, title, MB_ICONEXCLAMATION|MB_OK); |
||
116 | #else |
||
117 | fprintf(stderr, "%s: %s\n", title, message); |
||
118 | #endif |
||
119 | } |
||
120 | |||
121 | /* Pop up an out of memory message, returns to Windows */ |
||
122 | static BOOL OutOfMemory(void) |
||
123 | { |
||
124 | ShowError("Fatal Error", "Out of memory - aborting"); |
||
125 | return FALSE; |
||
126 | } |
||
127 | |||
128 | /* SDL_Quit() shouldn't be used with atexit() directly because |
||
129 | calling conventions may differ... */ |
||
130 | static void cleanup(void) |
||
131 | { |
||
132 | SDL_Quit(); |
||
133 | } |
||
134 | |||
135 | /* Remove the output files if there was no output written */ |
||
136 | static void cleanup_output(void) |
||
137 | { |
||
138 | #if 1 |
||
139 | #ifndef NO_STDIO_REDIRECT |
||
140 | FILE *file; |
||
141 | #endif |
||
142 | #endif |
||
143 | |||
144 | /* Flush the output in case anything is queued */ |
||
145 | fclose(stdout); |
||
146 | fclose(stderr); |
||
147 | |||
148 | #if 1 |
||
149 | #ifndef NO_STDIO_REDIRECT |
||
150 | /* See if the files have any output in them */ |
||
151 | if ( stdoutPath[0] ) { |
||
152 | file = fopen(stdoutPath, TEXT("r")); |
||
153 | if ( file ) { |
||
154 | char buf[16384]; |
||
155 | size_t readbytes = fread(buf, 1, 16383, file); |
||
156 | fclose(file); |
||
157 | |||
158 | if(readbytes != 0) |
||
159 | { |
||
160 | buf[readbytes] = 0; // cut after last byte (<=16383) |
||
161 | MessageBox(NULL, buf, "Wolf4SDL", MB_OK); |
||
162 | } |
||
163 | else |
||
164 | remove(stdoutPath); // remove empty file |
||
165 | } |
||
166 | } |
||
167 | if ( stderrPath[0] ) { |
||
168 | file = fopen(stderrPath, TEXT("rb")); |
||
169 | if ( file ) { |
||
170 | char buf[16384]; |
||
171 | size_t readbytes = fread(buf, 1, 16383, file); |
||
172 | fclose(file); |
||
173 | |||
174 | if(readbytes != 0) |
||
175 | { |
||
176 | buf[readbytes] = 0; // cut after last byte (<=16383) |
||
177 | MessageBox(NULL, buf, "Wolf4SDL", MB_OK); |
||
178 | } |
||
179 | else |
||
180 | remove(stderrPath); // remove empty file |
||
181 | } |
||
182 | } |
||
183 | #endif |
||
184 | #endif |
||
185 | } |
||
186 | |||
187 | //#if defined(_MSC_VER) && !defined(_WIN32_WCE) |
||
188 | ///* The VC++ compiler needs main defined */ |
||
189 | //#define console_main main |
||
190 | //#endif |
||
191 | |||
192 | /* This is where execution begins [console apps] */ |
||
193 | int console_main(int argc, char *argv[]) |
||
194 | { |
||
195 | size_t n; |
||
196 | char *bufp, *appname; |
||
197 | int status; |
||
198 | |||
199 | /* Get the class name from argv[0] */ |
||
200 | appname = argv[0]; |
||
201 | if ( (bufp=SDL_strrchr(argv[0], '\\')) != NULL ) { |
||
202 | appname = bufp+1; |
||
203 | } else |
||
204 | if ( (bufp=SDL_strrchr(argv[0], '/')) != NULL ) { |
||
205 | appname = bufp+1; |
||
206 | } |
||
207 | |||
208 | if ( (bufp=SDL_strrchr(appname, '.')) == NULL ) |
||
209 | n = SDL_strlen(appname); |
||
210 | else |
||
211 | n = (bufp-appname); |
||
212 | |||
213 | bufp = SDL_stack_alloc(char, n+1); |
||
214 | if ( bufp == NULL ) { |
||
215 | return OutOfMemory(); |
||
216 | } |
||
217 | SDL_strlcpy(bufp, appname, n+1); |
||
218 | appname = bufp; |
||
219 | |||
220 | /* Load SDL dynamic link library */ |
||
221 | if ( SDL_Init(SDL_INIT_NOPARACHUTE) < 0 ) { |
||
222 | ShowError("WinMain() error", SDL_GetError()); |
||
223 | return(FALSE); |
||
224 | } |
||
225 | atexit(cleanup_output); |
||
226 | atexit(cleanup); |
||
227 | |||
228 | /* Sam: |
||
229 | We still need to pass in the application handle so that |
||
230 | DirectInput will initialize properly when SDL_RegisterApp() |
||
231 | is called later in the video initialization. |
||
232 | */ |
||
233 | SDL_SetModuleHandle(GetModuleHandle(NULL)); |
||
234 | |||
235 | /* Run the application main() code */ |
||
236 | status = SDL_main(argc, argv); |
||
237 | |||
238 | /* Exit cleanly, calling atexit() functions */ |
||
239 | exit(status); |
||
240 | |||
241 | /* Hush little compiler, don't you cry... */ |
||
242 | return 0; |
||
243 | } |
||
244 | |||
245 | /* This is where execution begins [windowed apps] */ |
||
246 | #ifdef _WIN32_WCE |
||
247 | int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPWSTR szCmdLine, int sw) |
||
248 | #else |
||
249 | int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw) |
||
250 | #endif |
||
251 | { |
||
252 | HINSTANCE handle; |
||
253 | char **argv; |
||
254 | int argc; |
||
255 | char *cmdline; |
||
256 | #ifdef _WIN32_WCE |
||
257 | wchar_t *bufp; |
||
258 | int nLen; |
||
259 | #else |
||
260 | char *bufp; |
||
261 | size_t nLen; |
||
262 | #endif |
||
263 | #ifndef NO_STDIO_REDIRECT |
||
264 | DWORD pathlen; |
||
265 | #ifdef _WIN32_WCE |
||
266 | wchar_t path[MAX_PATH]; |
||
267 | #else |
||
268 | char path[MAX_PATH]; |
||
269 | #endif |
||
270 | FILE *newfp; |
||
271 | #endif |
||
272 | |||
273 | /* Start up DDHELP.EXE before opening any files, so DDHELP doesn't |
||
274 | keep them open. This is a hack.. hopefully it will be fixed |
||
275 | someday. DDHELP.EXE starts up the first time DDRAW.DLL is loaded. |
||
276 | */ |
||
277 | handle = LoadLibrary(TEXT("DDRAW.DLL")); |
||
278 | if ( handle != NULL ) { |
||
279 | FreeLibrary(handle); |
||
280 | } |
||
281 | |||
282 | #ifndef NO_STDIO_REDIRECT |
||
283 | pathlen = GetModuleFileName(NULL, path, SDL_arraysize(path)); |
||
284 | while ( pathlen > 0 && path[pathlen] != '\\' ) { |
||
285 | --pathlen; |
||
286 | } |
||
287 | path[pathlen] = '\0'; |
||
288 | |||
289 | #ifdef _WIN32_WCE |
||
290 | wcsncpy( stdoutPath, path, SDL_arraysize(stdoutPath) ); |
||
291 | wcsncat( stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) ); |
||
292 | #else |
||
293 | SDL_strlcpy( stdoutPath, path, SDL_arraysize(stdoutPath) ); |
||
294 | SDL_strlcat( stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) ); |
||
295 | #endif |
||
296 | |||
297 | /* Redirect standard input and standard output */ |
||
298 | newfp = freopen(stdoutPath, TEXT("w"), stdout); |
||
299 | |||
300 | #ifndef _WIN32_WCE |
||
301 | if ( newfp == NULL ) { /* This happens on NT */ |
||
302 | #if !defined(stdout) |
||
303 | stdout = fopen(stdoutPath, TEXT("w")); |
||
304 | #else |
||
305 | newfp = fopen(stdoutPath, TEXT("w")); |
||
306 | if ( newfp ) { |
||
307 | *stdout = *newfp; |
||
308 | } |
||
309 | #endif |
||
310 | } |
||
311 | #endif /* _WIN32_WCE */ |
||
312 | |||
313 | #ifdef _WIN32_WCE |
||
314 | wcsncpy( stderrPath, path, SDL_arraysize(stdoutPath) ); |
||
315 | wcsncat( stderrPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) ); |
||
316 | #else |
||
317 | SDL_strlcpy( stderrPath, path, SDL_arraysize(stderrPath) ); |
||
318 | SDL_strlcat( stderrPath, DIR_SEPERATOR STDERR_FILE, SDL_arraysize(stderrPath) ); |
||
319 | #endif |
||
320 | |||
321 | newfp = freopen(stderrPath, TEXT("w"), stderr); |
||
322 | #ifndef _WIN32_WCE |
||
323 | if ( newfp == NULL ) { /* This happens on NT */ |
||
324 | #if !defined(stderr) |
||
325 | stderr = fopen(stderrPath, TEXT("w")); |
||
326 | #else |
||
327 | newfp = fopen(stderrPath, TEXT("w")); |
||
328 | if ( newfp ) { |
||
329 | *stderr = *newfp; |
||
330 | } |
||
331 | #endif |
||
332 | } |
||
333 | #endif /* _WIN32_WCE */ |
||
334 | |||
335 | setvbuf(stdout, NULL, _IOLBF, BUFSIZ); /* Line buffered */ |
||
336 | setbuf(stderr, NULL); /* No buffering */ |
||
337 | #endif /* !NO_STDIO_REDIRECT */ |
||
338 | |||
339 | #ifdef _WIN32_WCE |
||
340 | nLen = wcslen(szCmdLine)+128+1; |
||
341 | bufp = SDL_stack_alloc(wchar_t, nLen*2); |
||
342 | wcscpy (bufp, TEXT("\"")); |
||
343 | GetModuleFileName(NULL, bufp+1, 128-3); |
||
344 | wcscpy (bufp+wcslen(bufp), TEXT("\" ")); |
||
345 | wcsncpy(bufp+wcslen(bufp), szCmdLine,nLen-wcslen(bufp)); |
||
346 | nLen = wcslen(bufp)+1; |
||
347 | cmdline = SDL_stack_alloc(char, nLen); |
||
348 | if ( cmdline == NULL ) { |
||
349 | return OutOfMemory(); |
||
350 | } |
||
351 | WideCharToMultiByte(CP_ACP, 0, bufp, -1, cmdline, nLen, NULL, NULL); |
||
352 | #else |
||
353 | /* Grab the command line */ |
||
354 | bufp = GetCommandLine(); |
||
355 | nLen = SDL_strlen(bufp)+1; |
||
356 | cmdline = SDL_stack_alloc(char, nLen); |
||
357 | if ( cmdline == NULL ) { |
||
358 | return OutOfMemory(); |
||
359 | } |
||
360 | SDL_strlcpy(cmdline, bufp, nLen); |
||
361 | #endif |
||
362 | |||
363 | /* Parse it into argv and argc */ |
||
364 | argc = ParseCommandLine(cmdline, NULL); |
||
365 | argv = SDL_stack_alloc(char*, argc+1); |
||
366 | if ( argv == NULL ) { |
||
367 | return OutOfMemory(); |
||
368 | } |
||
369 | ParseCommandLine(cmdline, argv); |
||
370 | |||
371 | /* Run the main program (after a little SDL initialization) */ |
||
372 | console_main(argc, argv); |
||
373 | |||
374 | /* Hush little compiler, don't you cry... */ |
||
375 | return 0; |
||
376 | } |
||
377 | |||
378 | #endif // _WIN32>=16383) |