sdl2_impl 0.1.0
Vendor SDL2 2.32 : IGraphic2Module / IAudioModule
Loading...
Searching...
No Matches
SdlAudioModule.hpp
Go to the documentation of this file.
1
13#ifndef SDLAUDIO_MODULE_HPP
14#define SDLAUDIO_MODULE_HPP
15
16#include "IAudioModule.hpp"
17
18#include "audio/SdlMusic.hpp"
19#include "audio/SdlSound.hpp"
21
22#include <SDL.h>
23#include <SDL_mixer.h>
24
32class SdlAudioModule : public IAudioModule {
33
34public:
35 SdlAudioModule() = default;
36 ~SdlAudioModule() { stop(); }
37
38 const char *type() const override { return IAudioModule::contract; }
39 const char *name() const override { return "sdl2"; }
40
41 // music
42 audio::IMusic *createMusic(std::string path) override {
43 if (!start())
44 return nullptr;
45 _opened++;
46 return new SdlMusic(path);
47 }
48 void deleteMusic(audio::IMusic *music) override { drop(music); }
49
50 // sound buffer
51 audio::ISoundBuffer *createSoundBuffer(std::string path) override {
52 if (!start())
53 return nullptr;
54 _opened++;
55 return new SdlSoundBuffer(path);
56 }
57 void deleteSoundBuffer(audio::ISoundBuffer *buffer) override { drop(buffer); }
58
59 // sound
60 audio::ISound *createSound(audio::ISoundBuffer *buffer) override {
61 if (!buffer)
62 return nullptr;
63 _opened++;
64 return new SdlSound(*static_cast<SdlSoundBuffer *>(buffer));
65 }
66 void deleteSound(audio::ISound *sound) override { drop(sound); }
67
68private:
70 bool start() {
71 if (_started)
72 return true;
73 if (SDL_InitSubSystem(SDL_INIT_AUDIO) != 0)
74 return false;
75 if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) != 0) {
76 SDL_QuitSubSystem(SDL_INIT_AUDIO);
77 return false;
78 }
79 _started = true;
80 return true;
81 }
82
83 void stop() {
84 if (!_started)
85 return;
86 Mix_CloseAudio();
87 SDL_QuitSubSystem(SDL_INIT_AUDIO);
88 _started = false;
89 }
90
92 template <typename T>
93 void drop(T *object) {
94 if (!object)
95 return;
96 delete object;
97 if (_opened > 0)
98 _opened--;
99 if (_opened == 0)
100 stop();
101 }
102
103 unsigned _opened = 0;
104 bool _started = false;
105};
106
109#endif /* !SDLAUDIO_MODULE_HPP */
SDL_mixer derriere IAudioModule.
Definition SdlAudioModule.hpp:32
~SdlAudioModule()
Definition SdlAudioModule.hpp:36
audio::IMusic * createMusic(std::string path) override
Definition SdlAudioModule.hpp:42
void deleteSoundBuffer(audio::ISoundBuffer *buffer) override
Definition SdlAudioModule.hpp:57
SdlAudioModule()=default
audio::ISoundBuffer * createSoundBuffer(std::string path) override
Definition SdlAudioModule.hpp:51
void deleteSound(audio::ISound *sound) override
Definition SdlAudioModule.hpp:66
const char * type() const override
Definition SdlAudioModule.hpp:38
audio::ISound * createSound(audio::ISoundBuffer *buffer) override
Definition SdlAudioModule.hpp:60
void deleteMusic(audio::IMusic *music) override
Definition SdlAudioModule.hpp:48
const char * name() const override
Definition SdlAudioModule.hpp:39
Un flux long, decode au fil de la lecture.
Definition SdlMusic.hpp:32
Un echantillon decode en memoire, partageable par plusieurs sons.
Definition SdlSoundBuffer.hpp:30
Une lecture d'un tampon, sur son propre canal.
Definition SdlSound.hpp:29