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

Latest commit

 

History

History
205 lines (180 loc) · 4.69 KB

SDL_windows_main.c

File metadata and controls

205 lines (180 loc) · 4.69 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 */
Jul 13, 2010
Jul 13, 2010
74
lastp = bufp;
Dec 29, 2007
Dec 29, 2007
75
76
while (*bufp && (*bufp != '"' || *lastp == '\\')) {
lastp = bufp;
Jul 10, 2006
Jul 10, 2006
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;
}
Dec 29, 2007
Dec 29, 2007
97
98
99
100
101
102
/* Strip out \ from \" sequences */
if (argv && last_argc != argc) {
UnEscapeQuotes(argv[last_argc]);
}
last_argc = argc;
Jul 10, 2006
Jul 10, 2006
103
104
105
106
107
}
if (argv) {
argv[argc] = NULL;
}
return (argc);
Apr 26, 2001
Apr 26, 2001
108
109
110
}
/* Show an error message */
Jul 10, 2006
Jul 10, 2006
111
112
static void
ShowError(const char *title, const char *message)
Apr 26, 2001
Apr 26, 2001
113
114
115
{
/* If USE_MESSAGEBOX is defined, you need to link with user32.lib */
#ifdef USE_MESSAGEBOX
Jul 10, 2006
Jul 10, 2006
116
MessageBox(NULL, message, title, MB_ICONEXCLAMATION | MB_OK);
Apr 26, 2001
Apr 26, 2001
117
#else
Jul 10, 2006
Jul 10, 2006
118
fprintf(stderr, "%s: %s\n", title, message);
Apr 26, 2001
Apr 26, 2001
119
120
121
122
#endif
}
/* Pop up an out of memory message, returns to Windows */
Jul 10, 2006
Jul 10, 2006
123
124
static BOOL
OutOfMemory(void)
Apr 26, 2001
Apr 26, 2001
125
{
Jul 10, 2006
Jul 10, 2006
126
127
ShowError("Fatal Error", "Out of memory - aborting");
return FALSE;
Apr 26, 2001
Apr 26, 2001
128
129
}
May 23, 2001
May 23, 2001
130
131
#if defined(_MSC_VER) && !defined(_WIN32_WCE)
/* The VC++ compiler needs main defined */
Apr 26, 2001
Apr 26, 2001
132
133
134
135
#define console_main main
#endif
/* This is where execution begins [console apps] */
Jul 10, 2006
Jul 10, 2006
136
137
int
console_main(int argc, char *argv[])
Apr 26, 2001
Apr 26, 2001
138
{
Jul 10, 2006
Jul 10, 2006
139
int status;
Apr 26, 2001
Apr 26, 2001
140
Jul 10, 2006
Jul 10, 2006
141
142
/* Run the application main() code */
status = SDL_main(argc, argv);
Apr 26, 2001
Apr 26, 2001
143
Jul 10, 2006
Jul 10, 2006
144
145
/* Exit cleanly, calling atexit() functions */
exit(status);
Apr 26, 2001
Apr 26, 2001
146
Jul 10, 2006
Jul 10, 2006
147
148
/* Hush little compiler, don't you cry... */
return 0;
Apr 26, 2001
Apr 26, 2001
149
150
151
}
/* This is where execution begins [windowed apps] */
Jul 10, 2006
Jul 10, 2006
152
153
int WINAPI
WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPTSTR szCmdLine, int sw)
Apr 26, 2001
Apr 26, 2001
154
{
Jul 10, 2006
Jul 10, 2006
155
156
157
char **argv;
int argc;
char *cmdline;
Mar 4, 2006
Mar 4, 2006
158
#ifdef _WIN32_WCE
Jul 10, 2006
Jul 10, 2006
159
160
wchar_t *bufp;
int nLen;
Mar 4, 2006
Mar 4, 2006
161
#else
Jul 10, 2006
Jul 10, 2006
162
163
char *bufp;
size_t nLen;
Mar 4, 2006
Mar 4, 2006
164
#endif
Apr 26, 2001
Apr 26, 2001
165
May 23, 2001
May 23, 2001
166
#ifdef _WIN32_WCE
Jul 10, 2006
Jul 10, 2006
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);
May 23, 2001
May 23, 2001
179
#else
Jul 10, 2006
Jul 10, 2006
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);
May 23, 2001
May 23, 2001
188
#endif
Apr 26, 2001
Apr 26, 2001
189
Jul 10, 2006
Jul 10, 2006
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);
Apr 26, 2001
Apr 26, 2001
197
Jul 10, 2006
Jul 10, 2006
198
199
/* Run the main program */
console_main(argc, argv);
Feb 19, 2006
Feb 19, 2006
200
Jul 10, 2006
Jul 10, 2006
201
202
/* Hush little compiler, don't you cry... */
return 0;
Apr 26, 2001
Apr 26, 2001
203
}
Jul 10, 2006
Jul 10, 2006
204
205
/* vi: set ts=4 sw=4 expandtab: */