Selaa lähdekoodia

Merge branch 'master' of github.com:taylor001/crown

Daniele Bartolini 10 vuotta sitten
vanhempi
sitoutus
3c103de392

+ 2 - 2
src/input/input_device.cpp

@@ -82,7 +82,7 @@ void InputDevice::update()
 	memcpy(_last_state, _current_state, sizeof(uint8_t)*_num_buttons);
 }
 
-InputDevice* InputDevice::create_input_device(Allocator& a, const char* name, uint8_t num_buttons, uint8_t num_axes)
+InputDevice* InputDevice::create(Allocator& a, const char* name, uint8_t num_buttons, uint8_t num_axes)
 {
 	const uint32_t size = 0
 		+ sizeof(InputDevice)
@@ -110,7 +110,7 @@ InputDevice* InputDevice::create_input_device(Allocator& a, const char* name, ui
 	return id;
 }
 
-void InputDevice::destroy_input_device(Allocator& a, InputDevice* id)
+void InputDevice::destroy(Allocator& a, InputDevice* id)
 {
 	a.deallocate(id);
 }

+ 2 - 2
src/input/input_device.h

@@ -66,8 +66,8 @@ public:
 
 public:
 
-	static InputDevice* create_input_device(Allocator& a, const char* name, uint8_t num_buttons, uint8_t num_axes);
-	static void destroy_input_device(Allocator& a, InputDevice* id);
+	static InputDevice* create(Allocator& a, const char* name, uint8_t num_buttons, uint8_t num_axes);
+	static void destroy(Allocator& a, InputDevice* id);
 };
 
 } // namespace crown

+ 8 - 8
src/input/input_manager.cpp

