minecraftvulkan/engine/xe_sound.cpp

60 lines
1.1 KiB
C++
Raw Normal View History

2022-09-22 15:14:00 +00:00
#include "xe_sound.hpp"
namespace xe {
2022-09-25 01:16:13 +00:00
Sound::Sound(const std::string& filename) {
2022-09-22 15:14:00 +00:00
2022-09-22 15:29:32 +00:00
buffer = alutCreateBufferFromFile(filename.c_str());
2022-09-22 15:14:00 +00:00
alGenSources(1, &source);
2022-09-22 15:29:32 +00:00
2022-09-22 15:14:00 +00:00
alSourcef(source, AL_GAIN, 1.f);
alSourcef(source, AL_PITCH, 1.f);
alSource3f(source, AL_POSITION, 0, 0, 0);
alSource3f(source, AL_VELOCITY, 0, 0, 0);
alSourcei(source, AL_LOOPING, AL_FALSE);
alSourcei(source, AL_BUFFER, buffer);
}
2022-09-25 01:16:13 +00:00
Sound::~Sound() {
2022-09-22 15:14:00 +00:00
alDeleteSources(1, &source);
alDeleteBuffers(1, &buffer);
}
2022-09-25 01:16:13 +00:00
void Sound::play() {
2022-09-22 15:14:00 +00:00
stop();
alSourcePlay(source);
};
2022-09-25 01:16:13 +00:00
void Sound::stop() {
2022-09-22 15:14:00 +00:00
alSourceStop(source);
};
2022-09-25 01:16:13 +00:00
void Sound::pause() {
2022-09-22 15:14:00 +00:00
alSourcePause(source);
};
2022-09-25 01:16:13 +00:00
void Sound::resume() {
2022-09-22 15:14:00 +00:00
alSourcePlay(source);
};
2022-09-25 01:16:13 +00:00
bool Sound::isPlaying() {
2022-09-22 15:14:00 +00:00
ALint playing;
alGetSourcei(source, AL_SOURCE_STATE, &playing);
return playing == AL_PLAYING;
};
2022-09-25 01:16:13 +00:00
void Sound::setPosition(glm::vec3 position) {
2022-09-22 15:14:00 +00:00
alSource3f(source, AL_POSITION, position.x, position.y, position.z);
};
2022-09-25 01:16:13 +00:00
void Sound::setLooping(bool looping) {
2022-09-22 15:14:00 +00:00
alSourcei(source, AL_LOOPING, looping ? 1 : 0);
};
2022-09-25 01:16:13 +00:00
void Sound::setVolume(float volume) {
2022-09-22 17:36:03 +00:00
alSourcef(source, AL_GAIN, volume);
}
2022-09-22 15:14:00 +00:00
}