Ver Fonte

Vec2 binding done, others updated or deleted

mikymod há 12 anos atrás
pai
commit
9021391084
7 ficheiros alterados com 49 adições e 203 exclusões
  1. 8 1
      lua/CMakeLists.txt
  2. 0 6
      lua/MathBinds.cpp
  3. 41 29
      lua/Vec2Binds.cpp
  4. 0 47
      lua/Vec2Binds.h
  5. 0 2
      src/CMakeLists.txt
  6. 0 78
      src/script/LuaStack.cpp
  7. 0 40
      src/script/LuaStack.h

+ 8 - 1
lua/CMakeLists.txt

@@ -3,7 +3,14 @@ cmake_minimum_required(VERSION 2.8)
 project(crown-lua)
 
 set(LUA_SRC
-	MathBinds.cpp
+	LuaStack.cpp
+	LuaEnvironment.cpp
+	Vec2Binds.cpp
+)
+
+set(LUA_HEADERS
+	LuaStack.h
+	LuaEnvironment.h
 )
 
 add_definitions(-Wl,-E)

+ 0 - 6
lua/MathBinds.cpp

@@ -178,12 +178,6 @@ static const struct luaL_Reg Math [] = {
 	{NULL, NULL}	
 };
 
-int32_t luaopen_libcrownlua(lua_State* L)
-{
-	luaL_register(L, "Math", Math);
-	return 1;
-}
-
 }
 
 } // namespace crown

+ 41 - 29
lua/Vec2Binds.cpp

@@ -1,7 +1,8 @@
 #include "LuaStack.h"
 #include "Device.h"
 #include "ScriptSystem.h"
-#include "Vec2Binds.h"
+#include "LuaEnvironment.h"
+#include "Vec2.h"
 #include "OS.h"
 
 namespace crown
@@ -22,12 +23,28 @@ int32_t	vec2(lua_State* L)
 	return 1;
 }
 
+int32_t vec2_values(lua_State* L)
+{
+	LuaStack stack(L);
+
+	Vec2* a = (Vec2*)stack.get_lightudata(1);
+
+	float x = a->x;
+	float y = a->y;
+
+	stack.push_float(x);
+	stack.push_float(y);
+
+	return 2;
+}
+
+
 int32_t vec2_add(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Vec2* a = (Vec2*) stack.get_lightudata(1);
-	Vec2* b = (Vec2*) stack.get_lightudata(2);
+	Vec2* a = (Vec2*)stack.get_lightudata(1);
+	Vec2* b = (Vec2*)stack.get_lightudata(2);
 
 	*a += *b;
 
@@ -221,33 +238,28 @@ int32_t	vec2_zero(lua_State* L)
 	return 0;
 }
 
-static const struct luaL_Reg Vec2 [] = {
-	{"new", 			vec2},
-	{"add", 			vec2_add},
-	{"sub", 			vec2_subtract},
-	{"mul", 			vec2_multiply},
-	{"div", 			vec2_divide},
-	{"dot", 			vec2_dot},
-	{"equals", 			vec2_equals},
-	{"lower", 			vec2_lower},
-	{"greater", 		vec2_greater},
-	{"len", 			vec2_length},
-	{"squared_len", 	vec2_squared_length},
-	{"set_len", 		vec2_set_length},
-	{"norm", 			vec2_normalize},
-	{"neg", 			vec2_negate},
-	{"dist_to", 		vec2_get_distance_to},
-	{"angle_between", 	vec2_get_angle_between},
-	{"zero", 			vec2_zero},
-	{NULL, NULL}	
-};
-
-int32_t luaopen_libcrownlua(lua_State* L)
-{
-	luaL_register(L, "Vec2", Vec2);
-	return 1;
-}
+} // extern "C"
 
+void load_vec2(LuaEnvironment& env)
+{
+	env.load_module_function("Vec2", "new", vec2);
+	env.load_module_function("Vec2", "val", vec2_values);
+	env.load_module_function("Vec2", "add", vec2_add);
+	env.load_module_function("Vec2", "sub", vec2_subtract);
+	env.load_module_function("Vec2", "mul", vec2_multiply);
+	env.load_module_function("Vec2", "div", vec2_divide);
+	env.load_module_function("Vec2", "dot", vec2_dot);
+	env.load_module_function("Vec2", "equals", vec2_equals);
+	env.load_module_function("Vec2", "lower", vec2_lower);
+	env.load_module_function("Vec2", "greater", vec2_greater);
+	env.load_module_function("Vec2", "length", vec2_length);
+	env.load_module_function("Vec2", "squared_length", vec2_squared_length);
+	env.load_module_function("Vec2", "set_length", vec2_set_length);
+	env.load_module_function("Vec2", "normalize", vec2_normalize);
+	env.load_module_function("Vec2", "negate", vec2_negate);
+	env.load_module_function("Vec2", "get_distance_to", vec2_get_distance_to);
+	env.load_module_function("Vec2", "get_angle_between", vec2_get_angle_between);
+	env.load_module_function("Vec2", "zero", vec2_zero);
 }
 
 } // namespace crown

