src/SDL.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 24 Jan 2011 15:46:11 -0800
changeset 5086 c2539ff054c8
parent 5062 e8916fe9cfc8
child 5090 327f181542f1
permissions -rw-r--r--
Fixed compiling on Windows Mobile SDK 5.0 with Visual Studio 2008
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@3697
     3
    Copyright (C) 1997-2010 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* Initialization code for SDL */
slouken@0
    25
slouken@1361
    26
#include "SDL.h"
slouken@1361
    27
#include "SDL_fatal.h"
slouken@4472
    28
#include "SDL_assert_c.h"
slouken@4472
    29
#include "haptic/SDL_haptic_c.h"
slouken@4472
    30
#include "joystick/SDL_joystick_c.h"
slouken@3647
    31
slouken@1361
    32
#if !SDL_VIDEO_DISABLED
slouken@1361
    33
#include "video/SDL_leaks.h"
slouken@1361
    34
#endif
slouken@1361
    35
slouken@0
    36
/* Initialization/Cleanup routines */
slouken@1361
    37
#if !SDL_TIMERS_DISABLED
slouken@0
    38
extern void SDL_StartTicks(void);
slouken@1895
    39
extern int SDL_TimerInit(void);
slouken@0
    40
extern void SDL_TimerQuit(void);
slouken@0
    41
#endif
slouken@5086
    42
#if defined(__WIN32__)
slouken@2713
    43
extern int SDL_HelperWindowCreate(void);
slouken@2713
    44
extern int SDL_HelperWindowDestroy(void);
slouken@2713
    45
#endif
slouken@0
    46
slouken@3647
    47
slouken@0
    48
/* The initialized subsystems */
slouken@0
    49
static Uint32 SDL_initialized = 0;
slouken@0
    50
static Uint32 ticks_started = 0;
slouken@0
    51
slouken@0
    52
#ifdef CHECK_LEAKS
slouken@0
    53
int surfaces_allocated = 0;
slouken@0
    54
#endif
slouken@0
    55
slouken@1895
    56
int
slouken@1895
    57
SDL_InitSubSystem(Uint32 flags)
slouken@0
    58
{
slouken@1361
    59
#if !SDL_VIDEO_DISABLED
slouken@1895
    60
    /* Initialize the video/event subsystem */
slouken@1895
    61
    if ((flags & SDL_INIT_VIDEO) && !(SDL_initialized & SDL_INIT_VIDEO)) {
slouken@1909
    62
        if (SDL_VideoInit(NULL, (flags & SDL_INIT_EVENTTHREAD)) < 0) {
slouken@1895
    63
            return (-1);
slouken@1895
    64
        }
slouken@1895
    65
        SDL_initialized |= SDL_INIT_VIDEO;
slouken@1895
    66
    }
slouken@0
    67
#else
slouken@1895
    68
    if (flags & SDL_INIT_VIDEO) {
slouken@1895
    69
        SDL_SetError("SDL not built with video support");
slouken@1895
    70
        return (-1);
slouken@1895
    71
    }
slouken@0
    72
#endif
slouken@0
    73
slouken@1361
    74
#if !SDL_AUDIO_DISABLED
slouken@1895
    75
    /* Initialize the audio subsystem */
slouken@1895
    76
    if ((flags & SDL_INIT_AUDIO) && !(SDL_initialized & SDL_INIT_AUDIO)) {
slouken@1909
    77
        if (SDL_AudioInit(NULL) < 0) {
slouken@1895
    78
            return (-1);
slouken@1895
    79
        }
slouken@1895
    80
        SDL_initialized |= SDL_INIT_AUDIO;
slouken@1895
    81
    }
slouken@0
    82
#else
slouken@1895
    83
    if (flags & SDL_INIT_AUDIO) {
slouken@1895
    84
        SDL_SetError("SDL not built with audio support");
slouken@1895
    85
        return (-1);
slouken@1895
    86
    }
slouken@0
    87
#endif
slouken@0
    88
slouken@1361
    89
#if !SDL_TIMERS_DISABLED
slouken@1895
    90
    /* Initialize the timer subsystem */
slouken@1895
    91
    if (!ticks_started) {
slouken@1895
    92
        SDL_StartTicks();
slouken@1895
    93
        ticks_started = 1;
slouken@1895
    94
    }
slouken@1895
    95
    if ((flags & SDL_INIT_TIMER) && !(SDL_initialized & SDL_INIT_TIMER)) {
slouken@1895
    96
        if (SDL_TimerInit() < 0) {
slouken@1895
    97
            return (-1);
slouken@1895
    98
        }
slouken@1895
    99
        SDL_initialized |= SDL_INIT_TIMER;
slouken@1895
   100
    }
slouken@0
   101
#else
slouken@1895
   102
    if (flags & SDL_INIT_TIMER) {
slouken@1895
   103
        SDL_SetError("SDL not built with timer support");
slouken@1895
   104
        return (-1);
slouken@1895
   105
    }
slouken@0
   106
#endif
slouken@0
   107
slouken@1361
   108
#if !SDL_JOYSTICK_DISABLED
slouken@1895
   109
    /* Initialize the joystick subsystem */
slouken@1895
   110
    if ((flags & SDL_INIT_JOYSTICK) && !(SDL_initialized & SDL_INIT_JOYSTICK)) {
slouken@1895
   111
        if (SDL_JoystickInit() < 0) {
slouken@1895
   112
            return (-1);
slouken@1895
   113
        }
slouken@1895
   114
        SDL_initialized |= SDL_INIT_JOYSTICK;
slouken@1895
   115
    }
slouken@0
   116
#else
slouken@1895
   117
    if (flags & SDL_INIT_JOYSTICK) {
slouken@1895
   118
        SDL_SetError("SDL not built with joystick support");
slouken@1895
   119
        return (-1);
slouken@1895
   120
    }
slouken@0
   121
#endif
slouken@0
   122
slouken@2713
   123
#if !SDL_HAPTIC_DISABLED
slouken@2713
   124
    /* Initialize the haptic subsystem */
slouken@2713
   125
    if ((flags & SDL_INIT_HAPTIC) && !(SDL_initialized & SDL_INIT_HAPTIC)) {
slouken@2713
   126
        if (SDL_HapticInit() < 0) {
slouken@2713
   127
            return (-1);
slouken@2713
   128
        }
slouken@2713
   129
        SDL_initialized |= SDL_INIT_HAPTIC;
slouken@2713
   130
    }
slouken@2713
   131
#else
slouken@2713
   132
    if (flags & SDL_INIT_HAPTIC) {
slouken@2713
   133
        SDL_SetError("SDL not built with haptic (force feedback) support");
slouken@2713
   134
        return (-1);
slouken@2713
   135
    }
slouken@2713
   136
#endif
slouken@1895
   137
    return (0);
slouken@0
   138
}
slouken@0
   139
