Fawkes API  Fawkes Development Version
sdl_keeper.cpp
1 
2 /***************************************************************************
3  * sdl_keeper.cpp - utility to keep track of SDL initialization state
4  *
5  * Created: Mon Nov 05 14:34:36 2007
6  * Copyright 2007 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <fvwidgets/sdl_keeper.h>
25 
26 #include <core/threading/mutex.h>
27 #include <core/threading/mutex_locker.h>
28 #include <core/exception.h>
29 
30 #include <SDL.h>
31 
32 using namespace fawkes;
33 
34 namespace firevision {
35 #if 0 /* just to make Emacs auto-indent happy */
36 }
37 #endif
38 
39 unsigned int SDLKeeper::_refcount = 0;
40 Mutex SDLKeeper::_mutex;
41 
42 
43 /** @class SDLKeeper <fvwidgets/sdl_keeper.h>
44  * SDL Reference keeper.
45  *
46  * Use this keeper to initialize and quit the SDL library. As there may be many
47  * modules using the SDL a central place for reference counting is needed.
48  *
49  * @author Tim Niemueller
50  */
51 
52 /** Private inaccessible constructor. */
53 SDLKeeper::SDLKeeper()
54 {
55 }
56 
57 
58 /** Init SDL.
59  * Keeps track of SDL_Init calls and only calls SDL_InitSubSystem on consecutive
60  * calls.
61  * @param flags Same flags as for SDL_Init
62  */
63 void
64 SDLKeeper::init(unsigned int flags)
65 {
66  MutexLocker lock(&_mutex);
67 
68  unsigned int alive_subsys = SDL_WasInit(SDL_INIT_EVERYTHING);
69  if ( (alive_subsys & flags) != flags ) {
70  // Subsystem has not been initialized, yet
71  if ( _refcount == 0 ) {
72  if ( SDL_Init(flags) != 0 ) {
73  throw Exception("SDL: initialization failed");
74  }
75  } else {
76  unsigned int still_to_init = ~alive_subsys & flags;
77  if ( SDL_Init(still_to_init) != 0 ) {
78  throw Exception("SDL: initialization failed");
79  }
80  }
81  }
82 
83  ++_refcount;
84 }
85 
86 
87 /** Conditionally quit SDL.
88  * Use this after you are done with the SDL. No subsystem will be closed after all
89  * users of SDL quit the usage. Then the whole SDL will be released at once.
90  */
91 void
92 SDLKeeper::quit() throw()
93 {
94  MutexLocker lock(&_mutex);
95 
96  if ( (_refcount > 0) && (--_refcount == 0) ) {
97  SDL_Quit();
98  }
99 }
100 
101 
102 /** Force quit of SDL.
103  * This will quit the SDL no matter of the reference count. Use with extreme care.
104  */
105 void
106 SDLKeeper::force_quit()
107 {
108  MutexLocker lock(&_mutex);
109 
110  SDL_Quit();
111  _refcount = 0;
112 }
113 
114 } // end namespace firevision
Fawkes library namespace.
Mutex locking helper.
Definition: mutex_locker.h:33
Base class for exceptions in Fawkes.
Definition: exception.h:36
Mutex mutual exclusion lock.
Definition: mutex.h:32