Sfoglia il codice sorgente

temporary object implemented for lua environment, now ScriptSystem is Singleton

mikymod 12 anni fa
parent
commit
7e79caf7e3
2 ha cambiato i file con 128 aggiunte e 73 eliminazioni
  1. 77 32
      src/ScriptSystem.cpp
  2. 51 41
      src/ScriptSystem.h

+ 77 - 32
src/ScriptSystem.cpp

@@ -3,6 +3,9 @@
 
 namespace crown
 {
+//-----------------------------------------------------------
+// Lua State
+//-----------------------------------------------------------
 
 //-----------------------------------------------------------
 LuaState::LuaState()
@@ -15,22 +18,7 @@ LuaState::LuaState()
 //-----------------------------------------------------------
 LuaState::~LuaState()
 {
-	if (m_instance != NULL)
-	{
-		lua_close(m_state);
-		delete m_instance;
-	}
-}
-
-//-----------------------------------------------------------
-LuaState* LuaState::instance()
-{
-	if (m_instance == NULL)
-	{
-		m_instance = new LuaState();
-	}
-
-	return m_instance;
+	lua_close(m_state);
 }
 
 //-----------------------------------------------------------
@@ -57,47 +45,104 @@ int32_t LuaState::execute()
     return s;
 }
 
+//-----------------------------------------------------------
+// ScriptSystem
+//-----------------------------------------------------------
 
 //-----------------------------------------------------------
 ScriptSystem::ScriptSystem() :
-	m_script_count(0)
+m_state(),
+m_vec3_count(0),
+m_mat4_count(0),
+m_quat_count(0)
 {
 }
 
 //-----------------------------------------------------------
 ScriptSystem::~ScriptSystem()
 {
-	// FIXME
+
+}
+
+//-----------------------------------------------------------
+void ScriptSystem::load(ScriptResource* script)
+{
+	assert(m_state.load_buffer((char*)script->data(), script->length()) == 0);
+}
+
+//-----------------------------------------------------------
+void ScriptSystem::execute()
+{
+	assert(m_state.execute() == 0);	
 }
 
 //-----------------------------------------------------------
-ScriptId ScriptSystem::load(ScriptResource* script)
+void ScriptSystem::unload(ScriptResource* resource)
 {
-	assert(LuaState::instance()->load_buffer((char*)script->data(), script->length()) == 0);
+	(void*) resource;
+	// FIXME
+}
 
-	ScriptId id;
-	id.index = m_script_count;
-	id.id = 0;
+//-----------------------------------------------------------
+Vec3* ScriptSystem::get_next_vec3(float nx, float ny, float nz)
+{
+	uint32_t current = m_vec3_count;
 
-	m_script[id.index].id = id;
-	m_script[id.index].script_resource = script;
+	m_vec3_list[current].x = nx;
+	m_vec3_list[current].y = ny;
+	m_vec3_list[current].z = nz;
 
-	m_script_count++;
+	m_vec3_count++;
 
-	return id;
+	return &m_vec3_list[current];
 }
 
 //-----------------------------------------------------------
-void ScriptSystem::execute()
+Mat4* ScriptSystem::get_next_mat4(float r1c1, float r2c1, float r3c1, float r1c2, float r2c2, float r3c2, float r1c3, float r2c3, float r3c3)
 {
-	assert(LuaState::instance()->execute() == 0);	
+	uint32_t current = m_mat4_count;
+
+	m_mat4_list[current].m[0] 	= r1c1;
+	m_mat4_list[current].m[1] 	= r2c1;
+	m_mat4_list[current].m[2] 	= r3c1;
+	m_mat4_list[current].m[3] 	= 0;
+	m_mat4_list[current].m[4] 	= r1c2;
+	m_mat4_list[current].m[5] 	= r2c2;
+	m_mat4_list[current].m[6] 	= r3c2;
+	m_mat4_list[current].m[7] 	= 0;
+	m_mat4_list[current].m[8] 	= r1c3;
+	m_mat4_list[current].m[9] 	= r2c3;
+	m_mat4_list[current].m[10] 	= r3c3;
+	m_mat4_list[current].m[11] 	= 0;
+	m_mat4_list[current].m[12] 	= 0;
+	m_mat4_list[current].m[13] 	= 0;
+	m_mat4_list[current].m[14] 	= 0;
+	m_mat4_list[current].m[15] 	= 1;
+
+	m_mat4_count++;
+
+	return &m_mat4_list[current];
 }
 
 //-----------------------------------------------------------
