Kaynağa Gözat

Use Vector3 internally to represent Vector2

Daniele Bartolini 11 yıl önce
ebeveyn
işleme
cb1afd7e6d

+ 0 - 4
engine/config.h

@@ -126,10 +126,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 	#define CE_MAX_DEBUG_LINES 2 * 1024 // Per DebugLine
 #endif // CE_MAX
 
-#ifndef CE_MAX_LUA_VECTOR2
-	#define CE_MAX_LUA_VECTOR2 4096
-#endif // CE_MAX
-
 #ifndef CE_MAX_LUA_VECTOR3
 	#define CE_MAX_LUA_VECTOR3 4096
 #endif // CE_MAX

+ 5 - 6
engine/lua/lua_stack.h

@@ -417,10 +417,11 @@ struct LuaStack
 		return l;
 	}
 
-	Vector2& get_vector2(int32_t index)
+	Vector2 get_vector2(int32_t index)
 	{
-		void* v = CHECKLIGHTDATA(_L, index, lua_globals::is_vector2, "Vector2");
-		return *(Vector2*)v;
+		void* v = CHECKLIGHTDATA(_L, index, lua_globals::is_vector3, "Vector2");
+		Vector3& vv = *(Vector3*)v;
+		return Vector2(vv.x, vv.y);
 	}
 
 	Vector3& get_vector3(int32_t index)
@@ -451,9 +452,7 @@ struct LuaStack
 
 	void push_vector2(const Vector2& v)
 	{
-		lua_pushlightuserdata(_L, lua_globals::next_vector2(v));
-		luaL_getmetatable(_L, "Lightuserdata_mt");
-		lua_setmetatable(_L, -2);
+		push_vector3(Vector3(v.x, v.y, 0.0f));
 	}
 
 	void push_vector3(const Vector3& v)

+ 30 - 125
engine/lua/lua_system.cpp

@@ -68,8 +68,6 @@ extern void load_sprite(LuaEnvironment& env);
 extern void load_string_setting(LuaEnvironment& env);
 extern void load_touch(LuaEnvironment& env);
 extern void load_unit(LuaEnvironment& env);
-extern void load_vector2(LuaEnvironment& env);
-extern void load_vector2box(LuaEnvironment& env);
 extern void load_vector3(LuaEnvironment& env);
 extern void load_vector3box(LuaEnvironment& env);
 extern void load_window(LuaEnvironment& env);
