src/SDL.c
changeset 6690 9548c8a58103
parent 6441 ada3545fed23
child 6866 e74a4b282450
equal deleted inserted replaced
6689:e2fc3ae07582 6690:9548c8a58103
    42 
    42 
    43 
    43 
    44 /* The initialized subsystems */
    44 /* The initialized subsystems */
    45 static Uint32 SDL_initialized = 0;
    45 static Uint32 SDL_initialized = 0;
    46 static Uint32 ticks_started = 0;
    46 static Uint32 ticks_started = 0;
    47 
    47 static SDL_bool SDL_bInMainQuit = SDL_FALSE;
       
    48 static Uint8 SDL_SubsystemRefCount[ 32 ]; // keep a per subsystem init
       
    49 
       
    50 /* helper func to return the index of the MSB in an int */
       
    51 int msb32_idx( Uint32 n)
       
    52 {
       
    53 	int b = 0;
       
    54 	if (!n) return -1;
       
    55 
       
    56 #define step(x) if (n >= ((Uint32)1) << x) b += x, n >>= x
       
    57 	step(16); step(8); step(4); step(2); step(1);
       
    58 #undef step
       
    59 	return b;
       
    60 }
    48 
    61 
    49 int
    62 int
    50 SDL_InitSubSystem(Uint32 flags)
    63 SDL_InitSubSystem(Uint32 flags)
    51 {
    64 {
    52 #if !SDL_TIMERS_DISABLED
    65 #if !SDL_TIMERS_DISABLED
    53     /* Initialize the timer subsystem */
    66     /* Initialize the timer subsystem */
    54     if (!ticks_started) {
    67     if (!ticks_started) {
    55         SDL_StartTicks();
    68         SDL_StartTicks();
    56         ticks_started = 1;
    69         ticks_started = 1;
    57     }
    70     }
    58     if ((flags & SDL_INIT_TIMER) && !(SDL_initialized & SDL_INIT_TIMER)) {
    71 
    59         if (SDL_TimerInit() < 0) {
    72     if ((flags & SDL_INIT_TIMER) ){
       
    73 		SDL_SubsystemRefCount[ msb32_idx(SDL_INIT_TIMER) ]++;
       
    74 		SDL_assert( SDL_SubsystemRefCount[ msb32_idx(SDL_INIT_TIMER) ] < 254 );
       
    75 		if ( !(SDL_initialized & SDL_INIT_TIMER)) {
       
    76 			if (SDL_TimerInit() < 0) {
       
    77 				return (-1);
       
    78 			}
       
    79 			SDL_initialized |= SDL_INIT_TIMER;
       
    80 		}
       
    81     }
       
    82 #else
       
    83     if (flags & SDL_INIT_TIMER) {
       
    84         SDL_SetError("SDL not built with timer support");
       
    85         return (-1);
       
    86     }
       
    87 #endif
       
    88 
       
    89 #if !SDL_VIDEO_DISABLED
       
    90     /* Initialize the video/event subsystem */
       
    91     if ((flags & SDL_INIT_VIDEO) ) {
       
    92 		SDL_SubsystemRefCount[ msb32_idx(SDL_INIT_VIDEO) ]++;
       
    93 		SDL_assert( SDL_SubsystemRefCount[ msb32_idx(SDL_INIT_VIDEO) ] < 254 );
       
    94 		if ( !(SDL_initialized & SDL_INIT_VIDEO)) {
       
    95 			if (SDL_VideoInit(NULL) < 0) {
       
    96 				return (-1);
       
    97 			}
       
    98 			SDL_initialized |= SDL_INIT_VIDEO;
       
    99 		}
       
   100     }
       
   101 #else
       
   102     if (flags & SDL_INIT_VIDEO) {
       
   103         SDL_SetError("SDL not built with video support");
       
   104         return (-1);
       
   105     }
       
   106 #endif
       
   107 
       
   108 #if !SDL_AUDIO_DISABLED
       
   109     /* Initialize the audio subsystem */
       
   110     if ((flags & SDL_INIT_AUDIO) ) {
       
   111 		SDL_SubsystemRefCount[ msb32_idx(SDL_INIT_AUDIO) ]++;
       
   112 		SDL_assert( SDL_SubsystemRefCount[ msb32_idx(SDL_INIT_AUDIO) ] < 254 );
       
   113 		if ( !(SDL_initialized & SDL_INIT_AUDIO)) {
       
   114 			if (SDL_AudioInit(NULL) < 0) {
       
   115 				return (-1);
       
   116 			}
       
   117 			SDL_initialized |= SDL_INIT_AUDIO;
       
   118 		}
       
   119     }
       
   120 #else
       
   121     if (flags & SDL_INIT_AUDIO) {
       
   122         SDL_SetError("SDL not built with audio support");
       
   123         return (-1);
       
   124     }
       
   125 #endif
       
   126 
       
   127 #if !SDL_JOYSTICK_DISABLED
       
   128     /* Initialize the joystick subsystem */
       
   129     if ( ( (flags & SDL_INIT_JOYSTICK)  ) || ((flags & SDL_INIT_GAMECONTROLLER) ) ) { // game controller implies joystick
       
   130 		SDL_SubsystemRefCount[ msb32_idx(SDL_INIT_JOYSTICK) ]++;
       
   131 		SDL_assert( SDL_SubsystemRefCount[ msb32_idx(SDL_INIT_JOYSTICK) ] < 254 );
       
   132         if ( !(SDL_initialized & SDL_INIT_JOYSTICK) && SDL_JoystickInit() < 0) {
    60             return (-1);
   133             return (-1);
    61         }
   134         }
    62         SDL_initialized |= SDL_INIT_TIMER;
   135 
    63     }
   136 		if ((flags & SDL_INIT_GAMECONTROLLER) ) {
    64 #else
   137 			SDL_SubsystemRefCount[ msb32_idx(SDL_INIT_GAMECONTROLLER) ]++;
    65     if (flags & SDL_INIT_TIMER) {
   138 			SDL_assert( SDL_SubsystemRefCount[ msb32_idx(SDL_INIT_GAMECONTROLLER) ] < 254 );
    66         SDL_SetError("SDL not built with timer support");
   139 			if ( !(SDL_initialized & SDL_INIT_GAMECONTROLLER)) {
    67         return (-1);
   140 				if (SDL_GameControllerInit() < 0) {
    68     }
   141 					return (-1);
    69 #endif
   142 				}
    70 
   143 				SDL_initialized |= SDL_INIT_GAMECONTROLLER;
    71 #if !SDL_VIDEO_DISABLED
   144 			}
    72     /* Initialize the video/event subsystem */
   145 		}
    73     if ((flags & SDL_INIT_VIDEO) && !(SDL_initialized & SDL_INIT_VIDEO)) {
       
    74         if (SDL_VideoInit(NULL) < 0) {
       
    75             return (-1);
       
    76         }
       
    77         SDL_initialized |= SDL_INIT_VIDEO;
       
    78     }
       
    79 #else
       
    80     if (flags & SDL_INIT_VIDEO) {
       
    81         SDL_SetError("SDL not built with video support");
       
    82         return (-1);
       
    83     }
       
    84 #endif
       
    85 
       
    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) {
       
    90             return (-1);
       
    91         }
       
    92         SDL_initialized |= SDL_INIT_AUDIO;
       
    93     }
       
    94 #else
       
    95     if (flags & SDL_INIT_AUDIO) {
       
    96         SDL_SetError("SDL not built with audio support");
       
    97         return (-1);
       
    98     }
       
    99 #endif
       
   100 
       
   101 #if !SDL_JOYSTICK_DISABLED
       
   102     /* Initialize the joystick subsystem */
       
   103     if ((flags & SDL_INIT_JOYSTICK) && !(SDL_initialized & SDL_INIT_JOYSTICK)) {
       
   104         if (SDL_JoystickInit() < 0) {
       
   105             return (-1);
       
   106         }
       
   107         SDL_initialized |= SDL_INIT_JOYSTICK;
   146         SDL_initialized |= SDL_INIT_JOYSTICK;
   108     }
   147     }
   109 #else
   148 #else
   110     if (flags & SDL_INIT_JOYSTICK) {
   149     if (flags & SDL_INIT_JOYSTICK) {
   111         SDL_SetError("SDL not built with joystick support");
   150         SDL_SetError("SDL not built with joystick support");
   113     }
   152     }
   114 #endif
   153 #endif
   115 
   154 
   116 #if !SDL_HAPTIC_DISABLED
   155 #if !SDL_HAPTIC_DISABLED
   117     /* Initialize the haptic subsystem */
   156     /* Initialize the haptic subsystem */
   118     if ((flags & SDL_INIT_HAPTIC) && !(SDL_initialized & SDL_INIT_HAPTIC)) {
   157     if ((flags & SDL_INIT_HAPTIC) ) {
   119         if (SDL_HapticInit() < 0) {
   158 		SDL_SubsystemRefCount[ msb32_idx(SDL_INIT_HAPTIC) ]++;
   120             return (-1);
   159 		SDL_assert( SDL_SubsystemRefCount[ msb32_idx(SDL_INIT_HAPTIC) ] < 254 );
   121         }
   160 		if ( !(SDL_initialized & SDL_INIT_HAPTIC)) {
   122         SDL_initialized |= SDL_INIT_HAPTIC;
   161 			if (SDL_HapticInit() < 0) {
       
   162 				return (-1);
       
   163 			}
       
   164 			SDL_initialized |= SDL_INIT_HAPTIC;
       
   165 		}
   123     }
   166     }
   124 #else
   167 #else
   125     if (flags & SDL_INIT_HAPTIC) {
   168     if (flags & SDL_INIT_HAPTIC) {
   126         SDL_SetError("SDL not built with haptic (force feedback) support");
   169         SDL_SetError("SDL not built with haptic (force feedback) support");
   127         return (-1);
   170         return (-1);
   154     /* Everything is initialized */
   197     /* Everything is initialized */
   155     if (!(flags & SDL_INIT_NOPARACHUTE)) {
   198     if (!(flags & SDL_INIT_NOPARACHUTE)) {
   156         SDL_InstallParachute();
   199         SDL_InstallParachute();
   157     }
   200     }
   158 
   201 
       
   202 	SDL_memset( SDL_SubsystemRefCount, 0x0, sizeof(SDL_SubsystemRefCount) );
   159     return (0);
   203     return (0);
   160 }
   204 }
   161 
   205 
   162 void
   206 void
   163 SDL_QuitSubSystem(Uint32 flags)
   207 SDL_QuitSubSystem(Uint32 flags)
   164 {
   208 {
   165     /* Shut down requested initialized subsystems */
   209     /* Shut down requested initialized subsystems */
   166 #if !SDL_JOYSTICK_DISABLED
   210 #if !SDL_JOYSTICK_DISABLED
   167     if ((flags & SDL_initialized & SDL_INIT_JOYSTICK)) {
   211     if ((flags & SDL_initialized & SDL_INIT_JOYSTICK) || (flags & SDL_initialized & SDL_INIT_GAMECONTROLLER)) {
   168         SDL_JoystickQuit();
   212 		if ( (flags & SDL_initialized & SDL_INIT_GAMECONTROLLER) ) {
   169         SDL_initialized &= ~SDL_INIT_JOYSTICK;
   213 			SDL_SubsystemRefCount[ msb32_idx(SDL_INIT_GAMECONTROLLER) ]--;
       
   214 			if ( SDL_bInMainQuit || SDL_SubsystemRefCount[ msb32_idx(SDL_INIT_GAMECONTROLLER) ] == 0 ) {
       
   215 				SDL_GameControllerQuit();
       
   216 				SDL_initialized &= ~SDL_INIT_GAMECONTROLLER;
       
   217 			}
       
   218 		}
       
   219 
       
   220 		SDL_SubsystemRefCount[ msb32_idx(SDL_INIT_JOYSTICK) ]--;
       
   221 		if ( SDL_bInMainQuit || SDL_SubsystemRefCount[ msb32_idx(SDL_INIT_JOYSTICK) ] == 0 )
       
   222 		{
       
   223 			SDL_JoystickQuit();
       
   224 			SDL_initialized &= ~SDL_INIT_JOYSTICK;
       
   225 		}
       
   226 
   170     }
   227     }
   171 #endif
   228 #endif
   172 #if !SDL_HAPTIC_DISABLED
   229 #if !SDL_HAPTIC_DISABLED
   173     if ((flags & SDL_initialized & SDL_INIT_HAPTIC)) {
   230     if ((flags & SDL_initialized & SDL_INIT_HAPTIC)) {
   174         SDL_HapticQuit();
   231 		SDL_SubsystemRefCount[ msb32_idx(SDL_INIT_HAPTIC) ]--;
   175         SDL_initialized &= ~SDL_INIT_HAPTIC;
   232 		if ( SDL_bInMainQuit || SDL_SubsystemRefCount[ msb32_idx(SDL_INIT_HAPTIC) ] == 0 )
       
   233 		{
       
   234 			SDL_HapticQuit();
       
   235 			SDL_initialized &= ~SDL_INIT_HAPTIC;
       
   236 		}
   176     }
   237     }
   177 #endif
   238 #endif
   178 #if !SDL_AUDIO_DISABLED
   239 #if !SDL_AUDIO_DISABLED
   179     if ((flags & SDL_initialized & SDL_INIT_AUDIO)) {
   240     if ((flags & SDL_initialized & SDL_INIT_AUDIO)) {
   180         SDL_AudioQuit();
   241 		SDL_SubsystemRefCount[ msb32_idx(SDL_INIT_AUDIO) ]--;
   181         SDL_initialized &= ~SDL_INIT_AUDIO;
   242 		if ( SDL_bInMainQuit || SDL_SubsystemRefCount[ msb32_idx(SDL_INIT_AUDIO) ] == 0 )
       
   243 		{
       
   244 			SDL_AudioQuit();
       
   245 			SDL_initialized &= ~SDL_INIT_AUDIO;
       
   246 		}
   182     }
   247     }
   183 #endif
   248 #endif
   184 #if !SDL_VIDEO_DISABLED
   249 #if !SDL_VIDEO_DISABLED
   185     if ((flags & SDL_initialized & SDL_INIT_VIDEO)) {
   250     if ((flags & SDL_initialized & SDL_INIT_VIDEO)) {
   186         SDL_VideoQuit();
   251 		SDL_SubsystemRefCount[ msb32_idx(SDL_INIT_VIDEO) ]--;
   187         SDL_initialized &= ~SDL_INIT_VIDEO;
   252 		if ( SDL_bInMainQuit || SDL_SubsystemRefCount[ msb32_idx(SDL_INIT_VIDEO) ] == 0 )
       
   253 		{
       
   254 			SDL_VideoQuit();
       
   255 			SDL_initialized &= ~SDL_INIT_VIDEO;
       
   256 		}
   188     }
   257     }
   189 #endif
   258 #endif
   190 #if !SDL_TIMERS_DISABLED
   259 #if !SDL_TIMERS_DISABLED
   191     if ((flags & SDL_initialized & SDL_INIT_TIMER)) {
   260     if ((flags & SDL_initialized & SDL_INIT_TIMER)) {
   192         SDL_TimerQuit();
   261 		SDL_SubsystemRefCount[ msb32_idx(SDL_INIT_TIMER) ]--;
   193         SDL_initialized &= ~SDL_INIT_TIMER;
   262 		if ( SDL_bInMainQuit || SDL_SubsystemRefCount[ msb32_idx(SDL_INIT_TIMER) ] == 0 )
       
   263 		{
       
   264 			SDL_TimerQuit();
       
   265 			SDL_initialized &= ~SDL_INIT_TIMER;
       
   266 		}
   194     }
   267     }
   195 #endif
   268 #endif
   196 }
   269 }
   197 
   270 
   198 Uint32
   271 Uint32
   205 }
   278 }
   206 
   279 
   207 void
   280 void
   208 SDL_Quit(void)
   281 SDL_Quit(void)
   209 {
   282 {
       
   283 	SDL_bInMainQuit = SDL_TRUE;
   210     /* Quit all subsystems */
   284     /* Quit all subsystems */
   211 #if defined(__WIN32__)
   285 #if defined(__WIN32__)
   212     SDL_HelperWindowDestroy();
   286     SDL_HelperWindowDestroy();
   213 #endif
   287 #endif
   214     SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
   288     SDL_QuitSubSystem(SDL_INIT_EVERYTHING);
   217     SDL_UninstallParachute();
   291     SDL_UninstallParachute();
   218 
   292 
   219     SDL_ClearHints();
   293     SDL_ClearHints();
   220     SDL_AssertionsQuit();
   294     SDL_AssertionsQuit();
   221     SDL_LogResetPriorities();
   295     SDL_LogResetPriorities();
       
   296 
       
   297 	SDL_memset( SDL_SubsystemRefCount, 0x0, sizeof(SDL_SubsystemRefCount) );
       
   298 	SDL_bInMainQuit = SDL_FALSE;
   222 }
   299 }
   223 
   300 
   224 /* Get the library version number */
   301 /* Get the library version number */
   225 void
   302 void
   226 SDL_GetVersion(SDL_version * ver)
   303 SDL_GetVersion(SDL_version * ver)