From: Charles McGarvey Date: Mon, 14 Sep 2009 00:18:10 +0000 (-0600) Subject: more featureful sound class X-Git-Url: https://git.brokenzipper.com/gitweb?a=commitdiff_plain;h=57b78ebe21b1b48acd337daa5a1cb8c383959cfa;p=chaz%2Fyoink more featureful sound class --- diff --git a/TODO b/TODO new file mode 100644 index 0000000..90c8f4d --- /dev/null +++ b/TODO @@ -0,0 +1,3 @@ + +The man page (doc/yoink.6) may have a list. + diff --git a/configure.ac b/configure.ac index 5a7791d..a87d981 100644 --- a/configure.ac +++ b/configure.ac @@ -83,30 +83,33 @@ AC_ARG_WITH([log-level], if test x$developer = xyes then debug=yes - profile=yes - extra_warnings=yes log_level=4 + + if test x$WIN32 != xyes + then + profile=yes + fi fi if test x$debug = xyes then - CFLAGS="$CFLAGS -Wall -O0 -g -DDEBUG" - CXXFLAGS="$CXXFLAGS -Wall -O0 -g -DDEBUG" + CFLAGS="$CFLAGS -Wall -O0 -gstabs -DDEBUG" + CXXFLAGS="$CXXFLAGS -Wall -O0 -gstabs -DDEBUG" else - CFLAGS="$CFLAGS -O2 -DNDEBUG" - CXXFLAGS="$CXXFLAGS -O2 -DNDEBUG" + CFLAGS="$CFLAGS -O2 -DNDEBUG" + CXXFLAGS="$CXXFLAGS -O2 -DNDEBUG" fi if test x$profile = xyes then - CFLAGS="$CFLAGS -pg" - CXXFLAGS="$CXXFLAGS -pg" + CFLAGS="$CFLAGS -pg" + CXXFLAGS="$CXXFLAGS -pg" fi if test x$extra_warnings = xyes then - CFLAGS="$CFLAGS -Wextra" - CXXFLAGS="$CXXFLAGS -Wextra" + CFLAGS="$CFLAGS -Wextra -Wno-unused-parameter" + CXXFLAGS="$CXXFLAGS -Wextra -Wno-unused-parameter" fi AC_DEFINE_UNQUOTED([YOINK_LOGLEVEL], [$log_level], diff --git a/data/sounds/NightFusionIntro.ogg b/data/sounds/NightFusionIntro.ogg new file mode 100644 index 0000000..785ad8a Binary files /dev/null and b/data/sounds/NightFusionIntro.ogg differ diff --git a/data/sounds/NightFusionLoop.ogg b/data/sounds/NightFusionLoop.ogg new file mode 100644 index 0000000..46a5b55 Binary files /dev/null and b/data/sounds/NightFusionLoop.ogg differ diff --git a/data/sounds/NightFusion.xm b/extra/NightFusion.xm similarity index 100% rename from data/sounds/NightFusion.xm rename to extra/NightFusion.xm diff --git a/extra/yoink.ebuild b/extra/yoink.ebuild index 6f7dd36..0f90316 100644 --- a/extra/yoink.ebuild +++ b/extra/yoink.ebuild @@ -19,7 +19,7 @@ IUSE="debug profile" RDEPEND="media-libs/freealut media-libs/libsdl[opengl] media-libs/sdl-image[png] - media-libs/sdl-sound[mikmod,vorbis] + media-libs/sdl-sound[vorbis] media-libs/openal virtual/opengl" DEPEND="${RDEPEND} diff --git a/extra/yoink.spec.in b/extra/yoink.spec.in index a1a433d..6c39987 100644 --- a/extra/yoink.spec.in +++ b/extra/yoink.spec.in @@ -12,10 +12,12 @@ BuildRequires: SDL_image-devel BuildRequires: SDL_sound-devel BuildRequires: mesa-libGL-devel BuildRequires: openal-devel +BuildRequires: freealut-devel Requires: SDL Requires: SDL_image Requires: SDL_sound Requires: openal +Requires: freealut %description Leap tall buildings! Crush stupid robots beneath your feet! Wield your extra-terrestrial powers in the defence of humanity, and send those alien diff --git a/src/Character.cc b/src/Character.cc index 411c032..1b184fa 100644 --- a/src/Character.cc +++ b/src/Character.cc @@ -26,6 +26,8 @@ *******************************************************************************/ +#include + #include "Character.hh" #include "Log.hh" @@ -125,7 +127,8 @@ void Character::handleEvent(const Mf::Event& event) break; } - Mf::logInfo("current force [%f %f]", current.force[0], current.force[1]); + //Mf::logInfo("current force [%f %f]", current.force[0], current.force[1]); + //std::cerr << "current force: " << current.force << std::endl; } diff --git a/src/Character.hh b/src/Character.hh index b72e8c8..5aeaeba 100644 --- a/src/Character.hh +++ b/src/Character.hh @@ -90,6 +90,11 @@ struct Character : public Mf::Entity Mf::Scalar mass; Mf::Scalar inverseMass; + void recalculate() + { + velocity = momentum * inverseMass; + } + void getDerivative(Derivative& derivative, Mf::Scalar t) const { @@ -99,11 +104,6 @@ struct Character : public Mf::Entity derivative.force = force; } - void recalculate() - { - velocity = momentum * inverseMass; - } - void applyDerivative(const Derivative& derivative, Mf::Scalar dt) { position += dt * derivative.velocity; @@ -133,7 +133,7 @@ struct Character : public Mf::Entity State previous; State current; - stlplus::ntree::iterator treeNode; + Mf::OctreeNodeP treeNode; private: @@ -164,7 +164,8 @@ public: }; -inline Character::State operator*(Mf::Scalar scalar, const Character::State& state) +inline Character::State operator*(Mf::Scalar scalar, + const Character::State& state) { Character::State newState = state; newState.position *= scalar; diff --git a/src/Hud.cc b/src/Hud.cc index ea0dfea..d5ec7bc 100644 --- a/src/Hud.cc +++ b/src/Hud.cc @@ -130,9 +130,9 @@ Hud::Hud() : void Hud::resize(int width, int height) { cml::matrix_orthographic_RH( projection_, - 0.0, - Mf::Scalar(width), 0.0, Mf::Scalar(height), - 1.0, -1.0, cml::z_clip_neg_one); + 0.0f, + Mf::Scalar(width), 0.0f, Mf::Scalar(height), + 1.0f, -1.0f, cml::z_clip_neg_one); // position the two progress bars at the top-left of the screen bar1_.resize(Mf::Rectangle(20, height - 51, diff --git a/src/Moof/Camera.hh b/src/Moof/Camera.hh index 72df5db..fae2507 100644 --- a/src/Moof/Camera.hh +++ b/src/Moof/Camera.hh @@ -46,7 +46,7 @@ public: Camera() : position_(0.0, 0.0, 0.0) { - cml::quaternion_rotation_world_y(rotation_, 0.0); + cml::quaternion_rotation_world_y(rotation_, 0.0f); calculateSecondary(); } diff --git a/src/Moof/Engine.cc b/src/Moof/Engine.cc index 2740652..51a3af0 100644 --- a/src/Moof/Engine.cc +++ b/src/Moof/Engine.cc @@ -27,7 +27,6 @@ *******************************************************************************/ #include // exit -#include #include #include @@ -35,7 +34,9 @@ #include #include +#include "Dispatcher.hh" #include "Engine.hh" +#include "Log.hh" #include "Random.hh" #include "Settings.hh" #include "Timer.hh" @@ -59,18 +60,17 @@ public: if (SDL_Init(SDL_INIT_EVERYTHING | SDL_INIT_EVENTTHREAD) != 0) #endif { - std::cerr << "sdl is complaining: " << SDL_GetError() << std::endl; + logError("sdl is complaining: %s", SDL_GetError()); throw Exception(Exception::SDL_ERROR); } if (FE_Init() != 0) { - std::cerr << "fast events error: " << FE_GetError() << std::endl; + logError("fast events error: %s", FE_GetError()); throw Exception(Exception::SDL_ERROR); } if (Sound_Init() == 0) { - std::cerr << "sound initialization failed: " << Sound_GetError() - << std::endl; + logError("sound initialization failed: %s", Sound_GetError()); throw Exception(Exception::SDL_ERROR); } alutInit(&argc, argv); @@ -174,7 +174,7 @@ public: if (printFps) { - std::cout << "FPS: " << fps << std::endl; + logInfo("framerate: %d fps", fps); } } diff --git a/src/Moof/Engine.hh b/src/Moof/Engine.hh index f088dc5..0617b1a 100644 --- a/src/Moof/Engine.hh +++ b/src/Moof/Engine.hh @@ -33,7 +33,6 @@ #include -#include #include #include #include diff --git a/src/Moof/Log.hh b/src/Moof/Log.hh index cdd5cd4..263ede8 100644 --- a/src/Moof/Log.hh +++ b/src/Moof/Log.hh @@ -37,7 +37,6 @@ */ #include // exit -#include // strerror /** diff --git a/src/Moof/Math.hh b/src/Moof/Math.hh index a38af8b..eb411e0 100644 --- a/src/Moof/Math.hh +++ b/src/Moof/Math.hh @@ -49,6 +49,8 @@ typedef cml::vector< Scalar, cml::fixed<2> > Vector2; typedef cml::vector< Scalar, cml::fixed<3> > Vector3; typedef cml::vector< Scalar, cml::fixed<4> > Vector4; +typedef cml::matrix< Scalar, cml::fixed<2,2>, + cml::col_basis, cml::col_major > Matrix2; typedef cml::matrix< Scalar, cml::fixed<3,3>, cml::col_basis, cml::col_major > Matrix3; typedef cml::matrix< Scalar, cml::fixed<4,4>, diff --git a/src/Moof/Mippleton.hh b/src/Moof/Mippleton.hh index e9b9732..cde68d2 100644 --- a/src/Moof/Mippleton.hh +++ b/src/Moof/Mippleton.hh @@ -51,8 +51,8 @@ namespace Mf { template class Mippleton { - typedef std::pair PtrValue; - typedef stlplus::hash PtrMap; + typedef std::pair PtrValue; + typedef stlplus::hash PtrMap; static PtrMap ptrs_; std::string name_; diff --git a/src/Moof/Octree.hh b/src/Moof/Octree.hh index 3614606..ade0a02 100644 --- a/src/Moof/Octree.hh +++ b/src/Moof/Octree.hh @@ -141,9 +141,9 @@ public: inline void print(OctreeNodeP node) { - logDebug("-----"); - logDebug("depth to node: %d", tree_.depth(node)); - logDebug("size of node: %d", tree_.size(node)); + //logDebug("-----"); + //logDebug("depth to node: %d", tree_.depth(node)); + //logDebug("size of node: %d", tree_.size(node)); } inline static OctreeP alloc(const OctreeNode& rootNode) diff --git a/src/Moof/OpenGL.hh b/src/Moof/OpenGL.hh index 0ac6612..a5ae3a0 100644 --- a/src/Moof/OpenGL.hh +++ b/src/Moof/OpenGL.hh @@ -33,7 +33,7 @@ /* Define to 1 if you want to use double precision floating-point numbers. */ -#define USE_DOUBLE_PRECISION 1 +//#define USE_DOUBLE_PRECISION 1 #if USE_DOUBLE_PRECISION diff --git a/src/Moof/Sound.cc b/src/Moof/Sound.cc index a0302fb..110dc4b 100644 --- a/src/Moof/Sound.cc +++ b/src/Moof/Sound.cc @@ -27,6 +27,8 @@ *******************************************************************************/ #include +#include +#include #include #include @@ -36,7 +38,8 @@ #include "Mippleton.hh" #include "Sound.hh" -#define BUFFER_SIZE (8 * 4096) +#define BUFFER_SIZE (64 * 1024) +//#define BUFFER_SIZE (5*2048) namespace Mf { @@ -58,39 +61,61 @@ struct Sound::Impl } } - struct Buffer : public Mippleton + class Buffer; + typedef boost::shared_ptr BufferP; + + class Buffer : public Mippleton { + Sound_Sample* sound; + std::vector objects; + + public: + Buffer(const std::string& name) : - Mippleton(name) + Mippleton(name), + sound(0) { - sound = 0; - objects[0] = 0; - objects[1] = 0; + openFile(); } ~Buffer() { - alDeleteBuffers(2, objects); + while (!objects.empty()) + { + alDeleteBuffers(1, &objects.back()); + objects.pop_back(); + } if (sound) Sound_FreeSample(sound); } - void loadFromFile(const std::string& filePath, bool stream) + void openFile() { - if (objects[0] != 0) return; + if (sound) Sound_FreeSample(sound); - sound = Sound_NewSampleFromFile(filePath.c_str(), - NULL, BUFFER_SIZE); + sound = Sound_NewSampleFromFile(Sound::getPath(getName()).c_str(), + 0, BUFFER_SIZE); if (!sound) { - logWarning("error while loading sound %s: %s", getName().c_str(), Sound_GetError()); + logWarning("error while loading sound %s: %s", + getName().c_str(), Sound_GetError()); throw Exception(Exception::FILE_NOT_FOUND); } - if (!stream) - { + logDebug("buffer size: %d", sound->buffer_size); + logDebug(" channels: %d", sound->actual.channels); + logDebug(" format: %d", sound->actual.format); + logDebug(" frequency: %d", sound->actual.rate); + } + + + void loadAll(ALuint source) + { + if (!sound) openFile(); + if (!sound) return; + unsigned decoded = Sound_DecodeAll(sound); if (decoded == 0) { @@ -99,89 +124,148 @@ struct Sound::Impl return; } - alGenBuffers(2, objects); - alBufferData(objects[0], getAudioFormat(sound->actual), sound->buffer, + ALuint obj; + alGenBuffers(1, &obj); + + alBufferData(obj, getAudioFormat(sound->actual), sound->buffer, sound->buffer_size, sound->actual.rate); - logDebug("buffer size: %d", sound->buffer_size); - logDebug(" channels: %d", sound->actual.channels); - logDebug(" format: %d", sound->actual.format); - logDebug(" frequency: %d", sound->actual.rate); + objects.push_back(obj); + + alSourcei(source, AL_BUFFER, obj); + + // don't need t his anymore Sound_FreeSample(sound); sound = 0; - } - else + } + + + void beginStream(ALuint source, int nBuffers = 4) + { + if (!sound) openFile(); + if (!sound) return; + + ALuint objs[nBuffers]; + alGenBuffers(nBuffers, objs); + + for (int i = 0; i < nBuffers; ++i) { - logDebug("buffer size: %d", sound->buffer_size); - logDebug(" channels: %d", sound->actual.channels); - logDebug(" format: %d", sound->actual.format); - logDebug(" frequency: %d", sound->actual.rate); - alGenBuffers(2, objects); + objects.push_back(objs[i]); + stream(objs[i]); } + + alSourceQueueBuffers(source, nBuffers, objs); } - bool stream(ALuint buffer) + enum StreamStatus + { + STREAM_OK = 0, + STREAM_EOF = 1, + STREAM_WRONG = 2 + }; + + StreamStatus stream(ALuint buffer) { - int bytes = Sound_Decode(sound); + std::vector::iterator it = + std::find(objects.begin(), objects.end(), buffer); - if (bytes < BUFFER_SIZE) return false; + // that buffer doesn't belong to us + if (it == objects.end()) return STREAM_WRONG; + + unsigned bytes = Sound_Decode(sound); + + if (bytes == 0) return STREAM_EOF; alBufferData(buffer, getAudioFormat(sound->actual), sound->buffer, - sound->buffer_size, sound->actual.rate); - return false; + bytes, sound->actual.rate); + + return STREAM_OK; + } + + inline void rewind() + { + if (!sound) openFile(); + else Sound_Rewind(sound); } - Sound_Sample* sound; - ALuint objects[2]; - //ALfloat location[] = {0.0f, 0.0f, 0.0f}; - //ALfloat location2[] = {0.0f, 0.0f, 0.0f}; - //ALfloat orient[] = {0.0f, 0.0f, -1.0f, 0.0, 1.0, 0.0}; + // delete unused buffers, return true if all buffers deleted + inline bool clear() + { + // clear any openal errors + alGetError(); + + while (!objects.empty()) + { + ALuint buffer = objects.back(); + alDeleteBuffers(1, &buffer); + // if an error occured, the buffer was not deleted because it's + // still in use by some source + if (alGetError() != AL_NO_ERROR) return false; - //alListenerfv(AL_POSITION, location); - //alListenerfv(AL_VELOCITY, location); - //alListenerfv(AL_VELOCITY, orient); + objects.pop_back(); + } + + return true; + } }; - Impl(const std::string& name, bool stream = false) : - buffer_(Buffer::getInstance(name)) - { - if (!stream) buffer_->loadFromFile(Sound::getPath(name), stream); - else buffer_->loadFromFile(SoundStream::getPath(name), stream); - ALfloat location[] = {0.0f, 0.0f, 0.0f}; + Impl(const std::string& name) : + buffer_(Buffer::getInstance(name)), + playing_(false), + looping_(false) + { + ALfloat zero[] = {0.0f, 0.0f, 0.0f}; alGenSources(1, &source_); alSourcef(source_, AL_PITCH, 1.0f); alSourcef(source_, AL_GAIN, 1.0f); - alSourcefv(source_, AL_POSITION, location); - alSourcefv(source_, AL_VELOCITY, location); - alSourcei(source_, AL_LOOPING, AL_FALSE); + alSourcefv(source_, AL_POSITION, zero); + alSourcefv(source_, AL_VELOCITY, zero); + } - if (!stream) + ~Impl() + { + alDeleteSources(1, &source_); + } + + + void play() + { + ALenum type; + alGetSourcei(source_, AL_SOURCE_TYPE, &type); + + if (type != AL_STATIC) { - alSourcei(source_, AL_BUFFER, buffer_->objects[0]); + buffer_->loadAll(source_); } - else - { - buffer_->stream(buffer_->objects[0]); - buffer_->stream(buffer_->objects[1]); - alSourceQueueBuffers(source_, 2, buffer_->objects); - } + alSourcei(source_, AL_LOOPING, looping_); + alSourcePlay(source_); + playing_ = true; } - ~Impl() + + void stream() { - alDeleteSources(1, &source_); - } + ALenum type; + alGetSourcei(source_, AL_SOURCE_TYPE, &type); + + alSourcei(source_, AL_BUFFER, AL_NONE); + buffer_->rewind(); + buffer_->beginStream(source_); + alSourcei(source_, AL_LOOPING, AL_FALSE); + alSourcePlay(source_); + playing_ = true; + } - void update() + inline void update() { - int finished = 0; + ALint finished = 0; alGetSourcei(source_, AL_BUFFERS_PROCESSED, &finished); @@ -190,17 +274,141 @@ struct Sound::Impl ALuint buffer; alSourceUnqueueBuffers(source_, 1, &buffer); - buffer_->stream(buffer); - alSourceQueueBuffers(source_, 1, &buffer); + + Buffer::StreamStatus status = buffer_->stream(buffer); + + if (status == Buffer::STREAM_OK) + { + alSourceQueueBuffers(source_, 1, &buffer); + } + else if (status == Buffer::STREAM_EOF) + { + if (!queue_.empty()) + { + // begin the next buffer in the queue + expired_.push_back(buffer_); + buffer_ = queue_.front(); + queue_.pop(); + buffer_->beginStream(source_, 1); + } + else if (looping_) + { + // restart from the beginning + buffer_->rewind(); + buffer_->stream(buffer); + alSourceQueueBuffers(source_, 1, &buffer); + } + } + else if (status == Buffer::STREAM_WRONG) + { + clear(); + buffer_->beginStream(source_, 1); + } + } + + ALenum state; + alGetSourcei(source_, AL_SOURCE_STATE, &state); + + // restart playing if we're stopped but supposed to be playing... this + // means we didn't queue enough and the audio skipped + if (playing_ && state != AL_PLAYING) + { + alSourcePlay(source_); } } + inline void clear() + { + // try to remove expired buffers + std::vector::iterator it; + for (it = expired_.end() - 1; it >= expired_.begin(); --it) + { + if ((*it)->clear()) expired_.erase(it); + } + } - boost::shared_ptr buffer_; - ALuint source_; - bool playing; -}; + void stop() + { + alSourceStop(source_); + playing_ = false; + } + + inline void pause() + { + alSourcePause(source_); + playing_ = false; + } + + inline void resume() + { + alSourcePlay(source_); + playing_ = true; + } + + + inline void setSample(const std::string& name) + { + bool playing = isPlaying(); + ALenum type; + alGetSourcei(source_, AL_SOURCE_TYPE, &type); + + stop(); + + //alSourcei(source_, AL_BUFFER, AL_NONE); + buffer_ = Buffer::getInstance(name); + + if (type == AL_STREAMING) + { + if (playing) stream(); + } + else + { + if (playing) play(); + } + } + + inline void enqueue(const std::string& name) + { + BufferP buffer = Buffer::getInstance(name); + queue_.push(buffer); + } + + + inline bool isPlaying() const + { + if (playing_) return true; + + ALenum state; + alGetSourcei(source_, AL_SOURCE_STATE, &state); + + return state == AL_PLAYING; + } + + + inline void setLooping(bool looping) + { + looping_ = looping; + + ALenum type; + alGetSourcei(source_, AL_SOURCE_TYPE, &type); + + if (type != AL_STREAMING) + { + alSourcei(source_, AL_LOOPING, looping_); + } + } + + + ALuint source_; + BufferP buffer_; + + bool playing_; + bool looping_; + + std::queue queue_; + std::vector expired_; +}; Sound::Sound(const std::string& name) : // pass through @@ -209,59 +417,100 @@ Sound::Sound(const std::string& name) : void Sound::play() { - if (!impl_->buffer_->sound) return; + // pass through + impl_->play(); +} - //alSourceRewind(impl_->source_); - alSourcePlay(impl_->source_); - impl_->playing = true; + +void Sound::stream() +{ + // pass through + impl_->stream(); } -void Sound::pause() +void Sound::update(Scalar t, Scalar dt) { - alSourcePause(impl_->source_); - impl_->playing = false; + // pass through + impl_->update(); } -void Sound::togglePlayPause() + +void Sound::stop() { - if (impl_->playing) pause(); - else play(); + // pass through + impl_->stop(); } -void Sound::setGain(Scalar gain) +void Sound::pause() { - alSourcef(impl_->source_, AL_GAIN, gain); + // pass through + impl_->pause(); } +void Sound::resume() +{ + // pass through + impl_->resume(); +} -std::string Sound::getPath(const std::string& name) +void Sound::toggle() { - std::string path = Resource::getPath("sounds/" + name + ".ogg"); - return path; + if (impl_->playing_) pause(); + else resume(); } -//############################################################################## +void Sound::setSample(const std::string& name) +{ + // pass through + impl_->setSample(name); +} + +void Sound::enqueue(const std::string& name) +{ + // pass through + impl_->enqueue(name); +} -SoundStream::SoundStream(const std::string& name) +bool Sound::isPlaying() const +{ // pass through - //impl_(name, true) {} + return impl_->isPlaying(); +} + +void Sound::setPosition(Vector3 position) { - impl_ = boost::shared_ptr(new Sound::Impl(name, true)); + float p[3] = {position[0], position[1], position[2]}; + alSourcefv(impl_->source_, AL_POSITION, p); } +void Sound::setVelocity(Vector3 velocity) +{ + float v[3] = {velocity[0], velocity[1], velocity[2]}; + alSourcefv(impl_->source_, AL_VELOCITY, v); +} -void SoundStream::update(Scalar t, Scalar dt) +void Sound::setGain(Scalar gain) +{ + alSourcef(impl_->source_, AL_GAIN, float(gain)); +} + +void Sound::setPitch(Scalar pitch) +{ + alSourcef(impl_->source_, AL_PITCH, float(pitch)); +} + +void Sound::setLooping(bool looping) { // pass through - impl_->update(); + impl_->setLooping(looping); } -std::string SoundStream::getPath(const std::string& name) +std::string Sound::getPath(const std::string& name) { - std::string path = Resource::getPath("sounds/" + name + ".xm"); + std::string path = Resource::getPath("sounds/" + name + ".ogg"); return path; } diff --git a/src/Moof/Sound.hh b/src/Moof/Sound.hh index 3f64162..32f4c87 100644 --- a/src/Moof/Sound.hh +++ b/src/Moof/Sound.hh @@ -34,8 +34,6 @@ * Image-loading and OpenGL texture loading. */ -#include - #include #include @@ -52,25 +50,39 @@ typedef boost::shared_ptr SoundP; class Sound : public Resource { -protected: - Sound() {} class Impl; boost::shared_ptr impl_; public: - static SoundP alloc(const std::string& name) + inline static SoundP alloc(const std::string& name) { return SoundP(new Sound(name)); } - Sound(const std::string& name); + explicit Sound(const std::string& name); + void play(); + + void stream(); + void update(Scalar t, Scalar dt); + + void stop(); void pause(); - void togglePlayPause(); + void resume(); + void toggle(); + + void setSample(const std::string& name); + void enqueue(const std::string& name); + bool isPlaying() const; + + void setPosition(Vector3 position); + void setVelocity(Vector3 velocity); void setGain(Scalar gain); + void setPitch(Scalar pitch); + void setLooping(bool looping); static std::string getPath(const std::string& name); @@ -103,17 +115,6 @@ public: }; -class SoundStream : public Sound -{ -public: - SoundStream(const std::string& name); - - void update(Scalar t, Scalar dt); - - static std::string getPath(const std::string& name); -}; - - } // namespace Mf #endif // _MOOF_SOUND_HH_ diff --git a/src/Moof/Timer.cc b/src/Moof/Timer.cc index fb17f1d..bc7aae2 100644 --- a/src/Moof/Timer.cc +++ b/src/Moof/Timer.cc @@ -30,12 +30,15 @@ #include #include +#include "Log.hh" #include "Timer.hh" #if HAVE_CONFIG_H #include "config.h" #endif +#include + namespace Mf { @@ -100,8 +103,6 @@ void sleep(Scalar seconds, bool absolute) // SDL only promises centisecond accuracy, but that's better than a kick in the // butt. -#include - Scalar getTicks() { Uint32 ms = SDL_GetTicks(); @@ -111,8 +112,7 @@ Scalar getTicks() void sleep(Scalar seconds, bool absolute) { if (absolute) seconds -= getTicks(); - - SDL_Delay(Uint32(seconds * 1000.0)); + SDL_Delay(Uint32(cml::clamp(int(seconds * 1000.0), 0, 1000))); } #endif // HAVE_CLOCK_GETTIME diff --git a/src/YoinkApp.cc b/src/YoinkApp.cc index 9ea4b37..d36f6da 100644 --- a/src/YoinkApp.cc +++ b/src/YoinkApp.cc @@ -42,10 +42,6 @@ #include "YoinkApp.hh" -#include -#include -#include - #if HAVE_CONFIG_H #include "config.h" #endif @@ -106,18 +102,16 @@ static std::string iconFile() YoinkApp::YoinkApp(int argc, char* argv[]) : Mf::Engine(argc, argv, configFiles(), PACKAGE_STRING, iconFile()), - music("NightFusion"), + music("NightFusionIntro"), punchSound("RobotPunch") { Mf::dispatcher::addHandler("video.context_recreated", boost::bind(&YoinkApp::contextRecreated, this, _1), this); setupGL(); - Mf::Scalar fade[2] = {0.0, 1.0}; - musicFade.init(fade, 0.0); - music.play(); - - state = 0.0; + music.setLooping(true); + music.enqueue("NightFusionLoop"); + music.stream(); heroine = Character::alloc("RobotTrooper"); heroine->getAnimation().startSequence("Run"); @@ -189,17 +183,14 @@ void YoinkApp::contextRecreated(const Mf::Notification* note) void YoinkApp::update(Mf::Scalar t, Mf::Scalar dt) { - //dt *= 0.2; + //dt *= 0.5; - musicFade.update(dt); music.update(t, dt); - music.setGain(musicFade.getValue()); - fadeIn.update(dt); - camera.update(t, dt); - heroine->update(t, dt); + + // reinsert heroine heroine->treeNode = testScene->getOctree()->reinsert(heroine, heroine->treeNode); testScene->getOctree()->print(heroine->treeNode); @@ -209,45 +200,26 @@ void YoinkApp::update(Mf::Scalar t, Mf::Scalar dt) interp.update(dt); hud.setBar1Progress(interp.getValue()); hud.setBar2Progress(1.0 - interp.getValue()); - - prevstate = state; - state += dt; } void YoinkApp::draw(Mf::Scalar alpha) { - //Mf::Vector4 meh; - //meh.random(0.0, 1.0); - //static Mf::Vector4 c1(meh); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - //Mf::Scalar drawstate = cml::lerp(prevstate, state, alpha); - //Mf::Scalar sinstate = std::sin(drawstate); - //Mf::Scalar cosstate = std::cos(drawstate); - - glMatrixMode(GL_MODELVIEW); - //glLoadIdentity(); - - //glRotatef(drawstate*15.0f, 0.0, 1.0, 0.0); - //glTranslatef(x, y, z); glLoadMatrix(camera.getModelviewMatrix().data()); // DRAW THE SCENE Mf::Texture::resetBind(); testScene->draw(alpha, camera); - - //heroine->draw(alpha); heroine->getAabb().draw(); - hud.draw(); - + // DRAW FADE glEnable(GL_BLEND); glMatrixMode(GL_PROJECTION); glPushMatrix(); @@ -272,123 +244,6 @@ void YoinkApp::draw(Mf::Scalar alpha) glPopMatrix(); glMatrixMode(GL_MODELVIEW); glPopMatrix(); - - /* - glLoadIdentity(); - - someChar->getTilemap().bind(); - glColor3f(1.0, 1.0, 1.0); - - Mf::Tilemap::Index heroFrame = someChar->getAnimation().getFrame(); - - Mf::Scalar coords[8]; - someChar->getTilemap().getTileCoords(heroFrame, coords); - - glBegin(GL_QUADS); - glTexCoord2f(coords[0], coords[1]); - glVertex3f(-1.0, 0.0, 0.0); - glTexCoord2f(coords[2], coords[3]); - glVertex3f(0.0, 0.0, 0.0); - glTexCoord2f(coords[4], coords[5]); - glVertex3f(0.0, 1.0, 0.0); - glTexCoord2f(coords[6], coords[7]); - glVertex3f(-1.0, 1.0, 0.0); - glEnd(); - - - someChar->getTilemap().getTileCoords(heroFrame, coords, - Mf::Tilemap::REVERSE); - - glBegin(GL_QUADS); - glTexCoord2f(coords[0], coords[1]); - glVertex3f(0.0, 0.0, 0.0); - glTexCoord2f(coords[2], coords[3]); - glVertex3f(1.0, 0.0, 0.0); - glTexCoord2f(coords[4], coords[5]); - glVertex3f(1.0, 1.0, 0.0); - glTexCoord2f(coords[6], coords[7]); - glVertex3f(0.0, 1.0, 0.0); - glEnd(); - - glColor4f(1.0,0.0,0.0,0.5); - - glBindTexture(GL_TEXTURE_2D, 0); - glColor4v(c1.data()); - - glRectd(-cosstate, -sinstate, sinstate, cosstate); - glRectf(0.0f, 0.0f, sinstate, cosstate); - - font->bind(); - - font->getTileCoords('c', coords); - - glBegin(GL_QUADS); - glTexCoord2f(coords[0], coords[1]); - glVertex3f(-1.0, 0.0, 0.0); - glTexCoord2f(coords[2], coords[3]); - glVertex3f(0.0, 0.0, 0.0); - glTexCoord2f(coords[4], coords[5]); - glVertex3f(0.0, 1.0, 0.0); - glTexCoord2f(coords[6], coords[7]); - glVertex3f(-1.0, 1.0, 0.0); - glEnd(); - - font->getTileCoords('h', coords); - - glBegin(GL_QUADS); - glTexCoord2f(coords[0], coords[1]); - glVertex3f(0.0, 0.0, 0.0); - glTexCoord2f(coords[2], coords[3]); - glVertex3f(1.0, 0.0, 0.0); - glTexCoord2f(coords[4], coords[5]); - glVertex3f(1.0, 1.0, 0.0); - glTexCoord2f(coords[6], coords[7]); - glVertex3f(0.0, 1.0, 0.0); - glEnd(); - - font->getTileCoords('a', coords); - - glBegin(GL_QUADS); - glTexCoord2f(coords[0], coords[1]); - glVertex3f(-1.0, -1.0, 0.0); - glTexCoord2f(coords[2], coords[3]); - glVertex3f(0.0, -1.0, 0.0); - glTexCoord2f(coords[4], coords[5]); - glVertex3f(0.0, 0.0, 0.0); - glTexCoord2f(coords[6], coords[7]); - glVertex3f(-1.0, 0.0, 0.0); - glEnd(); - - font->getTileCoords('z', coords); - - glBegin(GL_QUADS); - glTexCoord2f(coords[0], coords[1]); - glVertex3(0.0, -1.0, 0.0); - glTexCoord2f(coords[2], coords[3]); - glVertex3(1.0, -1.0, 0.0); - glTexCoord2f(coords[4], coords[5]); - glVertex3(1.0, 0.0, 0.0); - glTexCoord2f(coords[6], coords[7]); - glVertex3(0.0, 0.0, 0.0); - glEnd(); - - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - glDisable(GL_DEPTH_TEST); - - glBindTexture(GL_TEXTURE_2D, 0); - glColor4f(0.0f, 0.0f, 0.0f, 1.0f); - - glBegin(GL_LINES); - glVertex2f(0.0f, 0.0f); - glVertex2v(interp.getState(alpha).data()); - glEnd(); - - glColor4f(0.0f, 0.0f, 0.0f, fadeIn.getState(alpha)); - glRectf(-1.0f, -1.0f, 1.0f, 1.0f); - - glDisable(GL_BLEND); - glEnable(GL_DEPTH_TEST);*/ } void YoinkApp::handleEvent(const Mf::Event& event) @@ -424,7 +279,7 @@ void YoinkApp::handleEvent(const Mf::Event& event) } else if (event.key.keysym.sym == SDLK_p) { - music.togglePlayPause(); + music.toggle(); break; } else if (event.key.keysym.sym == SDLK_l) @@ -464,8 +319,16 @@ int main(int argc, char* argv[]) << "Send patches and bug reports to <" PACKAGE_BUGREPORT << ">." << std::endl << std::endl; -#if ! NDEBUG +#if YOINK_LOGLEVEL >= 4 Mf::setLogLevel(Mf::LOG_DEBUG); +#elif YOINK_LOGLEVEL >= 3 + Mf::setLogLevel(Mf::LOG_INFO); +#elif YOINK_LOGLEVEL >= 2 + Mf::setLogLevel(Mf::LOG_WARNING); +#elif YOINK_LOGLEVEL >= 1 + Mf::setLogLevel(Mf::LOG_ERROR); +#elif YOINK_LOGLEVEL + Mf::setLogLevel(Mf::LOG_NONE); #endif int status = 0; diff --git a/src/YoinkApp.hh b/src/YoinkApp.hh index 6a947f9..ad8d694 100644 --- a/src/YoinkApp.hh +++ b/src/YoinkApp.hh @@ -69,8 +69,7 @@ private: void setupGL(); void contextRecreated(const Mf::Notification* note); - Mf::Lerps musicFade; - Mf::SoundStream music; + Mf::Sound music; CharacterP heroine; Mf::Sound punchSound; @@ -82,9 +81,6 @@ private: Mf::Camera camera; Mf::SceneP testScene; - Mf::Scalar state; - Mf::Scalar prevstate; - Hud hud; };