Răsfoiți Sursa

actual cool scripting

Martin Felis 12 ani în urmă
părinte
comite
3614649365
3 a modificat fișierele cu 63 adăugiri și 29 ștergeri
  1. 1 0
      .gitignore
  2. 11 1
      assets/testfile.lua
  3. 51 28
      jni/src/main.cpp

+ 1 - 0
.gitignore

@@ -2,3 +2,4 @@ bin/
 libs/
 obj/
 .*.swp
+.*~

+ 11 - 1
assets/testfile.lua

@@ -1,7 +1,17 @@
-engine.log("logginf from FIIILLLEEEE")
+engine.log("Log Message from Lua ;D")
 engine.set_color (1., 0., 0.)
 
 function on_touch(x, y)
   engine.log ("got touch at " .. tostring(x) .. "," .. tostring(y))
   engine.set_color (0., 1., 0.)
 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

+ 51 - 28
jni/src/main.cpp

@@ -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");
 
 	if (lua_gettop(L) != 3) {
@@ -51,32 +59,32 @@ int l_graphics_setcolor(lua_State *L) {
 		return 0;
 	}
 
-		float r = 0.f;
-		if (!lua_isnumber(L, -3)) {
-			SDL_Log("[Lua] engine.set_color error: invalid color");
-			return 0;
-		}
-		r = (float) lua_tonumber (L, -3);
-
-		float g = 0.f;
-		if (!lua_isnumber(L, -2)) {
-			SDL_Log("[Lua] engine.set_color error: invalid color");
-			return 0;
-		}
-		g = (float) lua_tonumber (L, -2);
-
-		float b = 0.f;
-		if (!lua_isnumber(L, -1)) {
-			SDL_Log("[Lua] engine.set_color error: invalid color");
-			return 0;
-		}
-		b = (float) lua_tonumber (L, -1);
-
-		SDL_Log ("[Lua] got color %f, %f, %f", r, g, b );
-
-		graphics->color[0] = r;
-		graphics->color[1] = g;
-		graphics->color[2] = b;
+	float r = 0.f;
+	if (!lua_isnumber(L, -3)) {
+		SDL_Log("[Lua] engine.set_color error: invalid color");
+		return 0;
+	}
+	r = (float) lua_tonumber (L, -3);
+
+	float g = 0.f;
+	if (!lua_isnumber(L, -2)) {
+		SDL_Log("[Lua] engine.set_color error: invalid color");
+		return 0;
+	}
+	g = (float) lua_tonumber (L, -2);
+
+	float b = 0.f;
+	if (!lua_isnumber(L, -1)) {
+		SDL_Log("[Lua] engine.set_color error: invalid color");
+		return 0;
+	}
+	b = (float) lua_tonumber (L, -1);
+
+	SDL_Log ("[Lua] got color %f, %f, %f", r, g, b );
+
+	graphics->color[0] = r;
+	graphics->color[1] = g;
+	graphics->color[2] = b;
 
 	return 0;
 }
@@ -91,7 +99,8 @@ int l_log (lua_State *L) {
 }
 
 static const struct luaL_Reg enginelib[] = {
-		{"set_color", l_graphics_setcolor},
+		{"get_color", l_get_color},
+		{"set_color", l_set_color},
 		{"log", l_log},
 		{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 width = 1080;
@@ -190,6 +207,8 @@ int main(int argc, char * argv[])
     	SDL_Log ("could not open file");
     }
 
+    unsigned int last_time = SDL_GetTicks();
+
     //Game Loop
     SDL_Event event;
     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();
         SDL_GL_SwapWindow(window);
     }