Skip to content

Commit

Permalink
lua timers
Browse files Browse the repository at this point in the history
  • Loading branch information
robn committed Apr 19, 2011
1 parent e6414f9 commit eed7074
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 6 deletions.
138 changes: 138 additions & 0 deletions src/LuaTimer.cpp
@@ -0,0 +1,138 @@
#include "LuaTimer.h"
#include "LuaUtils.h"
#include "Pi.h"

void LuaTimer::Tick()
{
lua_State *l = LuaManager::Instance()->GetLuaState();

LUA_DEBUG_START(l);

lua_getfield(l, LUA_REGISTRYINDEX, "PiTimerCallbacks");
if (lua_isnil(l, -1)) {
lua_pop(l, 1);
LUA_DEBUG_END(l, 0);
return;
}
assert(lua_istable(l, -1));

double now = Pi::GetGameTime();

lua_pushnil(l);
while (lua_next(l, -2)) {
assert(lua_istable(l, -1));

LUA_DEBUG_END(l, 1);

lua_getfield(l, -1, "at");
double at = lua_tonumber(l, -1);
lua_pop(l, 1);

if (at <= now) {
lua_getfield(l, -1, "callback");
lua_call(l, 0, 1);
bool again = lua_toboolean(l, -1);
lua_pop(l, 1);

lua_getfield(l, -1, "every");
if (lua_isnil(l, -1) || !again) {
lua_pop(l, 1);

lua_pushvalue(l, -2);
lua_pushnil(l);
lua_settable(l, -5);
}
else {
double every = lua_tonumber(l, -1);
lua_pop(l, 1);

pi_lua_settable(l, "at", Pi::GetGameTime() + every);
}
}

LUA_DEBUG_END(l, 0);

lua_pop(l, 1);
}
lua_pop(l, 1);

LUA_DEBUG_END(l, 0);
}

static void _finish_timer_create(lua_State *l)
{
lua_pushstring(l, "callback");
lua_pushvalue(l, 3);
lua_settable(l, -3);

lua_getfield(l, LUA_REGISTRYINDEX, "PiTimerCallbacks");
if (lua_isnil(l, -1)) {
lua_pop(l, 1);
lua_newtable(l);
lua_pushvalue(l, -1);
lua_setfield(l, LUA_REGISTRYINDEX, "PiTimerCallbacks");
}

lua_insert(l, -2);
lua_pushinteger(l, lua_objlen(l, -2) + 1);
lua_insert(l, -2);
lua_settable(l, -3);

lua_pop(l, 1);
}

static int l_timer_call_at(lua_State *l)
{
double at = luaL_checknumber(l, 2);
if (!lua_isfunction(l, 3))
luaL_typerror(l, 3, lua_typename(l, LUA_TFUNCTION));

if (at <= Pi::GetGameTime())
luaL_error(l, "Specified time is in the past");

LUA_DEBUG_START(l);

lua_newtable(l);
pi_lua_settable(l, "at", at);

_finish_timer_create(l);

LUA_DEBUG_END(l, 0);

return 0;
}

static int l_timer_call_every(lua_State *l)
{
double every = luaL_checknumber(l, 2);
if (!lua_isfunction(l, 3))
luaL_typerror(l, 3, lua_typename(l, LUA_TFUNCTION));

if (every <= 0)
luaL_error(l, "Specified interval must be greater than zero");

LUA_DEBUG_START(l);

lua_newtable(l);
pi_lua_settable(l, "every", every);
pi_lua_settable(l, "at", Pi::GetGameTime() + every);

_finish_timer_create(l);

LUA_DEBUG_END(l, 0);

return 0;
}

template <> const char *LuaObject<LuaTimer>::s_type = "Timer";

template <> void LuaObject<LuaTimer>::RegisterClass()
{
static const luaL_reg l_methods[] = {
{ "CallAt", l_timer_call_at },
{ "CallEvery", l_timer_call_every },
{ 0, 0 }
};

LuaObjectBase::CreateClass(s_type, NULL, l_methods, NULL);
}
12 changes: 12 additions & 0 deletions src/LuaTimer.h
@@ -0,0 +1,12 @@
#ifndef _LUATIMER_H
#define _LUATIMER_H

#include "LuaManager.h"
#include "DeleteEmitter.h"

class LuaTimer : public DeleteEmitter {
public:
void Tick();
};

