src/SDL.c
changeset 0 74212992fb08
child 36 13ee9f4834ea
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/SDL.c	Thu Apr 26 16:45:43 2001 +0000
     1.3 @@ -0,0 +1,233 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Library General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Library General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Library General Public
    1.19 +    License along with this library; if not, write to the Free
    1.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@devolution.com
    1.24 +*/
    1.25 +
    1.26 +#ifdef SAVE_RCSID
    1.27 +static char rcsid =
    1.28 + "@(#) $Id$";
    1.29 +#endif
    1.30 +
    1.31 +/* Initialization code for SDL */
    1.32 +
    1.33 +#include <stdlib.h>		/* For getenv() */
    1.34 +
    1.35 +#include "SDL.h"
    1.36 +#include "SDL_endian.h"
    1.37 +#include "SDL_fatal.h"
    1.38 +#ifndef DISABLE_VIDEO
    1.39 +#include "SDL_leaks.h"
    1.40 +#endif
    1.41 +
    1.42 +/* Initialization/Cleanup routines */
    1.43 +#ifndef DISABLE_JOYSTICK
    1.44 +extern int  SDL_JoystickInit(void);
    1.45 +extern void SDL_JoystickQuit(void);
    1.46 +#endif
    1.47 +#ifndef DISABLE_CDROM
    1.48 +extern int  SDL_CDROMInit(void);
    1.49 +extern void SDL_CDROMQuit(void);
    1.50 +#endif
    1.51 +#ifndef DISABLE_TIMERS
    1.52 +extern void SDL_StartTicks(void);
    1.53 +extern int  SDL_TimerInit(void);
    1.54 +extern void SDL_TimerQuit(void);
    1.55 +#endif
    1.56 +
    1.57 +/* The current SDL version */
    1.58 +static SDL_version version = 
    1.59 +	{ SDL_MAJOR_VERSION, SDL_MINOR_VERSION, SDL_PATCHLEVEL };
    1.60 +
    1.61 +/* The initialized subsystems */
    1.62 +static Uint32 SDL_initialized = 0;
    1.63 +static Uint32 ticks_started = 0;
    1.64 +
    1.65 +#ifdef CHECK_LEAKS
    1.66 +int surfaces_allocated = 0;
    1.67 +#endif
    1.68 +
    1.69 +int SDL_InitSubSystem(Uint32 flags)
    1.70 +{
    1.71 +#ifndef DISABLE_VIDEO
    1.72 +	/* Initialize the video/event subsystem */
    1.73 +	if ( (flags & SDL_INIT_VIDEO) && !(SDL_initialized & SDL_INIT_VIDEO) ) {
    1.74 +		if ( SDL_VideoInit(getenv("SDL_VIDEODRIVER"),
    1.75 +		                   (flags&SDL_INIT_EVENTTHREAD)) < 0 ) {
    1.76 +			return(-1);
    1.77 +		}
    1.78 +		SDL_initialized |= SDL_INIT_VIDEO;
    1.79 +	}
    1.80 +#else
    1.81 +	if ( flags & SDL_INIT_VIDEO ) {
    1.82 +		SDL_SetError("SDL not built with video support");
    1.83 +		return(-1);
    1.84 +	}
    1.85 +#endif
    1.86 +
    1.87 +#ifndef DISABLE_AUDIO
    1.88 +	/* Initialize the audio subsystem */
    1.89 +	if ( (flags & SDL_INIT_AUDIO) && !(SDL_initialized & SDL_INIT_AUDIO) ) {
    1.90 +		if ( SDL_AudioInit(getenv("SDL_AUDIODRIVER")) < 0 ) {
    1.91 +			return(-1);
    1.92 +		}
    1.93 +		SDL_initialized |= SDL_INIT_AUDIO;
    1.94 +	}
    1.95 +#else
    1.96 +	if ( flags & SDL_INIT_AUDIO ) {
    1.97 +		SDL_SetError("SDL not built with audio support");
    1.98 +		return(-1);
    1.99 +	}
   1.100 +#endif
   1.101 +
   1.102 +#ifndef DISABLE_TIMERS
   1.103 +	/* Initialize the timer subsystem */
   1.104 +	if ( ! ticks_started ) {
   1.105 +		SDL_StartTicks();
   1.106 +		ticks_started = 1;
   1.107 +	}
   1.108 +	if ( (flags & SDL_INIT_TIMER) && !(SDL_initialized & SDL_INIT_TIMER) ) {
   1.109 +		if ( SDL_TimerInit() < 0 ) {
   1.110 +			return(-1);
   1.111 +		}
   1.112 +		SDL_initialized |= SDL_INIT_TIMER;
   1.113 +	}
   1.114 +#else
   1.115 +	if ( flags & SDL_INIT_TIMER ) {
   1.116 +		SDL_SetError("SDL not built with timer support");
   1.117 +		return(-1);
   1.118 +	}
   1.119 +#endif
   1.120 +
   1.121 +#ifndef DISABLE_JOYSTICK
   1.122 +	/* Initialize the joystick subsystem */
   1.123 +	if ( (flags & SDL_INIT_JOYSTICK) &&
   1.124 +	     !(SDL_initialized & SDL_INIT_JOYSTICK) ) {
   1.125 +		if ( SDL_JoystickInit() < 0 ) {
   1.126 +			return(-1);
   1.127 +		}
   1.128 +		SDL_initialized |= SDL_INIT_JOYSTICK;
   1.129 +	}
   1.130 +#else
   1.131 +	if ( flags & SDL_INIT_JOYSTICK ) {
   1.132 +		SDL_SetError("SDL not built with joystick support");
   1.133 +		return(-1);
   1.134 +	}
   1.135 +#endif
   1.136 +
   1.137 +#ifndef DISABLE_CDROM
   1.138 +	/* Initialize the CD-ROM subsystem */
   1.139 +	if ( (flags & SDL_INIT_CDROM) && !(SDL_initialized & SDL_INIT_CDROM) ) {
   1.140 +		if ( SDL_CDROMInit() < 0 ) {
   1.141 +			return(-1);
   1.142 +		}
   1.143 +		SDL_initialized |= SDL_INIT_CDROM;
   1.144 +	}
   1.145 +#else
   1.146 +	if ( flags & SDL_INIT_CDROM ) {
   1.147 +		SDL_SetError("SDL not built with cdrom support");
   1.148 +		return(-1);
   1.149 +	}
   1.150 +#endif
   1.151 +	return(0);
   1.152 +}
   1.153 +
   1.154 +int SDL_Init(Uint32 flags)
   1.155 +{
   1.156 +	/* Clear the error message */
   1.157 +	SDL_ClearError();
   1.158 +
   1.159 +	/* Initialize the desired subsystems */
   1.160 +	if ( SDL_InitSubSystem(flags) < 0 ) {
   1.161 +		return(-1);
   1.162 +	}
   1.163 +
   1.164 +	/* Everything is initialized */
   1.165 +	if ( !(flags & SDL_INIT_NOPARACHUTE) ) {
   1.166 +		SDL_InstallParachute();
   1.167 +	}
   1.168 +	return(0);
   1.169 +}
   1.170 +
   1.171 +void SDL_QuitSubSystem(Uint32 flags)
   1.172 +{
   1.173 +	/* Shut down requested initialized subsystems */
   1.174 +#ifndef DISABLE_CDROM
   1.175 +	if ( (flags & SDL_initialized & SDL_INIT_CDROM) ) {
   1.176 +		SDL_CDROMQuit();
   1.177 +		SDL_initialized &= ~SDL_INIT_CDROM;
   1.178 +	}
   1.179 +#endif
   1.180 +#ifndef DISABLE_JOYSTICK
   1.181 +	if ( (flags & SDL_initialized & SDL_INIT_JOYSTICK) ) {
   1.182 +		SDL_JoystickQuit();
   1.183 +		SDL_initialized &= ~SDL_INIT_JOYSTICK;
   1.184 +	}
   1.185 +#endif
   1.186 +#ifndef DISABLE_TIMERS
   1.187 +	if ( (flags & SDL_initialized & SDL_INIT_TIMER) ) {
   1.188 +		SDL_TimerQuit();
   1.189 +		SDL_initialized &= ~SDL_INIT_TIMER;
   1.190 +	}
   1.191 +#endif
   1.192 +#ifndef DISABLE_AUDIO
   1.193 +	if ( (flags & SDL_initialized & SDL_INIT_AUDIO) ) {
   1.194 +		SDL_AudioQuit();
   1.195 +		SDL_initialized &= ~SDL_INIT_AUDIO;
   1.196 +	}
   1.197 +#endif
   1.198 +#ifndef DISABLE_VIDEO
   1.199 +	if ( (flags & SDL_initialized & SDL_INIT_VIDEO) ) {
   1.200 +		SDL_VideoQuit();
   1.201 +		SDL_initialized &= ~SDL_INIT_VIDEO;
   1.202 +	}
   1.203 +#endif
   1.204 +}
   1.205 +
   1.206 +Uint32 SDL_WasInit(Uint32 flags)
   1.207 +{
   1.208 +	if ( ! flags ) {
   1.209 +		flags = SDL_INIT_EVERYTHING;
   1.210 +	}
   1.211 +	return (SDL_initialized&flags);
   1.212 +}
   1.213 +
   1.214 +void SDL_Quit(void)
   1.215 +{
   1.216 +	/* Quit all subsystems */
   1.217 +	SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
   1.218 +
   1.219 +#ifdef CHECK_LEAKS
   1.220 +	/* Print the number of surfaces not freed */
   1.221 +	if ( surfaces_allocated != 0 ) {
   1.222 +		fprintf(stderr, "SDL Warning: %d SDL surfaces extant\n", 
   1.223 +							surfaces_allocated);
   1.224 +	}
   1.225 +#endif
   1.226 +
   1.227 +	/* Uninstall any parachute signal handlers */
   1.228 +	SDL_UninstallParachute();
   1.229 +}
   1.230 +
   1.231 +/* Return the library version number */
   1.232 +const SDL_version * SDL_Linked_Version(void)
   1.233 +{
   1.234 +	return(&version);
   1.235 +}
   1.236 +