-void unload(ScriptResource* resource)
+Quat* ScriptSystem::get_next_quat(float angle, const Vec3* v)
 {
-	(void*) resource;
-	// FIXME
+	uint32_t current = m_quat_count;
+
+	m_quat_list[current].w = angle;
+	m_quat_list[current].v = *v;
+
+	m_quat_count++;
+
+	return &m_quat_list[current];
+
+}
+
+ScriptSystem g_script;
+
+ScriptSystem* scripter()
+{
+	return &g_script;
 }
 
 

+ 51 - 41
src/ScriptSystem.h

@@ -1,35 +1,29 @@
 #pragma once
 
+#include "lua.hpp"
+
 #include "Types.h"
 #include "String.h"
-#include "lua.hpp"
 #include "ScriptResource.h"
 
-namespace crown
-{
+#include "Vec3.h"
+#include "Mat4.h"
+#include "Quat.h"
 
-//------------------------------------------------------------------
-struct ScriptId
-{
-	uint16_t	index;
-	uint16_t	id;
-};
 
-//------------------------------------------------------------------
-struct Script
+namespace crown
 {
-	ScriptId		id;
-	ScriptResource* script_resource;
-};
 
-/// Singleton class that abstracts lua state
+/// Abstraction of lua state.
+/// It provides 2 ways for loading lua code, as a buffer or as a string.
+/// It must be included only by ScriptSystem. 
 class LuaState
 {
 public:
+							/// Constructor, private for singleton
+							LuaState();
 							/// Destructor
 							~LuaState();
-							/// Get singleton instance
-	static LuaState*		instance();
 							/// Load lua chunk as buffer
 	int32_t 				load_buffer(const char* buf, size_t len);
 							/// Load lua chunk as string
@@ -38,42 +32,58 @@ public:
 	int32_t 				execute();
 
 private:
-							/// Constructor, private for singleton
-							LuaState();
+
 							/// Lua state incapsulated by this class
 	lua_State*				m_state;
-							/// LuaState pointer for singleton
-	static LuaState* 		m_instance;
 };
 
-LuaState* LuaState::m_instance = NULL;
-
-/// Script System allows to execute lua code or lua bytecode chunk
+/// ScriptSystem allows to execute lua code or bytecode chunks
+///
 class ScriptSystem
 {
 public:
-							/// Max number of scripts which can be loaded by this system
-	static const int32_t	MAX_SCRIPTS = 256;
+								/// Constructor
+								ScriptSystem();
+								///	Destructor
+								~ScriptSystem();
+								/// Load script resource
+	void						load(ScriptResource* script);
+								/// Execute 
+	void						execute();
+								/// Unload script resource
+	void						unload(ScriptResource* script);
+								/// Returns the first free Vec3
+	Vec3*						get_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);
 
+								/// Max number of temporary objects allowed
+	static const uint32_t		MAX_TEMP_OBJECTS = 2048;
 
-							/// Constructor
-							ScriptSystem();
-							///	Destructor
-							~ScriptSystem();
-							/// Load scripr resource
-	ScriptId				load(ScriptResource* script);
-							/// Execute 
-	void					execute();
-							/// Unload script resource
-	void					unload(ScriptResource* script);
 
 private:
-							/// Number of scripts already loaded
-	uint32_t				m_script_count;
-							/// Resource handle
-	Script					m_script[MAX_SCRIPTS];
+								// Disable coping
+								ScriptSystem(const ScriptSystem&);
+								ScriptSystem& operator=(const ScriptSystem&);
 
-	friend class ScriptResource;
+	LuaState 					m_state;
+
+								/// Vectors used by lua environment
+	Vec3						m_vec3_list[MAX_TEMP_OBJECTS];
+								/// Counter which points to the next free Vec3
+	uint32_t					m_vec3_count;
+								/// Matrix used by lua environment
+	Mat4						m_mat4_list[MAX_TEMP_OBJECTS];
+								/// Counter which points to the next free Mat4
+	uint32_t					m_mat4_count;
+								/// Quaternions used by lua environment
+	Quat						m_quat_list[MAX_TEMP_OBJECTS];
+								/// Counter which points to the nect free Quat
+	uint32_t					m_quat_count;
 };
 
+ScriptSystem* scripter();
+
 } // namespace crown