#endif
4 changes: 2 additions & 2 deletions src/Makefile.am
Expand Up @@ -18,7 +18,7 @@ include_HEADERS = Body.h Frame.h glfreetype.h GuiButton.h GuiContainer.h GuiEven
BufferObject.h GuiMeterBar.h GuiLabelSet.h ShipAICmd.h PiLuaClasses.h LuaConstants.h CustomSystem.h \
DeleteEmitter.h LuaManager.h LuaUtils.h LuaObject.h LuaEventQueue.h RefList.h RefCounted.h \
LuaBody.h LuaPlanet.h LuaPlayer.h LuaShip.h LuaSpaceStation.h LuaStar.h LuaStarSystem.h LuaSBodyPath.h LuaShipType.h \
LuaSpace.h LuaGame.h LuaUI.h LuaDate.h
LuaSpace.h LuaGame.h LuaUI.h LuaDate.h LuaTimer.h

libgui_a_SOURCES = GuiButton.cpp Gui.cpp GuiFixed.cpp GuiScreen.cpp GuiLabel.cpp GuiToolTip.cpp GuiToggleButton.cpp GuiRadioButton.cpp \
GuiRadioGroup.cpp GuiImageButton.cpp GuiImage.cpp GuiImageRadioButton.cpp GuiMultiStateImageButton.cpp GuiWidget.cpp \
Expand All @@ -38,7 +38,7 @@ pioneer_SOURCES = main.cpp glfreetype.cpp Body.cpp Space.cpp Ship.cpp Player.cpp
GeoSphereStyle.cpp AmbientSounds.cpp KeyBindings.cpp ShipAICmd.cpp PiLuaClasses.cpp LuaConstants.cpp \
CustomSystem.cpp LuaManager.cpp LuaObject.cpp LuaEventQueue.cpp LuaUtils.cpp LuaSerializer.cpp \
LuaBody.cpp LuaShip.cpp LuaSpaceStation.cpp LuaPlanet.cpp LuaStar.cpp LuaPlayer.cpp LuaStarSystem.cpp \
LuaSBodyPath.cpp LuaShipType.cpp LuaSpace.cpp LuaGame.cpp LuaUI.cpp LuaDate.cpp
LuaSBodyPath.cpp LuaShipType.cpp LuaSpace.cpp LuaGame.cpp LuaUI.cpp LuaDate.cpp LuaTimer.cpp
pioneer_LDADD = lua/liblua.a oolua/liboolua.a collider/libcollider.a libgui.a

modelviewer_SOURCES = LuaModelViewer.cpp glfreetype.cpp LmrModel.cpp perlin.cpp Render.cpp utils.cpp MyLuaMathTypes.cpp LuaUtils.cpp
Expand Down
11 changes: 7 additions & 4 deletions src/Pi.cpp
Expand Up @@ -49,6 +49,7 @@
#include "LuaUI.h"
#include "LuaDate.h"
#include "LuaSpace.h"
#include "LuaTimer.h"
#include "PiLuaAPI.h"

float Pi::gameTickAlpha;
Expand All @@ -68,6 +69,7 @@ sigc::signal<void> Pi::onPlayerChangeFlightControlState;
sigc::signal<void> Pi::onPlayerChangeEquipment;
sigc::signal<void, const SpaceStation*> Pi::onDockingClearanceExpired;
LuaSerializer Pi::luaSerializer;
LuaTimer Pi::luaTimer;
LuaEventQueue<> Pi::luaOnTick("onTick");
LuaEventQueue<> Pi::luaOnGameStart("onGameStart");
LuaEventQueue<> Pi::luaOnGameEnd("onGameEnd");
Expand Down Expand Up @@ -169,11 +171,10 @@ static void LuaInit()
LuaShipType::RegisterClass();

LuaObject<LuaChatForm>::RegisterClass();

LuaObject<LuaSerializer>::RegisterClass();
LuaObject<LuaEventQueueBase>::RegisterClass();

Pi::luaSerializer.RegisterSerializer();
LuaObject<LuaSerializer>::RegisterClass();
LuaObject<LuaTimer>::RegisterClass();

Pi::luaOnTick.RegisterEventQueue();
Pi::luaOnGameStart.RegisterEventQueue();
Expand Down Expand Up @@ -1047,8 +1048,10 @@ void Pi::MainLoop()
// paused
}

if (frame_stat == 0)
if (frame_stat == 0) {
Pi::luaOnTick.Signal();
Pi::luaTimer.Tick();
}
frame_stat++;

Render::PrepareFrame();
Expand Down
2 changes: 2 additions & 0 deletions src/Pi.h
Expand Up @@ -10,6 +10,7 @@
#include "IniConfig.h"
#include "LuaEventQueue.h"
#include "LuaSerializer.h"
#include "LuaTimer.h"
#include <map>
#include <string>
#include <vector>
Expand Down Expand Up @@ -100,6 +101,7 @@ class Pi {
static sigc::signal<void, const SpaceStation*> onDockingClearanceExpired;

static LuaSerializer luaSerializer;
static LuaTimer luaTimer;

static LuaEventQueue<> luaOnTick;
static LuaEventQueue<> luaOnGameStart;
Expand Down

0 comments on commit eed7074

Please sign in to comment.