author | Sam Lantinga <slouken@lokigames.com> |
Wed, 02 May 2001 19:21:00 +0000 | |
changeset 15 | ac67f6758d63 |
parent 0 | 74212992fb08 |
child 36 | 13ee9f4834ea |
permissions | -rw-r--r-- |
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 |
||
22 |
/* The standard output files */ |
|
23 |
#define STDOUT_FILE "stdout.txt" |
|
24 |
#define STDERR_FILE "stderr.txt" |
|
25 |
||
26 |
/* Parse a command line buffer into arguments */ |
|
27 |
static int ParseCommandLine(char *cmdline, char **argv) |
|
28 |
{ |
|
29 |
char *bufp; |
|
30 |
int argc; |
|
31 |
||
32 |
argc = 0; |
|
33 |
for ( bufp = cmdline; *bufp; ) { |
|
34 |
/* Skip leading whitespace */ |
|
35 |
while ( isspace(*bufp) ) { |
|
36 |
++bufp; |
|
37 |
} |
|
38 |
/* Skip over argument */ |
|
39 |
if ( *bufp == '"' ) { |
|
40 |
++bufp; |
|
41 |
if ( *bufp ) { |
|
42 |
if ( argv ) { |
|
43 |
argv[argc] = bufp; |
|
44 |
} |
|
45 |
++argc; |
|
46 |
} |
|
47 |
/* Skip over word */ |
|
48 |
while ( *bufp && (*bufp != '"') ) { |
|
49 |
++bufp; |
|
50 |
} |
|
51 |
} else { |
|
52 |
if ( *bufp ) { |
|
53 |
if ( argv ) { |
|
54 |
argv[argc] = bufp; |
|
55 |
} |
|
56 |
++argc; |
|
57 |
} |
|
58 |
/* Skip over word */ |
|
59 |
while ( *bufp && ! isspace(*bufp) ) { |
|
60 |
++bufp; |
|
61 |
} |
|
62 |
} |
|
63 |
if ( *bufp ) { |
|
64 |
if ( argv ) { |
|
65 |
*bufp = '\0'; |
|
66 |
} |
|
67 |
++bufp; |
|
68 |
} |
|
69 |
} |
|
70 |
if ( argv ) { |
|
71 |
argv[argc] = NULL; |
|
72 |
} |
|
73 |
return(argc); |
|
74 |
} |
|
75 |
||
76 |
/* Show an error message */ |
|
77 |
static void ShowError(const char *title, const char *message) |
|
78 |
{ |
|
79 |
/* If USE_MESSAGEBOX is defined, you need to link with user32.lib */ |
|
80 |
#ifdef USE_MESSAGEBOX |
|
81 |
MessageBox(NULL, message, title, MB_ICONEXCLAMATION|MB_OK); |
|
82 |
#else |
|
83 |
fprintf(stderr, "%s: %s\n", title, message); |
|
84 |
#endif |
|
85 |
} |
|
86 |
||
87 |
/* Pop up an out of memory message, returns to Windows */ |
|
88 |
static BOOL OutOfMemory(void) |
|
89 |
{ |
|
90 |
ShowError("Fatal Error", "Out of memory - aborting"); |
|
91 |
return FALSE; |
|
92 |
} |
|
93 |
||
94 |
/* Remove the output files if there was no output written */ |
|
95 |
static void cleanup_output(void) |
|
96 |
{ |
|
97 |
FILE *file; |
|
98 |
int empty; |
|
99 |
||
100 |
/* Flush the output in case anything is queued */ |
|
101 |
fclose(stdout); |
|
102 |
fclose(stderr); |
|
103 |
||
104 |
/* See if the files have any output in them */ |
|
105 |
file = fopen(STDOUT_FILE, "rb"); |
|
106 |
if ( file ) { |
|
107 |
empty = (fgetc(file) == EOF) ? 1 : 0; |
|
108 |
fclose(file); |
|
109 |
if ( empty ) { |
|
110 |
remove(STDOUT_FILE); |
|
111 |
} |
|
112 |
} |
|
113 |
file = fopen(STDERR_FILE, "rb"); |
|
114 |
if ( file ) { |
|
115 |
empty = (fgetc(file) == EOF) ? 1 : 0; |
|
116 |
fclose(file); |
|
117 |
if ( empty ) { |
|
118 |
remove(STDERR_FILE); |
|
119 |
} |
|
120 |
} |
|
121 |
} |
|
122 |
||
123 |
#ifdef _MSC_VER /* The VC++ compiler needs main defined */ |
|
124 |
#define console_main main |
|
125 |
#endif |
|
126 |
||
127 |
/* This is where execution begins [console apps] */ |
|
128 |
int console_main(int argc, char *argv[]) |
|
129 |
{ |
|
130 |
int n; |
|
131 |
char *bufp, *appname; |
|
132 |
||
133 |
/* Get the class name from argv[0] */ |
|
134 |
appname = argv[0]; |
|
135 |
if ( (bufp=strrchr(argv[0], '\\')) != NULL ) { |
|
136 |
appname = bufp+1; |
|
137 |
} else |
|
138 |
if ( (bufp=strrchr(argv[0], '/')) != NULL ) { |
|
139 |
appname = bufp+1; |
|
140 |
} |
|
141 |
||
142 |
if ( (bufp=strrchr(appname, '.')) == NULL ) |
|
143 |
n = strlen(appname); |
|
144 |
else |
|
145 |
n = (bufp-appname); |
|
146 |
||
147 |
bufp = (char *)alloca(n+1); |
|
148 |
if ( bufp == NULL ) { |
|
149 |
return OutOfMemory(); |
|
150 |
} |
|
151 |
strncpy(bufp, appname, n); |
|
152 |
bufp[n] = '\0'; |
|
153 |
appname = bufp; |
|
154 |
||
155 |
/* Load SDL dynamic link library */ |
|
156 |
if ( SDL_Init(SDL_INIT_NOPARACHUTE) < 0 ) { |
|
157 |
ShowError("WinMain() error", SDL_GetError()); |
|
158 |
return(FALSE); |
|
159 |
} |
|
160 |
atexit(cleanup_output); |
|
161 |
atexit(SDL_Quit); |
|
162 |
||
15
ac67f6758d63
Don't try to register the app if video code is disabled
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
163 |
#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
|
164 |
/* Create and register our class */ |
0 | 165 |
if ( SDL_RegisterApp(appname, CS_BYTEALIGNCLIENT, |
166 |
GetModuleHandle(NULL)) < 0 ) { |
|
167 |
ShowError("WinMain() error", SDL_GetError()); |
|
168 |
exit(1); |
|
169 |
} |
|
15
ac67f6758d63
Don't try to register the app if video code is disabled
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
170 |
#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
|
171 |
|
ac67f6758d63
Don't try to register the app if video code is disabled
Sam Lantinga <slouken@lokigames.com>
parents:
0
diff
changeset
|
172 |
/* Run the application main() code */ |
0 | 173 |
SDL_main(argc, argv); |
174 |
||
175 |
/* Exit cleanly, calling atexit() functions */ |
|
176 |
exit(0); |
|
177 |
} |
|
178 |
||
179 |
/* This is where execution begins [windowed apps] */ |
|
180 |
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw) |
|
181 |
{ |
|
182 |
HINSTANCE handle; |
|
183 |
char **argv; |
|
184 |
int argc; |
|
185 |
char *cmdline; |
|
186 |
char *bufp; |
|
187 |
#ifndef NO_STDIO_REDIRECT |
|
188 |
FILE *newfp; |
|
189 |
#endif |
|
190 |
||
191 |
/* Start up DDHELP.EXE before opening any files, so DDHELP doesn't |
|
192 |
keep them open. This is a hack.. hopefully it will be fixed |
|
193 |
someday. DDHELP.EXE starts up the first time DDRAW.DLL is loaded. |
|
194 |
*/ |
|
195 |
handle = LoadLibrary("DDRAW.DLL"); |
|
196 |
if ( handle != NULL ) { |
|
197 |
FreeLibrary(handle); |
|
198 |
} |
|
199 |
||
200 |
#ifndef NO_STDIO_REDIRECT |
|
201 |
/* Redirect standard input and standard output */ |
|
202 |
newfp = freopen(STDOUT_FILE, "w", stdout); |
|
203 |
if ( newfp == NULL ) { /* This happens on NT */ |
|
204 |
#if !defined(stdout) |
|
205 |
stdout = fopen(STDOUT_FILE, "w"); |
|
206 |
#else |
|
207 |
newfp = fopen(STDOUT_FILE, "w"); |
|
208 |
if ( newfp ) { |
|
209 |
*stdout = *newfp; |
|
210 |
} |
|
211 |
#endif |
|
212 |
} |
|
213 |
newfp = freopen(STDERR_FILE, "w", stderr); |
|
214 |
if ( newfp == NULL ) { /* This happens on NT */ |
|
215 |
#if !defined(stderr) |
|
216 |
stderr = fopen(STDERR_FILE, "w"); |
|
217 |
#else |
|
218 |
newfp = fopen(STDERR_FILE, "w"); |
|
219 |
if ( newfp ) { |
|
220 |
*stderr = *newfp; |
|
221 |
} |
|
222 |
#endif |
|
223 |
} |
|
224 |
setvbuf(stdout, NULL, _IOLBF, BUFSIZ); /* Line buffered */ |
|
225 |
setbuf(stderr, NULL); /* No buffering */ |
|
226 |
#endif /* !NO_STDIO_REDIRECT */ |
|
227 |
||
228 |
/* Grab the command line (use alloca() on Windows) */ |
|
229 |
bufp = GetCommandLine(); |
|
230 |
cmdline = (char *)alloca(strlen(bufp)+1); |
|
231 |
if ( cmdline == NULL ) { |
|
232 |
return OutOfMemory(); |
|
233 |
} |
|
234 |
strcpy(cmdline, bufp); |
|
235 |
||
236 |
/* Parse it into argv and argc */ |
|
237 |
argc = ParseCommandLine(cmdline, NULL); |
|
238 |
argv = (char **)alloca((argc+1)*(sizeof *argv)); |
|
239 |
if ( argv == NULL ) { |
|
240 |
return OutOfMemory(); |
|
241 |
} |
|
242 |
ParseCommandLine(cmdline, argv); |
|
243 |
||
244 |
/* Run the main program (after a little SDL initialization) */ |
|
245 |
return(console_main(argc, argv)); |
|
246 |
} |