mikymod 12 лет назад
Родитель
Сommit
d463557777
5 измененных файлов с 118 добавлено и 9 удалено
  1. 3 3
      lua/CMakeLists.txt
  2. 9 6
      lua/MathBinds.cpp
  3. 2 0
      src/CMakeLists.txt
  4. 66 0
      src/script/LuaStack.cpp
  5. 38 0
      src/script/LuaStack.h

+ 3 - 3
lua/CMakeLists.txt

@@ -7,8 +7,8 @@ set(LUA_SRC
 )
 
 add_definitions(-Wl,-E)
-add_library(Math SHARED ${LUA_SRC})
-target_link_libraries(Math crown)
+add_library(crownlua SHARED ${LUA_SRC})
+target_link_libraries(crownlua crown)
 
-install (TARGETS Math DESTINATION lib/${CMAKE_PROJECT_NAME})
+install (TARGETS crownlua DESTINATION lib/${CMAKE_PROJECT_NAME})
 

+ 9 - 6
lua/MathBinds.cpp

@@ -1,6 +1,6 @@
-#include "lua.hpp"
 #include "MathUtils.h"
 #include "Types.h"
+#include "LuaStack.h"
 #include "OS.h"
 
 namespace crown
@@ -49,10 +49,13 @@ extern "C"
 //-------------------------------------------------------------------
 int32_t math_equals(lua_State* L)
 {
-	os::printf("binding called\n");
-	float b = luaL_checknumber(L, 1);
-	float a = luaL_checknumber(L, 1);
-	lua_pushboolean(L, math::equals(a, b, math::FLOAT_PRECISION));
+	LuaStack stack(L);
+
+	float a = stack.get_float(1);
+	float b = stack.get_float(2);
+
+	stack.push_bool(math::equals(a, b, math::FLOAT_PRECISION));
+
 	return 1;
 }
 
@@ -175,7 +178,7 @@ static const struct luaL_Reg Math [] = {
 	{NULL, NULL}	
 };
 
-int32_t luaopen_Math(lua_State* L)
+int32_t luaopen_libcrownlua(lua_State* L)
 {
 	luaL_register(L, "Math", Math);
 	return 1;

+ 2 - 0
src/CMakeLists.txt

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

+ 66 - 0
src/script/LuaStack.cpp

@@ -0,0 +1,66 @@
+#include "LuaStack.h"
+
+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);
+}
+
+} // namespace crown

+ 38 - 0
src/script/LuaStack.h

@@ -0,0 +1,38 @@
+#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);
+
+private:
+
+	lua_State* 				m_state;
+};
+
+} // namespace crown