slouken@1895
   140
int
slouken@1895
   141
SDL_Init(Uint32 flags)
slouken@0
   142
{
slouken@3647
   143
    if (SDL_AssertionsInit() < 0) {
slouken@3647
   144
        return -1;
slouken@3647
   145
    }
slouken@3647
   146
slouken@1895
   147
    /* Clear the error message */
slouken@1895
   148
    SDL_ClearError();
slouken@0
   149
slouken@5086
   150
#if defined(__WIN32__)
slouken@2713
   151
    if (SDL_HelperWindowCreate() < 0) {
slouken@2713
   152
        return -1;
slouken@2713
   153
    }
slouken@2713
   154
#endif
slouken@2713
   155
slouken@1895
   156
    /* Initialize the desired subsystems */
slouken@1895
   157
    if (SDL_InitSubSystem(flags) < 0) {
slouken@1895
   158
        return (-1);
slouken@1895
   159
    }
slouken@0
   160
slouken@1895
   161
    /* Everything is initialized */
slouken@1895
   162
    if (!(flags & SDL_INIT_NOPARACHUTE)) {
slouken@1895
   163
        SDL_InstallParachute();
slouken@1895
   164
    }
slouken@3647
   165
slouken@1895
   166
    return (0);
slouken@0
   167
}
slouken@0
   168
slouken@1895
   169
void
slouken@1895
   170
