Просмотр исходного кода

Script utility implemented in lua environment

mikymod 12 лет назад
Родитель
Сommit
b6ad9619f5
4 измененных файлов с 122 добавлено и 38 удалено
  1. 28 7
      game/lua/init.lua
  2. 16 0
      game/lua/script.lua
  3. 42 9
      src/ScriptSystem.cpp
  4. 36 22
      src/ScriptSystem.h

+ 28 - 7
game/lua/init.lua

@@ -9,15 +9,14 @@ require("mat4")
 require("quat")
 require("math_utils")
 require("camera")
+require("script")
 
 --------------------------------------------------------------
 --------------------------------------------------------------
 --------------------------------------------------------------
-print("-- testing Vec3 --\n")
+print("-- Testing Vec3 --\n")
 local pos = Vec3.vec3(1.0, 1.0, 1.0)
 
-
-
 --------------------------------------------------------------
 --------------------------------------------------------------
 --------------------------------------------------------------
@@ -44,20 +43,42 @@ print("\n")
 --------------------------------------------------------------
 --------------------------------------------------------------
 --------------------------------------------------------------
+print("-- Testing MathUtils --\n")
 
-local s = Math.sin(0.0)
 
 
-print("sin of 0 is " .. s)
+print("sin of 0 is " .. Math.sin(0.0))
 
 --------------------------------------------------------------
 --------------------------------------------------------------
 --------------------------------------------------------------
+print("-- Testing Camera --\n")
 
 local cam = Camera.camera(pos, 90.0, 1.6)
 
-print(Camera.position(cam).x)
-print(Camera.fov(cam))
+print("@move forward by 1 meter")
+print("x:" .. Camera.position(cam).x)
+print("y:" .. Camera.position(cam).y)
+print("z:" .. Camera.position(cam).z)
+
+for i=1,10 do	
+	Camera.move_forward(cam, 1.0);
+	print("@move forward by 1 meter")
+	print("x:" .. Camera.position(cam).x)
+	print("y:" .. Camera.position(cam).y)
+	print("z:" .. Camera.position(cam).z)
+end
+
+--------------------------------------------------------------
+--------------------------------------------------------------
+--------------------------------------------------------------
+print("-- Testing Script --\n")
+
+print(Script.vec3_used())
+print(Script.mat4_used())
+print(Script.quat_used())
+
+
 
 
 

+ 16 - 0
game/lua/script.lua

@@ -0,0 +1,16 @@
+local ffi = require("ffi")
+
+ffi.cdef
+[[
+	uint32_t script_system_vec3_used();
+
+	uint32_t script_system_mat4_used();
+
+	uint32_t script_system_quat_used();
+]]
+
+Script = {}
+
+Script.vec3_used = lib.script_system_vec3_used
+Script.mat4_used = lib.script_system_mat4_used
+Script.quat_used = lib.script_system_quat_used

+ 42 - 9
src/ScriptSystem.cpp

@@ -58,12 +58,6 @@ m_quat_count(0)
 {
 }
 
-//-----------------------------------------------------------
-ScriptSystem::~ScriptSystem()
-{
-
-}
-
 //-----------------------------------------------------------
 void ScriptSystem::load(ScriptResource* script)
 {
@@ -84,7 +78,7 @@ void ScriptSystem::unload(ScriptResource* resource)
 }
 
 //-----------------------------------------------------------
-Vec3* ScriptSystem::get_next_vec3(float nx, float ny, float nz)
+Vec3* ScriptSystem::next_vec3(float nx, float ny, float nz)
 {
 	uint32_t current = m_vec3_count;
 
@@ -98,7 +92,7 @@ Vec3* ScriptSystem::get_next_vec3(float nx, float ny, float nz)
 }
 
 //-----------------------------------------------------------
