sfml_impl 0.1.0
Vendor SFML 3.0.2 : IGraphic2Module / IAudioModule
Loading...
Searching...
No Matches
SfmlMusic.hpp
Go to the documentation of this file.
1
11#ifndef SFMLMUSIC_HPP_
12#define SFMLMUSIC_HPP_
13
14//Sfml
15#include <SFML/Audio.hpp>
16
17//Interface
18#include "IMusic.hpp"
19
28class SfmlMusic : public audio::IMusic {
29
30 public:
31 SfmlMusic(std::string path) : _music(path) {
32 _music.setVolume(100.f);
33 }
34
35 ~SfmlMusic() override = default;
36
37 bool isReady() const override {
38 return true;
39 }
40
41 // play/pause/stop do ONLY what they say. sf::Music already carries
42 // the three states, so it is enough not to restart a live stream.
43 void play() override {
44 if (_music.getStatus() == sf::Music::Status::Playing)
45 return;
46 //resumes if paused, restarts from zero if stopped
47 _music.play();
48 }
49
50 void pause() override {
51 if (_music.getStatus() != sf::Music::Status::Playing)
52 return;
53 _music.pause();
54 }
55
56 void stop() override {
57 if (_music.getStatus() == sf::Music::Status::Stopped)
58 return;
59 _music.stop();
60 }
61
62 void update() override {
63 }
64
65 void setVolume(float volume) override {
66 _music.setVolume(volume * 100.f);
67 }
68
69 float getVolume() const override {
70 return _music.getVolume() * 0.01f;
71 }
72
73 void setLoop(bool loop) override {
74 _music.setLooping(loop);
75 }
76
77 bool getLoop() const override {
78 return _music.isLooping();
79 }
80
81 void setTime(float position) override {
82 _music.setPlayingOffset(sf::seconds(clampTime(position)));
83 }
84
85 float getTime() const override {
86 return _music.getPlayingOffset().asSeconds();
87 }
88
89 float getLength() const override {
90 return _music.getDuration().asSeconds();
91 }
92
93 //sfml (OpenAL) genuinely supports positional audio for streams too
94 void setPosition(Vector3f position) override {
95 _position = position;
96 _music.setPosition({static_cast<float>(position.x), static_cast<float>(position.y), static_cast<float>(position.z)});
97 }
98 Vector3f getPosition() const override {
99 return _position;
100 }
101
102 //sfml has no velocity/doppler API on sf::Music : graceful no-op
103 void setVelocity(Vector3f velocity) override {
104 _velocity = velocity;
105 }
106 Vector3f getVelocity() const override {
107 return _velocity;
108 }
109
110 private:
111 float clampTime(float position) const {
112 const float length = getLength();
113
114 if (position < 0.f) return 0.f;
115 return (position > length) ? length : position;
116 }
117
118 sf::Music _music;
119 Vector3f _position{};
120 Vector3f _velocity{};
121};
122
125#endif /* !SFMLMUSIC_HPP_ */
sfml music stream.
Definition SfmlMusic.hpp:28
void setVelocity(Vector3f velocity) override
Definition SfmlMusic.hpp:103
void setLoop(bool loop) override
Definition SfmlMusic.hpp:73
void pause() override
Definition SfmlMusic.hpp:50
~SfmlMusic() override=default
bool getLoop() const override
Definition SfmlMusic.hpp:77
Vector3f getVelocity() const override
Definition SfmlMusic.hpp:106
SfmlMusic(std::string path)
Definition SfmlMusic.hpp:31
float getTime() const override
Definition SfmlMusic.hpp:85
bool isReady() const override
Definition SfmlMusic.hpp:37
float getLength() const override
Definition SfmlMusic.hpp:89
Vector3f getPosition() const override
Definition SfmlMusic.hpp:98
void setPosition(Vector3f position) override
Definition SfmlMusic.hpp:94
void setTime(float position) override
Definition SfmlMusic.hpp:81
float getVolume() const override
Definition SfmlMusic.hpp:69
void stop() override
Definition SfmlMusic.hpp:56
void play() override
Definition SfmlMusic.hpp:43
void update() override
Definition SfmlMusic.hpp:62
void setVolume(float volume) override
Definition SfmlMusic.hpp:65