From d4d44dea86463f162c6e523d9280df6d8f5b970c Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Thu, 24 Jun 2010 02:46:28 +0000 Subject: [PATCH] added AV::Sound::pause(), added Lua interface for sounds (play/pause/stop/loop/loopForever) git-svn-id: svn://anubis/anaglym/trunk@290 99a6e188-d820-4881-8870-2d33a10e2619 --- AV.cc | 12 ++++- AV.h | 1 + Engine.cc | 9 ++-- Engine.h | 1 + ag.cc | 120 +++++++++++++++++++++++++++++++++++++++---- ag.h | 12 +++++ tests/cratestack.lua | 12 ++++- 7 files changed, 150 insertions(+), 17 deletions(-) diff --git a/AV.cc b/AV.cc index c23cd76..3a110ce 100644 --- a/AV.cc +++ b/AV.cc @@ -4,7 +4,7 @@ #include #include #include -#include +#include /* INT_MAX */ #include @@ -224,10 +224,19 @@ void AV::Sound::play() loop(1); } +void AV::Sound::pause() +{ + if (m_ss != NULL) + { + m_av.stopSound(this); + } +} + void AV::Sound::stop() { if (m_ss != NULL) { + rewind(); m_av.stopSound(this); } } @@ -236,7 +245,6 @@ void AV::Sound::loop(int times) { if (m_ss != NULL) { - rewind(); m_loop_count = times; m_av.playSound(this); } diff --git a/AV.h b/AV.h index 76221a1..abcb05c 100644 --- a/AV.h +++ b/AV.h @@ -21,6 +21,7 @@ class AV bool load(FileLoader & fileLoader, const FileLoader::Path & path); void play(); + void pause(); void stop(); void loop(int times); void rewind(); diff --git a/Engine.cc b/Engine.cc index fa03671..0e3353c 100644 --- a/Engine.cc +++ b/Engine.cc @@ -414,6 +414,11 @@ Engine::Object * Engine::getObject(int id) return m_objects.contains(id) ? m_objects[id] : NULL; } +refptr Engine::getSound(int id) +{ + return m_sounds.contains(id) ? m_sounds[id] : NULL; +} + void Engine::startFrame() { checkGLError(); @@ -570,9 +575,7 @@ int Engine::loadSound(const string & name) if (avs->load(*m_fileLoader, path)) { - int id = addSound(avs); - avs->play(); - return id; + return addSound(avs); } else { diff --git a/Engine.h b/Engine.h index b09cce9..41365e2 100644 --- a/Engine.h +++ b/Engine.h @@ -166,6 +166,7 @@ class Engine void removeObject(int id); int cloneObject(const Object * obj); Object * getObject(int id); + refptr getSound(int id); void doPhysics(); void drawObjects(); void setAutoPhysics(bool val) { m_autoPhysics = val; } diff --git a/ag.cc b/ag.cc index fd5865f..5acba29 100644 --- a/ag.cc +++ b/ag.cc @@ -1,19 +1,24 @@ -#include "ag.h" -#include "ag_lua.h" -#include "anaglym.h" -#include "Engine.h" #include -#include "wfobj/WFObj.h" #include /* usleep() */ -#include -#include -#include #include #include #ifdef _WIN32 #include /* Sleep() */ #endif +#include /* INT_MAX */ + +#include +#include +#include + +#include "ag.h" +#include "ag_lua.h" +#include "anaglym.h" +#include "Engine.h" +#include "wfobj/WFObj.h" +#include "AV.h" + using namespace std; namespace ag @@ -312,10 +317,16 @@ namespace ag 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 + lua_pushcfunction(L, sound::pause); + lua_setfield(L, -2, "pause"); + lua_pushcfunction(L, sound::stop); + lua_setfield(L, -2, "stop"); + lua_pushcfunction(L, sound::loop); + lua_setfield(L, -2, "loop"); + lua_pushcfunction(L, sound::loopForever); + lua_setfield(L, -2, "loopForever"); } static void createLuaAMotor(lua_State * L, int id) @@ -1602,4 +1613,93 @@ namespace ag return 0; } } + + namespace sound + { + refptr getSound(lua_State * L, int index) + { + refptr ret; + + lua_getfield(L, index, "id"); + if (lua_isnumber(L, -1)) + { + int id = lua_tointeger(L, -1); + ret = g_engine->getSound(id); + } + lua_pop(L, 1); + + return ret; + } + + int play(lua_State * L) + { + int argc = lua_gettop(L); + if (argc == 1 && lua_istable(L, 1)) + { + refptr snd = getSound(L, 1); + if (!snd.isNull()) + { + snd->play(); + } + } + return 0; + } + + int pause(lua_State * L) + { + int argc = lua_gettop(L); + if (argc == 1 && lua_istable(L, 1)) + { + refptr snd = getSound(L, 1); + if (!snd.isNull()) + { + snd->pause(); + } + } + return 0; + } + + int stop(lua_State * L) + { + int argc = lua_gettop(L); + if (argc == 1 && lua_istable(L, 1)) + { + refptr snd = getSound(L, 1); + if (!snd.isNull()) + { + snd->stop(); + } + } + return 0; + } + + int loop(lua_State * L) + { + int argc = lua_gettop(L); + if (argc == 2 && lua_istable(L, 1) && lua_isnumber(L, 2)) + { + refptr snd = getSound(L, 1); + if (!snd.isNull()) + { + int count = lua_tointeger(L, 2); + snd->loop(count); + } + } + return 0; + } + + int loopForever(lua_State * L) + { + int argc = lua_gettop(L); + if (argc == 1 && lua_istable(L, 1)) + { + refptr snd = getSound(L, 1); + if (!snd.isNull()) + { + snd->loop(INT_MAX); + } + } + return 0; + } + } } diff --git a/ag.h b/ag.h index abeafe6..ce548d0 100644 --- a/ag.h +++ b/ag.h @@ -3,7 +3,9 @@ #define AG_H #include + #include "Engine.h" +#include "AV.h" namespace ag { @@ -103,6 +105,16 @@ namespace ag int setAMotorFMax(lua_State * L); int setAMotorBounce(lua_State * L); } + + namespace sound + { + refptr getSound(lua_State * L, int index); + int play(lua_State * L); + int pause(lua_State * L); + int stop(lua_State * L); + int loop(lua_State * L); + int loopForever(lua_State * L); + } } #endif diff --git a/tests/cratestack.lua b/tests/cratestack.lua index 88a8e22..1895c3a 100644 --- a/tests/cratestack.lua +++ b/tests/cratestack.lua @@ -21,6 +21,8 @@ function init_event() ground = ag.loadModel("crate", {static = true, scale = 10}) ground:setPosition(0, 0, -10) ag.setCamera(2, -12, 8, 0, 0, 2, 0, 0, 1) + + iris = ag.loadSound("iris.mp3") end function key_down_event(key) @@ -38,8 +40,14 @@ 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") + elseif (key == "p") then + if (playing) then + iris:pause() + playing = false + else + iris:play() + playing = true + end end end