9#include <smk/Audio.hpp>
10#include <smk/Sound.hpp>
14void Sound::EnsureSourceIsCreated() {
20 static bool once =
true;
23 <<
"Error: smk::Audio has not been initialized. Please create a "
24 "smk::Audio instance in the main() function before creating a "
31 alGenSources(1, &source_);
44 alDeleteSources(1, &source_);
50 if (!buffer_ || !buffer_->buffer()) {
56 EnsureSourceIsCreated();
57 alSourcei(source_, AL_BUFFER, ALint(buffer_->buffer()));
58 alSourcePlay(source_);
64 if (!source_ || !buffer_ || !is_playing_) {
67 alSourceStop(source_);
68 alSourcei(source_, AL_BUFFER, 0);
75 EnsureSourceIsCreated();
76 alSourcei(source_, AL_LOOPING, looping);
85 alGetSourcei(source_, AL_SOURCE_STATE, &state);
86 return (state == AL_PLAYING);
90 operator=(std::move(o));
93Sound& Sound::operator=(Sound&& o)
noexcept {
94 std::swap(buffer_, o.buffer_);
95 std::swap(source_, o.source_);
96 std::swap(is_playing_, o.is_playing_);
100void Sound::SetVolume(
float volume) {
104 EnsureSourceIsCreated();
105 alSourcef(source_, AL_GAIN, volume);
static bool Initialized()
A sound file loaded in memory.
Represent a sound being played.
void Stop()
Stop playing the sound.
void SetLoop(bool looping)
Specify whether the sound must restart when it has reached the end.
void Play()
Start playing the sound.
Sound()
Create a null Sound.