src/SDL.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 16 Feb 2011 02:37:09 -0800
changeset 5310 a9945ce48999
parent 5270 7aba0406c273
child 5359 d5fe4ed3a28d
permissions -rw-r--r--
Made it possible to build SDL from a fresh checkout without any additional steps.

The trick is that if you're using configure and you don't want to have SDL_config.h and SDL_revision.h to show up as modified, you need to configure and build from a separate directory.

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