2
0
Эх сурвалжийг харах

game sample which provides the idea of using lua, Game.cpp is the same of lua.cpp

mikymod 12 жил өмнө
parent
commit
5a1e754619

+ 45 - 0
game/Game.cpp

@@ -0,0 +1,45 @@
+#include "lua.hpp"
+#include "Device.h"
+#include "Game.h"
+
+
+namespace crown
+{
+
+lua_State* state;
+int z;
+
+void init()
+{
+	state = luaL_newstate();
+	luaL_openlibs(state);
+
+	if (luaL_loadfile(state, "lua/lua/game.lua.script") || lua_pcall(state, 0, 0, 0))
+	{
+		os::printf("error: %s", lua_tostring(state, -1));
+	}
+
+	lua_getglobal(state, "init");
+
+	lua_pcall(state, 0, 0, 0);
+}
+
+void shutdown()
+{
+	lua_getglobal(state, "shutdown");
+
+	lua_pcall(state, 0, 0, 0);
+
+	lua_close(state);
+}
+
+void frame(float dt)
+{
+	lua_getglobal(state, "frame");
+
+	lua_pushnumber(state, dt);
+
+	lua_pcall(state, 1, 0, 0);
+}
+
+}

+ 2 - 2
game/lua/game.lua

@@ -6,6 +6,6 @@ function shutdown()
 	print("Lua Shutdown called.")
 end
 
-function update(dt)
-	print("Lua Update called.")
+function frame(dt)
+	print("Lua Frame called.")
 end

+ 8 - 2
samples/lua/CMakeLists.txt

@@ -1,5 +1,11 @@
 cmake_minimum_required(VERSION 2.8)
 
-add_executable(luas lua.cpp)
-target_link_libraries(luas crown)
+set (SRC
+	lua.cpp
+)
+
+add_library (game SHARED ${SRC})
+target_link_libraries(game crown)
+
+install (TARGETS game DESTINATION bin/lua)
 

+ 39 - 5
samples/lua/lua.cpp

@@ -1,12 +1,46 @@
-#include <iostream>
+#include "lua.hpp"
 #include "Crown.h"
-#include <unistd.h>
+#include "Game.h"
 
-using namespace crown;
 
-int main(int argc, char** argv)
+namespace crown
 {
 
+lua_State* state;
+int z;
 
-  return 0;
+void init()
+{
+	state = luaL_newstate();
+	luaL_openlibs(state);
+
+	if (luaL_loadfile(state, "lua/lua/game.lua.script") || lua_pcall(state, 0, 0, 0))
+	{
+		os::printf("error: %s", lua_tostring(state, -1));
+	}
+
+	lua_getglobal(state, "init");
+
+	lua_pcall(state, 0, 0, 0);
+}
+
+void shutdown()
+{
+	lua_getglobal(state, "shutdown");
+
+	lua_pcall(state, 0, 0, 0);
+
+	lua_close(state);
+}
+
+void frame(float dt)
+{
+	lua_getglobal(state, "frame");
+
+	lua_pushnumber(state, dt);
+
+	lua_pcall(state, 1, 0, 0);
 }
+
+
+}

+ 3 - 3
samples/terrain/CMakeLists.txt

@@ -9,7 +9,7 @@ set (HEADERS
 	Terrain.h
 )
 
-add_library (game SHARED ${SRC} ${HEADERS})
-target_link_libraries(game crown)
+#add_library (game SHARED ${SRC} ${HEADERS})
+#target_link_libraries(game crown)
 
-install (TARGETS game DESTINATION bin/terrain)
+#install (TARGETS game DESTINATION bin/terrain)