P-E-R-R-Y
Utilitaires, contrats et vendors du moteur, en une seule doc
Loading...
Searching...
No Matches
RayMusic.hpp
Go to the documentation of this file.
1
10
11#ifndef RAYMUSIC_HPP_
12#define RAYMUSIC_HPP_
13
14//Raylib (without conflict)
15#include "RaylibAliases.hpp"
16
17//Interface
18#include "IMusic.hpp"
19
28class RayMusic : public audio::IMusic {
29
30 public:
31 RayMusic(std::string path) {
32 _music = raylib::LoadMusicStream(path.c_str());
33 _state = STOPPED;
34 _loop = false;
35 _volume = 1.0f;
36 }
37
38 ~RayMusic() override {
39 raylib::UnloadMusicStream(_music);
40 }
41
42 bool isReady() const override {
43 return raylib::IsMusicValid(_music);
44 }
45
46 // play/pause/stop do ONLY what they say : from PLAYING, play() has
47 // nothing to do. raylib has no readable "paused" state
48 // (IsMusicStreamPlaying is false there just as when stopped), so we
49 // track it ourselves.
50 void play() override {
51 if (_state == PLAYING)
52 return;
53 if (_state == PAUSED)
54 raylib::ResumeMusicStream(_music);
55 else
56 raylib::PlayMusicStream(_music);
57 _state = PLAYING;
58 }
59
60 void pause() override {
61 if (_state != PLAYING)
62 return;
63 raylib::PauseMusicStream(_music);
64 _state = PAUSED;
65 }
66
67 void stop() override {
68 if (_state == STOPPED)
69 return;
70 raylib::StopMusicStream(_music);
71 _state = STOPPED;
72 }
73
74 void update() override {
75 raylib::UpdateMusicStream(_music);
76
77 // raylib loops on its own : without loop it is up to us to stop
78 // at the end, which leaves the state of a stop()
79 if (!_loop && _state == PLAYING &&
80 raylib::GetMusicTimePlayed(_music) >= raylib::GetMusicTimeLength(_music)) {
81 this->stop();
82 }
83 }
84
85 void setVolume(float volume) override {
86 _volume = volume;
87 raylib::SetMusicVolume(_music, volume);
88 }
89
90 float getVolume() const override {
91 return _volume;
92 }
93
94 void setLoop(bool loop) override {
95 _loop = loop;
96 }
97
98 bool getLoop() const override {
99 return _loop;
100 }
101
102 void setTime(float position) override {
103 // raylib hands the raw position to dr_wav, which wraps past the
104 // end : clamp to honour the contract
105 raylib::SeekMusicStream(_music, clampTime(position));
106 }
107
108 float getTime() const override {
109 return raylib::GetMusicTimePlayed(_music);
110 }
111
112 float getLength() const override {
113 return raylib::GetMusicTimeLength(_music);
114 }
115
116 //raylib has no positional audio for music streams either : same
117 //graceful no-op as RaySound
118 void setPosition(Vector3f position) override {
119 _position = position;
120 }
121 Vector3f getPosition() const override {
122 return _position;
123 }
124
125 void setVelocity(Vector3f velocity) override {
126 _velocity = velocity;
127 }
128 Vector3f getVelocity() const override {
129 return _velocity;
130 }
131
132 private:
133 float clampTime(float position) const {
134 const float length = getLength();
135
136 if (position < 0.f) return 0.f;
137 return (position > length) ? length : position;
138 }
139
140 enum State { STOPPED, PLAYING, PAUSED };
141
142 raylib::Music _music;
143 State _state;
144 bool _loop;
145 float _volume;
146 Vector3f _position{};
147 Vector3f _velocity{};
148};
149
151
152#endif /* !RAYMUSIC_HPP_ */
void pause() override
Suspends playback without touching the position.
Definition RayMusic.hpp:60
float getTime() const override
Get the current time of the music in seconds.
Definition RayMusic.hpp:108
Vector3f getVelocity() const override
get the velocity of the music
Definition RayMusic.hpp:128
void setLoop(bool loop) override
set the music to loop or not
Definition RayMusic.hpp:94
void stop() override
Stops playback AND rewinds to zero.
Definition RayMusic.hpp:67
bool isReady() const override
Definition RayMusic.hpp:42
void setVelocity(Vector3f velocity) override
set the velocity of the music, used by backends that apply a doppler effect
Definition RayMusic.hpp:125
void play() override
Starts playback.
Definition RayMusic.hpp:50
void setTime(float position) override
Set the time of the music in seconds.
Definition RayMusic.hpp:102
void update() override
Must be called every frame.
Definition RayMusic.hpp:74
float getVolume() const override
get the volume of the music
Definition RayMusic.hpp:90
void setPosition(Vector3f position) override
set the 3D position of the music, for backends that spatialize against a listener....
Definition RayMusic.hpp:118
void setVolume(float volume) override
set the volume of the music
Definition RayMusic.hpp:85
RayMusic(std::string path)
Definition RayMusic.hpp:31
float getLength() const override
Get the Length of the music.
Definition RayMusic.hpp:112
bool getLoop() const override
get if the music is looping or not
Definition RayMusic.hpp:98
~RayMusic() override
Definition RayMusic.hpp:38
Vector3f getPosition() const override
get the 3D position of the music
Definition RayMusic.hpp:121
Definition IMusic.hpp:19
Vector3< double > Vector3f
Definition Vector3.hpp:121