src/SDL.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 30 Dec 2011 06:41:41 -0500
changeset 6128 2804d71badce
parent 5535 96594ac5fd1a
child 6138 4c64952a58fb
permissions -rw-r--r--
Initialize timers first so the tick counter is valid by the time the audio and video systems initialize.
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "SDL_config.h"
    22 
    23 /* Initialization code for SDL */
    24 
    25 #include "SDL.h"
    26 #include "SDL_revision.h"
    27 #include "SDL_fatal.h"
    28 #include "SDL_assert_c.h"
    29 #include "haptic/SDL_haptic_c.h"
    30 #include "joystick/SDL_joystick_c.h"
    31 
    32 /* Initialization/Cleanup routines */
    33 #if !SDL_TIMERS_DISABLED
    34 extern void SDL_StartTicks(void);
    35 extern int SDL_TimerInit(void);
    36 extern void SDL_TimerQuit(void);
    37 #endif
    38 #if defined(__WIN32__)
    39 extern int SDL_HelperWindowCreate(void);
    40 extern int SDL_HelperWindowDestroy(void);
    41 #endif
    42 
    43 
    44 /* The initialized subsystems */
    45 static Uint32 SDL_initialized = 0;
    46 static Uint32 ticks_started = 0;
    47 
    48 
    49 int
    50 SDL_InitSubSystem(Uint32 flags)
    51 {
    52 #if !SDL_TIMERS_DISABLED
    53     /* Initialize the timer subsystem */
    54     if (!ticks_started) {
    55         SDL_StartTicks();
    56         ticks_started = 1;
    57     }
    58     if ((flags & SDL_INIT_TIMER) && !(SDL_initialized & SDL_INIT_TIMER)) {
    59         if (SDL_TimerInit() < 0) {
    60             return (-1);
    61         }
    62         SDL_initialized |= SDL_INIT_TIMER;
    63     }
    64 #else
    65     if (flags & SDL_INIT_TIMER) {
    66         SDL_SetError("SDL not built with timer support");
    67         return (-1);
    68     }
    69 #endif
    70 
    71 #if !SDL_VIDEO_DISABLED
    72     /* Initialize the video/event subsystem */
    73     if ((flags & SDL_INIT_VIDEO) && !(SDL_initialized & SDL_INIT_VIDEO)) {
    74         if (SDL_VideoInit(NULL) < 0) {
    75             return (-1);
    76         }
    77         SDL_initialized |= SDL_INIT_VIDEO;
    78     }
    79 #else
    80     if (flags & SDL_INIT_VIDEO) {
    81         SDL_SetError("SDL not built with video support");
    82         return (-1);
    83     }
    84 #endif
    85 
    86 #if !SDL_AUDIO_DISABLED
    87     /* Initialize the audio subsystem */
    88     if ((flags & SDL_INIT_AUDIO) && !(SDL_initialized & SDL_INIT_AUDIO)) {
    89         if (SDL_AudioInit(NULL) < 0) {
    90             return (-1);
    91         }
    92         SDL_initialized |= SDL_INIT_AUDIO;
    93     }
    94 #else
    95     if (flags & SDL_INIT_AUDIO) {
    96         SDL_SetError("SDL not built with audio support");
    97         return (-1);
    98     }
    99 #endif
   100 
   101 #if !SDL_JOYSTICK_DISABLED
   102     /* Initialize the joystick subsystem */
   103     if ((flags & SDL_INIT_JOYSTICK) && !(SDL_initialized & SDL_INIT_JOYSTICK)) {
   104         if (SDL_JoystickInit() < 0) {
   105             return (-1);
   106         }
   107         SDL_initialized |= SDL_INIT_JOYSTICK;
   108     }
   109 #else
   110     if (flags & SDL_INIT_JOYSTICK) {
   111         SDL_SetError("SDL not built with joystick support");
   112         return (-1);
   113     }
   114 #endif
   115 
   116 #if !SDL_HAPTIC_DISABLED
   117     /* Initialize the haptic subsystem */
   118     if ((flags & SDL_INIT_HAPTIC) && !(SDL_initialized & SDL_INIT_HAPTIC)) {
   119         if (SDL_HapticInit() < 0) {
   120             return (-1);
   121         }
   122         SDL_initialized |= SDL_INIT_HAPTIC;
   123     }
   124 #else
   125     if (flags & SDL_INIT_HAPTIC) {
   126         SDL_SetError("SDL not built with haptic (force feedback) support");
   127         return (-1);
   128     }
   129 #endif
   130     return (0);
   131 }
   132 
   133 int
   134 SDL_Init(Uint32 flags)
   135 {
   136     if (SDL_AssertionsInit() < 0) {
   137         return -1;
   138     }
   139 
   140     /* Clear the error message */
   141     SDL_ClearError();
   142 
   143 #if defined(__WIN32__)
   144     if (SDL_HelperWindowCreate() < 0) {
   145         return -1;
   146     }
   147 #endif
   148 
   149     /* Initialize the desired subsystems */
   150     if (SDL_InitSubSystem(flags) < 0) {
   151         return (-1);
   152     }
   153 
   154     /* Everything is initialized */
   155     if (!(flags & SDL_INIT_NOPARACHUTE)) {
   156         SDL_InstallParachute();
   157     }
   158 
   159     return (0);
   160 }
   161 
   162 void
   163 SDL_QuitSubSystem(Uint32 flags)
   164 {
   165     /* Shut down requested initialized subsystems */
   166 #if !SDL_JOYSTICK_DISABLED
   167     if ((flags & SDL_initialized & SDL_INIT_JOYSTICK)) {
   168         SDL_JoystickQuit();
   169         SDL_initialized &= ~SDL_INIT_JOYSTICK;
   170     }
   171 #endif
   172 #if !SDL_HAPTIC_DISABLED
   173     if ((flags & SDL_initialized & SDL_INIT_HAPTIC)) {
   174         SDL_HapticQuit();
   175         SDL_initialized &= ~SDL_INIT_HAPTIC;
   176     }
   177 #endif
   178 #if !SDL_AUDIO_DISABLED
   179     if ((flags & SDL_initialized & SDL_INIT_AUDIO)) {
   180         SDL_AudioQuit();
   181         SDL_initialized &= ~SDL_INIT_AUDIO;
   182     }
   183 #endif
   184 #if !SDL_VIDEO_DISABLED
   185     if ((flags & SDL_initialized & SDL_INIT_VIDEO)) {
   186         SDL_VideoQuit();
   187         SDL_initialized &= ~SDL_INIT_VIDEO;
   188     }
   189 #endif
   190 #if !SDL_TIMERS_DISABLED
   191     if ((flags & SDL_initialized & SDL_INIT_TIMER)) {
   192         SDL_TimerQuit();
   193         SDL_initialized &= ~SDL_INIT_TIMER;
   194     }
   195 #endif
   196 }
   197 
   198 Uint32
   199 SDL_WasInit(Uint32 flags)
   200 {
   201     if (!flags) {
   202         flags = SDL_INIT_EVERYTHING;
   203     }
   204     return (SDL_initialized & flags);
   205 }
   206 
   207 void
   208 SDL_Quit(void)
   209 {
   210     /* Quit all subsystems */
   211 #if defined(__WIN32__)
   212     SDL_HelperWindowDestroy();
   213 #endif
   214     SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
   215 
   216     /* Uninstall any parachute signal handlers */
   217     SDL_UninstallParachute();
   218 
   219     SDL_ClearHints();
   220     SDL_AssertionsQuit();
   221     SDL_LogResetPriorities();
   222 }
   223 
   224 /* Get the library version number */
   225 void
   226 SDL_GetVersion(SDL_version * ver)
   227 {
   228     SDL_VERSION(ver);
   229 }
   230 
   231 /* Get the library source revision */
   232 const char *
   233 SDL_GetRevision(void)
   234 {
   235     return SDL_REVISION;
   236 }
   237 
   238 /* Get the library source revision number */
   239 int
   240 SDL_GetRevisionNumber(void)
   241 {
   242     return SDL_REVISION_NUMBER;
   243 }
   244 
   245 /* Get the name of the platform */
   246 const char *
   247 SDL_GetPlatform()
   248 {
   249 #if __AIX__
   250     return "AIX";
   251 #elif __HAIKU__
   252 /* Haiku must appear here before BeOS, since it also defines __BEOS__ */
   253     return "Haiku";
   254 #elif __BEOS__
   255     return "BeOS";
   256 #elif __BSDI__
   257     return "BSDI";
   258 #elif __DREAMCAST__
   259     return "Dreamcast";
   260 #elif __FREEBSD__
   261     return "FreeBSD";
   262 #elif __HPUX__
   263     return "HP-UX";
   264 #elif __IRIX__
   265     return "Irix";
   266 #elif __LINUX__
   267     return "Linux";
   268 #elif __MINT__
   269     return "Atari MiNT";
   270 #elif __MACOS__
   271     return "MacOS Classic";
   272 #elif __MACOSX__
   273     return "Mac OS X";
   274 #elif __NETBSD__
   275     return "NetBSD";
   276 #elif __NDS__
   277     return "Nintendo DS";
   278 #elif __OPENBSD__
   279     return "OpenBSD";
   280 #elif __OS2__
   281     return "OS/2";
   282 #elif __OSF__
   283     return "OSF/1";
   284 #elif __QNXNTO__
   285     return "QNX Neutrino";
   286 #elif __RISCOS__
   287     return "RISC OS";
   288 #elif __SOLARIS__
   289     return "Solaris";
   290 #elif __WIN32__
   291 #ifdef _WIN32_WCE
   292     return "Windows CE";
   293 #else
   294     return "Windows";
   295 #endif
   296 #elif __IPHONEOS__
   297     return "iPhone OS";
   298 #else
   299     return "Unknown (see SDL_platform.h)";
   300 #endif
   301 }
   302 
   303 #if defined(__WIN32__)
   304 
   305 #if !defined(HAVE_LIBC) || (defined(__WATCOMC__) && defined(BUILD_DLL))
   306 /* Need to include DllMain() on Watcom C for some reason.. */
   307 #include "core/windows/SDL_windows.h"
   308 
   309 BOOL APIENTRY
   310 _DllMainCRTStartup(HANDLE hModule,
   311                    DWORD ul_reason_for_call, LPVOID lpReserved)
   312 {
   313     switch (ul_reason_for_call) {
   314     case DLL_PROCESS_ATTACH:
   315     case DLL_THREAD_ATTACH:
   316     case DLL_THREAD_DETACH:
   317     case DLL_PROCESS_DETACH:
   318         break;
   319     }
   320     return TRUE;
   321 }
   322 #endif /* building DLL with Watcom C */
   323 
   324 #endif /* __WIN32__ */
   325 
   326 /* vi: set ts=4 sw=4 expandtab: */