raylib_impl 0.1.0
Vendor raylib 5.5 : IGraphic2Module / IGraphic3Module / IAudioModule
Loading...
Searching...
No Matches
RayAudioModule.hpp
Go to the documentation of this file.
1
13#ifndef RAYAUDIO_MODULE_HPP
14#define RAYAUDIO_MODULE_HPP
15
16#include "IAudioModule.hpp"
17
18#include "audio/RayMusic.hpp"
19#include "audio/RaySound.hpp"
21
22//Raylib (without conflict)
23#include "RaylibAliases.hpp"
24
40class RayAudioModule : public IAudioModule {
41
42public:
43 RayAudioModule() = default;
44 ~RayAudioModule() { stop(); }
45
46 const char *type() const override { return IAudioModule::contract; }
47 const char *name() const override { return "raylib"; }
48
49 // music
50 audio::IMusic *createMusic(std::string path) override {
51 if (!start())
52 return nullptr;
53 _opened++;
54 return new RayMusic(path);
55 }
56 void deleteMusic(audio::IMusic *music) override { drop(music); }
57
58 // sound buffer
59 audio::ISoundBuffer *createSoundBuffer(std::string path) override {
60 if (!start())
61 return nullptr;
62 _opened++;
63 return new RaySoundBuffer(path);
64 }
65 void deleteSoundBuffer(audio::ISoundBuffer *buffer) override { drop(buffer); }
66
67 // sound
68 audio::ISound *createSound(audio::ISoundBuffer *buffer) override {
69 if (!buffer || !start())
70 return nullptr;
71 _opened++;
72 return new RaySound(*static_cast<RaySoundBuffer *>(buffer));
73 }
74 void deleteSound(audio::ISound *sound) override { drop(sound); }
75
76private:
78 bool start() {
79 if (_started)
80 return true;
81 raylib::InitAudioDevice();
82 /* On demande a raylib plutot que de croire l'appel : sans carte son
83 * disponible, InitAudioDevice ne dit rien mais ne prepare rien, et
84 * un LoadSound derriere travaillerait dans le vide. */
85 if (!raylib::IsAudioDeviceReady())
86 return false;
87 _started = true;
88 return true;
89 }
90
91 void stop() {
92 if (!_started)
93 return;
94 raylib::CloseAudioDevice();
95 _started = false;
96 }
97
99 template <typename T>
100 void drop(T *object) {
101 if (!object)
102 return;
103 delete object;
104 if (_opened > 0)
105 _opened--;
106 if (_opened == 0)
107 stop();
108 }
109
110 unsigned _opened = 0;
111 bool _started = false;
112};
113
116#endif /* !RAYAUDIO_MODULE_HPP */
L'ouverture du peripherique audio de raylib, comptee par reference.
Definition RayAudioModule.hpp:40
void deleteMusic(audio::IMusic *music) override
Definition RayAudioModule.hpp:56
audio::ISound * createSound(audio::ISoundBuffer *buffer) override
Definition RayAudioModule.hpp:68
~RayAudioModule()
Definition RayAudioModule.hpp:44
audio::IMusic * createMusic(std::string path) override
Definition RayAudioModule.hpp:50
const char * name() const override
Definition RayAudioModule.hpp:47
RayAudioModule()=default
const char * type() const override
Definition RayAudioModule.hpp:46
void deleteSound(audio::ISound *sound) override
Definition RayAudioModule.hpp:74
void deleteSoundBuffer(audio::ISoundBuffer *buffer) override
Definition RayAudioModule.hpp:65
audio::ISoundBuffer * createSoundBuffer(std::string path) override
Definition RayAudioModule.hpp:59
raylib music stream.
Definition RayMusic.hpp:28
Raylib SoundBuffer class - owns the loaded samples (raylib's own LoadSound() result)....
Definition RaySoundBuffer.hpp:25
Raylib Sound class - a LoadSoundAlias() of a RaySoundBuffer, does not own the buffer....
Definition RaySound.hpp:31