AaronCameron.net
No ads. No Profit. No Master, But Truth.
Not a Member? - Login or Create an Account
Thursday the 24th of May 2012 @ 01:07am
Front Page Journal Projects Your Profile About
[]

LibN2L-4 Library Code Reference

Classes
Compounds
Files
Members
Method Index
Full Reference

subSystemManager.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002 Nova-2 Library (libN2L, or simply n2l) Game development C++ Library
00003 Copyright (C) 2003  Aaron Cameron
00004 
00005 This library is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU Lesser General Public
00007 License as published by the Free Software Foundation; either
00008 version 2.1 of the License, or (at your option) any later version.
00009 
00010 This library is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 Lesser General Public License for more details.
00014 
00015 You should have received a copy of the GNU Lesser General Public
00016 License along with this library; if not, write to the Free Software
00017 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00018 
00019 A copy of the GNU Lesser General Public License has been provided with
00020 this library in the file 'COPYING'.
00021 
00022 Contact information for the author of this library has been provided
00023 with this library in the file 'AUTHOR'.
00024 ************************************************************************/
00025 #include "subSystemManager.h"
00026 
00027 #include "SDL.h"
00028 #include "SDL_net.h"
00029 #include "SDL_ttf.h"
00030 
00031 #include "cSDLException.h"
00032 
00033 /******************************************************************************/
00034 namespace n2l
00035 {
00036     
00037     /* ************************************************************ */
00038     void requireSDLSubSystem( const tSDLSubSystemInitFlag i_subSystemFlag )
00039     {
00040         tUint SDL_flag;
00041         tUint * const refCountPtr = _n2l::getSDLSubSystemReferenceCountPtr(
00042             i_subSystemFlag,SDL_flag);
00043         
00044         if (0 == refCountPtr)
00045             throw cException("requireSDLSubSystem","Ref count ptr was null");
00046         
00047         requireSDL();
00048 
00049         if (*refCountPtr) {
00050             ++(*refCountPtr);
00051             return;
00052          }
00053         if (-1 == SDL_InitSubSystem(SDL_flag)) 
00054             throw cSDLException(    "requireSDLSubSystem",
00055                                     "Failed to initalize subsystem" );
00056 
00057         ++(*refCountPtr);
00058     }
00059 
00060 
00061     /* ************************************************************ */
00062     void releaseSDLSubSystem( const tSDLSubSystemInitFlag i_subSystemFlag )
00063     {
00064         tUint SDL_flag;
00065         tUint * const refCountPtr = _n2l::getSDLSubSystemReferenceCountPtr(
00066             i_subSystemFlag,SDL_flag);
00067         if (0 == refCountPtr)
00068             throw cException("releaseSDLSubSystem","Ref count ptr was null");
00069 
00070         if (0==*refCountPtr)
00071             throw cException("releaseSDLSubSystem","Too many releases of "
00072                 "subsystem");
00073         --(*refCountPtr);
00074 
00075         
00076         if (0==*refCountPtr) {
00077             SDL_QuitSubSystem(SDL_flag);
00078         }
00079         
00080         releaseSDL();
00081     }
00082 
00083 
00084     /* ************************************************************ */
00085     void requireSDLNet()
00086     {
00087         tUint * const refCountPtr = _n2l::getSDLNetInitReferenceCountPtr();
00088         if (0 == refCountPtr)
00089             throw cException("requireSDLNet","Ref count ptr was null");
00090             
00091         requireSDL();
00092 
00093         if (*refCountPtr) {
00094             ++(*refCountPtr);
00095             return;
00096          }
00097 
00098         if (-1==SDLNet_Init())
00099             throw cDriverLayerException("requireSDLNet",
00100                                         "Failed to initalize system",
00101                                         "SDLNet", SDL_GetError());
00102         ++(*refCountPtr);
00103     }
00104 
00105 
00106     /* ************************************************************ */
00107     void releaseSDLNet()
00108     {
00109         tUint * const refCountPtr = _n2l::getSDLNetInitReferenceCountPtr();
00110         if (0 == refCountPtr)
00111             throw cException("releaseSDLNet","Ref count ptr was null");
00112         
00113         if (0==*refCountPtr)
00114             throw cException("releaseSDLNet","Too many release calls");
00115 
00116         --(*refCountPtr);
00117         
00118         if (0==*refCountPtr) SDLNet_Quit();
00119 
00120         releaseSDL();
00121     }
00122 
00123 
00124     /* ************************************************************ */
00125     void requireSDL()
00126     {
00127         tUint * const refCountPtr = _n2l::getSDLInitReferenceCountPtr();
00128         if (0 == refCountPtr)
00129             throw cException("requireSDL","Ref count ptr was null");
00130 
00131         if (*refCountPtr) {
00132             ++(*refCountPtr);
00133             return;
00134          }
00135 
00136         if (-1==SDL_Init(0))
00137             throw cSDLException(    "requireSDL",
00138                                     "Failed to initalize system" );
00139         ++(*refCountPtr);
00140     }
00141     
00142 
00143     /* ************************************************************ */
00144     void releaseSDL()
00145     {
00146         tUint * const refCountPtr = _n2l::getSDLInitReferenceCountPtr();
00147         if (0 == refCountPtr)
00148             throw cException("releaseSDL","Ref count ptr was null");
00149         
00150         if (0==*refCountPtr)
00151             throw cException("releaseSDL","Too many release calls");
00152 
00153         --(*refCountPtr);
00154 
00155         
00156         if (0==*refCountPtr) {
00157             SDL_Quit();
00158         }
00159     }
00160 
00161 
00162     /* ************************************************************ */
00163     void requireSDLTTF()
00164     {
00165         tUint * const refCountPtr = _n2l::getSDLTTFInitReferenceCountPtr();
00166         if (0 == refCountPtr)
00167             throw cException("requireSDLTTF","Ref count ptr was null");
00168             
00169         requireSDL();
00170 
00171         if (*refCountPtr) {
00172             ++(*refCountPtr);
00173             return;
00174          }
00175 
00176         if (-1==TTF_Init())
00177             throw cDriverLayerException("requireSDLTTF",
00178                                         "Failed to initalize system",
00179                                         "SDLTTF", SDL_GetError());
00180         ++(*refCountPtr);
00181     }
00182 
00183 
00184     /* ************************************************************ */
00185     void releaseSDLTTF()
00186     {
00187         tUint * const refCountPtr = _n2l::getSDLTTFInitReferenceCountPtr();
00188         if (0 == refCountPtr)
00189             throw cException("releaseSDLTTF","Ref count ptr was null");
00190         
00191         if (0==*refCountPtr)
00192             throw cException("releaseSDLTTF","Too many release calls");
00193 
00194         --(*refCountPtr);
00195         
00196         if (0==*refCountPtr) {
00197             TTF_Quit();
00198         }
00199         releaseSDL();
00200     }
00201 
00202 
00203 
00204 } // namespace n2l
00205 
00206 
00207 
00208 namespace _n2l
00209 {
00210 
00211     /* ************************************************************ */
00212     n2l::tUint * getSDLInitReferenceCountPtr()
00213     {
00214         static n2l::tUint sm_SDLInitRefCount = 0;
00215         return &sm_SDLInitRefCount;
00216     }
00217 
00218 
00219     /* ************************************************************ */
00220     n2l::tUint * getSDLNetInitReferenceCountPtr()
00221     {
00222         static n2l::tUint sm_SDLNetInitRefCount = 0;
00223         return &sm_SDLNetInitRefCount;
00224     }
00225 
00226 
00227     /* ************************************************************ */
00228     n2l::tUint * getSDLTTFInitReferenceCountPtr()
00229     {
00230         static n2l::tUint sm_SDLTTFInitRefCount = 0;
00231         return &sm_SDLTTFInitRefCount;
00232     }
00233 
00234 
00235     /* ************************************************************ */
00236     n2l::tUint * getSDLSubSystemReferenceCountPtr(
00237         const n2l::tSDLSubSystemInitFlag i_subSystemFlag,
00238         n2l::tUint & o_flag )
00239     {
00240         static n2l::tUint sm_SDLTimerRefCount = 0;
00241         static n2l::tUint sm_SDLAudioRefCount = 0;
00242         static n2l::tUint sm_SDLVideoRefCount = 0;
00243         static n2l::tUint sm_SDLJoystickRefCount = 0;
00244         static n2l::tUint sm_SDLCdromRefCount = 0;
00245         
00246         switch (i_subSystemFlag) {
00247             case n2l::SDLSubSystemInitFlag_Timer:
00248                 o_flag = SDL_INIT_TIMER;
00249                 return &sm_SDLTimerRefCount;
00250 
00251             case n2l::SDLSubSystemInitFlag_Audio:
00252                 o_flag = SDL_INIT_AUDIO;
00253                 return &sm_SDLAudioRefCount;
00254 
00255             case n2l::SDLSubSystemInitFlag_Video:
00256                 o_flag = SDL_INIT_VIDEO;
00257                 return &sm_SDLVideoRefCount;
00258 
00259             case n2l::SDLSubSystemInitFlag_Joystick:
00260                 o_flag = SDL_INIT_JOYSTICK;
00261                 return &sm_SDLJoystickRefCount;
00262 
00263             case n2l::SDLSubSystemInitFlag_Cdrom:
00264                 o_flag = SDL_INIT_CDROM;
00265                 return &sm_SDLCdromRefCount;
00266 
00267             default:
00268                 o_flag = 0;
00269                 return 0;
00270          }
00271         // It should never come to this, but...
00272         return 0;
00273     }
00274 
00275 } // namespace
©2012 Aaron Cameron