First pass at Ryan's assertion code, minor tweaks to come.
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 #include "SDL_assert.h"
30 #if !SDL_VIDEO_DISABLED
31 #include "video/SDL_leaks.h"
38 /* Initialization/Cleanup routines */
39 #if !SDL_JOYSTICK_DISABLED
40 extern int SDL_JoystickInit(void);
41 extern void SDL_JoystickQuit(void);
43 #if !SDL_HAPTIC_DISABLED
44 extern int SDL_HapticInit(void);
45 extern int SDL_HapticQuit(void);
47 #if !SDL_TIMERS_DISABLED
48 extern void SDL_StartTicks(void);
49 extern int SDL_TimerInit(void);
50 extern void SDL_TimerQuit(void);
52 #if defined(__WIN32__)
53 extern int SDL_HelperWindowCreate(void);
54 extern int SDL_HelperWindowDestroy(void);
57 extern int SDL_AssertionsInit(void);
58 extern void SDL_AssertionsQuit(void);
60 /* The initialized subsystems */
61 static Uint32 SDL_initialized = 0;
62 static Uint32 ticks_started = 0;
65 int surfaces_allocated = 0;
69 SDL_InitSubSystem(Uint32 flags)
71 #if !SDL_VIDEO_DISABLED
72 /* Initialize the video/event subsystem */
73 if ((flags & SDL_INIT_VIDEO) && !(SDL_initialized & SDL_INIT_VIDEO)) {
74 if (SDL_VideoInit(NULL, (flags & SDL_INIT_EVENTTHREAD)) < 0) {
77 SDL_initialized |= SDL_INIT_VIDEO;
80 if (flags & SDL_INIT_VIDEO) {
81 SDL_SetError("SDL not built with video support");
86 #if !SDL_AUDIO_DISABLED
87 /* Initialize the audio subsystem */
88 if ((flags & SDL_INIT_AUDIO) && !(SDL_initialized & SDL_INIT_AUDIO)) {
89 if (SDL_AudioInit(NULL) < 0) {
92 SDL_initialized |= SDL_INIT_AUDIO;
95 if (flags & SDL_INIT_AUDIO) {
96 SDL_SetError("SDL not built with audio support");
101 #if !SDL_TIMERS_DISABLED
102 /* Initialize the timer subsystem */
103 if (!ticks_started) {
107 if ((flags & SDL_INIT_TIMER) && !(SDL_initialized & SDL_INIT_TIMER)) {
108 if (SDL_TimerInit() < 0) {
111 SDL_initialized |= SDL_INIT_TIMER;
114 if (flags & SDL_INIT_TIMER) {
115 SDL_SetError("SDL not built with timer support");
120 #if !SDL_JOYSTICK_DISABLED
121 /* Initialize the joystick subsystem */
122 if ((flags & SDL_INIT_JOYSTICK) && !(SDL_initialized & SDL_INIT_JOYSTICK)) {
123 if (SDL_JoystickInit() < 0) {
126 SDL_initialized |= SDL_INIT_JOYSTICK;
129 if (flags & SDL_INIT_JOYSTICK) {
130 SDL_SetError("SDL not built with joystick support");
135 #if !SDL_HAPTIC_DISABLED
136 /* Initialize the haptic subsystem */
137 if ((flags & SDL_INIT_HAPTIC) && !(SDL_initialized & SDL_INIT_HAPTIC)) {
138 if (SDL_HapticInit() < 0) {
141 SDL_initialized |= SDL_INIT_HAPTIC;
144 if (flags & SDL_INIT_HAPTIC) {
145 SDL_SetError("SDL not built with haptic (force feedback) support");
153 SDL_Init(Uint32 flags)
155 #if !SDL_THREADS_DISABLED && SDL_THREAD_PTH
161 if (SDL_AssertionsInit() < 0) {
165 /* Clear the error message */
168 #if defined(__WIN32__)
169 if (SDL_HelperWindowCreate() < 0) {
174 /* Initialize the desired subsystems */
175 if (SDL_InitSubSystem(flags) < 0) {
179 /* Everything is initialized */
180 if (!(flags & SDL_INIT_NOPARACHUTE)) {
181 SDL_InstallParachute();
184 /* brief sanity checks for the sanity checks. :) */
186 SDL_assert_release(1);
187 SDL_assert_paranoid(1);
189 SDL_assert_release(0 || 1);
190 SDL_assert_paranoid(0 || 1);
192 #if 0 /* enable this to test assertion failures. */
193 SDL_assert_release(1 == 2);
194 SDL_assert_release(5 < 4);
195 SDL_assert_release(0 && "This is a test");
202 SDL_QuitSubSystem(Uint32 flags)
204 /* Shut down requested initialized subsystems */
205 #if !SDL_JOYSTICK_DISABLED
206 if ((flags & SDL_initialized & SDL_INIT_JOYSTICK)) {
208 SDL_initialized &= ~SDL_INIT_JOYSTICK;
211 #if !SDL_HAPTIC_DISABLED
212 if ((flags & SDL_initialized & SDL_INIT_HAPTIC)) {
214 SDL_initialized &= ~SDL_INIT_HAPTIC;
217 #if !SDL_TIMERS_DISABLED
218 if ((flags & SDL_initialized & SDL_INIT_TIMER)) {
220 SDL_initialized &= ~SDL_INIT_TIMER;
223 #if !SDL_AUDIO_DISABLED
224 if ((flags & SDL_initialized & SDL_INIT_AUDIO)) {
226 SDL_initialized &= ~SDL_INIT_AUDIO;
229 #if !SDL_VIDEO_DISABLED
230 if ((flags & SDL_initialized & SDL_INIT_VIDEO)) {
232 SDL_initialized &= ~SDL_INIT_VIDEO;
238 SDL_WasInit(Uint32 flags)
241 flags = SDL_INIT_EVERYTHING;
243 return (SDL_initialized & flags);
249 /* Quit all subsystems */
251 printf("[SDL_Quit] : Enter! Calling QuitSubSystem()\n");
255 #if defined(__WIN32__)
256 SDL_HelperWindowDestroy();
258 SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
262 printf("[SDL_Quit] : CHECK_LEAKS\n");
266 /* !!! FIXME: make this an assertion. */
267 /* Print the number of surfaces not freed */
268 if (surfaces_allocated != 0) {
269 fprintf(stderr, "SDL Warning: %d SDL surfaces extant\n",
274 printf("[SDL_Quit] : SDL_UninstallParachute()\n");
278 /* Uninstall any parachute signal handlers */
279 SDL_UninstallParachute();
281 SDL_AssertionsQuit();
283 #if !SDL_THREADS_DISABLED && SDL_THREAD_PTH
287 printf("[SDL_Quit] : Returning!\n");
293 /* Get the library version number */
295 SDL_GetVersion(SDL_version * ver)
300 /* Get the library source revision */
302 SDL_GetRevision(void)
307 /* Get the name of the platform */
314 /* Haiku must appear here before BeOS, since it also defines __BEOS__ */
333 return "MacOS Classic";
345 return "QNX Neutrino";
359 return "Unknown (see SDL_platform.h)";
363 #if defined(__WIN32__)
365 #if !defined(HAVE_LIBC) || (defined(__WATCOMC__) && defined(BUILD_DLL))
366 /* Need to include DllMain() on Watcom C for some reason.. */
367 #define WIN32_LEAN_AND_MEAN
371 _DllMainCRTStartup(HANDLE hModule,
372 DWORD ul_reason_for_call, LPVOID lpReserved)
374 switch (ul_reason_for_call) {
375 case DLL_PROCESS_ATTACH:
376 case DLL_THREAD_ATTACH:
377 case DLL_THREAD_DETACH:
378 case DLL_PROCESS_DETACH:
383 #endif /* building DLL with Watcom C */
385 #endif /* __WIN32__ */
387 /* vi: set ts=4 sw=4 expandtab: */