Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
204 lines (179 loc) · 4.67 KB

SDL_win32_main.c

File metadata and controls

204 lines (179 loc) · 4.67 KB
 
Apr 26, 2001
Apr 26, 2001
1
2
3
/*
SDL_main.c, placed in the public domain by Sam Lantinga 4/13/98
Jan 21, 2003
Jan 21, 2003
4
The WinMain function -- calls your program's main() function
Apr 26, 2001
Apr 26, 2001
5
6
7
8
9
*/
#include <stdio.h>
#include <stdlib.h>
Feb 25, 2006
Feb 25, 2006
10
11
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
Feb 7, 2006
Feb 7, 2006
12
Apr 26, 2001
Apr 26, 2001
13
14
15
16
/* Include the SDL main definition header */
#include "SDL.h"
#include "SDL_main.h"
Jan 21, 2003
Jan 21, 2003
17
18
19
20
21
#ifdef main
# ifndef _WIN32_WCE_EMULATION
# undef main
# endif /* _WIN32_WCE_EMULATION */
#endif /* main */
May 23, 2001
May 23, 2001
22
Jul 18, 2001
Jul 18, 2001
23
#if defined(_WIN32_WCE) && _WIN32_WCE < 300
May 23, 2001
May 23, 2001
24
25
/* seems to be undefined in Win CE although in online help */
#define isspace(a) (((CHAR)a == ' ') || ((CHAR)a == '\t'))
Jul 18, 2001
Jul 18, 2001
26
#endif /* _WIN32_WCE < 300 */
Apr 26, 2001
Apr 26, 2001
27
Dec 29, 2007
Dec 29, 2007
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++;
}
}
Apr 26, 2001
Apr 26, 2001
50
/* Parse a command line buffer into arguments */
Jul 10, 2006
Jul 10, 2006
51
52
static int
ParseCommandLine(char *cmdline, char **argv)
Apr 26, 2001
Apr 26, 2001
53
{
Jul 10, 2006
Jul 10, 2006
54
char *bufp;
Dec 29, 2007
Dec 29, 2007
55
56
char *lastp = NULL;
int argc, last_argc;
Jul 10, 2006
Jul 10, 2006
57
Dec 29, 2007
Dec 29, 2007
58
argc = last_argc = 0;
Jul 10, 2006
Jul 10, 2006
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 */
Dec 29, 2007
Dec 29, 2007
74
75
while (*bufp && (*bufp != '"' || *lastp == '\\')) {
lastp = bufp;
Jul 10, 2006
Jul 10, 2006
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;
}
Dec 29, 2007
Dec 29, 2007
96
97
98
99
100
101
/* Strip out \ from \" sequences */
if (argv && last_argc != argc) {
UnEscapeQuotes(argv[last_argc]);
}
last_argc = argc;
Jul 10, 2006
Jul 10, 2006
102
103
104
105
106
}
if (argv) {
argv[argc] = NULL;
}
return (argc);
Apr 26, 2001
Apr 26, 2001
107
108
109
}
/* Show an error message */
Jul 10, 2006
Jul 10, 2006
110
111
static void
ShowError(const char *title, const char *message)
Apr 26, 2001
Apr 26, 2001
112
113
114
{
/* If USE_MESSAGEBOX is defined, you need to link with user32.lib */
#ifdef USE_MESSAGEBOX
Jul 10, 2006
Jul 10, 2006
115
MessageBox(NULL, message, title, MB_ICONEXCLAMATION | MB_OK);
Apr 26, 2001
Apr 26, 2001
116
#else
Jul 10, 2006
Jul 10, 2006
117
fprintf(stderr, "%s: %s\n", title, message);
Apr 26, 2001
Apr 26, 2001
118
119
120
121
#endif
}
/* Pop up an out of memory message, returns to Windows */
Jul 10, 2006
Jul 10, 2006
122
123
static BOOL
OutOfMemory(void)
Apr 26, 2001
Apr 26, 2001
124
{
Jul 10, 2006
Jul 10, 2006
125
126
ShowError("Fatal Error", "Out of memory - aborting");
return FALSE;
Apr 26, 2001
Apr 26, 2001
127
128
}
May 23, 2001
May 23, 2001
129
130
#if defined(_MSC_VER) && !defined(_WIN32_WCE)
/* The VC++ compiler needs main defined */
Apr 26, 2001
Apr 26, 2001
131
132
133
134
#define console_main main
#endif
/* This is where execution begins [console apps] */
Jul 10, 2006
Jul 10, 2006
135
136
int
console_main(int argc, char *argv[])
Apr 26, 2001
Apr 26, 2001
137
{
Jul 10, 2006
Jul 10, 2006
138
int status;
Apr 26, 2001
Apr 26, 2001
139
Jul 10, 2006
Jul 10, 2006
140
141
/* Run the application main() code */
status = SDL_main(argc, argv);
Apr 26, 2001
Apr 26, 2001
142
Jul 10, 2006
Jul 10, 2006
143
144
/* Exit cleanly, calling atexit() functions */
exit(status);
Apr 26, 2001
Apr 26, 2001
145
Jul 10, 2006
Jul 10, 2006
146
147
/* Hush little compiler, don't you cry... */
return 0;
Apr 26, 2001
Apr 26, 2001
148
149
150
}
/* This is where execution begins [windowed apps] */
Jul 10, 2006
Jul 10, 2006
151
152
int WINAPI
WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPTSTR szCmdLine, int sw)
Apr 26, 2001
Apr 26, 2001
153
{
Jul 10, 2006
Jul 10, 2006
154
155
156
char **argv;
int argc;
char *cmdline;
Mar 4, 2006
Mar 4, 2006
157
#ifdef _WIN32_WCE
Jul 10, 2006
Jul 10, 2006
158
159
wchar_t *bufp;
int nLen;
Mar 4, 2006
Mar 4, 2006
160
#else
Jul 10, 2006
Jul 10, 2006
161
162
char *bufp;
size_t nLen;
Mar 4, 2006
Mar 4, 2006
163
#endif
Apr 26, 2001
Apr 26, 2001
164
May 23, 2001
May 23, 2001
165
#ifdef _WIN32_WCE
Jul 10, 2006
Jul 10, 2006
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);
May 23, 2001
May 23, 2001
178
#else
Jul 10, 2006
Jul 10, 2006
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);
May 23, 2001
May 23, 2001
187
#endif
Apr 26, 2001
Apr 26, 2001
188
Jul 10, 2006
Jul 10, 2006
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);
Apr 26, 2001
Apr 26, 2001
196
Jul 10, 2006
Jul 10, 2006
197
198
/* Run the main program */
console_main(argc, argv);
Feb 19, 2006
Feb 19, 2006
199
Jul 10, 2006
Jul 10, 2006
200
201
/* Hush little compiler, don't you cry... */
return 0;
Apr 26, 2001
Apr 26, 2001
202
}
Jul 10, 2006
Jul 10, 2006
203
204
/* vi: set ts=4 sw=4 expandtab: */