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