diff --git a/AV.cc b/AV.cc index 4454b47..c5765a2 100644 --- a/AV.cc +++ b/AV.cc @@ -114,15 +114,30 @@ void AV::stop() } } +refptr AV::createSound() +{ + refptr s = new Sound(*this); +#if 0 + m_sounds.insert(s); +#endif + return s; +} + +void AV::playSound(AV::Sound * s) +{ + m_active_sounds.insert(s); +} + void AV::playCallback(Uint8 * stream, int len) { - static int pos = 0; - Sint16 * str = (Sint16 *) stream; - for (int i = 0; i < len / 4; i++) + for (std::set::const_iterator it = m_active_sounds.begin(); + it != m_active_sounds.end(); + it++) { - str[i*2] = (Sint16) (32000.0 * sin(pos * 440.0 / AV_SOUND_RATE * 2.0 * M_PI)); - str[i*2+1] = 0; - pos++; + if ((*it)->decode(stream, len) == 0) + { + m_active_sounds.erase(it); + } } } @@ -166,3 +181,34 @@ bool AV::Sound::load(FileLoader & fileLoader, const FileLoader::Path & path) } return false; } + +void AV::Sound::play() +{ + if (m_ss != NULL) + { +#if 0 + cerr << "play()" << endl; + Sound_Rewind(m_ss); + cerr << "end play()" << endl; +#endif + m_av.playSound(this); + } +} + +void AV::Sound::stop() +{ +} + +void AV::Sound::loop(int times) +{ +} + +int AV::Sound::decode(Uint8 * stream, int len) +{ + /* TODO: fix for len != m_ss->buffer_size */ + cerr << "AV::Sound::decode()" << endl; + Uint32 len_decoded = Sound_Decode(m_ss); + cerr << "AV::Sound::decode() len: " << len << ", len_decoded: " << len_decoded << endl; + memcpy(stream, m_ss->buffer, len_decoded); + return len_decoded; +} diff --git a/AV.h b/AV.h index 8dcfcab..a39cfe2 100644 --- a/AV.h +++ b/AV.h @@ -2,11 +2,14 @@ #ifndef VIDEO_H #define VIDEO_H -#include "FileLoader/FileLoader.h" -#include "refptr/refptr.h" #include #include +#include + +#include "FileLoader/FileLoader.h" +#include "refptr/refptr.h" + #define AV_SOUND_RATE 44100 class AV @@ -19,6 +22,10 @@ class AV ~Sound(); bool load(FileLoader & fileLoader, const FileLoader::Path & path); + void play(); + void stop(); + void loop(int times); + int decode(Uint8 * stream, int len); protected: AV & m_av; SDL_RWops * m_rwops; @@ -53,7 +60,8 @@ class AV { return SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON; } - refptr createSound() { return new Sound(*this); } + refptr createSound(); + void playSound(Sound * s); friend void AV_sound_callback(void * userdata, Uint8 * stream, int len); @@ -66,6 +74,10 @@ class AV int m_height; SDL_Surface * m_surface; bool m_fullscreen; +#if 0 + std::set< refptr > m_sounds; +#endif + std::set< Sound * > m_active_sounds; }; #endif diff --git a/Engine.cc b/Engine.cc index 8487b28..d73567a 100644 --- a/Engine.cc +++ b/Engine.cc @@ -571,6 +571,7 @@ int Engine::loadSound(const string & name) if (avs->load(*m_fileLoader, path)) { int id = addSound(avs); + avs->play(); return id; } else diff --git a/tests/cratestack.lua b/tests/cratestack.lua index 373b709..88a8e22 100644 --- a/tests/cratestack.lua +++ b/tests/cratestack.lua @@ -38,6 +38,8 @@ function key_down_event(key) ag.registerEventHandler(mouse_moves, "mouse_motion") elseif (key == "n") then ag.clearEventHandler("mouse_motion") + elseif (key == "s") then + ag.loadSound("iris.mp3") end end