SDL_QuitSubSystem(Uint32 flags)
slouken@0
   171
{
slouken@1895
   172
    /* Shut down requested initialized subsystems */
slouken@1361
   173
#if !SDL_JOYSTICK_DISABLED
slouken@1895
   174
    if ((flags & SDL_initialized & SDL_INIT_JOYSTICK)) {
slouken@1895
   175
        SDL_JoystickQuit();
slouken@1895
   176
        SDL_initialized &= ~SDL_INIT_JOYSTICK;
slouken@1895
   177
    }
slouken@0
   178
#endif
slouken@2713
   179
#if !SDL_HAPTIC_DISABLED
slouken@2713
   180
    if ((flags & SDL_initialized & SDL_INIT_HAPTIC)) {
slouken@2713
   181
        SDL_HapticQuit();
slouken@2713
   182
        SDL_initialized &= ~SDL_INIT_HAPTIC;
slouken@2713
   183
    }
slouken@2713
   184
#endif
slouken@1361
   185
#if !SDL_TIMERS_DISABLED
slouken@1895
   186
    if ((flags & SDL_initialized & SDL_INIT_TIMER)) {
slouken@1895
   187
        SDL_TimerQuit();
slouken@1895
   188
        SDL_initialized &= ~SDL_INIT_TIMER;
slouken@1895
   189
    }
slouken@0
   190
#endif
slouken@1361
   191
#if !SDL_AUDIO_DISABLED
slouken@1895
   192
    if ((flags & SDL_initialized & SDL_INIT_AUDIO)) {
slouken@1895
   193
        SDL_AudioQuit();
slouken@1895
   194
        SDL_initialized &= ~SDL_INIT_AUDIO;
slouken@1895
   195
    }
slouken@0
   196
#endif
slouken@1361
   197
#if !SDL_VIDEO_DISABLED
slouken@1895
   198
    if ((flags & SDL_initialized & SDL_INIT_VIDEO)) {
slouken@1895
   199
        SDL_VideoQuit();
slouken@1895
   200
        SDL_initialized &= ~SDL_INIT_VIDEO;
slouken@1895
   201
    }
slouken@0
   202
#endif
slouken@0
   203
}
slouken@0
   204
slouken@1895
   205
Uint32
slouken@1895
   206
SDL_WasInit(Uint32 flags)
slouken@0
   207
{
slouken@1895
   208
    if (!flags) {
slouken@1895
   209
        flags = SDL_INIT_EVERYTHING;
slouken@1895
   210
    }
slouken@1895
   211
    return (SDL_initialized & flags);
slouken@0
   212
}
slouken@0
   213
slouken@1895
   214
void
slouken@1895
   215
SDL_Quit(void)
slouken@0
   216
{
slouken@1895
   217
    /* Quit all subsystems */
icculus@1190
   218
#ifdef DEBUG_BUILD
slouken@1895
   219
    printf("[SDL_Quit] : Enter! Calling QuitSubSystem()\n");
slouken@1895
   220
    fflush(stdout);
icculus@1190
   221
#endif
slouken@2713
   222
slouken@5086
   223
#if defined(__WIN32__)
slouken@2713
   224
    SDL_HelperWindowDestroy();
slouken@2713
   225
#endif
slouken@1895
   226
    SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
slouken@0
   227
slouken@0
   228
#ifdef CHECK_LEAKS
icculus@1190
   229
#ifdef DEBUG_BUILD
slouken@1895
   230
    printf("[SDL_Quit] : CHECK_LEAKS\n");
slouken@1895
   231
    fflush(stdout);
icculus@1190
   232
#endif
icculus@1190
   233
slouken@3647
   234
    /* !!! FIXME: make this an assertion. */
slouken@1895
   235
    /* Print the number of surfaces not freed */
slouken@1895
   236
    if (surfaces_allocated != 0) {
slouken@1895
   237
        fprintf(stderr, "SDL Warning: %d SDL surfaces extant\n",
slouken@1895
   238
                surfaces_allocated);
slouken@1895
   239
    }
slouken@0
   240
#endif
icculus@1190
   241
#ifdef DEBUG_BUILD
slouken@1895
   242
    printf("[SDL_Quit] : SDL_UninstallParachute()\n");
slouken@1895
   243
    fflush(stdout);
icculus@1190
   244
#endif
slouken@0
   245
slouken@1895
   246
    /* Uninstall any parachute signal handlers */
slouken@1895
   247
    SDL_UninstallParachute();
slouken@397
   248
slouken@3647
   249
    SDL_AssertionsQuit();
slouken@3647
   250
icculus@1190
   251
#ifdef DEBUG_BUILD
slouken@1895
   252
    printf("[SDL_Quit] : Returning!\n");
slouken@1895
   253
    fflush(stdout);
icculus@1190
   254
#endif
icculus@1190
   255
slouken@0
   256
}
slouken@0
   257
slouken@1895
   258
/* Get the library version number */
slouken@1895
   259
void
slouken@1895
   260
SDL_GetVersion(SDL_version * ver)
slouken@0
   261
{
slouken@1895
   262
    SDL_VERSION(ver);
slouken@0
   263
}
slouken@0
   264
slouken@2982
   265
/* Get the library source revision */
icculus@4419
   266