@@ -81,8 +79,6 @@ namespace lua_globals
 {
 	static lua_State* s_L;
 
-	static uint32_t 		s_vec2_used = 0;
-	static Vector2 			s_vec2_buffer[CE_MAX_LUA_VECTOR2];
 	static uint32_t 		s_vec3_used = 0;
 	static Vector3 			s_vec3_buffer[CE_MAX_LUA_VECTOR3];
 	static uint32_t 		s_mat4_used = 0;
@@ -132,128 +128,66 @@ namespace lua_globals
 	static int lightuserdata_add(lua_State* L)
 	{
 		LuaStack stack(L);
-
-		if (lua_globals::is_vector3(1))
-		{
-			const Vector3& a = stack.get_vector3(1);
-			const Vector3& b = stack.get_vector3(2);
-			stack.push_vector3(a + b);
-			return 1;
-		}
-
-		const Vector2& a = stack.get_vector2(1);
-		const Vector2& b = stack.get_vector2(2);
-		stack.push_vector2(a + b);
+		const Vector3& a = stack.get_vector3(1);
+		const Vector3& b = stack.get_vector3(2);
+		stack.push_vector3(a + b);
 		return 1;
 	}
 
 	static int lightuserdata_sub(lua_State* L)
 	{
 		LuaStack stack(L);
-
-		if (lua_globals::is_vector3(1))
-		{
-			const Vector3& a = stack.get_vector3(1);
-			const Vector3& b = stack.get_vector3(2);
-			stack.push_vector3(a - b);
-			return 1;
-		}
-
-		const Vector2& a = stack.get_vector2(1);
-		const Vector2& b = stack.get_vector2(2);
-		stack.push_vector2(a - b);
+		const Vector3& a = stack.get_vector3(1);
+		const Vector3& b = stack.get_vector3(2);
+		stack.push_vector3(a - b);
 		return 1;
 	}
 
 	static int lightuserdata_mul(lua_State* L)
 	{
 		LuaStack stack(L);
-
-		if (lua_globals::is_vector3(1))
-		{
-			const Vector3& a = stack.get_vector3(1);
-			const float b = stack.get_float(2);
-			stack.push_vector3(a * b);
-			return 1;
-		}
-
-		const Vector2& a = stack.get_vector2(1);
+		const Vector3& a = stack.get_vector3(1);
 		const float b = stack.get_float(2);
-		stack.push_vector2(a * b);
+		stack.push_vector3(a * b);
 		return 1;
 	}
 
 	static int lightuserdata_div(lua_State* L)
 	{
 		LuaStack stack(L);
-
-		if (lua_globals::is_vector3(1))
-		{
-			const Vector3& a = stack.get_vector3(1);
-			const float b = stack.get_float(2);
-			stack.push_vector3(a / b);
-			return 1;
-		}
-
-		const Vector2& a = stack.get_vector2(1);
+		const Vector3& a = stack.get_vector3(1);
 		const float b = stack.get_float(2);
-		stack.push_vector2(a / b);
+		stack.push_vector3(a / b);
 		return 1;
 	}
 
 	static int lightuserdata_unm(lua_State* L)
 	{
 		LuaStack stack(L);
-
-		if (lua_globals::is_vector3(1))
-		{
-			stack.push_vector3(-stack.get_vector3(1));
-			return 1;
-		}
-		stack.push_vector2(-stack.get_vector2(1));
+		stack.push_vector3(-stack.get_vector3(1));
 		return 1;
 	}
 
 	static int lightuserdata_index(lua_State* L)
 	{
 		LuaStack stack(L);
+		Vector3& v = stack.get_vector3(1);
+		const char* s = stack.get_string(2);
 
-		if (lua_globals::is_vector3(1))
+		if (string::strcmp(s, "x") == 0)
 		{
-			Vector3& v = stack.get_vector3(1);
-			const char* s = stack.get_string(2);
-
-			if (string::strcmp(s, "x") == 0)
-			{
-				stack.push_float(v.x);
-				return 1;
-			}
-			else if (string::strcmp(s, "y") == 0)
-			{
-				stack.push_float(v.y);
-				return 1;
-			}
-			else if (string::strcmp(s, "z") == 0)
-			{
-				stack.push_float(v.z);
-				return 1;
-			}
+			stack.push_float(v.x);
+			return 1;
 		}
-		else if (lua_globals::is_vector2(1))
+		else if (string::strcmp(s, "y") == 0)
 		{
-			Vector2& v = stack.get_vector2(1);
-			const char* s = stack.get_string(2);
-
-			if (string::strcmp(s, "x") == 0)
-			{
-				stack.push_float(v.x);
-				return 1;
-			}
-			else if (string::strcmp(s, "y") == 0)
-			{
-				stack.push_float(v.y);
-				return 1;
-			}
+			stack.push_float(v.y);
+			return 1;
+		}
+		else if (string::strcmp(s, "z") == 0)
+		{
+			stack.push_float(v.z);
+			return 1;
 		}
 
 		return 0;
@@ -262,26 +196,13 @@ namespace lua_globals
 	static int lightuserdata_newindex(lua_State* L)
 	{
 		LuaStack stack(L);
+		Vector3& v = stack.get_vector3(1);
+		const char* s = stack.get_string(2);
+		const float value = stack.get_float(3);
 
-		if (lua_globals::is_vector3(1))
-		{
-			Vector3& v = stack.get_vector3(1);
-			const char* s = stack.get_string(2);
-			const float value = stack.get_float(3);
-
-			if (string::strcmp(s, "x") == 0) v.x = value;
-			else if (string::strcmp(s, "y") == 0) v.y = value;
-			else if (string::strcmp(s, "z") == 0) v.z = value;
-		}
-		else if (lua_globals::is_vector2(1))
-		{
-			Vector2& v = stack.get_vector2(1);
-			const char* s = stack.get_string(2);
-			const float value = stack.get_float(3);
-
-			if (string::strcmp(s, "x") == 0) v.x = value;
-			else if (string::strcmp(s, "y") == 0) v.y = value;
-		}
+		if (string::strcmp(s, "x") == 0) v.x = value;
+		else if (string::strcmp(s, "y") == 0) v.y = value;
+		else if (string::strcmp(s, "z") == 0) v.z = value;
 
 		return 0;
 	}
@@ -320,8 +241,6 @@ namespace lua_globals
 		load_string_setting(env);
 		load_touch(env);
 		load_unit(env);
-		load_vector2(env);
-		load_vector2box(env);
 		load_vector3(env);
 		load_vector3box(env);
 		load_window(env);
@@ -392,13 +311,6 @@ namespace lua_globals
 		return s_L;
 	}
 
-	Vector2* next_vector2(const Vector2& v)
-	{
-		CE_ASSERT(s_vec2_used < CE_MAX_LUA_VECTOR2, "Maximum number of Vector2 reached");
-
-		return &(s_vec2_buffer[s_vec2_used++] = v);
-	}
-
 	Vector3* next_vector3(const Vector3& v)
 	{
 		CE_ASSERT(s_vec3_used < CE_MAX_LUA_VECTOR3, "Maximum number of Vector3 reached");
@@ -419,12 +331,6 @@ namespace lua_globals
 		return &(s_quat_buffer[s_quat_used++] = q);
 	}
 
-	bool is_vector2(int32_t index)
-	{
-		void* type = lua_touserdata(s_L, index);
-		return (type >= &s_vec2_buffer[0] && type <= &s_vec2_buffer[CE_MAX_LUA_VECTOR2 - 1]);
-	}
-
 	bool is_vector3(int32_t index)
 	{
 		void* type = lua_touserdata(s_L, index);
@@ -445,7 +351,6 @@ namespace lua_globals
 
 	void clear_temporaries()
 	{
-		s_vec2_used = 0;
 		s_vec3_used = 0;
 		s_mat4_used = 0;
 		s_quat_used = 0;

+ 1 - 4
engine/lua/lua_system.h

@@ -30,7 +30,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 namespace crown
 {
 
-struct Vector2;
 struct Vector3;
 struct Matrix4x4;
 struct Quaternion;
@@ -51,13 +50,11 @@ namespace lua_globals
 	void clear_temporaries();
 
 	/// Returns a new temporary Vector2, Vector3, Matrix4x4 or Quaternion
-	Vector2* next_vector2(const Vector2& v);
 	Vector3* next_vector3(const Vector3& v);
 	Matrix4x4* next_matrix4x4(const Matrix4x4& m);
 	Quaternion* next_quaternion(const Quaternion& q);
 
-	/// Returns whether the object at stack @a index is a Vector2, Vector3, Matrix4x4 or Quaternion
-	bool is_vector2(int32_t index);
+	/// Returns whether the object at stack @a index is a Vector3, Matrix4x4 or Quaternion
 	bool is_vector3(int32_t index);
 	bool is_matrix4x4(int32_t index);
 	bool is_quaternion(int32_t index);

+ 0 - 193
engine/lua/lua_vector2.cpp

@@ -1,193 +0,0 @@
-/*
-Copyright (c) 2013 Daniele Bartolini, Michele Rossi
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-#include "vector2.h"
-#include "lua_stack.h"
-#include "lua_environment.h"
-
-namespace crown
-{
-
-static int vector2_new(lua_State* L)
-{
-	LuaStack stack(L);
-	stack.push_vector2(Vector2(stack.get_float(1), stack.get_float(2)));
-	return 1;
-}
-
-static int vector2_ctor(lua_State* L)
-{
-	LuaStack stack(L);
-	stack.remove(1); // Remove table
-	return vector2_new(L);
-}
-
-static int vector2_x(lua_State* L)
-{
-	LuaStack stack(L);
-	stack.push_float(stack.get_vector2(1).x);
-	return 1;
-}
-
-static int vector2_y(lua_State* L)
-{
-	LuaStack stack(L);
-	stack.push_float(stack.get_vector2(1).y);
-	return 1;
-}
-
-static int vector2_set_x(lua_State* L)
-{
-	LuaStack stack(L);
-	stack.get_vector2(1).x = stack.get_float(2);
-	return 0;
-}
-
-static int vector2_set_y(lua_State* L)
-{
-	LuaStack stack(L);
-	stack.get_vector2(1).y = stack.get_float(2);
-	return 0;
-}
-
-static int vector2_values(lua_State* L)
-{
-	LuaStack stack(L);
-	Vector2& a = stack.get_vector2(1);
-	stack.push_float(a.x);
-	stack.push_float(a.y);
-	return 2;
-}
-
-static int vector2_add(lua_State* L)
-{
-	LuaStack stack(L);
-	stack.push_vector2(stack.get_vector2(1) + stack.get_vector2(2));
-	return 1;
-}
-
-static int vector2_subtract(lua_State* L)
-{
-	LuaStack stack(L);
-	stack.push_vector2(stack.get_vector2(1) - stack.get_vector2(2));
-	return 1;
-}
-
-static int vector2_multiply(lua_State* L)
-{
-	LuaStack stack(L);
-	stack.push_vector2(stack.get_vector2(1) * stack.get_float(2));
-	return 1;
-}
-
-static int vector2_divide(lua_State* L)
-{
-	LuaStack stack(L);
-	stack.push_vector2(stack.get_vector2(1) / stack.get_float(2));
-	return 1;
-}
-
-static int vector2_dot(lua_State* L)
-{
-	LuaStack stack(L);
-	stack.push_float(vector2::dot(stack.get_vector2(1), stack.get_vector2(2)));
-	return 1;
-}
-
-static int vector2_equal(lua_State* L)
-{
-	LuaStack stack(L);
-	stack.push_bool(stack.get_vector2(1) == stack.get_vector2(2));
-	return 1;
-}
-
-static int vector2_length(lua_State* L)
-{
-	LuaStack stack(L);
-	stack.push_float(vector2::length(stack.get_vector2(1)));
-	return 1;
-}
-
-static int vector2_squared_length(lua_State* L)
-{
-	LuaStack stack(L);
-	stack.push_float(vector2::squared_length(stack.get_vector2(1)));
-	return 1;
-}
-
-static int vector2_set_length(lua_State* L)
-{
-	LuaStack stack(L);
-	vector2::set_length(stack.get_vector2(1), stack.get_float(2));
-	return 0;
-}
-
-static int vector2_normalize(lua_State* L)
-{
-	LuaStack stack(L);
-	stack.push_vector2(vector2::normalize(stack.get_vector2(1)));
-	return 1;
-}
-
-static int vector2_distance(lua_State* L)
-{
-	LuaStack stack(L);
-	stack.push_float(vector2::distance(stack.get_vector2(1), stack.get_vector2(2)));
-	return 1;
-}
-
-static int vector2_angle(lua_State* L)
-{
-	LuaStack stack(L);
-	stack.push_float(vector2::angle(stack.get_vector2(1), stack.get_vector2(2)));
-	return 1;
-}
-
-void load_vector2(LuaEnvironment& env)
-{
-	env.load_module_function("Vector2", "new",            vector2_new);
-	env.load_module_function("Vector2", "x",              vector2_x);
-	env.load_module_function("Vector2", "y",              vector2_y);
-	env.load_module_function("Vector2", "set_x",          vector2_set_x);
-	env.load_module_function("Vector2", "set_y",          vector2_set_y);
-	env.load_module_function("Vector2", "values",         vector2_values);
-	env.load_module_function("Vector2", "add",            vector2_add);
-	env.load_module_function("Vector2", "subtract",       vector2_subtract);
-	env.load_module_function("Vector2", "multiply",       vector2_multiply);
-	env.load_module_function("Vector2", "divide",         vector2_divide);
-	env.load_module_function("Vector2", "dot",            vector2_dot);
-	env.load_module_function("Vector2", "equal",          vector2_equal);
-	env.load_module_function("Vector2", "length",         vector2_length);
-	env.load_module_function("Vector2", "squared_length", vector2_squared_length);
-	env.load_module_function("Vector2", "set_length",     vector2_set_length);
-	env.load_module_function("Vector2", "normalize",      vector2_normalize);
-	env.load_module_function("Vector2", "distance",       vector2_distance);
-	env.load_module_function("Vector2", "angle",          vector2_angle);
-
-	env.load_module_constructor("Vector2", vector2_ctor);
-}
-
-} // namespace crown

+ 0 - 154
engine/lua/lua_vector2_box.cpp

@@ -1,154 +0,0 @@
-/*
-Copyright (c) 2013 Daniele Bartolini, Michele Rossi
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-#include "lua_environment.h"
-#include "lua_stack.h"
-#include "vector2.h"
-#include "string_utils.h"
-
-namespace crown
-{
-
-static int vector2box_new(lua_State* L)
-{
-	LuaStack stack(L);
-
-	Vector2 v;
-	if (stack.num_args() == 1)
-	{
-		const Vector2 tv = stack.get_vector2(1);
-		v = tv;
-	}
-	else if (stack.num_args() == 2)
-	{
-		v.x = stack.get_float(1);
-		v.y = stack.get_float(2);
-	}
-
-	stack.push_vector2box(v);
-	return 1;
-}
-
-static int vector2box_ctor(lua_State* L)
-{
-	LuaStack stack(L);
-	stack.remove(1); // Remove table
-	return vector2box_new(L);
-}
-
-static int vector2box_get_value(lua_State* L)
-{
-	LuaStack stack(L);
-
-	Vector2& v = stack.get_vector2box(1);
-	const char* s = stack.get_string(2);
-
-	if (string::strcmp(s, "x") == 0)
-	{
-		stack.push_float(v.x);
-		return 1;
-	}
-	else if (string::strcmp(s, "y") == 0)
-	{
-		stack.push_float(v.y);
-		return 1;
-	}
-
-	// Never happens
-	return 0;
-}
-
-static int vector2box_set_value(lua_State* L)
-{
-	LuaStack stack(L);
-
-	Vector2& v = stack.get_vector2box(1);
-	const char* s = stack.get_string(2);
-	float value = stack.get_float(3);
-
-	if (string::strcmp(s, "x") == 0)
-	{
-		v.x = value;
-	}
-	else if (string::strcmp(s, "y") == 0)
-	{
-		v.y = value;
-	}
-
-	return 0;
-}
-
-static int vector2box_store(lua_State* L)
-{
-	LuaStack stack(L);
-
-	Vector2& v = stack.get_vector2box(1);
-
-	if (stack.num_args() == 2)
-	{
-		Vector2 tv = stack.get_vector2(2);
-
-		v = tv;
-	}
-	else if (stack.num_args() == 3)
-	{
-		v.x = stack.get_float(2);
-		v.y = stack.get_float(3);
-	}
-	return 0;
-}
-
-static int vector2box_unbox(lua_State* L)
-{
-	LuaStack stack(L);
-
-	Vector2& v = stack.get_vector2box(1);
-
-	stack.push_vector2(v);
-	return 1;
-}
-
-static int vector2box_tostring(lua_State* L)
-{
-	LuaStack stack(L);
-	Vector2& v = stack.get_vector2box(1);
-	stack.push_fstring("Vector2Box (%p)", &v);
-	return 1;
-}
-
-void load_vector2box(LuaEnvironment& env)
-{
-	env.load_module_function("Vector2Box", "new",        vector2box_new);
-	env.load_module_function("Vector2Box", "store",      vector2box_store);
-	env.load_module_function("Vector2Box", "unbox",      vector2box_unbox);
-	env.load_module_function("Vector2Box", "__index",    vector2box_get_value);
-	env.load_module_function("Vector2Box", "__newindex", vector2box_set_value);
-	env.load_module_function("Vector2Box", "__tostring", vector2box_tostring);
-	
-	env.load_module_constructor("Vector2Box", vector2box_ctor);
-}
-
-} // namespace crown

+ 17 - 0
engine/lua/lua_vector3.cpp

@@ -188,6 +188,20 @@ static int vector3_angle(lua_State* L)
 	return 1;
 }
 
+static int vector2_new(lua_State* L)
+{
+	LuaStack stack(L);
+	stack.push_vector2(Vector2(stack.get_float(1), stack.get_float(2)));
+	return 1;
+}
+
+static int vector2_ctor(lua_State* L)
+{
+	LuaStack stack(L);
+	stack.remove(1); // Remove table
+	return vector2_new(L);
+}
+
 void load_vector3(LuaEnvironment& env)
 {
 	env.load_module_function("Vector3", "new",            vector3_new);
@@ -213,6 +227,9 @@ void load_vector3(LuaEnvironment& env)
 	env.load_module_function("Vector3", "angle",          vector3_angle);
 
 	env.load_module_constructor("Vector3", vector3_ctor);
+
+	env.load_module_function("Vector2", "new",            vector2_new);
+	env.load_module_constructor("Vector2", vector2_ctor);
 }
 
 } // namespace crown