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

Vec3Binds re-implemented without ffi

mikymod 12 лет назад
Родитель
Сommit
830ddf8378
4 измененных файлов с 219 добавлено и 76 удалено
  1. 1 0
      lua/CMakeLists.txt
  2. 1 0
      lua/LuaEnvironment.cpp
  3. 12 5
      lua/Vec2Binds.cpp
  4. 205 71
      lua/Vec3Binds.cpp

+ 1 - 0
lua/CMakeLists.txt

@@ -6,6 +6,7 @@ set(LUA_SRC
 	LuaStack.cpp
 	LuaEnvironment.cpp
 	Vec2Binds.cpp
+	Vec3Binds.cpp
 )
 
 set(LUA_HEADERS

+ 1 - 0
lua/LuaEnvironment.cpp

@@ -32,6 +32,7 @@ extern "C"
 		LuaEnvironment env(L);
 
 		load_vec2(env);
+		load_vec3(env);
 	}
 }
 

+ 12 - 5
lua/Vec2Binds.cpp

@@ -1,9 +1,7 @@
+#include "Vec2.h"
 #include "LuaStack.h"
-#include "Device.h"
-#include "ScriptSystem.h"
 #include "LuaEnvironment.h"
-#include "Vec2.h"
-#include "OS.h"
+
 
 namespace crown
 {
@@ -11,6 +9,10 @@ namespace crown
 extern "C"
 {
 
+const int32_t 	LUA_VEC2_BUFFER_SIZE = 4096;
+Vec2 			vec2_buffer[LUA_VEC2_BUFFER_SIZE];
+uint32_t 		vec2_used = 0;	
+
 int32_t	vec2(lua_State* L)
 {
 	LuaStack stack(L);
@@ -18,7 +20,12 @@ int32_t	vec2(lua_State* L)
 	float x = stack.get_float(1);
 	float y = stack.get_float(2);
 
-	stack.push_lightudata(device()->script_system()->next_vec2(x, y));
+	vec2_buffer[vec2_used].x = x;
+	vec2_buffer[vec2_used].y = y;
+
+	stack.push_lightudata(&vec2_buffer[vec2_used]);
+
+	vec2_used++;
 
 	return 1;
 }

+ 205 - 71
lua/Vec3Binds.cpp

@@ -1,5 +1,6 @@
-#include "Device.h"
-#include "ScriptSystem.h"
+#include "Vec3.h"
+#include "LuaStack.h"
+#include "LuaEnvironment.h"
 
 namespace crown
 {
@@ -8,166 +9,299 @@ namespace crown
 
 extern "C"
 {
-	/// Constructor
-	Vec3&				vec3(float nx, float ny, float nz);
 
-	Vec3&				vec3_add(Vec3& self, const Vec3& v);
+const int32_t 	LUA_VEC3_BUFFER_SIZE = 4096;
+Vec3 			vec3_buffer[LUA_VEC3_BUFFER_SIZE];
+uint32_t 		vec3_used = 0;
 
-	Vec3&				vec3_subtract(Vec3& self, const Vec3& v);
+//------------------------------------------------------------
+int32_t vec3(lua_State* L)
+{
+	LuaStack stack(L);
 
-	Vec3&				vec3_multiply(Vec3& self, const float s);
+	float x = stack.get_float(1);
+	float y = stack.get_float(2);
+	float z = stack.get_float(3);
 
-	Vec3&				vec3_divide(Vec3& self, const float s);
+	vec3_buffer[vec3_used].x = x;
+	vec3_buffer[vec3_used].y = y;
+	vec3_buffer[vec3_used].z = z;
 
-	float				vec3_dot(Vec3& self, const Vec3& v);
+	stack.push_lightudata(&vec3_buffer[vec3_used]);
 
-	Vec3&				vec3_cross(Vec3& self, const Vec3& v);
+	vec3_used++;
 
-	bool				vec3_equal(Vec3& self, const Vec3& other);	
-	
-	bool				vec3_lower(Vec3& self, const Vec3& other);
+	return 1;
+}
 
-	bool				vec3_greater(Vec3& self, const Vec3& other);		
+//------------------------------------------------------------
+int32_t vec3_values(lua_State* L)
+{
+	LuaStack stack(L);
 
-	float				vec3_length(Vec3& self);	
+	Vec3* a = (Vec3*)stack.get_lightudata(1);
 
-	float				vec3_squared_length(Vec3& self);
+	float x = a->x;
+	float y = a->y;
+	float z = a->z;
 
-	void				vec3_set_length(Vec3& self, float len);
+	stack.push_float(x);
+	stack.push_float(y);
+	stack.push_float(z);
 
-	Vec3&				vec3_normalize(Vec3& self);
+	return 3;
+}
 
-	Vec3&				vec3_negate(Vec3& self);					
+//------------------------------------------------------------
+int32_t vec3_add(lua_State* L)
+{
+	LuaStack stack(L);
 
-	float				vec3_get_distance_to(Vec3& self, const Vec3& a);	
+	Vec3* a = (Vec3*) stack.get_lightudata(1);
+	Vec3* b = (Vec3*) stack.get_lightudata(2);
 
-	float				vec3_get_angle_between(Vec3& self, const Vec3& a);	
+	*a += *b;
 
-	void				vec3_zero(Vec3& self);										
-} // extern "C"
+	stack.push_lightudata(a);
 
-//------------------------------------------------------------
-Vec3& vec3(float nx, float ny, float nz)
-{
-	return device()->script_system()->next_vec3(nx, ny, nz);
+	return 1;
 }
 
 //------------------------------------------------------------
-Vec3& vec3_add(Vec3& self, const Vec3& v)
+int32_t vec3_subtract(lua_State* L)
 {
-	self += v;
+	LuaStack stack(L);
 
-	return self;
-}
+	Vec3* a = (Vec3*) stack.get_lightudata(1);
+	Vec3* b = (Vec3*) stack.get_lightudata(2);
 
-//------------------------------------------------------------
-Vec3& vec3_subtract(Vec3& self, const Vec3& v)
-{
-	self -= v;
+	*a -= *b;
+
+	stack.push_lightudata(a);
 
-	return self;
+	return 1;
 }
 
 //------------------------------------------------------------
-Vec3& vec3_multiply(Vec3& self, const float s)
+int32_t vec3_multiply(lua_State* L)
 {
-	self *= s;
+	LuaStack stack(L);
 
-	return self;
+	Vec3* a = (Vec3*) stack.get_lightudata(1);
+	float b = stack.get_float(2);
+
+	*a *= b;
+
+	stack.push_lightudata(a);
+
+	return 1;
 }
 
 //------------------------------------------------------------
-Vec3& vec3_divide(Vec3& self, const float s)
+int32_t vec3_divide(lua_State* L)
 {
-	self /= s;
+	LuaStack stack(L);
+
+	Vec3* a = (Vec3*) stack.get_lightudata(1);
+	float b = stack.get_float(2);
 
-	return self;
+	*a /= b;
+
+	stack.push_lightudata(a);
+
+	return 1;
 }
 
 //------------------------------------------------------------
-float vec3_dot(Vec3& self, const Vec3& v)
+int32_t vec3_dot(lua_State* L)
 {
-	return self.dot(v);
+	LuaStack stack(L);
+	
+	Vec3* a = (Vec3*) stack.get_lightudata(1);
+	Vec3* b = (Vec3*) stack.get_lightudata(2);
 
+	stack.push_float(a->dot(*b));
 
+	return 1;
 }
 
 //------------------------------------------------------------
-Vec3& vec3_cross(Vec3& self, const Vec3& v)
+int32_t vec3_cross(lua_State* L)
 {
-	self.cross(v);
+	LuaStack stack(L);
+	
+	Vec3* a = (Vec3*) stack.get_lightudata(1);
+	Vec3* b = (Vec3*) stack.get_lightudata(2);
 
-	return self;
+	/// CHECK CHECK CHECK
+	*a = a->cross(*b);
+
+	stack.push_lightudata(a);
+
+	return 1;
 }
 
 //------------------------------------------------------------
-bool vec3_equal(Vec3& self, const Vec3& other)
+int32_t vec3_equals(lua_State* L)
 {
-	return self == other;
+	LuaStack stack(L);
+
+	Vec3* a = (Vec3*) stack.get_lightudata(1);
+	Vec3* b = (Vec3*) stack.get_lightudata(2);
+
+	stack.push_bool(*a == *b);
+
+	return 1;
 }
 
 //------------------------------------------------------------
-bool vec3_lower(Vec3& self, const Vec3& other)
+int32_t vec3_lower(lua_State* L)
 {
-	return self < other;
+	LuaStack stack(L);
+
+	Vec3* a = (Vec3*) stack.get_lightudata(1);
+	Vec3* b = (Vec3*) stack.get_lightudata(2);
+
+	stack.push_bool(*a < *b);
+
+	return 1;
 }
 
 //------------------------------------------------------------
-bool vec3_greater(Vec3& self, const Vec3& other)
+int32_t vec3_greater(lua_State* L)
 {
-	return self > other;
+	LuaStack stack(L);
+
+	Vec3* a = (Vec3*) stack.get_lightudata(1);
+	Vec3* b = (Vec3*) stack.get_lightudata(2);
+
+	stack.push_bool(*a > *b);
+
+	return 1;
 }
 
 //------------------------------------------------------------
-float vec3_length(Vec3& self)
+int32_t vec3_length(lua_State* L)
 {
-	return self.length();
+	LuaStack stack(L);
+
+	Vec3* a = (Vec3*) stack.get_lightudata(1);
+
+	stack.push_float(a->length());
+
+	return 1;
 }
 
 //------------------------------------------------------------
-float vec3_squared_length(Vec3& self)
+int32_t vec3_squared_length(lua_State* L)
 {
-	return self.squared_length();
+	LuaStack stack(L);
+
+	Vec3* a = (Vec3*) stack.get_lightudata(1);
+
+	stack.push_float(a->squared_length());
+
+	return 1;
 }
 
 //------------------------------------------------------------
-void vec3_set_length(Vec3& self, float len)
+int32_t vec3_set_length(lua_State* L)
 {
-	self.set_length(len);
+	LuaStack stack(L);
+
+	Vec3* a = (Vec3*) stack.get_lightudata(1);
+	float len = stack.get_float(2);
+
+	a->set_length(len);
+
+	return 0;
 }
 
 //------------------------------------------------------------
-Vec3& vec3_normalize(Vec3& self)
+int32_t vec3_normalize(lua_State* L)
 {
-	self.normalize();
+	LuaStack stack(L);
+
+	Vec3* a = (Vec3*) stack.get_lightudata(1);
 
-	return self;
+	a->normalize();
+
+	return 0;
 }
 
 //------------------------------------------------------------
-Vec3& vec3_negate(Vec3& self)
+int32_t vec3_negate(lua_State* L)
 {
-	self.negate();
+	LuaStack stack(L);
+
+	Vec3* a = (Vec3*) stack.get_lightudata(1);
 
-	return self;
+	a->negate();
+
+	return 0;
 }
 
 //------------------------------------------------------------
-float vec3_get_distance_to(Vec3& self, const Vec3& a)
+int32_t vec3_get_distance_to(lua_State* L)
 {
-	return self.get_distance_to(a);
+	LuaStack stack(L);
+
+	Vec3* a = (Vec3*) stack.get_lightudata(1);
+	Vec3* b = (Vec3*) stack.get_lightudata(2);
+
+	stack.push_float(a->get_distance_to(*b));
+
+	return 1;
 }
 
 //------------------------------------------------------------
-float vec3_get_angle_between(Vec3& self, const Vec3& a)
+int32_t vec3_get_angle_between(lua_State* L)
 {
-	return self.get_angle_between(a);
+	LuaStack stack(L);
+
+	Vec3* a = (Vec3*) stack.get_lightudata(1);
+	Vec3* b = (Vec3*) stack.get_lightudata(2);
+
+	stack.push_float(a->get_angle_between(*b));
+
+	return 1;
 }
 
 //------------------------------------------------------------
-void vec3_zero(Vec3& self)
+int32_t vec3_zero(lua_State* L)
 {
-	self.zero();
+	LuaStack stack(L);
+
+	Vec3* a = (Vec3*) stack.get_lightudata(1);
+
+	a->zero();
+
+	return 0;
 }	
 
+} // extern "C"
+
+void load_vec3(LuaEnvironment& env)
+{
+	env.load_module_function("Vec3", "new", 				vec3);
+	env.load_module_function("Vec3", "val", 				vec3_values);
+	env.load_module_function("Vec3", "add", 				vec3_add);
+	env.load_module_function("Vec3", "sub", 				vec3_subtract);
+	env.load_module_function("Vec3", "mul", 				vec3_multiply);
+	env.load_module_function("Vec3", "div", 				vec3_divide);
+	env.load_module_function("Vec3", "dot", 				vec3_dot);
+	env.load_module_function("Vec3", "cross", 				vec3_cross);
+	env.load_module_function("Vec3", "equals", 				vec3_equals);
+	env.load_module_function("Vec3", "lower", 				vec3_lower);
+	env.load_module_function("Vec3", "greater", 			vec3_greater);
+	env.load_module_function("Vec3", "length", 				vec3_length);
+	env.load_module_function("Vec3", "squared_length", 		vec3_squared_length);
+	env.load_module_function("Vec3", "set_length", 			vec3_set_length);
+	env.load_module_function("Vec3", "normalize", 			vec3_normalize);
+	env.load_module_function("Vec3", "negate", 				vec3_negate);
+	env.load_module_function("Vec3", "get_distance_to", 	vec3_get_distance_to);
+	env.load_module_function("Vec3", "get_angle_between", 	vec3_get_angle_between);
+	env.load_module_function("Vec3", "zero", 				vec3_zero);	
+}
+
 } // namespace crown