2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
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.
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.
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
22 #include "SDL_config.h"
24 /* Initialization code for SDL */
27 #include "SDL_fatal.h"
28 #if !SDL_VIDEO_DISABLED
29 #include "video/SDL_leaks.h"
36 /* Initialization/Cleanup routines */
37 #if !SDL_JOYSTICK_DISABLED
38 extern int SDL_JoystickInit(void);
39 extern void SDL_JoystickQuit(void);
41 #if !SDL_HAPTIC_DISABLED
42 extern int SDL_HapticInit(void);
43 extern int SDL_HapticQuit(void);
45 #if !SDL_TIMERS_DISABLED
46 extern void SDL_StartTicks(void);
47 extern int SDL_TimerInit(void);
48 extern void SDL_TimerQuit(void);
50 #if defined(__WIN32__)
51 extern int SDL_HelperWindowCreate(void);
52 extern int SDL_HelperWindowDestroy(void);
55 /* The initialized subsystems */
56 static Uint32 SDL_initialized = 0;
57 static Uint32 ticks_started = 0;
60 int surfaces_allocated = 0;
64 SDL_InitSubSystem(Uint32 flags)
66 #if !SDL_VIDEO_DISABLED
67 /* Initialize the video/event subsystem */
68 if ((flags & SDL_INIT_VIDEO) && !(SDL_initialized & SDL_INIT_VIDEO)) {
69 if (SDL_VideoInit(NULL, (flags & SDL_INIT_EVENTTHREAD)) < 0) {
72 SDL_initialized |= SDL_INIT_VIDEO;
75 if (flags & SDL_INIT_VIDEO) {
76 SDL_SetError("SDL not built with video support");
81 #if !SDL_AUDIO_DISABLED
82 /* Initialize the audio subsystem */
83 if ((flags & SDL_INIT_AUDIO) && !(SDL_initialized & SDL_INIT_AUDIO)) {
84 if (SDL_AudioInit(NULL) < 0) {
87 SDL_initialized |= SDL_INIT_AUDIO;
90 if (flags & SDL_INIT_AUDIO) {
91 SDL_SetError("SDL not built with audio support");
96 #if !SDL_TIMERS_DISABLED
97 /* Initialize the timer subsystem */
102 if ((flags & SDL_INIT_TIMER) && !(SDL_initialized & SDL_INIT_TIMER)) {
103 if (SDL_TimerInit() < 0) {
106 SDL_initialized |= SDL_INIT_TIMER;
109 if (flags & SDL_INIT_TIMER) {
110 SDL_SetError("SDL not built with timer support");
115 #if !SDL_JOYSTICK_DISABLED
116 /* Initialize the joystick subsystem */
117 if ((flags & SDL_INIT_JOYSTICK) && !(SDL_initialized & SDL_INIT_JOYSTICK)) {
118 if (SDL_JoystickInit() < 0) {
121 SDL_initialized |= SDL_INIT_JOYSTICK;
124 if (flags & SDL_INIT_JOYSTICK) {
125 SDL_SetError("SDL not built with joystick support");
130 #if !SDL_HAPTIC_DISABLED
131 /* Initialize the haptic subsystem */
132 if ((flags & SDL_INIT_HAPTIC) && !(SDL_initialized & SDL_INIT_HAPTIC)) {
133 if (SDL_HapticInit() < 0) {
136 SDL_initialized |= SDL_INIT_HAPTIC;
139 if (flags & SDL_INIT_HAPTIC) {
140 SDL_SetError("SDL not built with haptic (force feedback) support");
148 SDL_Init(Uint32 flags)
150 #if !SDL_THREADS_DISABLED && SDL_THREAD_PTH
156 /* Clear the error message */
159 #if defined(__WIN32__)
160 if (SDL_HelperWindowCreate() < 0) {
165 /* Initialize the desired subsystems */
166 if (SDL_InitSubSystem(flags) < 0) {
170 /* Everything is initialized */
171 if (!(flags & SDL_INIT_NOPARACHUTE)) {
172 SDL_InstallParachute();
178 SDL_QuitSubSystem(Uint32 flags)
180 /* Shut down requested initialized subsystems */
181 #if !SDL_JOYSTICK_DISABLED
182 if ((flags & SDL_initialized & SDL_INIT_JOYSTICK)) {
184 SDL_initialized &= ~SDL_INIT_JOYSTICK;
187 #if !SDL_HAPTIC_DISABLED
188 if ((flags & SDL_initialized & SDL_INIT_HAPTIC)) {
190 SDL_initialized &= ~SDL_INIT_HAPTIC;
193 #if !SDL_TIMERS_DISABLED
194 if ((flags & SDL_initialized & SDL_INIT_TIMER)) {
196 SDL_initialized &= ~SDL_INIT_TIMER;
199 #if !SDL_AUDIO_DISABLED
200 if ((flags & SDL_initialized & SDL_INIT_AUDIO)) {
202 SDL_initialized &= ~SDL_INIT_AUDIO;
205 #if !SDL_VIDEO_DISABLED
206 if ((flags & SDL_initialized & SDL_INIT_VIDEO)) {
208 SDL_initialized &= ~SDL_INIT_VIDEO;
214 SDL_WasInit(Uint32 flags)
217 flags = SDL_INIT_EVERYTHING;
219 return (SDL_initialized & flags);
225 /* Quit all subsystems */
227 printf("[SDL_Quit] : Enter! Calling QuitSubSystem()\n");
231 #if defined(__WIN32__)
232 SDL_HelperWindowDestroy();
234 SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
238 printf("[SDL_Quit] : CHECK_LEAKS\n");
242 /* Print the number of surfaces not freed */
243 if (surfaces_allocated != 0) {
244 fprintf(stderr, "SDL Warning: %d SDL surfaces extant\n",
249 printf("[SDL_Quit] : SDL_UninstallParachute()\n");
253 /* Uninstall any parachute signal handlers */
254 SDL_UninstallParachute();
256 #if !SDL_THREADS_DISABLED && SDL_THREAD_PTH
260 printf("[SDL_Quit] : Returning!\n");
266 /* Get the library version number */
268 SDL_GetVersion(SDL_version * ver)
273 /* Get the library source revision */
275 SDL_GetRevision(void)
280 #if defined(__WIN32__)
282 #if !defined(HAVE_LIBC) || (defined(__WATCOMC__) && defined(BUILD_DLL))
283 /* Need to include DllMain() on Watcom C for some reason.. */
284 #define WIN32_LEAN_AND_MEAN
288 _DllMainCRTStartup(HANDLE hModule,
289 DWORD ul_reason_for_call, LPVOID lpReserved)
291 switch (ul_reason_for_call) {
292 case DLL_PROCESS_ATTACH:
293 case DLL_THREAD_ATTACH:
294 case DLL_THREAD_DETACH:
295 case DLL_PROCESS_DETACH:
300 #endif /* building DLL with Watcom C */
302 #endif /* __WIN32__ */
304 /* vi: set ts=4 sw=4 expandtab: */