src/SDL.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 24 Jan 2011 21:20:30 -0800
changeset 5090 327f181542f1
parent 5086 c2539ff054c8
child 5123 dc0dfdd58f27
permissions -rw-r--r--
Include windows.h in a single point in the source, so we can be consistent about the definition of UNICODE and have core utility functions for Windows that all modules can share.

I think this also fixes the bug relating to non-latin characters in filenames, since UNICODE wasn't defined in SDL_rwops.c
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2010 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 #include "SDL_assert_c.h"
    29 #include "haptic/SDL_haptic_c.h"
    30 #include "joystick/SDL_joystick_c.h"
    31 
    32 #if !SDL_VIDEO_DISABLED
    33 #include "video/SDL_leaks.h"
    34 #endif
    35 
    36 /* Initialization/Cleanup routines */
    37 #if !SDL_TIMERS_DISABLED
    38 extern void SDL_StartTicks(void);
    39 extern int SDL_TimerInit(void);
    40 extern void SDL_TimerQuit(void);
    41 #endif
    42 #if defined(__WIN32__)
    43 extern int SDL_HelperWindowCreate(void);
    44 extern int SDL_HelperWindowDestroy(void);
    45 #endif
    46 
    47 
    48 /* The initialized subsystems */
    49 static Uint32 SDL_initialized = 0;
    50 static Uint32 ticks_started = 0;
    51 
    52 #ifdef CHECK_LEAKS
    53 int surfaces_allocated = 0;
    54 #endif
    55 
    56 int
    57 SDL_InitSubSystem(Uint32 flags)
    58 {
    59 #if !SDL_VIDEO_DISABLED
    60     /* Initialize the video/event subsystem */
    61     if ((flags & SDL_INIT_VIDEO) && !(SDL_initialized & SDL_INIT_VIDEO)) {
    62         if (SDL_VideoInit(NULL, (flags & SDL_INIT_EVENTTHREAD)) < 0) {
    63             return (-1);
    64         }
    65         SDL_initialized |= SDL_INIT_VIDEO;
    66     }
    67 #else
    68     if (flags & SDL_INIT_VIDEO) {
    69         SDL_SetError("SDL not built with video support");
    70         return (-1);
    71     }
    72 #endif
    73 
    74 #if !SDL_AUDIO_DISABLED
    75     /* Initialize the audio subsystem */
    76     if ((flags & SDL_INIT_AUDIO) && !(SDL_initialized & SDL_INIT_AUDIO)) {
    77         if (SDL_AudioInit(NULL) < 0) {
    78             return (-1);
    79         }
    80         SDL_initialized |= SDL_INIT_AUDIO;
    81     }
    82 #else
    83     if (flags & SDL_INIT_AUDIO) {
    84         SDL_SetError("SDL not built with audio support");
    85         return (-1);
    86     }
    87 #endif
    88 
    89 #if !SDL_TIMERS_DISABLED
    90     /* Initialize the timer subsystem */
    91     if (!ticks_started) {
    92         SDL_StartTicks();
    93         ticks_started = 1;
    94     }
    95     if ((flags & SDL_INIT_TIMER) && !(SDL_initialized & SDL_INIT_TIMER)) {
    96         if (SDL_TimerInit() < 0) {
    97             return (-1);
    98         }
    99         SDL_initialized |= SDL_INIT_TIMER;
   100     }
   101 #else
   102     if (flags & SDL_INIT_TIMER) {
   103         SDL_SetError("SDL not built with timer support");
   104         return (-1);
   105     }
   106 #endif
   107 
   108 #if !SDL_JOYSTICK_DISABLED
   109     /* Initialize the joystick subsystem */
   110     if ((flags & SDL_INIT_JOYSTICK) && !(SDL_initialized & SDL_INIT_JOYSTICK)) {
   111         if (SDL_JoystickInit() < 0) {
   112             return (-1);
   113         }
   114         SDL_initialized |= SDL_INIT_JOYSTICK;
   115     }
   116 #else
   117     if (flags & SDL_INIT_JOYSTICK) {
   118         SDL_SetError("SDL not built with joystick support");
   119         return (-1);
   120     }
   121 #endif
   122 
   123 #if !SDL_HAPTIC_DISABLED
   124     /* Initialize the haptic subsystem */
   125     if ((flags & SDL_INIT_HAPTIC) && !(SDL_initialized & SDL_INIT_HAPTIC)) {
   126         if (SDL_HapticInit() < 0) {
   127             return (-1);
   128         }
   129         SDL_initialized |= SDL_INIT_HAPTIC;
   130     }
   131 #else
   132     if (flags & SDL_INIT_HAPTIC) {
   133         SDL_SetError("SDL not built with haptic (force feedback) support");
   134         return (-1);
   135     }
   136 #endif
   137     return (0);
   138 }
   139 
   140 int
   141 SDL_Init(Uint32 flags)
   142 {
   143     if (SDL_AssertionsInit() < 0) {
   144         return -1;
   145     }
   146 
   147     /* Clear the error message */
   148     SDL_ClearError();
   149 
   150 #if defined(__WIN32__)
   151     if (SDL_HelperWindowCreate() < 0) {
   152         return -1;
   153     }
   154 #endif
   155 
   156     /* Initialize the desired subsystems */
   157     if (SDL_InitSubSystem(flags) < 0) {
   158         return (-1);
   159     }
   160 
   161     /* Everything is initialized */
   162     if (!(flags & SDL_INIT_NOPARACHUTE)) {
   163         SDL_InstallParachute();
   164     }
   165 
   166     return (0);
   167 }
   168 
   169 void
   170 SDL_QuitSubSystem(Uint32 flags)
   171 {
   172     /* Shut down requested initialized subsystems */
   173 #if !SDL_JOYSTICK_DISABLED
   174     if ((flags & SDL_initialized & SDL_INIT_JOYSTICK)) {
   175         SDL_JoystickQuit();
   176         SDL_initialized &= ~SDL_INIT_JOYSTICK;
   177     }
   178 #endif
   179 #if !SDL_HAPTIC_DISABLED
   180     if ((flags & SDL_initialized & SDL_INIT_HAPTIC)) {
   181         SDL_HapticQuit();
   182         SDL_initialized &= ~SDL_INIT_HAPTIC;
   183     }
   184 #endif
   185 #if !SDL_TIMERS_DISABLED
   186     if ((flags & SDL_initialized & SDL_INIT_TIMER)) {
   187         SDL_TimerQuit();
   188         SDL_initialized &= ~SDL_INIT_TIMER;
   189     }
   190 #endif
   191 #if !SDL_AUDIO_DISABLED
   192     if ((flags & SDL_initialized & SDL_INIT_AUDIO)) {
   193         SDL_AudioQuit();
   194         SDL_initialized &= ~SDL_INIT_AUDIO;
   195     }
   196 #endif
   197 #if !SDL_VIDEO_DISABLED
   198     if ((flags & SDL_initialized & SDL_INIT_VIDEO)) {
   199         SDL_VideoQuit();
   200         SDL_initialized &= ~SDL_INIT_VIDEO;
   201     }
   202 #endif
   203 }
   204 
   205 Uint32
   206 SDL_WasInit(Uint32 flags)
   207 {
   208     if (!flags) {
   209         flags = SDL_INIT_EVERYTHING;
   210     }
   211     return (SDL_initialized & flags);
   212 }
   213 
   214 void
   215 SDL_Quit(void)
   216 {
   217     /* Quit all subsystems */
   218 #ifdef DEBUG_BUILD
   219     printf("[SDL_Quit] : Enter! Calling QuitSubSystem()\n");
   220     fflush(stdout);
   221 #endif
   222 
   223 #if defined(__WIN32__)
   224     SDL_HelperWindowDestroy();
   225 #endif
   226     SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
   227 
   228 #ifdef CHECK_LEAKS
   229 #ifdef DEBUG_BUILD
   230     printf("[SDL_Quit] : CHECK_LEAKS\n");
   231     fflush(stdout);
   232 #endif
   233 
   234     /* !!! FIXME: make this an assertion. */
   235     /* Print the number of surfaces not freed */
   236     if (surfaces_allocated != 0) {
   237         fprintf(stderr, "SDL Warning: %d SDL surfaces extant\n",
   238                 surfaces_allocated);
   239     }
   240 #endif
   241 #ifdef DEBUG_BUILD
   242     printf("[SDL_Quit] : SDL_UninstallParachute()\n");
   243     fflush(stdout);
   244 #endif
   245 
   246     /* Uninstall any parachute signal handlers */
   247     SDL_UninstallParachute();
   248 
   249     SDL_AssertionsQuit();
   250 
   251 #ifdef DEBUG_BUILD
   252     printf("[SDL_Quit] : Returning!\n");
   253     fflush(stdout);
   254 #endif
   255 
   256 }
   257 
   258 /* Get the library version number */
   259 void
   260 SDL_GetVersion(SDL_version * ver)
   261 {
   262     SDL_VERSION(ver);
   263 }
   264 
   265 /* Get the library source revision */
   266 const char *
   267 SDL_GetRevision(void)
   268 {
   269     return SDL_REVISION;
   270 }
   271 
   272 /* Get the name of the platform */
   273 const char *
   274 SDL_GetPlatform()
   275 {
   276 #if __AIX__
   277     return "AIX";
   278 #elif __HAIKU__
   279 /* Haiku must appear here before BeOS, since it also defines __BEOS__ */
   280     return "Haiku";
   281 #elif __BEOS__
   282     return "BeOS";
   283 #elif __BSDI__
   284     return "BSDI";
   285 #elif __DREAMCAST__
   286     return "Dreamcast";
   287 #elif __FREEBSD__
   288     return "FreeBSD";
   289 #elif __HPUX__
   290     return "HP-UX";
   291 #elif __IRIX__
   292     return "Irix";
   293 #elif __LINUX__
   294     return "Linux";
   295 #elif __MINT__
   296     return "Atari MiNT";
   297 #elif __MACOS__
   298     return "MacOS Classic";
   299 #elif __MACOSX__
   300     return "Mac OS X";
   301 #elif __NETBSD__
   302     return "NetBSD";
   303 #elif __OPENBSD__
   304     return "OpenBSD";
   305 #elif __OS2__
   306     return "OS/2";
   307 #elif __OSF__
   308     return "OSF/1";
   309 #elif __QNXNTO__
   310     return "QNX Neutrino";
   311 #elif __RISCOS__
   312     return "RISC OS";
   313 #elif __SOLARIS__
   314     return "Solaris";
   315 #elif __WIN32__
   316 #ifdef _WIN32_WCE
   317     return "Windows CE";
   318 #else
   319     return "Windows";
   320 #endif
   321 #elif __IPHONEOS__
   322     return "iPhone OS";
   323 #else
   324     return "Unknown (see SDL_platform.h)";
   325 #endif
   326 }
   327 
   328 #if defined(__WIN32__)
   329 
   330 #if !defined(HAVE_LIBC) || (defined(__WATCOMC__) && defined(BUILD_DLL))
   331 /* Need to include DllMain() on Watcom C for some reason.. */
   332 #include "core/windows/SDL_windows.h"
   333 
   334 BOOL APIENTRY
   335 _DllMainCRTStartup(HANDLE hModule,
   336                    DWORD ul_reason_for_call, LPVOID lpReserved)
   337 {
   338     switch (ul_reason_for_call) {
   339     case DLL_PROCESS_ATTACH:
   340     case DLL_THREAD_ATTACH:
   341     case DLL_THREAD_DETACH:
   342     case DLL_PROCESS_DETACH:
   343         break;
   344     }
   345     return TRUE;
   346 }
   347 #endif /* building DLL with Watcom C */
   348 
   349 #endif /* __WIN32__ */
   350 
   351 /* vi: set ts=4 sw=4 expandtab: */