This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_windows_main.c
205 lines (180 loc) · 4.69 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
15
16
/* Include the SDL main definition header */
#include "SDL.h"
#include "SDL_main.h"
17
18
19
20
21
#ifdef main
# ifndef _WIN32_WCE_EMULATION
# undef main
# endif /* _WIN32_WCE_EMULATION */
#endif /* main */
22
23
#if defined(_WIN32_WCE) && _WIN32_WCE < 300
24
25
/* seems to be undefined in Win CE although in online help */
#define isspace(a) (((CHAR)a == ' ') || ((CHAR)a == '\t'))
26
#endif /* _WIN32_WCE < 300 */
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
static void
UnEscapeQuotes(char *arg)
{
char *last = NULL;
while (*arg) {
if (*arg == '"' && *last == '\\') {
char *c_curr = arg;
char *c_last = last;
while (*c_curr) {
*c_last = *c_curr;
c_last = c_curr;
c_curr++;
}
*c_last = '\0';
}
last = arg;
arg++;
}
}
50
/* Parse a command line buffer into arguments */
51
52
static int
ParseCommandLine(char *cmdline, char **argv)
53
{
54
char *bufp;
55
56
char *lastp = NULL;
int argc, last_argc;
57
58
argc = last_argc = 0;
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
for (bufp = cmdline; *bufp;) {
/* Skip leading whitespace */
while (isspace(*bufp)) {
++bufp;
}
/* Skip over argument */
if (*bufp == '"') {
++bufp;
if (*bufp) {
if (argv) {
argv[argc] = bufp;
}
++argc;
}
/* Skip over word */
74
lastp = bufp;
75
76
while (*bufp && (*bufp != '"' || *lastp == '\\')) {
lastp = bufp;
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
++bufp;
}
} else {
if (*bufp) {
if (argv) {
argv[argc] = bufp;
}
++argc;
}
/* Skip over word */
while (*bufp && !isspace(*bufp)) {
++bufp;
}
}
if (*bufp) {
if (argv) {
*bufp = '\0';
}
++bufp;
}
97
98
99
100
101
102
/* Strip out \ from \" sequences */
if (argv && last_argc != argc) {
UnEscapeQuotes(argv[last_argc]);
}
last_argc = argc;
103
104
105
106
107
}
if (argv) {
argv[argc] = NULL;
}
return (argc);
108
109
110
}
/* Show an error message */
111
112
static void
ShowError(const char *title, const char *message)
113
114
115
{
/* If USE_MESSAGEBOX is defined, you need to link with user32.lib */
#ifdef USE_MESSAGEBOX
116
MessageBox(NULL, message, title, MB_ICONEXCLAMATION | MB_OK);
117
#else
118
fprintf(stderr, "%s: %s\n", title, message);
119
120
121
122
#endif
}
/* Pop up an out of memory message, returns to Windows */
123
124
static BOOL
OutOfMemory(void)
125
{
126
127
ShowError("Fatal Error", "Out of memory - aborting");
return FALSE;
128
129
}
130
131
#if defined(_MSC_VER) && !defined(_WIN32_WCE)
/* The VC++ compiler needs main defined */
132
133
134
135
#define console_main main
#endif
/* This is where execution begins [console apps] */
136
137
int
console_main(int argc, char *argv[])
138
{
139
int status;
140
141
142
/* Run the application main() code */
status = SDL_main(argc, argv);
143
144
145
/* Exit cleanly, calling atexit() functions */
exit(status);
146
147
148
/* Hush little compiler, don't you cry... */
return 0;
149
150
151
}
/* This is where execution begins [windowed apps] */
152
153
int WINAPI
WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPTSTR szCmdLine, int sw)
154
{
155
156
157
char **argv;
int argc;
char *cmdline;
158
#ifdef _WIN32_WCE
159
160
wchar_t *bufp;
int nLen;
161
#else
162
163
char *bufp;
size_t nLen;
164
#endif
165
166
#ifdef _WIN32_WCE
167
168
169
170
171
172
173
174
175
176
177
178
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);
if (cmdline == NULL) {
return OutOfMemory();
}
WideCharToMultiByte(CP_ACP, 0, bufp, -1, cmdline, nLen, NULL, NULL);
179
#else
180
181
182
183
184
185
186
187
/* Grab the command line */
bufp = GetCommandLine();
nLen = SDL_strlen(bufp) + 1;
cmdline = SDL_stack_alloc(char, nLen);
if (cmdline == NULL) {
return OutOfMemory();
}
SDL_strlcpy(cmdline, bufp, nLen);
188
#endif
189
190
191
192
193
194
195
196
/* Parse it into argv and argc */
argc = ParseCommandLine(cmdline, NULL);
argv = SDL_stack_alloc(char *, argc + 1);
if (argv == NULL) {
return OutOfMemory();
}
ParseCommandLine(cmdline, argv);
197
198
199
/* Run the main program */
console_main(argc, argv);
200
201
202
/* Hush little compiler, don't you cry... */
return 0;
203
}
204
205
/* vi: set ts=4 sw=4 expandtab: */