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