src/main/win32/SDL_win32_main.c
author Sam Lantinga
Mon, 10 Jul 2006 21:04:37 +0000
changeset 1895 c121d94672cb
parent 1769 290b5baf2fca
child 2286 41a6675d8700
permissions -rw-r--r--
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
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@1895
    29
static int
slouken@1895
    30
ParseCommandLine(char *cmdline, char **argv)
slouken@754
    31
{
slouken@1895
    32
    char *bufp;
slouken@1895
    33
    int argc;
slouken@754
    34
slouken@1895
    35
    argc = 0;
slouken@1895
    36
    for (bufp = cmdline; *bufp;) {
slouken@1895
    37
        /* Skip leading whitespace */
slouken@1895
    38
        while (isspace(*bufp)) {
slouken@1895
    39
            ++bufp;
slouken@1895
    40
        }
slouken@1895
    41
        /* Skip over argument */
slouken@1895
    42
        if (*bufp == '"') {
slouken@1895
    43
            ++bufp;
slouken@1895
    44
            if (*bufp) {
slouken@1895
    45
                if (argv) {
slouken@1895
    46
                    argv[argc] = bufp;
slouken@1895
    47
                }
slouken@1895
    48
                ++argc;
slouken@1895
    49
            }
slouken@1895
    50
            /* Skip over word */
slouken@1895
    51
            while (*bufp && (*bufp != '"')) {
slouken@1895
    52
                ++bufp;
slouken@1895
    53
            }
slouken@1895
    54
        } else {
slouken@1895
    55
            if (*bufp) {
slouken@1895
    56
                if (argv) {
slouken@1895
    57
                    argv[argc] = bufp;
slouken@1895
    58
                }
slouken@1895
    59
                ++argc;
slouken@1895
    60
            }
slouken@1895
    61
            /* Skip over word */
slouken@1895
    62
            while (*bufp && !isspace(*bufp)) {
slouken@1895
    63
                ++bufp;
slouken@1895
    64
            }
slouken@1895
    65
        }
slouken@1895
    66
        if (*bufp) {
slouken@1895
    67
            if (argv) {
slouken@1895
    68
                *bufp = '\0';
slouken@1895
    69
            }
slouken@1895
    70
            ++bufp;
slouken@1895
    71
        }
slouken@1895
    72
    }
slouken@1895
    73
    if (argv) {
slouken@1895
    74
        argv[argc] = NULL;
slouken@1895
    75
    }
slouken@1895
    76
    return (argc);
slouken@754
    77
}
slouken@754
    78
slouken@754
    79
/* Show an error message */
slouken@1895
    80
static void
slouken@1895
    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@1895
    85
    MessageBox(NULL, message, title, MB_ICONEXCLAMATION | MB_OK);
slouken@754
    86
#else
slouken@1895
    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@1895
    92
static BOOL
slouken@1895
    93
OutOfMemory(void)
slouken@754
    94
{
slouken@1895
    95
    ShowError("Fatal Error", "Out of memory - aborting");
slouken@1895
    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@1895
   105
int
slouken@1895
   106
console_main(int argc, char *argv[])
slouken@754
   107
{
slouken@1895
   108
    int status;
slouken@754
   109
slouken@1895
   110
    /* Run the application main() code */
slouken@1895
   111
    status = SDL_main(argc, argv);
slouken@754
   112
slouken@1895
   113
    /* Exit cleanly, calling atexit() functions */
slouken@1895
   114
    exit(status);
slouken@754
   115
slouken@1895
   116
    /* Hush little compiler, don't you cry... */
slouken@1895
   117
    return 0;
slouken@754
   118
}
slouken@754
   119
slouken@754
   120
/* This is where execution begins [windowed apps] */
slouken@1895
   121
int WINAPI
slouken@1895
   122
WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPTSTR szCmdLine, int sw)
slouken@754
   123
{
slouken@1895
   124
    char **argv;
slouken@1895
   125
    int argc;
slouken@1895
   126
    char *cmdline;
slouken@1286
   127
#ifdef _WIN32_WCE
slouken@1895
   128
    wchar_t *bufp;
slouken@1895
   129
    int nLen;
slouken@754
   130
#else
slouken@1895
   131
    char *bufp;
slouken@1895
   132
    size_t nLen;
slouken@754
   133
#endif
slouken@754
   134
slouken@1465
   135
#ifdef _WIN32_WCE
slouken@1895
   136
    nLen = wcslen(szCmdLine) + 128 + 1;
slouken@1895
   137
    bufp = SDL_stack_alloc(wchar_t, nLen * 2);
slouken@1895
   138
    wcscpy(bufp, TEXT("\""));
slouken@1895
   139
    GetModuleFileName(NULL, bufp + 1, 128 - 3);
slouken@1895
   140
    wcscpy(bufp + wcslen(bufp), TEXT("\" "));
slouken@1895
   141
    wcsncpy(bufp + wcslen(bufp), szCmdLine, nLen - wcslen(bufp));
slouken@1895
   142
    nLen = wcslen(bufp) + 1;
slouken@1895
   143
    cmdline = SDL_stack_alloc(char, nLen);
slouken@1895
   144
    if (cmdline == NULL) {
slouken@1895
   145
        return OutOfMemory();
slouken@1895
   146
    }
slouken@1895
   147
    WideCharToMultiByte(CP_ACP, 0, bufp, -1, cmdline, nLen, NULL, NULL);
slouken@754
   148
#else
slouken@1895
   149
    /* Grab the command line */
slouken@1895
   150
    bufp = GetCommandLine();
slouken@1895
   151
    nLen = SDL_strlen(bufp) + 1;
slouken@1895
   152
    cmdline = SDL_stack_alloc(char, nLen);
slouken@1895
   153
    if (cmdline == NULL) {
slouken@1895
   154
        return OutOfMemory();
slouken@1895
   155
    }
slouken@1895
   156
    SDL_strlcpy(cmdline, bufp, nLen);
slouken@1465
   157
#endif
slouken@754
   158
slouken@1895
   159
    /* Parse it into argv and argc */
slouken@1895
   160
    argc = ParseCommandLine(cmdline, NULL);
slouken@1895
   161
    argv = SDL_stack_alloc(char *, argc + 1);
slouken@1895
   162
    if (argv == NULL) {
slouken@1895
   163
        return OutOfMemory();
slouken@1895
   164
    }
slouken@1895
   165
    ParseCommandLine(cmdline, argv);
slouken@754
   166
slouken@1895
   167
    /* Run the main program */
slouken@1895
   168
    console_main(argc, argv);
slouken@754
   169
slouken@1895
   170
    /* Hush little compiler, don't you cry... */
slouken@1895
   171
    return 0;
slouken@1895
   172
}
slouken@754
   173
slouken@1895
   174
/* vi: set ts=4 sw=4 expandtab: */