diff --git a/AV.cc b/AV.cc index 20b0129..ff038ef 100644 --- a/AV.cc +++ b/AV.cc @@ -119,3 +119,19 @@ void AV::stop() m_surface = NULL; } } + +AV::Sound::Sound() +{ + m_rwops = NULL; +} + +bool AV::Sound::load(FileLoader & fileLoader, const FileLoader::Path & path) +{ + FileLoader::Buffer buff = fileLoader.load(path); + if (buff.size > 0) + { + m_rwops = SDL_RWFromMem(buff.data, buff.size); + return true; + } + return false; +} diff --git a/AV.h b/AV.h index a1ab2c3..e8b240f 100644 --- a/AV.h +++ b/AV.h @@ -2,11 +2,22 @@ #ifndef VIDEO_H #define VIDEO_H +#include "FileLoader/FileLoader.h" #include class AV { public: + class Sound + { + public: + Sound(); + bool load(FileLoader & fileLoader, + const FileLoader::Path & path); + protected: + SDL_RWops * m_rwops; + }; + AV(); ~AV(); void start(int width = 0, int height = 0, diff --git a/Engine.cc b/Engine.cc index 058c588..917b6f3 100644 --- a/Engine.cc +++ b/Engine.cc @@ -89,8 +89,8 @@ Engine::Engine(const string & path, AV & av) : m_av(av) { m_next_object_index = 1; + m_next_sound_index = 1; m_next_joint_index = 1; - m_next_text_index = 1; m_eye[0] = 0; m_eye[1] = -1; m_eye[2] = 0; @@ -298,6 +298,13 @@ int Engine::addObject(bool is_static, bool is_reference, return id; } +int Engine::addSound(AV::Sound * avs) +{ + int id = m_next_sound_index++; + m_sounds[id] = avs; + return id; +} + int Engine::addAMotor(Object * o1, Object * o2) { dBodyID b1 = 0; @@ -564,6 +571,28 @@ int Engine::loadModel(const string & name, bool is_static, bool is_reference, return 0; } +int Engine::loadSound(const string & name) +{ + size_t pos = name.find_first_not_of(FILENAME_SAFE_CHARS); + if (pos == string::npos) + { + FileLoader::Path path("", name); + + AV::Sound * avs = new AV::Sound(); + + if (avs->load(*m_fileLoader, path)) + { + int id = addSound(avs); + return id; + } + else + { + cerr << "error loading sound '" << name << "'" << endl; + } + } + return 0; +} + bool Engine::isKeyDown(const std::string & key) { return m_keysDown.find(key) != m_keysDown.end(); diff --git a/Engine.h b/Engine.h index 2401346..0591db3 100644 --- a/Engine.h +++ b/Engine.h @@ -9,6 +9,7 @@ #include #include "OdeWorld/OdeWorld.h" #include "TextureCache/TextureCache.h" +#include "FileLoader/FileLoader.h" #include "wfobj/WFObj.h" #include "AV.h" #include "refptr/refptr.h" @@ -147,6 +148,7 @@ class Engine int addObject(bool is_static, bool is_reference, OdeWorld::GeomType geom_type, refptr< std::vector > args); + int addSound(AV::Sound * avs); int addAMotor(Object * o1, Object * o2); int addHinge(Object * o1, Object * o2, dReal anchor_x, dReal anchor_y, dReal anchor_z, @@ -177,6 +179,7 @@ class Engine void endFrame(); int loadModel(const std::string & name, bool is_static, bool is_reference, float scale = 1.0f); + int loadSound(const std::string & name); bool isKeyDown(const std::string & key); void exit(); bool import(const char * name); @@ -261,10 +264,11 @@ class Engine std::string m_engine_path; OdeWorld m_world; std::map m_objects; + std::map m_sounds; std::map m_joints; int m_next_object_index; + int m_next_sound_index; int m_next_joint_index; - int m_next_text_index; GLdouble m_eye[3]; GLdouble m_center[3]; GLdouble m_up[3]; diff --git a/ag.cc b/ag.cc index a1d4359..263fc51 100644 --- a/ag.cc +++ b/ag.cc @@ -44,6 +44,7 @@ namespace ag { "import", import }, { "isKeyDown", isKeyDown }, { "loadModelSpecify", loadModelSpecify }, + { "loadSound", loadSound }, { "loadTexture", loadTexture }, { "next", next }, { "pickObjects", pickObjects }, @@ -304,6 +305,17 @@ namespace ag lua_pop(L, 2); /* pops 2 */ } + static void createLuaSound(lua_State * L, int id) + { + lua_newtable(L); + lua_pushinteger(L, id); + lua_setfield(L, -2, "id"); +#if 0 + lua_pushcfunction(L, sound::play); + lua_setfield(L, -2, "play"); +#endif + } + static void createLuaAMotor(lua_State * L, int id) { lua_newtable(L); @@ -419,6 +431,26 @@ namespace ag return 1; } + int loadSound(lua_State * L) + { + bool created = false; + int argc = lua_gettop(L); + if (argc == 1 && lua_isstring(L, 1)) + { + int id = g_engine->loadSound(lua_tostring(L, 1)); + if (id > 0) + { + created = true; + createLuaSound(L, id); + } + } + if (!created) + { + lua_pushnil(L); + } + return 1; + } + int sleep(lua_State * L) { int argc = lua_gettop(L); diff --git a/ag.h b/ag.h index f763098..49074ae 100644 --- a/ag.h +++ b/ag.h @@ -24,6 +24,7 @@ namespace ag int import(lua_State * L); int isKeyDown(lua_State * L); int loadModelSpecify(lua_State * L); + int loadSound(lua_State * L); int loadTexture(lua_State * L); int next(lua_State * L); int pickObjects(lua_State * L);