Bladeren bron

Simplify LuaStack

Daniele Bartolini 12 jaren geleden
bovenliggende
commit
6c7d9f3fb3
2 gewijzigde bestanden met toevoegingen van 148 en 189 verwijderingen
  1. 44 146
      engine/lua/LuaStack.cpp
  2. 104 43
      engine/lua/LuaStack.h

+ 44 - 146
engine/lua/LuaStack.cpp

@@ -35,177 +35,51 @@ namespace crown
 {
 
 static const int32_t 	LUA_VEC2_BUFFER_SIZE = 4096;
-static Vec2 			vec2_buffer[LUA_VEC2_BUFFER_SIZE];
-static uint32_t 		vec2_used = 0;
+static Vec2 			g_vec2_buffer[LUA_VEC2_BUFFER_SIZE];
+static uint32_t 		g_vec2_used = 0;
 
 static const int32_t 	LUA_VEC3_BUFFER_SIZE = 4096;
-static Vec3 			vec3_buffer[LUA_VEC3_BUFFER_SIZE];
-static uint32_t 		vec3_used = 0;
+static Vec3 			g_vec3_buffer[LUA_VEC3_BUFFER_SIZE];
+static uint32_t 		g_vec3_used = 0;
 
 static const int32_t 	LUA_MAT4_BUFFER_SIZE = 4096;
-static Mat4 			mat4_buffer[LUA_MAT4_BUFFER_SIZE];
-static uint32_t 		mat4_used = 0;
+static Mat4 			g_mat4_buffer[LUA_MAT4_BUFFER_SIZE];
+static uint32_t 		g_mat4_used = 0;
 
 static const int32_t 	LUA_QUAT_BUFFER_SIZE = 4096;
-static Quat 			quat_buffer[LUA_QUAT_BUFFER_SIZE];
-static uint32_t 		quat_used = 0;
+static Quat 			g_quat_buffer[LUA_QUAT_BUFFER_SIZE];
+static uint32_t 		g_quat_used = 0;
 
 //-----------------------------------------------------------------------------
 static Vec2* next_vec2(const Vec2& v)
 {
-	CE_ASSERT(vec2_used < LUA_VEC2_BUFFER_SIZE, "Maximum number of Vec2 reached");
+	CE_ASSERT(g_vec2_used < LUA_VEC2_BUFFER_SIZE, "Maximum number of Vec2 reached");
 
-	return &(vec2_buffer[vec2_used++] = v);
+	return &(g_vec2_buffer[g_vec2_used++] = v);
 }
 
 //-----------------------------------------------------------------------------
 static Vec3* next_vec3(const Vec3& v)
 {
-	CE_ASSERT(vec3_used < LUA_VEC3_BUFFER_SIZE, "Maximum number of Vec3 reached");
+	CE_ASSERT(g_vec3_used < LUA_VEC3_BUFFER_SIZE, "Maximum number of Vec3 reached");
 
-	return &(vec3_buffer[vec3_used++] = v);
+	return &(g_vec3_buffer[g_vec3_used++] = v);
 }
 
 //-----------------------------------------------------------------------------
 static Mat4* next_mat4(const Mat4& m)
 {
-	CE_ASSERT(mat4_used < LUA_MAT4_BUFFER_SIZE, "Maximum number of Mat4 reached");
+	CE_ASSERT(g_mat4_used < LUA_MAT4_BUFFER_SIZE, "Maximum number of Mat4 reached");
 
-	return &(mat4_buffer[mat4_used++] = m);
+	return &(g_mat4_buffer[g_mat4_used++] = m);
 }
 
 //-----------------------------------------------------------------------------
 static Quat* next_quat(const Quat& q)
 {
-	CE_ASSERT(quat_used < LUA_QUAT_BUFFER_SIZE, "Maximum number of Quat reached");
+	CE_ASSERT(g_quat_used < LUA_QUAT_BUFFER_SIZE, "Maximum number of Quat reached");
 
-	return &(quat_buffer[quat_used++] = q);
-}
-
-//-----------------------------------------------------------------------------	
-LuaStack::LuaStack(lua_State* L)
-{
-	m_state = L;
-}
-
-//-----------------------------------------------------------------------------
-lua_State* LuaStack::state()
-{
-	return m_state;
-}
-
-//-----------------------------------------------------------------------------
-int32_t LuaStack::num_args()
-{
-	return lua_gettop(m_state);
-}
-
-//-----------------------------------------------------------------------------
-void LuaStack::push_bool(bool value)
-{
-	lua_pushboolean(m_state, value);
-}
-
-//-----------------------------------------------------------------------------
-void LuaStack::push_int32(int32_t value)
-{
-	lua_pushinteger(m_state, value);
-}
-
-//-----------------------------------------------------------------------------
-void LuaStack::push_uint32(uint32_t value)
-{
-	lua_pushinteger(m_state, value);
-}
-
-//-----------------------------------------------------------------------------
-void LuaStack::push_int64(int64_t value)
-{
-	lua_pushinteger(m_state, value);
-}
-
-//-----------------------------------------------------------------------------
-void LuaStack::push_uint64(uint64_t value)
-{
-	lua_pushinteger(m_state, value);
-}
-
-//-----------------------------------------------------------------------------
-void LuaStack::push_float(float value)
-{
-	lua_pushnumber(m_state, value);
-}
-
-//-----------------------------------------------------------------------------
-void LuaStack::push_string(const char* s)
-{
-	lua_pushstring(m_state, s);
-}
-
-//-----------------------------------------------------------------------------
-void LuaStack::push_literal(const char* s, size_t len)
-{
-	lua_pushlstring(m_state, s, len);
-}
-
-//-----------------------------------------------------------------------------
-void LuaStack::push_lightdata(void* data)
-{
-	lua_pushlightuserdata(m_state, data);
-}
-
-//-----------------------------------------------------------------------------
-void LuaStack::push_vec2(const Vec2& v)
-{
-	lua_pushlightuserdata(m_state, next_vec2(v));
-}
-
-//-----------------------------------------------------------------------------
-void LuaStack::push_vec3(const Vec3& v)
-{
-	lua_pushlightuserdata(m_state, next_vec3(v));
-}
-
-//-----------------------------------------------------------------------------
-void LuaStack::push_mat4(const Mat4& m)
-{
-	lua_pushlightuserdata(m_state, next_mat4(m));
-}
-
-//-----------------------------------------------------------------------------
-void LuaStack::push_quat(const Quat& q)
-{
-	lua_pushlightuserdata(m_state, next_quat(q));
-}
-
-//-----------------------------------------------------------------------------
-bool LuaStack::get_bool(int32_t index)
-{
-	return (bool) luaL_checkinteger(m_state, index);
-}
-
-//-----------------------------------------------------------------------------
-int32_t LuaStack::get_int(int32_t index)
-{
-	return luaL_checkinteger(m_state, index);
-}
-
-//-----------------------------------------------------------------------------
-float LuaStack::get_float(int32_t index)
-{
-	return luaL_checknumber(m_state, index);
-}
-
-//-----------------------------------------------------------------------------
-const char* LuaStack::get_string(int32_t index)
-{
-	return luaL_checkstring(m_state, index);
-}
-
-//-----------------------------------------------------------------------------
-void* LuaStack::get_lightdata(int32_t index)
-{
-	return lua_touserdata(m_state, index);	
+	return &(g_quat_buffer[g_quat_used++] = q);
 }
 
 //-----------------------------------------------------------------------------
@@ -213,7 +87,7 @@ Vec2& LuaStack::get_vec2(int32_t index)
 {
 	void* v = lua_touserdata(m_state, index);
 
-	if (v < &vec2_buffer[0] || v > &vec2_buffer[LUA_VEC2_BUFFER_SIZE-1])
+	if (v < &g_vec2_buffer[0] || v > &g_vec2_buffer[LUA_VEC2_BUFFER_SIZE-1])
 	{
 		luaL_typerror(m_state, index, "Vec2");
 	}
@@ -226,7 +100,7 @@ Vec3& LuaStack::get_vec3(int32_t index)
 {
 	void* v = lua_touserdata(m_state, index);
 
-	if (v < &vec3_buffer[0] || v > &vec3_buffer[LUA_VEC3_BUFFER_SIZE-1])
+	if (v < &g_vec3_buffer[0] || v > &g_vec3_buffer[LUA_VEC3_BUFFER_SIZE-1])
 	{
 		luaL_typerror(m_state, index, "Vec3");
 	}
@@ -239,7 +113,7 @@ Mat4& LuaStack::get_mat4(int32_t index)
 {
 	void* m = lua_touserdata(m_state, index);
 
-	if (m < &mat4_buffer[0] || m > &mat4_buffer[LUA_MAT4_BUFFER_SIZE-1])
+	if (m < &g_mat4_buffer[0] || m > &g_mat4_buffer[LUA_MAT4_BUFFER_SIZE-1])
 	{
 		luaL_typerror(m_state, index, "Mat4");
 	}
@@ -252,7 +126,7 @@ Quat& LuaStack::get_quat(int32_t index)
 {
 	void* q = lua_touserdata(m_state, index);
 
-	if (q < &quat_buffer[0] || q > &quat_buffer[LUA_QUAT_BUFFER_SIZE-1])
+	if (q < &g_quat_buffer[0] || q > &g_quat_buffer[LUA_QUAT_BUFFER_SIZE-1])
 	{
 		luaL_typerror(m_state, index, "Quat");
 	}
@@ -260,4 +134,28 @@ Quat& LuaStack::get_quat(int32_t index)
 	return *(Quat*)q;
 }
 
+//-----------------------------------------------------------------------------
+void LuaStack::push_vec2(const Vec2& v)
+{
+	lua_pushlightuserdata(m_state, next_vec2(v));
+}
+
+//-----------------------------------------------------------------------------
+void LuaStack::push_vec3(const Vec3& v)
+{
+	lua_pushlightuserdata(m_state, next_vec3(v));
+}
+
+//-----------------------------------------------------------------------------
+void LuaStack::push_mat4(const Mat4& m)
+{
+	lua_pushlightuserdata(m_state, next_mat4(m));
+}
+
+//-----------------------------------------------------------------------------
+void LuaStack::push_quat(const Quat& q)
+{
+	lua_pushlightuserdata(m_state, next_quat(q));
+}
+
 } // namespace crown

+ 104 - 43
engine/lua/LuaStack.h

@@ -41,54 +41,115 @@ class LuaStack
 {
 public:
 
-							LuaStack(lua_State* L);
+	//-----------------------------------------------------------------------------	
+	LuaStack(lua_State* L) : m_state(L) {}
 
-	lua_State*				state();
+	//-----------------------------------------------------------------------------
+	lua_State* state()
+	{
+		return m_state;
+	}
 
 	/// Returns the number of elements in the stack.
 	/// When called inside a function, it can be used to count
 	/// the number of arguments passed to the function itself.
-	int32_t					num_args();
-
-	void					push_bool(bool value);
-
-	void					push_int32(int32_t value);
-
-	void					push_uint32(uint32_t value);
-
-	void					push_int64(int64_t value);
-
-	void					push_uint64(uint64_t value);
-
-	void 					push_float(float value);
-
-	void 					push_string(const char* s);
-	void					push_literal(const char* s, size_t len);
-
-	void					push_lightdata(void* data);
-
-	void					push_vec2(const Vec2& v);
-
-	void					push_vec3(const Vec3& v);
-
-	void					push_mat4(const Mat4& m);
-
-	void					push_quat(const Quat& q);
-
-	bool 					get_bool(int32_t index);
-
-	int32_t					get_int(int32_t index);
-
-	float 					get_float(int32_t index);
-
-	const char*				get_string(int32_t index);
-
-	void*					get_lightdata(int32_t index);
-
-	Vec2&					get_vec2(int32_t index);
-	Vec3&					get_vec3(int32_t index);
-	Mat4&					get_mat4(int32_t index);
-	Quat&					get_quat(int32_t index);
+	int32_t num_args()
+	{
+		return lua_gettop(m_state);
+	}
+
+	//-----------------------------------------------------------------------------
+	void push_bool(bool value)
+	{
+		lua_pushboolean(m_state, value);
+	}
+
+	//-----------------------------------------------------------------------------
+	void push_int32(int32_t value)
+	{
+		lua_pushinteger(m_state, value);
+	}
+
+	//-----------------------------------------------------------------------------
+	void push_uint32(uint32_t value)
+	{
+		lua_pushinteger(m_state, value);
+	}
+
+	//-----------------------------------------------------------------------------
+	void push_int64(int64_t value)
+	{
+		lua_pushinteger(m_state, value);
+	}
+
+	//-----------------------------------------------------------------------------
+	void push_uint64(uint64_t value)
+	{
+		lua_pushinteger(m_state, value);
+	}
+
+	//-----------------------------------------------------------------------------
+	void push_float(float value)
+	{
+		lua_pushnumber(m_state, value);
+	}
+
+	//-----------------------------------------------------------------------------
+	void push_string(const char* s)
+	{
+		lua_pushstring(m_state, s);
+	}
+
+	//-----------------------------------------------------------------------------
+	void push_literal(const char* s, size_t len)
+	{
+		lua_pushlstring(m_state, s, len);
+	}
+
+	//-----------------------------------------------------------------------------
+	void push_lightdata(void* data)
+	{
+		lua_pushlightuserdata(m_state, data);
+	}
+
+	//-----------------------------------------------------------------------------
+	bool get_bool(int32_t index)
+	{
+		return (bool) luaL_checkinteger(m_state, index);
+	}
+
+	//-----------------------------------------------------------------------------
+	int32_t get_int(int32_t index)
+	{
+		return luaL_checkinteger(m_state, index);
+	}
+
+	//-----------------------------------------------------------------------------
+	float get_float(int32_t index)
+	{
+		return luaL_checknumber(m_state, index);
+	}
+
+	//-----------------------------------------------------------------------------
+	const char* get_string(int32_t index)
+	{
+		return luaL_checkstring(m_state, index);
+	}
+
+	//-----------------------------------------------------------------------------
+	void* get_lightdata(int32_t index)
+	{
+		return lua_touserdata(m_state, index);	
+	}
+
+	Vec2& get_vec2(int32_t index);
+	Vec3& get_vec3(int32_t index);
+	Mat4& get_mat4(int32_t index);
+	Quat& get_quat(int32_t index);
+	void push_vec2(const Vec2& v);
+	void push_vec3(const Vec3& v);
+	void push_mat4(const Mat4& m);
+	void push_quat(const Quat& q);
 
 private: