added stb, more binaryout changes"
[henge/apc.git] / stb / tests / caveview / win32 / SDL_windows_main.c
1 /*
2 SDL_windows_main.c, placed in the public domain by Sam Lantinga 4/13/98
3
4 The WinMain function -- calls your program's main() function
5 */
6 #include "SDL_config.h"
7
8 #ifdef __WIN32__
9
10 //#include "../../core/windows/SDL_windows.h"
11
12 /* Include this so we define UNICODE properly */
13 #if defined(__WIN32__)
14 #define WIN32_LEAN_AND_MEAN
15 #define STRICT
16 #ifndef UNICODE
17 #define UNICODE 1
18 #endif
19 #undef _WIN32_WINNT
20 #define _WIN32_WINNT 0x501 /* Need 0x410 for AlphaBlend() and 0x500 for EnumDisplayDevices(), 0x501 for raw input */
21 #endif
22
23 #include <windows.h>
24
25 /* Routines to convert from UTF8 to native Windows text */
26 #if UNICODE
27 #define WIN_StringToUTF8(S) SDL_iconv_string("UTF-8", "UTF-16LE", (char *)(S), (SDL_wcslen(S)+1)*sizeof(WCHAR))
28 #define WIN_UTF8ToString(S) (WCHAR *)SDL_iconv_string("UTF-16LE", "UTF-8", (char *)(S), SDL_strlen(S)+1)
29 #else
30 /* !!! FIXME: UTF8ToString() can just be a SDL_strdup() here. */
31 #define WIN_StringToUTF8(S) SDL_iconv_string("UTF-8", "ASCII", (char *)(S), (SDL_strlen(S)+1))
32 #define WIN_UTF8ToString(S) SDL_iconv_string("ASCII", "UTF-8", (char *)(S), SDL_strlen(S)+1)
33 #endif
34
35 /* Sets an error message based on a given HRESULT */
36 extern int WIN_SetErrorFromHRESULT(const char *prefix, HRESULT hr);
37
38 /* Sets an error message based on GetLastError(). Always return -1. */
39 extern int WIN_SetError(const char *prefix);
40
41 /* Wrap up the oddities of CoInitialize() into a common function. */
42 extern HRESULT WIN_CoInitialize(void);
43 extern void WIN_CoUninitialize(void);
44
45 /* Returns SDL_TRUE if we're running on Windows Vista and newer */
46 extern BOOL WIN_IsWindowsVistaOrGreater();
47
48 #include <stdio.h>
49 #include <stdlib.h>
50
51 /* Include the SDL main definition header */
52 #include "SDL.h"
53 #include "SDL_main.h"
54
55 #ifdef main
56 # undef main
57 #endif /* main */
58
59 static void
60 UnEscapeQuotes(char *arg)
61 {
62 char *last = NULL;
63
64 while (*arg) {
65 if (*arg == '"' && (last != NULL && *last == '\\')) {
66 char *c_curr = arg;
67 char *c_last = last;
68
69 while (*c_curr) {
70 *c_last = *c_curr;
71 c_last = c_curr;
72 c_curr++;
73 }
74 *c_last = '\0';
75 }
76 last = arg;
77 arg++;
78 }
79 }
80
81 /* Parse a command line buffer into arguments */
82 static int
83 ParseCommandLine(char *cmdline, char **argv)
84 {
85 char *bufp;
86 char *lastp = NULL;
87 int argc, last_argc;
88
89 argc = last_argc = 0;
90 for (bufp = cmdline; *bufp;) {
91 /* Skip leading whitespace */
92 while (SDL_isspace(*bufp)) {
93 ++bufp;
94 }
95 /* Skip over argument */
96 if (*bufp == '"') {
97 ++bufp;
98 if (*bufp) {
99 if (argv) {
100 argv[argc] = bufp;
101 }
102 ++argc;
103 }
104 /* Skip over word */
105 lastp = bufp;
106 while (*bufp && (*bufp != '"' || *lastp == '\\')) {
107 lastp = bufp;
108 ++bufp;
109 }
110 } else {
111 if (*bufp) {
112 if (argv) {
113 argv[argc] = bufp;
114 }
115 ++argc;
116 }
117 /* Skip over word */
118 while (*bufp && !SDL_isspace(*bufp)) {
119 ++bufp;
120 }
121 }
122 if (*bufp) {
123 if (argv) {
124 *bufp = '\0';
125 }
126 ++bufp;
127 }
128
129 /* Strip out \ from \" sequences */
130 if (argv && last_argc != argc) {
131 UnEscapeQuotes(argv[last_argc]);
132 }
133 last_argc = argc;
134 }
135 if (argv) {
136 argv[argc] = NULL;
137 }
138 return (argc);
139 }
140
141 /* Show an error message */
142 static void
143 ShowError(const char *title, const char *message)
144 {
145 /* If USE_MESSAGEBOX is defined, you need to link with user32.lib */
146 #ifdef USE_MESSAGEBOX
147 MessageBox(NULL, message, title, MB_ICONEXCLAMATION | MB_OK);
148 #else
149 fprintf(stderr, "%s: %s\n", title, message);
150 #endif
151 }
152
153 /* Pop up an out of memory message, returns to Windows */
154 static BOOL
155 OutOfMemory(void)
156 {
157 ShowError("Fatal Error", "Out of memory - aborting");
158 return FALSE;
159 }
160
161 #if defined(_MSC_VER)
162 /* The VC++ compiler needs main defined */
163 #define console_main main
164 #endif
165
166 /* This is where execution begins [console apps] */
167 int
168 console_main(int argc, char *argv[])
169 {
170 int status;
171
172 SDL_SetMainReady();
173
174 /* Run the application main() code */
175 status = SDL_main(argc, argv);
176
177 /* Exit cleanly, calling atexit() functions */
178 exit(status);
179
180 /* Hush little compiler, don't you cry... */
181 return 0;
182 }
183
184 /* This is where execution begins [windowed apps] */
185 int WINAPI
186 WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
187 {
188 char **argv;
189 int argc;
190 char *cmdline;
191
192 /* Grab the command line */
193 TCHAR *text = GetCommandLine();
194 #if UNICODE
195 cmdline = SDL_iconv_string("UTF-8", "UCS-2-INTERNAL", (char *)(text), (SDL_wcslen(text)+1)*sizeof(WCHAR));
196 #else
197 cmdline = SDL_strdup(text);
198 #endif
199 if (cmdline == NULL) {
200 return OutOfMemory();
201 }
202
203 /* Parse it into argv and argc */
204 argc = ParseCommandLine(cmdline, NULL);
205 argv = SDL_stack_alloc(char *, argc + 1);
206 if (argv == NULL) {
207 return OutOfMemory();
208 }
209 ParseCommandLine(cmdline, argv);
210
211 /* Run the main program */
212 console_main(argc, argv);
213
214 SDL_stack_free(argv);
215
216 SDL_free(cmdline);
217
218 /* Hush little compiler, don't you cry... */
219 return 0;
220 }
221
222 #endif /* __WIN32__ */
223
224 /* vi: set ts=4 sw=4 expandtab: */