This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_win32_main.c
204 lines (179 loc) · 4.67 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
75
while (*bufp && (*bufp != '"' || *lastp == '\\')) {
lastp = bufp;
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
++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;
}
96
97
98
99
100
101
/* Strip out \ from \" sequences */
if (argv && last_argc != argc) {
UnEscapeQuotes(argv[last_argc]);
}
last_argc = argc;
102
103
104
105
106
}
if (argv) {
argv[argc] = NULL;
}
return (argc);
107
108
109
}
/* Show an error message */
110
111
static void
ShowError(const char *title, const char *message)
112
113
114
{
/* If USE_MESSAGEBOX is defined, you need to link with user32.lib */
#ifdef USE_MESSAGEBOX
115
MessageBox(NULL, message, title, MB_ICONEXCLAMATION | MB_OK);
116
#else
117
fprintf(stderr, "%s: %s\n", title, message);
118
119
120
121
#endif
}
/* Pop up an out of memory message, returns to Windows */
122
123
static BOOL
OutOfMemory(void)
124
{
125
126
ShowError("Fatal Error", "Out of memory - aborting");
return FALSE;
127
128
}
129
130
#if defined(_MSC_VER) && !defined(_WIN32_WCE)
/* The VC++ compiler needs main defined */
131
132
133
134
#define console_main main
#endif
/* This is where execution begins [console apps] */
135
136
int
console_main(int argc, char *argv[])
137
{
138
int status;
139
140
141
/* Run the application main() code */
status = SDL_main(argc, argv);
142
143
144
/* Exit cleanly, calling atexit() functions */
exit(status);
145
146
147
/* Hush little compiler, don't you cry... */
return 0;
148
149
150
}
/* This is where execution begins [windowed apps] */
151
152
int WINAPI
WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPTSTR szCmdLine, int sw)
153
{
154
155
156
char **argv;
int argc;
char *cmdline;
157
#ifdef _WIN32_WCE
158
159
wchar_t *bufp;
int nLen;
160
#else
161
162
char *bufp;
size_t nLen;
163
#endif
164
165
#ifdef _WIN32_WCE
166
167
168
169
170
171
172
173
174
175
176
177
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);
178
#else
179
180
181
182
183
184
185
186
/* 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);
187
#endif
188
189
190
191
192
193
194
195
/* 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);
196
197
198
/* Run the main program */
console_main(argc, argv);
199
200
201
/* Hush little compiler, don't you cry... */
return 0;
202
}
203
204
/* vi: set ts=4 sw=4 expandtab: */