2 SDL_main.c, placed in the public domain by Sam Lantinga 4/13/98
4 The WinMain function -- calls your program's main() function
10 #define WIN32_LEAN_AND_MEAN
14 # define DIR_SEPERATOR TEXT("\\")
16 # define _getcwd(str,len) wcscpy(str,TEXT(""))
18 # define setvbuf(w,x,y,z)
19 # define fopen _wfopen
20 # define freopen _wfreopen
21 # define remove(x) DeleteFile(x)
23 # define DIR_SEPERATOR TEXT("/")
27 /* Include the SDL main definition header */
32 # ifndef _WIN32_WCE_EMULATION
34 # endif /* _WIN32_WCE_EMULATION */
37 /* The standard output files */
38 #define STDOUT_FILE TEXT("stdout.txt")
39 #define STDERR_FILE TEXT("stderr.txt")
41 /* Set a variable to tell if the stdio redirect has been enabled. */
42 static int stdioRedirectEnabled = 0;
45 static wchar_t stdoutPath[MAX_PATH];
46 static wchar_t stderrPath[MAX_PATH];
48 static char stdoutPath[MAX_PATH];
49 static char stderrPath[MAX_PATH];
52 #if defined(_WIN32_WCE) && _WIN32_WCE < 300
53 /* seems to be undefined in Win CE although in online help */
54 #define isspace(a) (((CHAR)a == ' ') || ((CHAR)a == '\t'))
55 #endif /* _WIN32_WCE < 300 */
57 static void UnEscapeQuotes( char *arg )
62 if( *arg == '"' && *last == '\\' ) {
78 /* Parse a command line buffer into arguments */
79 static int ParseCommandLine(char *cmdline, char **argv)
86 for ( bufp = cmdline; *bufp; ) {
87 /* Skip leading whitespace */
88 while ( isspace(*bufp) ) {
91 /* Skip over argument */
101 while ( *bufp && ( *bufp != '"' || *lastp == '\\' ) ) {
113 while ( *bufp && ! isspace(*bufp) ) {
124 /* Strip out \ from \" sequences */
125 if( argv && last_argc != argc ) {
126 UnEscapeQuotes( argv[last_argc] );
136 /* Show an error message */
137 static void ShowError(const char *title, const char *message)
139 /* If USE_MESSAGEBOX is defined, you need to link with user32.lib */
140 #ifdef USE_MESSAGEBOX
141 MessageBox(NULL, message, title, MB_ICONEXCLAMATION|MB_OK);
143 fprintf(stderr, "%s: %s\n", title, message);
147 /* Pop up an out of memory message, returns to Windows */
148 static BOOL OutOfMemory(void)
150 ShowError("Fatal Error", "Out of memory - aborting");
154 /* SDL_Quit() shouldn't be used with atexit() directly because
155 calling conventions may differ... */
156 static void cleanup(void)
161 /* Remove the output files if there was no output written */
162 static void cleanup_output(void) {
166 /* Flush the output in case anything is queued */
170 /* Without redirection we're done */
171 if (!stdioRedirectEnabled) {
175 /* See if the files have any output in them */
176 if ( stdoutPath[0] ) {
177 file = fopen(stdoutPath, TEXT("rb"));
179 empty = (fgetc(file) == EOF) ? 1 : 0;
186 if ( stderrPath[0] ) {
187 file = fopen(stderrPath, TEXT("rb"));
189 empty = (fgetc(file) == EOF) ? 1 : 0;
198 /* Redirect the output (stdout and stderr) to a file */
199 static void redirect_output(void)
203 wchar_t path[MAX_PATH];
209 pathlen = GetModuleFileName(NULL, path, SDL_arraysize(path));
210 while ( pathlen > 0 && path[pathlen] != '\\' ) {
213 path[pathlen] = '\0';
216 wcsncpy( stdoutPath, path, SDL_arraysize(stdoutPath) );
217 wcsncat( stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) );
219 SDL_strlcpy( stdoutPath, path, SDL_arraysize(stdoutPath) );
220 SDL_strlcat( stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) );
223 /* Redirect standard input and standard output */
224 newfp = freopen(stdoutPath, TEXT("w"), stdout);
227 if ( newfp == NULL ) { /* This happens on NT */
229 stdout = fopen(stdoutPath, TEXT("w"));
231 newfp = fopen(stdoutPath, TEXT("w"));
237 #endif /* _WIN32_WCE */
240 wcsncpy( stderrPath, path, SDL_arraysize(stdoutPath) );
241 wcsncat( stderrPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) );
243 SDL_strlcpy( stderrPath, path, SDL_arraysize(stderrPath) );
244 SDL_strlcat( stderrPath, DIR_SEPERATOR STDERR_FILE, SDL_arraysize(stderrPath) );
247 newfp = freopen(stderrPath, TEXT("w"), stderr);
249 if ( newfp == NULL ) { /* This happens on NT */
251 stderr = fopen(stderrPath, TEXT("w"));
253 newfp = fopen(stderrPath, TEXT("w"));
259 #endif /* _WIN32_WCE */
261 setvbuf(stdout, NULL, _IOLBF, BUFSIZ); /* Line buffered */
262 setbuf(stderr, NULL); /* No buffering */
263 stdioRedirectEnabled = 1;
266 #if defined(_MSC_VER) && !defined(_WIN32_WCE)
267 /* The VC++ compiler needs main defined */
268 #define console_main main
271 /* This is where execution begins [console apps] */
272 int console_main(int argc, char *argv[])
275 char *bufp, *appname;
278 /* Get the class name from argv[0] */
280 if ( (bufp=SDL_strrchr(argv[0], '\\')) != NULL ) {
283 if ( (bufp=SDL_strrchr(argv[0], '/')) != NULL ) {
287 if ( (bufp=SDL_strrchr(appname, '.')) == NULL )
288 n = SDL_strlen(appname);
292 bufp = SDL_stack_alloc(char, n+1);
293 if ( bufp == NULL ) {
294 return OutOfMemory();
296 SDL_strlcpy(bufp, appname, n+1);
299 /* Load SDL dynamic link library */
300 if ( SDL_Init(SDL_INIT_NOPARACHUTE) < 0 ) {
301 ShowError("WinMain() error", SDL_GetError());
304 atexit(cleanup_output);
308 We still need to pass in the application handle so that
309 DirectInput will initialize properly when SDL_RegisterApp()
310 is called later in the video initialization.
312 SDL_SetModuleHandle(GetModuleHandle(NULL));
314 /* Run the application main() code */
315 status = SDL_main(argc, argv);
317 /* Exit cleanly, calling atexit() functions */
320 /* Hush little compiler, don't you cry... */
324 /* This is where execution begins [windowed apps] */
326 int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPWSTR szCmdLine, int sw)
328 int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
344 /* Start up DDHELP.EXE before opening any files, so DDHELP doesn't
345 keep them open. This is a hack.. hopefully it will be fixed
346 someday. DDHELP.EXE starts up the first time DDRAW.DLL is loaded.
348 handle = LoadLibrary(TEXT("DDRAW.DLL"));
349 if ( handle != NULL ) {
353 /* Check for stdio redirect settings and do the redirection */
354 if ((env_str = SDL_getenv("SDL_STDIO_REDIRECT"))) {
355 if (SDL_atoi(env_str)) {
359 #ifndef NO_STDIO_REDIRECT
366 nLen = wcslen(szCmdLine)+128+1;
367 bufp = SDL_stack_alloc(wchar_t, nLen*2);
368 wcscpy (bufp, TEXT("\""));
369 GetModuleFileName(NULL, bufp+1, 128-3);
370 wcscpy (bufp+wcslen(bufp), TEXT("\" "));
371 wcsncpy(bufp+wcslen(bufp), szCmdLine,nLen-wcslen(bufp));
372 nLen = wcslen(bufp)+1;
373 cmdline = SDL_stack_alloc(char, nLen);
374 if ( cmdline == NULL ) {
375 return OutOfMemory();
377 WideCharToMultiByte(CP_ACP, 0, bufp, -1, cmdline, nLen, NULL, NULL);
379 /* Grab the command line */
380 bufp = GetCommandLine();
381 nLen = SDL_strlen(bufp)+1;
382 cmdline = SDL_stack_alloc(char, nLen);
383 if ( cmdline == NULL ) {
384 return OutOfMemory();
386 SDL_strlcpy(cmdline, bufp, nLen);
389 /* Parse it into argv and argc */
390 argc = ParseCommandLine(cmdline, NULL);
391 argv = SDL_stack_alloc(char*, argc+1);
392 if ( argv == NULL ) {
393 return OutOfMemory();
395 ParseCommandLine(cmdline, argv);
397 /* Run the main program (after a little SDL initialization) */
398 console_main(argc, argv);
400 /* Hush little compiler, don't you cry... */