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 <core/exception.h>
25#include <core/threading/mutex.h>
26#include <core/threading/mutex_locker.h>
27#include <fvwidgets/sdl_keeper.h>
28
29#include <SDL.h>
30
31using namespace fawkes;
32
33namespace firevision {
34
35unsigned int SDLKeeper::_refcount = 0;
36Mutex SDLKeeper::_mutex;
37
38/** @class SDLKeeper <fvwidgets/sdl_keeper.h>
39 * SDL Reference keeper.
40 *
41 * Use this keeper to initialize and quit the SDL library. As there may be many
42 * modules using the SDL a central place for reference counting is needed.
43 *
44 * @author Tim Niemueller
45 */
46
47/** Private inaccessible constructor. */
48SDLKeeper::SDLKeeper()
49{
50}
51
52/** Init SDL.
53 * Keeps track of SDL_Init calls and only calls SDL_InitSubSystem on consecutive
54 * calls.
55 * @param flags Same flags as for SDL_Init
56 */
57void
58SDLKeeper::init(unsigned int flags)
59{
60 MutexLocker lock(&_mutex);
61
62 unsigned int alive_subsys = SDL_WasInit(SDL_INIT_EVERYTHING);
63 if ((alive_subsys & flags) != flags) {
64 // Subsystem has not been initialized, yet
65 if (_refcount == 0) {
66 if (SDL_Init(flags) != 0) {
67 throw Exception("SDL: initialization failed");
68 }
69 } else {
70 unsigned int still_to_init = ~alive_subsys & flags;
71 if (SDL_Init(still_to_init) != 0) {
72 throw Exception("SDL: initialization failed");
73 }
74 }
75 }
76
77 ++_refcount;
78}
79
80/** Conditionally quit SDL.
81 * Use this after you are done with the SDL. No subsystem will be closed after all
82 * users of SDL quit the usage. Then the whole SDL will be released at once.
83 */
84void
86{
87 MutexLocker lock(&_mutex);
88
89 if ((_refcount > 0) && (--_refcount == 0)) {
90 SDL_Quit();
91 }
92}
93
94/** Force quit of SDL.
95 * This will quit the SDL no matter of the reference count. Use with extreme care.
96 */
97void
99{
100 MutexLocker lock(&_mutex);
101
102 SDL_Quit();
103 _refcount = 0;
104}
105
106} // end namespace firevision
Base class for exceptions in Fawkes.
Definition: exception.h:36
Mutex locking helper.
Definition: mutex_locker.h:34
Mutex mutual exclusion lock.
Definition: mutex.h:33
static void force_quit()
Force quit of SDL.
Definition: sdl_keeper.cpp:98
static void quit() noexcept
Conditionally quit SDL.
Definition: sdl_keeper.cpp:85
static void init(unsigned int flags)
Init SDL.
Definition: sdl_keeper.cpp:58
Fawkes library namespace.