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