const char *
slouken@2996
   267
SDL_GetRevision(void)
slouken@2982
   268
{
slouken@2983
   269
    return SDL_REVISION;
slouken@2982
   270
}
slouken@2982
   271
slouken@3319
   272
/* Get the name of the platform */
slouken@3319
   273
const char *
slouken@3319
   274
SDL_GetPlatform()
slouken@3319
   275
{
slouken@3319
   276
#if __AIX__
slouken@3319
   277
    return "AIX";
slouken@3319
   278
#elif __HAIKU__
slouken@3319
   279
/* Haiku must appear here before BeOS, since it also defines __BEOS__ */
slouken@3319
   280
    return "Haiku";
slouken@3319
   281
#elif __BEOS__
slouken@3319
   282
    return "BeOS";
slouken@3319
   283
#elif __BSDI__
slouken@3319
   284
    return "BSDI";
slouken@3319
   285
#elif __DREAMCAST__
slouken@3319
   286
    return "Dreamcast";
slouken@3319
   287
#elif __FREEBSD__
slouken@3319
   288
    return "FreeBSD";
slouken@3319
   289
#elif __HPUX__
slouken@3319
   290
    return "HP-UX";
slouken@3319
   291
#elif __IRIX__
slouken@3319
   292
    return "Irix";
slouken@3319
   293
#elif __LINUX__
slouken@3319
   294
    return "Linux";
slouken@3319
   295
#elif __MINT__
slouken@3319
   296
    return "Atari MiNT";
slouken@3319
   297
#elif __MACOS__
slouken@3319
   298
    return "MacOS Classic";
slouken@3319
   299
#elif __MACOSX__
slouken@3319
   300
    return "Mac OS X";
slouken@3319
   301
#elif __NETBSD__
slouken@3319
   302
    return "NetBSD";
slouken@3319
   303
#elif __OPENBSD__
slouken@3319
   304
    return "OpenBSD";
slouken@3319
   305
#elif __OS2__
slouken@3319
   306
    return "OS/2";
slouken@3319
   307
#elif __OSF__
slouken@3319
   308
    return "OSF/1";
slouken@3319
   309
#elif __QNXNTO__
slouken@3319
   310
    return "QNX Neutrino";
slouken@3319
   311
#elif __RISCOS__
slouken@3319
   312
    return "RISC OS";
slouken@3319
   313
#elif __SOLARIS__
slouken@3319
   314
    return "Solaris";
slouken@5086
   315
#elif __WIN32__
slouken@3319
   316
#ifdef _WIN32_WCE
slouken@3319
   317
    return "Windows CE";
slouken@3319
   318
#else
slouken@3319
   319
    return "Windows";
slouken@3319
   320
#endif
slouken@3319
   321
#elif __IPHONEOS__
slouken@3319
   322
    return "iPhone OS";
slouken@3319
   323
#else
slouken@3319
   324
    return "Unknown (see SDL_platform.h)";
slouken@3319
   325
#endif
slouken@3319
   326
}
slouken@3319
   327
slouken@5086
   328
#if defined(__WIN32__)
slouken@1330
   329
slouken@1465
   330
#if !defined(HAVE_LIBC) || (defined(__WATCOMC__) && defined(BUILD_DLL))
slouken@1465
   331
/* Need to include DllMain() on Watcom C for some reason.. */
slouken@1433
   332
#define WIN32_LEAN_AND_MEAN
slouken@1433
   333
#include <windows.h>
slouken@1330
   334
slouken@1895
   335
BOOL APIENTRY
slouken@1895
   336
_DllMainCRTStartup(HANDLE hModule,
slouken@1895
   337
                   DWORD ul_reason_for_call, LPVOID lpReserved)
slouken@1330
   338
{
slouken@1895
   339
    switch (ul_reason_for_call) {
slouken@1895
   340
    case DLL_PROCESS_ATTACH:
slouken@1895
   341
    case DLL_THREAD_ATTACH:
slouken@1895
   342
    case DLL_THREAD_DETACH:
slouken@1895
   343
    case DLL_PROCESS_DETACH:
slouken@1895
   344
        break;
slouken@1895
   345
    }
slouken@1895
   346
    return TRUE;
slouken@1330
   347
}
slouken@1465
   348
#endif /* building DLL with Watcom C */
slouken@1330
   349
slouken@5086
   350
#endif /* __WIN32__ */
slouken@1895
   351
slouken@1895
   352
/* vi: set ts=4 sw=4 expandtab: */