dungeon_crawler
diff prototype/src/audio/source.cc @ 48:aa9e28670ae2
added sound playback, more to do
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 17 Sep 2012 08:40:59 +0300 |
parents | |
children | d57df51f6b50 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/prototype/src/audio/source.cc Mon Sep 17 08:40:59 2012 +0300 1.3 @@ -0,0 +1,73 @@ 1.4 +#include "openal.h" 1.5 +#include "source.h" 1.6 + 1.7 +AudioSource::AudioSource() 1.8 +{ 1.9 + sample = 0; 1.10 + 1.11 + alGenSources(1, &alsrc); 1.12 + alSourcei(alsrc, AL_LOOPING, AL_TRUE); 1.13 +} 1.14 + 1.15 +AudioSource::~AudioSource() 1.16 +{ 1.17 + if(alsrc) { 1.18 + if(is_playing()) { 1.19 + stop(); 1.20 + } 1.21 + alDeleteSources(1, &alsrc); 1.22 + } 1.23 +} 1.24 + 1.25 +void AudioSource::set_sample(const AudioSample *sample) 1.26 +{ 1.27 + stop(); 1.28 + 1.29 + if(sample) { 1.30 + if(!sample->albuffer) { 1.31 + fprintf(stderr, "%s: trying to attach null buffer!\n", __FUNCTION__); 1.32 + return; 1.33 + } 1.34 + alSourcei(alsrc, AL_BUFFER, sample->albuffer); 1.35 + } 1.36 + this->sample = sample; 1.37 +} 1.38 + 1.39 +const AudioSample *AudioSource::get_sample() const 1.40 +{ 1.41 + return sample; 1.42 +} 1.43 + 1.44 +void AudioSource::set_position(const Vector3 &pos, bool viewspace) 1.45 +{ 1.46 + alSourcei(alsrc, AL_SOURCE_RELATIVE, viewspace ? AL_TRUE : AL_FALSE); 1.47 + alSource3f(alsrc, AL_POSITION, pos.x, pos.y, pos.z); 1.48 +} 1.49 + 1.50 +Vector3 AudioSource::get_position() const 1.51 +{ 1.52 + float pos[3]; 1.53 + alGetSourcefv(alsrc, AL_POSITION, pos); 1.54 + return Vector3(pos[0], pos[1], pos[2]); 1.55 +} 1.56 + 1.57 +bool AudioSource::is_playing() const 1.58 +{ 1.59 + int state; 1.60 + alGetSourcei(alsrc, AL_SOURCE_STATE, &state); 1.61 + return state == AL_PLAYING; 1.62 +} 1.63 + 1.64 +void AudioSource::play() 1.65 +{ 1.66 + if(sample) { 1.67 + alSourcePlay(alsrc); 1.68 + } 1.69 +} 1.70 + 1.71 +void AudioSource::stop() 1.72 +{ 1.73 + if(sample) { 1.74 + alSourceStop(alsrc); 1.75 + } 1.76 +}