actual cool scripting

This commit is contained in:
Martin Felis
2013-12-04 19:25:03 +01:00
parent db5083d46e
commit 3614649365
3 changed files with 59 additions and 25 deletions
+1
View File
@@ -2,3 +2,4 @@ bin/
libs/ libs/
obj/ obj/
.*.swp .*.swp
.*~
+11 -1
View File
@@ -1,7 +1,17 @@
engine.log("logginf from FIIILLLEEEE") engine.log("Log Message from Lua ;D")
engine.set_color (1., 0., 0.) engine.set_color (1., 0., 0.)
function on_touch(x, y) function on_touch(x, y)
engine.log ("got touch at " .. tostring(x) .. "," .. tostring(y)) engine.log ("got touch at " .. tostring(x) .. "," .. tostring(y))
engine.set_color (0., 1., 0.) engine.set_color (0., 1., 0.)
end end
function update(dt)
r,g,b = engine.get_color()
r = math.max(0., r - dt * 0.4)
g = math.max(0., g - dt * 0.4)
b = math.max(0., b - dt * 0.4)
engine.set_color (r, g, b)
end
+47 -24
View File
@@ -43,7 +43,15 @@ public:
} }
}; };
int l_graphics_setcolor(lua_State *L) { int l_get_color (lua_State *L) {
lua_pushnumber (L, graphics->color[0]);
lua_pushnumber (L, graphics->color[1]);
lua_pushnumber (L, graphics->color[2]);
return 3;
}
int l_set_color(lua_State *L) {
SDL_Log ("[Lua] setcolor was called"); SDL_Log ("[Lua] setcolor was called");
if (lua_gettop(L) != 3) { if (lua_gettop(L) != 3) {
@@ -51,32 +59,32 @@ int l_graphics_setcolor(lua_State *L) {
return 0; return 0;
} }
float r = 0.f; float r = 0.f;
if (!lua_isnumber(L, -3)) { if (!lua_isnumber(L, -3)) {
SDL_Log("[Lua] engine.set_color error: invalid color"); SDL_Log("[Lua] engine.set_color error: invalid color");
return 0; return 0;
} }
r = (float) lua_tonumber (L, -3); r = (float) lua_tonumber (L, -3);
float g = 0.f; float g = 0.f;
if (!lua_isnumber(L, -2)) { if (!lua_isnumber(L, -2)) {
SDL_Log("[Lua] engine.set_color error: invalid color"); SDL_Log("[Lua] engine.set_color error: invalid color");
return 0; return 0;
} }
g = (float) lua_tonumber (L, -2); g = (float) lua_tonumber (L, -2);
float b = 0.f; float b = 0.f;
if (!lua_isnumber(L, -1)) { if (!lua_isnumber(L, -1)) {
SDL_Log("[Lua] engine.set_color error: invalid color"); SDL_Log("[Lua] engine.set_color error: invalid color");
return 0; return 0;
} }
b = (float) lua_tonumber (L, -1); b = (float) lua_tonumber (L, -1);
SDL_Log ("[Lua] got color %f, %f, %f", r, g, b ); SDL_Log ("[Lua] got color %f, %f, %f", r, g, b );
graphics->color[0] = r; graphics->color[0] = r;
graphics->color[1] = g; graphics->color[1] = g;
graphics->color[2] = b; graphics->color[2] = b;
return 0; return 0;
} }
@@ -91,7 +99,8 @@ int l_log (lua_State *L) {
} }
static const struct luaL_Reg enginelib[] = { static const struct luaL_Reg enginelib[] = {
{"set_color", l_graphics_setcolor}, {"get_color", l_get_color},
{"set_color", l_set_color},
{"log", l_log}, {"log", l_log},
{NULL, NULL} {NULL, NULL}
}; };
@@ -138,6 +147,14 @@ void l_call_on_touch (lua_State *L, int x, int y) {
} }
} }
void l_call_update (lua_State *L, float dt) {
lua_getglobal (L, "update");
if (lua_isfunction(L, -1)) {
lua_pushnumber (L, dt);
lua_pcall (L, 1, 0, 0);
}
}
int main(int argc, char * argv[]) int main(int argc, char * argv[])
{ {
int width = 1080; int width = 1080;
@@ -190,6 +207,8 @@ int main(int argc, char * argv[])
SDL_Log ("could not open file"); SDL_Log ("could not open file");
} }
unsigned int last_time = SDL_GetTicks();
//Game Loop //Game Loop
SDL_Event event; SDL_Event event;
auto done = false; auto done = false;
@@ -286,6 +305,10 @@ int main(int argc, char * argv[])
} }
} }
unsigned int current_time = SDL_GetTicks();
l_call_update(L, static_cast<float>(current_time - last_time) * 0.001f);
last_time = current_time;
graphics->update(); graphics->update();
SDL_GL_SwapWindow(window); SDL_GL_SwapWindow(window);
} }