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
This commit is contained in:
Josh Holtrop 2010-06-24 02:46:28 +00:00
parent ed1f9c8f7f
commit d4d44dea86
7 changed files with 150 additions and 17 deletions

12
AV.cc
View File

@ -4,7 +4,7 @@
#include <SDL.h>
#include <SDL_sound.h>
#include <math.h>
#include <limits.h>
#include <limits.h> /* INT_MAX */
#include <iostream>
@ -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);
}

1
AV.h
View File

@ -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();

View File

@ -414,6 +414,11 @@ Engine::Object * Engine::getObject(int id)
return m_objects.contains(id) ? m_objects[id] : NULL;
}
refptr<AV::Sound> 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
{

View File

@ -166,6 +166,7 @@ class Engine
void removeObject(int id);
int cloneObject(const Object * obj);
Object * getObject(int id);
refptr<AV::Sound> getSound(int id);
void doPhysics();
void drawObjects();
void setAutoPhysics(bool val) { m_autoPhysics = val; }

120
ag.cc
View File

@ -1,19 +1,24 @@
#include "ag.h"
#include "ag_lua.h"
#include "anaglym.h"
#include "Engine.h"
#include <SDL.h>
#include "wfobj/WFObj.h"
#include <unistd.h> /* usleep() */
#include <lua.hpp>
#include <iostream>
#include <string>
#include <GL/gl.h>
#include <GL/glu.h>
#ifdef _WIN32
#include <windows.h> /* Sleep() */
#endif
#include <limits.h> /* INT_MAX */
#include <lua.hpp>
#include <iostream>
#include <string>
#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<AV::Sound> getSound(lua_State * L, int index)
{
refptr<AV::Sound> 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<AV::Sound> 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<AV::Sound> 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<AV::Sound> 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<AV::Sound> 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<AV::Sound> snd = getSound(L, 1);
if (!snd.isNull())
{
snd->loop(INT_MAX);
}
}
return 0;
}
}
}

12
ag.h
View File

@ -3,7 +3,9 @@
#define AG_H
#include <lua.hpp>
#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<AV::Sound> 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

View File

@ -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