src/SDL.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 15 Dec 2009 08:11:06 +0000
changeset 3565 f43c8f688f77
parent 3319 63d4517fc4ac
child 3647 c5925cd41955
permissions -rw-r--r--
Fixed bug #906

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