-Mat4* ScriptSystem::get_next_mat4(float r1c1, float r2c1, float r3c1, float r1c2, float r2c2, float r3c2, float r1c3, float r2c3, float r3c3)
+Mat4* ScriptSystem::next_mat4(float r1c1, float r2c1, float r3c1, float r1c2, float r2c2, float r3c2, float r1c3, float r2c3, float r3c3)
 {
 	uint32_t current = m_mat4_count;
 
@@ -125,7 +119,7 @@ Mat4* ScriptSystem::get_next_mat4(float r1c1, float r2c1, float r3c1, float r1c2
 }
 
 //-----------------------------------------------------------
-Quat* ScriptSystem::get_next_quat(float angle, const Vec3* v)
+Quat* ScriptSystem::next_quat(float angle, const Vec3* v)
 {
 	uint32_t current = m_quat_count;
 
@@ -135,6 +129,25 @@ Quat* ScriptSystem::get_next_quat(float angle, const Vec3* v)
 	m_quat_count++;
 
 	return &m_quat_list[current];
+}
+
+uint32_t ScriptSystem::vec3_used()
+{
+	return m_vec3_count;
+}
+
+uint32_t ScriptSystem::mat4_used()
+{
+	return m_mat4_count;
+}
+
+uint32_t ScriptSystem::quat_used()
+{
+	return m_quat_count;
+}
+
+uint32_t temp_used()
+{
 
 }
 
@@ -145,5 +158,25 @@ ScriptSystem* scripter()
 	return &g_script;
 }
 
+extern "C"
+{
+
+uint32_t script_system_vec3_used()
+{
+	return scripter()->vec3_used();
+}
+
+uint32_t script_system_mat4_used()
+{
+	return scripter()->mat4_used();
+}
+
+uint32_t script_system_quat_used()
+{
+	return scripter()->quat_used();
+}
+
+} // extern "C"
+
 
 } // namespace crown

+ 36 - 22
src/ScriptSystem.h

@@ -20,21 +20,21 @@ namespace crown
 class LuaState
 {
 public:
-							/// Constructor, private for singleton
-							LuaState();
-							/// Destructor
-							~LuaState();
-							/// Load lua chunk as buffer
-	int32_t 				load_buffer(const char* buf, size_t len);
-							/// Load lua chunk as string
-	int32_t					load_string(const char* str);
-							/// Executes lua chunk loaded in stack
-	int32_t 				execute();
+								/// Constructor, private for singleton
+								LuaState();
+								/// Destructor
+								~LuaState();
+								/// Load lua chunk as buffer
+	int32_t 					load_buffer(const char* buf, size_t len);
+								/// Load lua chunk as string
+	int32_t						load_string(const char* str);
+								/// Executes lua chunk loaded in stack
+	int32_t 					execute();
 
 private:
 
-							/// Lua state incapsulated by this class
-	lua_State*				m_state;
+								/// Lua state incapsulated by this class
+	lua_State*					m_state;
 };
 
 /// ScriptSystem allows to execute lua code or bytecode chunks
@@ -44,23 +44,28 @@ class ScriptSystem
 public:
 								/// Constructor
 								ScriptSystem();
-								///	Destructor
-								~ScriptSystem();
-								/// Load script resource
+
+								/// Loads script resource
 	void						load(ScriptResource* script);
-								/// Execute 
+								/// Executes
 	void						execute();
-								/// Unload script resource
+								/// Unloads script resource
 	void						unload(ScriptResource* script);
 								/// Returns the first free Vec3
-	Vec3*						get_next_vec3(float nx, float ny, float nz);
+	Vec3*						next_vec3(float nx, float ny, float nz);
 								/// Returns the first free Mat4
-	Mat4*						get_next_mat4(float r1c1, float r2c1, float r3c1, float r1c2, float r2c2, float r3c2, float r1c3, float r2c3, float r3c3);	
-								/// Return the first free Quat
-	Quat*						get_next_quat(float angle, const Vec3* v);
+	Mat4*						next_mat4(float r1c1, float r2c1, float r3c1, float r1c2, float r2c2, float r3c2, float r1c3, float r2c3, float r3c3);	
+								/// Returns the first free Quat
+	Quat*						next_quat(float angle, const Vec3* v);
+								/// Returns the number of vec3 used in lua environment
+	uint32_t					vec3_used();
+								/// Returns the number of mat4 used in lua environment
+	uint32_t					mat4_used();
+								/// Returns the number of quat used in lua environment
+	uint32_t 					quat_used();
 
 								/// Max number of temporary objects allowed
-	static const uint32_t		MAX_TEMP_OBJECTS = 2048;
+	static const uint32_t		MAX_TEMP_OBJECTS = 1024;
 
 
 private:
@@ -86,4 +91,13 @@ private:
 
 ScriptSystem* scripter();
 
+extern "C"
+{
+	uint32_t script_system_vec3_used();
+
+	uint32_t script_system_mat4_used();
+
+	uint32_t script_system_quat_used();
+}
+
 } // namespace crown