src/SDL.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 05 Mar 2002 19:55:32 +0000
changeset 292 eadc0746dfaf
parent 252 e8157fcb3114
child 297 f6ffac90895c
permissions -rw-r--r--
Added SDL_LockRect() and SDL_UnlockRect()
Incorporated XFree86 extension libraries into the source
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Library General Public
     7     License as published by the Free Software Foundation; either
     8     version 2 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     Library General Public License for more details.
    14 
    15     You should have received a copy of the GNU Library General Public
    16     License along with this library; if not, write to the Free
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 #ifdef SAVE_RCSID
    24 static char rcsid =
    25  "@(#) $Id$";
    26 #endif
    27 
    28 /* Initialization code for SDL */
    29 
    30 #include <stdlib.h>		/* For getenv() */
    31 
    32 #include "SDL.h"
    33 #include "SDL_endian.h"
    34 #include "SDL_fatal.h"
    35 #ifndef DISABLE_VIDEO
    36 #include "SDL_leaks.h"
    37 #endif
    38 
    39 /* Initialization/Cleanup routines */
    40 #ifndef DISABLE_JOYSTICK
    41 extern int  SDL_JoystickInit(void);
    42 extern void SDL_JoystickQuit(void);
    43 #endif
    44 #ifndef DISABLE_CDROM
    45 extern int  SDL_CDROMInit(void);
    46 extern void SDL_CDROMQuit(void);
    47 #endif
    48 #ifndef DISABLE_TIMERS
    49 extern void SDL_StartTicks(void);
    50 extern int  SDL_TimerInit(void);
    51 extern void SDL_TimerQuit(void);
    52 #endif
    53 
    54 /* The current SDL version */
    55 static SDL_version version = 
    56 	{ SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL };
    57 
    58 /* The initialized subsystems */
    59 static Uint32 SDL_initialized = 0;
    60 static Uint32 ticks_started = 0;
    61 
    62 #ifdef CHECK_LEAKS
    63 int surfaces_allocated = 0;
    64 #endif
    65 
    66 int SDL_InitSubSystem(Uint32 flags)
    67 {
    68 #ifndef DISABLE_VIDEO
    69 	/* Initialize the video/event subsystem */
    70 	if ( (flags & SDL_INIT_VIDEO) && !(SDL_initialized & SDL_INIT_VIDEO) ) {
    71 		if ( SDL_VideoInit(getenv("SDL_VIDEODRIVER"),
    72 		                   (flags&SDL_INIT_EVENTTHREAD)) < 0 ) {
    73 			return(-1);
    74 		}
    75 		SDL_initialized |= SDL_INIT_VIDEO;
    76 	}
    77 #else
    78 	if ( flags & SDL_INIT_VIDEO ) {
    79 		SDL_SetError("SDL not built with video support");
    80 		return(-1);
    81 	}
    82 #endif
    83 
    84 #ifndef DISABLE_AUDIO
    85 	/* Initialize the audio subsystem */
    86 	if ( (flags & SDL_INIT_AUDIO) && !(SDL_initialized & SDL_INIT_AUDIO) ) {
    87 		if ( SDL_AudioInit(getenv("SDL_AUDIODRIVER")) < 0 ) {
    88 			return(-1);
    89 		}
    90 		SDL_initialized |= SDL_INIT_AUDIO;
    91 	}
    92 #else
    93 	if ( flags & SDL_INIT_AUDIO ) {
    94 		SDL_SetError("SDL not built with audio support");
    95 		return(-1);
    96 	}
    97 #endif
    98 
    99 #ifndef DISABLE_TIMERS
   100 	/* Initialize the timer subsystem */
   101 	if ( ! ticks_started ) {
   102 		SDL_StartTicks();
   103 		ticks_started = 1;
   104 	}
   105 	if ( (flags & SDL_INIT_TIMER) && !(SDL_initialized & SDL_INIT_TIMER) ) {
   106 		if ( SDL_TimerInit() < 0 ) {
   107 			return(-1);
   108 		}
   109 		SDL_initialized |= SDL_INIT_TIMER;
   110 	}
   111 #else
   112 	if ( flags & SDL_INIT_TIMER ) {
   113 		SDL_SetError("SDL not built with timer support");
   114 		return(-1);
   115 	}
   116 #endif
   117 
   118 #ifndef DISABLE_JOYSTICK
   119 	/* Initialize the joystick subsystem */
   120 	if ( (flags & SDL_INIT_JOYSTICK) &&
   121 	     !(SDL_initialized & SDL_INIT_JOYSTICK) ) {
   122 		if ( SDL_JoystickInit() < 0 ) {
   123 			return(-1);
   124 		}
   125 		SDL_initialized |= SDL_INIT_JOYSTICK;
   126 	}
   127 #else
   128 	if ( flags & SDL_INIT_JOYSTICK ) {
   129 		SDL_SetError("SDL not built with joystick support");
   130 		return(-1);
   131 	}
   132 #endif
   133 
   134 #ifndef DISABLE_CDROM
   135 	/* Initialize the CD-ROM subsystem */
   136 	if ( (flags & SDL_INIT_CDROM) && !(SDL_initialized & SDL_INIT_CDROM) ) {
   137 		if ( SDL_CDROMInit() < 0 ) {
   138 			return(-1);
   139 		}
   140 		SDL_initialized |= SDL_INIT_CDROM;
   141 	}
   142 #else
   143 	if ( flags & SDL_INIT_CDROM ) {
   144 		SDL_SetError("SDL not built with cdrom support");
   145 		return(-1);
   146 	}
   147 #endif
   148 	return(0);
   149 }
   150 
   151 int SDL_Init(Uint32 flags)
   152 {
   153 	/* Clear the error message */
   154 	SDL_ClearError();
   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 	return(0);
   166 }
   167 
   168 void SDL_QuitSubSystem(Uint32 flags)
   169 {
   170 	/* Shut down requested initialized subsystems */
   171 #ifndef DISABLE_CDROM
   172 	if ( (flags & SDL_initialized & SDL_INIT_CDROM) ) {
   173 		SDL_CDROMQuit();
   174 		SDL_initialized &= ~SDL_INIT_CDROM;
   175 	}
   176 #endif
   177 #ifndef DISABLE_JOYSTICK
   178 	if ( (flags & SDL_initialized & SDL_INIT_JOYSTICK) ) {
   179 		SDL_JoystickQuit();
   180 		SDL_initialized &= ~SDL_INIT_JOYSTICK;
   181 	}
   182 #endif
   183 #ifndef DISABLE_TIMERS
   184 	if ( (flags & SDL_initialized & SDL_INIT_TIMER) ) {
   185 		SDL_TimerQuit();
   186 		SDL_initialized &= ~SDL_INIT_TIMER;
   187 	}
   188 #endif
   189 #ifndef DISABLE_AUDIO
   190 	if ( (flags & SDL_initialized & SDL_INIT_AUDIO) ) {
   191 		SDL_AudioQuit();
   192 		SDL_initialized &= ~SDL_INIT_AUDIO;
   193 	}
   194 #endif
   195 #ifndef DISABLE_VIDEO
   196 	if ( (flags & SDL_initialized & SDL_INIT_VIDEO) ) {
   197 		SDL_VideoQuit();
   198 		SDL_initialized &= ~SDL_INIT_VIDEO;
   199 	}
   200 #endif
   201 }
   202 
   203 Uint32 SDL_WasInit(Uint32 flags)
   204 {
   205 	if ( ! flags ) {
   206 		flags = SDL_INIT_EVERYTHING;
   207 	}
   208 	return (SDL_initialized&flags);
   209 }
   210 
   211 void SDL_Quit(void)
   212 {
   213 	/* Quit all subsystems */
   214 	SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
   215 
   216 #ifdef CHECK_LEAKS
   217 	/* Print the number of surfaces not freed */
   218 	if ( surfaces_allocated != 0 ) {
   219 		fprintf(stderr, "SDL Warning: %d SDL surfaces extant\n", 
   220 							surfaces_allocated);
   221 	}
   222 #endif
   223 
   224 	/* Uninstall any parachute signal handlers */
   225 	SDL_UninstallParachute();
   226 }
   227 
   228 /* Return the library version number */
   229 const SDL_version * SDL_Linked_Version(void)
   230 {
   231 	return(&version);
   232 }
   233 
   234 #if defined(_WIN32_WCE)
   235 /* Need to include DllMain() on Windows CE for some reason.. */
   236 #include <windows.h>
   237 
   238 BOOL APIENTRY DllMain( HANDLE hModule, 
   239                        DWORD  ul_reason_for_call, 
   240                        LPVOID lpReserved )
   241 {
   242 	switch (ul_reason_for_call) {
   243 		case DLL_PROCESS_ATTACH:
   244 		case DLL_THREAD_ATTACH:
   245 		case DLL_THREAD_DETACH:
   246 		case DLL_PROCESS_DETACH:
   247 			break;
   248 	}
   249 	return TRUE;
   250 }
   251 #endif /* _WIN32_WCE */