src/main/win32/SDL_win32_main.c
author Sam Lantinga
Tue, 27 Jun 2006 07:46:36 +0000
branchSDL-1.3
changeset 1718 ed4d4f1ea201
parent 1668 4da1ee79c9af
permissions -rw-r--r--
Further progress on the new Windows video driver:
* SDL_SetModuleHandle() is obsolete, I hope.
* SDL 1.3 uses the UNICODE API
* I'm ignoring Windows CE for the moment, we'll reevaluate what needs to be different for Windows CE later.
* Pulled the stdio redirection from WinMain()
     1 /*
     2     SDL_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 
     7 #include <stdio.h>
     8 #include <stdlib.h>
     9 
    10 #define WIN32_LEAN_AND_MEAN
    11 #include <windows.h>
    12 
    13 /* Include the SDL main definition header */
    14 #include "SDL.h"
    15 #include "SDL_main.h"
    16 
    17 #ifdef main
    18 # ifndef _WIN32_WCE_EMULATION
    19 #  undef main
    20 # endif /* _WIN32_WCE_EMULATION */
    21 #endif /* main */
    22 
    23 #if defined(_WIN32_WCE) && _WIN32_WCE < 300
    24 /* seems to be undefined in Win CE although in online help */
    25 #define isspace(a) (((CHAR)a == ' ') || ((CHAR)a == '\t'))
    26 #endif /* _WIN32_WCE < 300 */
    27 
    28 /* Parse a command line buffer into arguments */
    29 static int
    30 ParseCommandLine(char *cmdline, char **argv)
    31 {
    32     char *bufp;
    33     int argc;
    34 
    35     argc = 0;
    36     for (bufp = cmdline; *bufp;) {
    37         /* Skip leading whitespace */
    38         while (isspace(*bufp)) {
    39             ++bufp;
    40         }
    41         /* Skip over argument */
    42         if (*bufp == '"') {
    43             ++bufp;
    44             if (*bufp) {
    45                 if (argv) {
    46                     argv[argc] = bufp;
    47                 }
    48                 ++argc;
    49             }
    50             /* Skip over word */
    51             while (*bufp && (*bufp != '"')) {
    52                 ++bufp;
    53             }
    54         } else {
    55             if (*bufp) {
    56                 if (argv) {
    57                     argv[argc] = bufp;
    58                 }
    59                 ++argc;
    60             }
    61             /* Skip over word */
    62             while (*bufp && !isspace(*bufp)) {
    63                 ++bufp;
    64             }
    65         }
    66         if (*bufp) {
    67             if (argv) {
    68                 *bufp = '\0';
    69             }
    70             ++bufp;
    71         }
    72     }
    73     if (argv) {
    74         argv[argc] = NULL;
    75     }
    76     return (argc);
    77 }
    78 
    79 /* Show an error message */
    80 static void
    81 ShowError(const char *title, const char *message)
    82 {
    83 /* If USE_MESSAGEBOX is defined, you need to link with user32.lib */
    84 #ifdef USE_MESSAGEBOX
    85     MessageBox(NULL, message, title, MB_ICONEXCLAMATION | MB_OK);
    86 #else
    87     fprintf(stderr, "%s: %s\n", title, message);
    88 #endif
    89 }
    90 
    91 /* Pop up an out of memory message, returns to Windows */
    92 static BOOL
    93 OutOfMemory(void)
    94 {
    95     ShowError("Fatal Error", "Out of memory - aborting");
    96     return FALSE;
    97 }
    98 
    99 #if defined(_MSC_VER) && !defined(_WIN32_WCE)
   100 /* The VC++ compiler needs main defined */
   101 #define console_main main
   102 #endif
   103 
   104 /* This is where execution begins [console apps] */
   105 int
   106 console_main(int argc, char *argv[])
   107 {
   108     int status;
   109 
   110     /* Run the application main() code */
   111     status = SDL_main(argc, argv);
   112 
   113     /* Exit cleanly, calling atexit() functions */
   114     exit(status);
   115 
   116     /* Hush little compiler, don't you cry... */
   117     return 0;
   118 }
   119 
   120 /* This is where execution begins [windowed apps] */
   121 int WINAPI
   122 WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPTSTR szCmdLine, int sw)
   123 {
   124     char **argv;
   125     int argc;
   126     char *cmdline;
   127 #ifdef _WIN32_WCE
   128     wchar_t *bufp;
   129     int nLen;
   130 #else
   131     char *bufp;
   132     size_t nLen;
   133 #endif
   134 
   135 #ifdef _WIN32_WCE
   136     nLen = wcslen(szCmdLine) + 128 + 1;
   137     bufp = SDL_stack_alloc(wchar_t, nLen * 2);
   138     wcscpy(bufp, TEXT("\""));
   139     GetModuleFileName(NULL, bufp + 1, 128 - 3);
   140     wcscpy(bufp + wcslen(bufp), TEXT("\" "));
   141     wcsncpy(bufp + wcslen(bufp), szCmdLine, nLen - wcslen(bufp));
   142     nLen = wcslen(bufp) + 1;
   143     cmdline = SDL_stack_alloc(char, nLen);
   144     if (cmdline == NULL) {
   145         return OutOfMemory();
   146     }
   147     WideCharToMultiByte(CP_ACP, 0, bufp, -1, cmdline, nLen, NULL, NULL);
   148 #else
   149     /* Grab the command line */
   150     bufp = GetCommandLine();
   151     nLen = SDL_strlen(bufp) + 1;
   152     cmdline = SDL_stack_alloc(char, nLen);
   153     if (cmdline == NULL) {
   154         return OutOfMemory();
   155     }
   156     SDL_strlcpy(cmdline, bufp, nLen);
   157 #endif
   158 
   159     /* Parse it into argv and argc */
   160     argc = ParseCommandLine(cmdline, NULL);
   161     argv = SDL_stack_alloc(char *, argc + 1);
   162     if (argv == NULL) {
   163         return OutOfMemory();
   164     }
   165     ParseCommandLine(cmdline, argv);
   166 
   167     /* Run the main program */
   168     console_main(argc, argv);
   169 
   170     /* Hush little compiler, don't you cry... */
   171     return 0;
   172 }
   173 
   174 /* vi: set ts=4 sw=4 expandtab: */