@@ -15,17 +15,17 @@ InputManager::InputManager()
 	, _mouse(NULL)
 	, _touch(NULL)
 {
-	_keyboard = InputDevice::create_input_device(default_allocator()
+	_keyboard = InputDevice::create(default_allocator()
 		, "Keyboard"
 		, KeyboardButton::COUNT
 		, 0
 		);
-	_mouse = InputDevice::create_input_device(default_allocator()
+	_mouse = InputDevice::create(default_allocator()
 		, "Mouse"
 		, MouseButton::COUNT
 		, MouseAxis::COUNT
 		);
-	_touch = InputDevice::create_input_device(default_allocator()
+	_touch = InputDevice::create(default_allocator()
 		, "Touch"
 		, TouchButton::COUNT
 		, TouchButton::COUNT
@@ -33,7 +33,7 @@ InputManager::InputManager()
 
 	for (uint8_t i = 0; i < CROWN_MAX_JOYPADS; ++i)
 	{
-		_joypad[i] = InputDevice::create_input_device(default_allocator()
+		_joypad[i] = InputDevice::create(default_allocator()
 			, "Joypad"
 			, JoypadButton::COUNT
 			, JoypadAxis::COUNT
@@ -48,11 +48,11 @@ InputManager::InputManager()
 InputManager::~InputManager()
 {
 	for (uint8_t i = 0; i < CROWN_MAX_JOYPADS; ++i)
-		InputDevice::destroy_input_device(default_allocator(), _joypad[i]);
+		InputDevice::destroy(default_allocator(), _joypad[i]);
 
-	InputDevice::destroy_input_device(default_allocator(), _touch);
-	InputDevice::destroy_input_device(default_allocator(), _mouse);
-	InputDevice::destroy_input_device(default_allocator(), _keyboard);
+	InputDevice::destroy(default_allocator(), _touch);
+	InputDevice::destroy(default_allocator(), _mouse);
+	InputDevice::destroy(default_allocator(), _keyboard);
 }
 
 InputDevice* InputManager::keyboard()

+ 4 - 108
src/lua/lua_environment.cpp

@@ -73,84 +73,6 @@ static int require(lua_State* L)
 	return 1;
 }
 
-static int lightuserdata_add(lua_State* L)
-{
-	LuaStack stack(L);
-	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);
-	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);
-	const Vector3& a = stack.get_vector3(1);
-	const float b = stack.get_float(2);
-	stack.push_vector3(a * b);
-	return 1;
-}
-
-static int lightuserdata_div(lua_State* L)
-{
-	LuaStack stack(L);
-	const Vector3& a = stack.get_vector3(1);
-	const float b = stack.get_float(2);
-	stack.push_vector3(a / b);
-	return 1;
-}
-
-static int lightuserdata_unm(lua_State* L)
-{
-	LuaStack stack(L);
-	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);
-
-	switch (s[0])
-	{
-		case 'x': stack.push_float(v.x); return 1;
-		case 'y': stack.push_float(v.y); return 1;
-		case 'z': stack.push_float(v.z); return 1;
-		default: LUA_ASSERT(false, stack, "Bad index: '%c'", s[0]); break;
-	}
-
-	return 0;
-}
-
-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);
-
-	switch (s[0])
-	{
-		case 'x': v.x = value; break;
-		case 'y': v.y = value; break;
-		case 'z': v.z = value; break;
-		default: LUA_ASSERT(false, stack, "Bad index: '%c'", s[0]); break;
-	}
-
-	return 0;
-}
-
 LuaEnvironment::LuaEnvironment()
 	: L(NULL)
 	, _vec3_used(0)
@@ -208,36 +130,10 @@ void LuaEnvironment::load_libs()
 	lua_pop(L, 1);
 
 	// Create metatable for lightuserdata
-	luaL_newmetatable(L, "Lightuserdata_mt");
-	lua_pushstring(L, "__add");
-	lua_pushcfunction(L, lightuserdata_add);
-	lua_settable(L, 1);
-
-	lua_pushstring(L, "__sub");
-	lua_pushcfunction(L, lightuserdata_sub);
-	lua_settable(L, 1);
-
-	lua_pushstring(L, "__mul");
-	lua_pushcfunction(L, lightuserdata_mul);
-	lua_settable(L, 1);
-
-	lua_pushstring(L, "__div");
-	lua_pushcfunction(L, lightuserdata_div);
-	lua_settable(L, 1);
-
-	lua_pushstring(L, "__unm");
-	lua_pushcfunction(L, lightuserdata_unm);
-	lua_settable(L, 1);
-
-	lua_pushstring(L, "__index");
-	lua_pushcfunction(L, lightuserdata_index);
-	lua_settable(L, 1);
-
-	lua_pushstring(L, "__newindex");
-	lua_pushcfunction(L, lightuserdata_newindex);
-	lua_settable(L, 1);
-
-	lua_pop(L, 1); // Pop Lightuserdata_mt
+	lua_pushlightuserdata(L, 0);
+	lua_getfield(L, LUA_REGISTRYINDEX, "Lightuserdata_mt");
+	lua_setmetatable(L, -2);
+	lua_pop(L, 1);
 
 	// Ensure stack is clean
 	CE_ASSERT(lua_gettop(L) == 0, "Stack not clean");

+ 86 - 0
src/lua/lua_math.cpp

@@ -825,6 +825,84 @@ static int color4_ctor(lua_State* L)
 	return color4_new(L);
 }
 
+static int lightuserdata_add(lua_State* L)
+{
+	LuaStack stack(L);
+	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);
+	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);
+	const Vector3& a = stack.get_vector3(1);
+	const float b = stack.get_float(2);
+	stack.push_vector3(a * b);
+	return 1;
+}
+
+static int lightuserdata_div(lua_State* L)
+{
+	LuaStack stack(L);
+	const Vector3& a = stack.get_vector3(1);
+	const float b = stack.get_float(2);
+	stack.push_vector3(a / b);
+	return 1;
+}
+
+static int lightuserdata_unm(lua_State* L)
+{
+	LuaStack stack(L);
+	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);
+
+	switch (s[0])
+	{
+		case 'x': stack.push_float(v.x); return 1;
+		case 'y': stack.push_float(v.y); return 1;
+		case 'z': stack.push_float(v.z); return 1;
+		default: LUA_ASSERT(false, stack, "Bad index: '%c'", s[0]); break;
+	}
+
+	return 0;
+}
+
+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);
+
+	switch (s[0])
+	{
+		case 'x': v.x = value; break;
+		case 'y': v.y = value; break;
+		case 'z': v.z = value; break;
+		default: LUA_ASSERT(false, stack, "Bad index: '%c'", s[0]); break;
+	}
+
+	return 0;
+}
+
 void load_math(LuaEnvironment& env)
 {
 	env.load_module_function("Math", "to_rad",               math_to_rad);
@@ -943,6 +1021,14 @@ void load_math(LuaEnvironment& env)
 
 	env.load_module_function("Color4", "new", color4_new);
 	env.load_module_constructor("Color4",     color4_ctor);
+
+	env.load_module_function("Lightuserdata_mt", "__add", lightuserdata_add);
+	env.load_module_function("Lightuserdata_mt", "__sub", lightuserdata_sub);
+	env.load_module_function("Lightuserdata_mt", "__mul", lightuserdata_mul);
+	env.load_module_function("Lightuserdata_mt", "__div", lightuserdata_div);
+	env.load_module_function("Lightuserdata_mt", "__unm", lightuserdata_unm);
+	env.load_module_function("Lightuserdata_mt", "__index", lightuserdata_index);
+	env.load_module_function("Lightuserdata_mt", "__newindex", lightuserdata_newindex);
 }
 
 } // namespace crown