sfml_impl 0.1.0
Vendor SFML 3.0.2 : IGraphic2Module / IAudioModule
Loading...
Searching...
No Matches
SfmlSound.hpp
Go to the documentation of this file.
1
11#ifndef SFMLSOUND_HPP_
12#define SFMLSOUND_HPP_
13
14//Sfml
15#include <SFML/Audio.hpp>
16
17//Interface
18#include "ISound.hpp"
19
20//encapsulation
21#include "SfmlSoundBuffer.hpp"
22
27class SfmlSound : public audio::ISound {
28
29 public:
30 SfmlSound(SfmlSoundBuffer &buffer) : _buffer(buffer), _sound(buffer.handle()) {
31 _sound.setVolume(100.f);
32 }
33
34 ~SfmlSound() override = default;
35
36 bool isReady() const override {
37 return _buffer.isReady();
38 }
39
40 void play() override {
41 //sfml already restarts a sound that's already playing - matches
42 //the contract's documented play() semantics natively
43 _sound.play();
44 }
45
46 void pause() override {
47 _sound.pause();
48 }
49
50 void stop() override {
51 _sound.stop();
52 }
53
54 void setVolume(float volume) override {
55 _sound.setVolume(volume * 100.f);
56 }
57
58 float getVolume() const override {
59 return _sound.getVolume() * 0.01f;
60 }
61
62 //sfml (OpenAL) genuinely supports positional audio, unlike raylib
63 void setPosition(Vector3f position) override {
64 _position = position;
65 _sound.setPosition({static_cast<float>(position.x), static_cast<float>(position.y), static_cast<float>(position.z)});
66 }
67 Vector3f getPosition() const override {
68 return _position;
69 }
70
71 //sfml has no velocity/doppler API on sf::Sound : graceful no-op
72 void setVelocity(Vector3f velocity) override {
73 _velocity = velocity;
74 }
75 Vector3f getVelocity() const override {
76 return _velocity;
77 }
78
79 private:
80 SfmlSoundBuffer &_buffer;
81 sf::Sound _sound;
82 Vector3f _position{};
83 Vector3f _velocity{};
84};
85
88#endif /* !SFMLSOUND_HPP_ */
Sfml SoundBuffer class - owns the loaded samples. SfmlSound only references it (sf::Sound itself alre...
Definition SfmlSoundBuffer.hpp:28
bool isReady() const override
Definition SfmlSoundBuffer.hpp:41
Sfml Sound class - references a SfmlSoundBuffer, does not own it. Deleting the sound never touches th...
Definition SfmlSound.hpp:27
void setVolume(float volume) override
Definition SfmlSound.hpp:54
Vector3f getPosition() const override
Definition SfmlSound.hpp:67
void setVelocity(Vector3f velocity) override
Definition SfmlSound.hpp:72
void pause() override
Definition SfmlSound.hpp:46
void stop() override
Definition SfmlSound.hpp:50
SfmlSound(SfmlSoundBuffer &buffer)
Definition SfmlSound.hpp:30
float getVolume() const override
Definition SfmlSound.hpp:58
bool isReady() const override
Definition SfmlSound.hpp:36
~SfmlSound() override=default
void setPosition(Vector3f position) override
Definition SfmlSound.hpp:63
void play() override
Definition SfmlSound.hpp:40
Vector3f getVelocity() const override
Definition SfmlSound.hpp:75