2 SDL_windows_main.c, placed in the public domain by Sam Lantinga 4/13/98
4 The WinMain function -- calls your program's main() function
6 #include "SDL_config.h"
10 //#include "../../core/windows/SDL_windows.h"
12 /* Include this so we define UNICODE properly */
13 #if defined(__WIN32__)
14 #define WIN32_LEAN_AND_MEAN
20 #define _WIN32_WINNT 0x501 /* Need 0x410 for AlphaBlend() and 0x500 for EnumDisplayDevices(), 0x501 for raw input */
25 /* Routines to convert from UTF8 to native Windows text */
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)
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)
35 /* Sets an error message based on a given HRESULT */
36 extern int WIN_SetErrorFromHRESULT(const char *prefix
, HRESULT hr
);
38 /* Sets an error message based on GetLastError(). Always return -1. */
39 extern int WIN_SetError(const char *prefix
);
41 /* Wrap up the oddities of CoInitialize() into a common function. */
42 extern HRESULT
WIN_CoInitialize(void);
43 extern void WIN_CoUninitialize(void);
45 /* Returns SDL_TRUE if we're running on Windows Vista and newer */
46 extern BOOL
WIN_IsWindowsVistaOrGreater();
51 /* Include the SDL main definition header */
60 UnEscapeQuotes(char *arg
)
65 if (*arg
== '"' && (last
!= NULL
&& *last
== '\\')) {
81 /* Parse a command line buffer into arguments */
83 ParseCommandLine(char *cmdline
, char **argv
)
90 for (bufp
= cmdline
; *bufp
;) {
91 /* Skip leading whitespace */
92 while (SDL_isspace(*bufp
)) {
95 /* Skip over argument */
106 while (*bufp
&& (*bufp
!= '"' || *lastp
== '\\')) {
118 while (*bufp
&& !SDL_isspace(*bufp
)) {
129 /* Strip out \ from \" sequences */
130 if (argv
&& last_argc
!= argc
) {
131 UnEscapeQuotes(argv
[last_argc
]);
141 /* Show an error message */
143 ShowError(const char *title
, const char *message
)
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
);
149 fprintf(stderr
, "%s: %s\n", title
, message
);
153 /* Pop up an out of memory message, returns to Windows */
157 ShowError("Fatal Error", "Out of memory - aborting");
161 #if defined(_MSC_VER)
162 /* The VC++ compiler needs main defined */
163 #define console_main main
166 /* This is where execution begins [console apps] */
168 console_main(int argc
, char *argv
[])
174 /* Run the application main() code */
175 status
= SDL_main(argc
, argv
);
177 /* Exit cleanly, calling atexit() functions */
180 /* Hush little compiler, don't you cry... */
184 /* This is where execution begins [windowed apps] */
186 WinMain(HINSTANCE hInst
, HINSTANCE hPrev
, LPSTR szCmdLine
, int sw
)
192 /* Grab the command line */
193 TCHAR
*text
= GetCommandLine();
195 cmdline
= SDL_iconv_string("UTF-8", "UCS-2-INTERNAL", (char *)(text
), (SDL_wcslen(text
)+1)*sizeof(WCHAR
));
197 cmdline
= SDL_strdup(text
);
199 if (cmdline
== NULL
) {
200 return OutOfMemory();
203 /* Parse it into argv and argc */
204 argc
= ParseCommandLine(cmdline
, NULL
);
205 argv
= SDL_stack_alloc(char *, argc
+ 1);
207 return OutOfMemory();
209 ParseCommandLine(cmdline
, argv
);
211 /* Run the main program */
212 console_main(argc
, argv
);
214 SDL_stack_free(argv
);
218 /* Hush little compiler, don't you cry... */
222 #endif /* __WIN32__ */
224 /* vi: set ts=4 sw=4 expandtab: */