+ 0 - 47
lua/Vec2Binds.h

@@ -1,47 +0,0 @@
-#pragma once
-
-#include "Vec2.h"
-#include "lua.hpp"
-
-namespace crown
-{
-
-extern "C"
-{
-	/// Constructor
-	int32_t				vec2(lua_State* L);					
-	/// a + b
-	int32_t				vec2_add(lua_State* L);			
-	/// a - b
-	int32_t 			vec2_subtract(lua_State* L);			
-	///
-	int32_t				vec2_multiply(lua_State* L);			
-	///
-	int32_t				vec2_divide(lua_State* L);
-	///
-	int32_t				vec2_dot(lua_State* L);				
-	///
-	int32_t				vec2_equals(lua_State* L);	
-	///
-	int32_t				vec2_lower(lua_State* L);		
-	///
-	int32_t				vec2_greater(lua_State* L);
-	///
-	int32_t				vec2_length(lua_State* L);
-	///
-	int32_t				vec2_squared_length(lua_State* L);
-	///
-	int32_t				vec2_set_length(lua_State* L);					
-	///
-	int32_t				vec2_normalize(lua_State* L);
-	///
-	int32_t				vec2_negate(lua_State* L);
-	///
-	int32_t				vec2_get_distance_to(lua_State* L);
-	///
-	int32_t				vec2_get_angle_between(lua_State* L);
-	///
-	int32_t				vec2_zero(lua_State* L);
-}
-
-} // namespace crown

+ 0 - 2
src/CMakeLists.txt

@@ -227,12 +227,10 @@ set (NETWORK_HEADERS
 
 set (SCRIPT_SRC
 	script/ScriptSystem.cpp
-	script/LuaStack.cpp
 )
 
 set (SCRIPT_HEADERS
 	script/ScriptSystem.h
-	script/LuaStack.cpp
 )
 
 

+ 0 - 78
src/script/LuaStack.cpp

@@ -1,78 +0,0 @@
-#include "LuaStack.h"
-#include <cassert>
-
-namespace crown
-{
-
-//-------------------------------------------------------	
-LuaStack::LuaStack(lua_State* L)
-{
-	m_state = L;
-}
-
-//-------------------------------------------------------
-void LuaStack::push_bool(bool value)
-{
-	lua_pushboolean(m_state, value);
-}
-
-//-------------------------------------------------------
-void LuaStack::push_int(int32_t value)
-{
-	lua_pushinteger(m_state, value);
-}
-
-//-------------------------------------------------------
-void LuaStack::push_float(float value)
-{
-	lua_pushnumber(m_state, value);
-}
-
-//-------------------------------------------------------
-void LuaStack::push_string(const char* str, size_t len)
-{
-	lua_pushlstring(m_state, str, len);
-}
-
-//-------------------------------------------------------
-void LuaStack::push_lightudata(void* ptr)
-{
-	lua_pushlightuserdata(m_state, ptr);
-}
-
-//-------------------------------------------------------
-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);
-}
-
-const void* LuaStack::get_lightudata(int32_t index)
-{
-	if (!lua_islightuserdata(m_state, index))
-	{
-		assert(0);
-	}
-
-	return lua_touserdata(m_state, index);
-}	
-
-
-} // namespace crown

+ 0 - 40
src/script/LuaStack.h

@@ -1,40 +0,0 @@
-#pragma once
-
-#include "lua.hpp"
-#include "Types.h"
-
-namespace crown
-{
-
-class LuaStack
-{
-public:
-
-							LuaStack(lua_State* L);
-
-	void					push_bool(bool value);
-
-	void					push_int(int32_t value);
-
-	void 					push_float(float value);
-
-	void 					push_string(const char* str, size_t len);
-
-	void					push_lightudata(void* ptr);
-
-	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);
-
-	const void*				get_lightudata(int32_t index);
-
-private:
-
-	lua_State* 				m_state;
-};
-
-} // namespace crown