-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSdlGuard.cpp
More file actions
37 lines (28 loc) · 722 Bytes
/
Copy pathSdlGuard.cpp
File metadata and controls
37 lines (28 loc) · 722 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include "SdlGuard.h"
#define SDL_MAIN_HANDLED
#include "SDL2/SDL.h"
#include <stdexcept> // for std::runtime_error
namespace audio {
unsigned int SdlGuard::m_instanceCount{};
std::mutex SdlGuard::m_mtx{};
SdlGuard::SdlGuard()
{
std::lock_guard<std::mutex> lock(m_mtx);
if(m_instanceCount == 0U) {
// we are the first instance -> initialize
if(SDL_Init(SDL_INIT_AUDIO)) {
throw std::runtime_error(std::string("Failed to initialize SDL: ") + SDL_GetError());
}
}
++m_instanceCount;
}
SdlGuard::~SdlGuard()
{
std::lock_guard<std::mutex> lock(m_mtx);
--m_instanceCount;
if(m_instanceCount == 0U) {
// we are the last instance -> cleanup
SDL_Quit();
}
}
} // namespace audio