Daniele Bartolini пре 10 година
родитељ
комит
f6156a417c

+ 0 - 4
engine/crown.cpp

@@ -20,7 +20,6 @@
 #include "disk_filesystem.h"
 #include "config.h"
 #include "math_utils.h"
-#include "lua_system.h"
 #include "profiler.h"
 #include "temp_allocator.h"
 #include <bgfx.h>
@@ -187,7 +186,6 @@ bool init(Filesystem& fs, const ConfigSettings& cs)
 	audio_globals::init();
 	physics_globals::init();
 	bgfx::init();
-	lua_globals::init();
 	device_globals::init(cs, fs);
 	device()->init();
 	return true;
@@ -201,7 +199,6 @@ void update()
 		console_server_globals::update();
 		device()->update();
 		bgfx::frame();
-		lua_globals::clear_temporaries();
 		input_globals::update();
 		profiler_globals::flush();
 	}
@@ -211,7 +208,6 @@ void shutdown()
 {
 	device()->shutdown();
 	device_globals::shutdown();
-	lua_globals::shutdown();
 	bgfx::shutdown();
 	physics_globals::shutdown();
 	audio_globals::shutdown();

+ 4 - 2
engine/device.cpp

@@ -8,7 +8,6 @@
 #include "device.h"
 #include "log.h"
 #include "lua_environment.h"
-#include "lua_system.h"
 #include "material_manager.h"
 #include "resource_manager.h"
 #include "resource_package.h"
@@ -57,7 +56,8 @@ void Device::init()
 	material_manager::init();
 	debug_line::init();
 
-	_lua_environment = CE_NEW(_allocator, LuaEnvironment)(lua_globals::state());
+	_lua_environment = CE_NEW(_allocator, LuaEnvironment)();
+	_lua_environment->load_libs();
 
 	CE_LOGD("Crown Engine initialized.");
 	CE_LOGD("Initializing Game...");
@@ -161,6 +161,8 @@ void Device::update()
 	}
 
 	_frame_count++;
+
+	_lua_environment->clear_temporaries();
 }
 
 void Device::render_world(World* world, Camera* camera)

+ 296 - 8
engine/lua/lua_environment.cpp

@@ -9,22 +9,265 @@
 #include "lua_environment.h"
 #include "lua_stack.h"
 #include "lua_resource.h"
+#include "device.h"
+#include "lua_assert.h"
+#include "resource_manager.h"
+#include "log.h"
 #include <stdarg.h>
 
 namespace crown
 {
 
-namespace lua_globals { extern int error_handler(lua_State*); }
+// Lua modules
+extern void load_actor(LuaEnvironment& env);
+extern void load_camera(LuaEnvironment& env);
+extern void load_controller(LuaEnvironment& env);
+extern void load_debug_line(LuaEnvironment& env);
+extern void load_device(LuaEnvironment& env);
+extern void load_float_setting(LuaEnvironment& env);
+extern void load_gui(LuaEnvironment& env);
+extern void load_int_setting(LuaEnvironment& env);
+extern void load_keyboard(LuaEnvironment& env);
+extern void load_math(LuaEnvironment& env);
+extern void load_matrix4x4(LuaEnvironment& env);
+extern void load_mouse(LuaEnvironment& env);
+extern void load_physics_world(LuaEnvironment& env);
+extern void load_quaternion(LuaEnvironment& env);
+extern void load_raycast(LuaEnvironment& env);
+extern void load_resource_package(LuaEnvironment& env);
+extern void load_sound_world(LuaEnvironment& env);
+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_vector3(LuaEnvironment& env);
+extern void load_window(LuaEnvironment& env);
+extern void load_world(LuaEnvironment& env);
+extern void load_color4(LuaEnvironment& env);
+extern void load_material(LuaEnvironment& env);
 
-LuaEnvironment::LuaEnvironment(lua_State* L)
-	: L(L)
+// When an error occurs, logs the error message and pauses the engine.
+static int error_handler(lua_State* L)
 {
+	lua_getfield(L, LUA_GLOBALSINDEX, "debug");
+	if (!lua_istable(L, -1))
+	{
+		lua_pop(L, 1);
+		return 0;
+	}
+
+	lua_getfield(L, -1, "traceback");
+	if (!lua_isfunction(L, -1))
+	{
+		lua_pop(L, 2);
+		return 0;
+	}
+
+	lua_pushvalue(L, 1); // Pass error message
+	lua_pushinteger(L, 2);
+	lua_call(L, 2, 1); // Call debug.traceback
+
+	CE_LOGE(lua_tostring(L, -1)); // Print error message
+	lua_pop(L, 1); // Remove error message from stack
+	lua_pop(L, 1); // Remove debug.traceback from stack
+
+	device()->pause();
+	return 0;
+}
+
+// Redirects require to the resource manager.
+static int require(lua_State* L)
+{
+	using namespace lua_resource;
+	LuaStack stack(L);
+	const LuaResource* lr = (LuaResource*)device()->resource_manager()->get(LUA_TYPE, stack.get_resource_id(1));
+	luaL_loadbuffer(L, program(lr), size(lr), "");
+	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)
+	, _quat_used(0)
+	, _mat4_used(0)
+{
+	L = luaL_newstate();
+	CE_ASSERT(L, "Unable to create lua state");
+}
+
+LuaEnvironment::~LuaEnvironment()
+{
+	lua_close(L);
+}
+
+void LuaEnvironment::load_libs()
+{
+	// Open default libraries
+	luaL_openlibs(L);
+
+	// Register crown libraries
+	load_actor(*this);
+	load_camera(*this);
+	load_controller(*this);
+	load_debug_line(*this);
+	load_device(*this);
+	load_float_setting(*this);
+	load_gui(*this);
+	load_int_setting(*this);
+	load_keyboard(*this);
+	load_math(*this);
+	load_matrix4x4(*this);
+	load_mouse(*this);
+	load_physics_world(*this);
+	load_quaternion(*this);
+	load_raycast(*this);
+	load_resource_package(*this);
+	load_sound_world(*this);
+	load_sprite(*this);
+	load_string_setting(*this);
+	load_touch(*this);
+	load_unit(*this);
+	load_vector3(*this);
+	load_window(*this);
+	load_world(*this);
+	load_color4(*this);
+	load_material(*this);
+
+	// Register custom loader
+	lua_getfield(L, LUA_GLOBALSINDEX, "package");
+	lua_getfield(L, -1, "loaders");
+	lua_remove(L, -2);
+
+	int num_loaders = 0;
+	lua_pushnil(L);
+	while (lua_next(L, -2) != 0)
+	{
+		lua_pop(L, 1);
+		num_loaders++;
+	}
+	lua_pushinteger(L, num_loaders + 1);
+	lua_pushcfunction(L, require);
+	lua_rawset(L, -3);
+	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
+
+	// Ensure stack is clean
+	CE_ASSERT(lua_gettop(L) == 0, "Stack not clean");
 }
 
 void LuaEnvironment::execute(const LuaResource* lr)
 {
 	using namespace lua_resource;
-	lua_pushcfunction(L, lua_globals::error_handler);
+	lua_pushcfunction(L, error_handler);
 	luaL_loadbuffer(L, program(lr), size(lr), "<unknown>");
 	lua_pcall(L, 0, 0, -2);
 	lua_pop(L, 1);
@@ -32,7 +275,7 @@ void LuaEnvironment::execute(const LuaResource* lr)
 
 void LuaEnvironment::execute_string(const char* s)
 {
-	lua_pushcfunction(L, lua_globals::error_handler);
+	lua_pushcfunction(L, error_handler);
 	luaL_loadstring(L, s);
 	lua_pcall(L, 0, 0, -2);
 	lua_pop(L, 1);
@@ -102,7 +345,7 @@ void LuaEnvironment::call_global(const char* func, uint8_t argc, ...)
 	va_list vl;
 	va_start(vl, argc);
 
-	lua_pushcfunction(L, lua_globals::error_handler);
+	lua_pushcfunction(L, error_handler);
 	lua_getglobal(L, func);
 
 	for (uint8_t i = 0; i < argc; i++)
@@ -132,7 +375,7 @@ void LuaEnvironment::call_physics_callback(Actor* actor_0, Actor* actor_1, Unit*
 {
 	LuaStack stack(L);
 
-	lua_pushcfunction(L, lua_globals::error_handler);
+	lua_pushcfunction(L, error_handler);
 	lua_getglobal(L, "g_physics_callback");
 
 	stack.push_table();
@@ -152,7 +395,7 @@ void LuaEnvironment::call_trigger_callback(Actor* trigger, Actor* other, const c
 {
 	LuaStack stack(L);
 
-	lua_pushcfunction(L, lua_globals::error_handler);
+	lua_pushcfunction(L, error_handler);
 	lua_getglobal(L, "g_trigger_callback");
 
 	stack.push_table();
@@ -164,4 +407,49 @@ void LuaEnvironment::call_trigger_callback(Actor* trigger, Actor* other, const c
 	lua_pop(L, -1);
 }
 
+Vector3* LuaEnvironment::next_vector3(const Vector3& v)
+{
+	CE_ASSERT(_vec3_used < CROWN_MAX_LUA_VECTOR3, "Maximum number of Vector3 reached");
+
+	return &(_vec3_buffer[_vec3_used++] = v);
+}
+
+Matrix4x4* LuaEnvironment::next_matrix4x4(const Matrix4x4& m)
+{
+	CE_ASSERT(_mat4_used < CROWN_MAX_LUA_MATRIX4X4, "Maximum number of Matrix4x4 reached");
+
+	return &(s_mat4_buffer[_mat4_used++] = m);
+}
+
+Quaternion* LuaEnvironment::next_quaternion(const Quaternion& q)
+{
+	CE_ASSERT(_quat_used < CROWN_MAX_LUA_QUATERNION, "Maximum number of Quaternion reached");
+	return &(_quat_buffer[_quat_used++] = q);
+}
+
+bool LuaEnvironment::is_vector3(int index)
+{
+	void* type = lua_touserdata(L, index);
+	return (type >= &_vec3_buffer[0] && type <= &_vec3_buffer[CROWN_MAX_LUA_VECTOR3 - 1]);
+}
+
+bool LuaEnvironment::is_matrix4x4(int index)
+{
+	void* type = lua_touserdata(L, index);
+	return (type >= &s_mat4_buffer[0] && type <= &s_mat4_buffer[CROWN_MAX_LUA_MATRIX4X4 - 1]);
+}
+
+bool LuaEnvironment::is_quaternion(int index)
+{
+	void* type = lua_touserdata(L, index);
+	return (type >= &_quat_buffer[0] && type <= &_quat_buffer[CROWN_MAX_LUA_QUATERNION - 1]);
+}
+
+void LuaEnvironment::clear_temporaries()
+{
+	_vec3_used = 0;
+	_mat4_used = 0;
+	_quat_used = 0;
+}
+
 } // namespace crown

+ 21 - 2
engine/lua/lua_environment.h

@@ -28,7 +28,10 @@ struct Unit;
 /// provides utilities for extending Lua
 struct LuaEnvironment
 {
-	LuaEnvironment(lua_State* L);
+	LuaEnvironment();
+	~LuaEnvironment();
+
+	void load_libs();
 
 	void execute(const LuaResource* lr);
 
@@ -50,7 +53,16 @@ struct LuaEnvironment
 	/// Returns true if success, false otherwise
 	void call_global(const char* func, uint8_t argc, ...);
 
-	// HACK
+	void clear_temporaries();
+
+	Vector3* next_vector3(const Vector3& v);
+	Quaternion* next_quaternion(const Quaternion& q);
+	Matrix4x4* next_matrix4x4(const Matrix4x4& m);
+	bool is_vector3(int i);
+	bool is_quaternion(int i);
+	bool is_matrix4x4(int i);
+
+	// FIXME
 	void call_physics_callback(Actor* actor_0, Actor* actor_1, Unit* unit_0, Unit* unit_1, const Vector3& where, const Vector3& normal, const char* type);
 	void call_trigger_callback(Actor* trigger, Actor* other, const char* type);
 
@@ -58,6 +70,13 @@ private:
 
 	lua_State* L;
 
+	uint32_t _vec3_used;
+	Vector3 _vec3_buffer[CROWN_MAX_LUA_VECTOR3];
+	uint32_t _quat_used;
+	Quaternion _quat_buffer[CROWN_MAX_LUA_QUATERNION];
+	uint32_t _mat4_used;
+	Matrix4x4 s_mat4_buffer[CROWN_MAX_LUA_MATRIX4X4];
+
 private:
 
 	// Disable copying

+ 9 - 59
engine/lua/lua_stack.h

@@ -6,7 +6,6 @@
 #pragma once
 
 #include "types.h"
-#include "lua_system.h"
 #include "vector3.h"
 #include "vector2.h"
 #include "quaternion.h"
@@ -411,64 +410,15 @@ struct LuaStack
 		return l;
 	}
 
-	Vector2 get_vector2(int32_t index)
-	{
-		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)
-	{
-		void* v = CHECKLIGHTDATA(L, index, lua_globals::is_vector3, "Vector3");
-		return *(Vector3*)v;
-	}
-
-	Matrix4x4& get_matrix4x4(int32_t index)
-	{
-		void* m = CHECKLIGHTDATA(L, index, lua_globals::is_matrix4x4, "Matrix4x4");
-		return *(Matrix4x4*)m;
-	}
-
-	Quaternion& get_quaternion(int32_t index)
-	{
-		void* q = CHECKLIGHTDATA(L, index, lua_globals::is_quaternion, "Quaternion");
-		return *(Quaternion*)q;
-	}
-
-	Color4 get_color4(int32_t index)
-	{
-		// Color4 represented as Quaternion
-		void* c = CHECKLIGHTDATA(L, index, lua_globals::is_quaternion, "Color4");
-		Quaternion& q = *(Quaternion*)c;
-		return Color4(q.x, q.y, q.z, q.w);
-	}
-
-	void push_vector2(const Vector2& v)
-	{
-		push_vector3(Vector3(v.x, v.y, 0.0f));
-	}
-
-	void push_vector3(const Vector3& v)
-	{
-		lua_pushlightuserdata(L, lua_globals::next_vector3(v));
-		luaL_getmetatable(L, "Lightuserdata_mt");
-		lua_setmetatable(L, -2);
-	}
-
-	void push_matrix4x4(const Matrix4x4& m)
-	{
-		lua_pushlightuserdata(L, lua_globals::next_matrix4x4(m));
-		luaL_getmetatable(L, "Lightuserdata_mt");
-		lua_setmetatable(L, -2);
-	}
-
-	void push_quaternion(const Quaternion& q)
-	{
-		lua_pushlightuserdata(L, lua_globals::next_quaternion(q));
-		luaL_getmetatable(L, "Lightuserdata_mt");
-		lua_setmetatable(L, -2);
-	}
+	Vector2 get_vector2(int32_t index);
+	Vector3& get_vector3(int32_t index);
+	Matrix4x4& get_matrix4x4(int32_t index);
+	Quaternion& get_quaternion(int32_t index);
+	Color4 get_color4(int32_t index);
+	void push_vector2(const Vector2& v);
+	void push_vector3(const Vector3& v);
+	void push_matrix4x4(const Matrix4x4& m);
+	void push_quaternion(const Quaternion& q);
 
 	void push_vector2box(const Vector2& v)
 	{

+ 0 - 329
engine/lua/lua_system.cpp

@@ -1,329 +0,0 @@
-/*
- * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
- * License: https://github.com/taylor001/crown/blob/master/LICENSE
- */
-
-#include "lua_system.h"
-#include "config.h"
-#include "lua_stack.h"
-#include "lua_assert.h"
-#include "error.h"
-#include "vector2.h"
-#include "vector3.h"
-#include "matrix4x4.h"
-#include "quaternion.h"
-#include "device.h"
-#include "resource_manager.h"
-#include "lua_resource.h"
-#include "lua_environment.h"
-#include "lua_stack.h"
-#include "log.h"
-#include "lua.hpp"
-
-namespace crown
-{
-
-// Lua modules
-extern void load_actor(LuaEnvironment& env);
-extern void load_camera(LuaEnvironment& env);
-extern void load_controller(LuaEnvironment& env);
-extern void load_debug_line(LuaEnvironment& env);
-extern void load_device(LuaEnvironment& env);
-extern void load_float_setting(LuaEnvironment& env);
-extern void load_gui(LuaEnvironment& env);
-extern void load_int_setting(LuaEnvironment& env);
-extern void load_keyboard(LuaEnvironment& env);
-extern void load_math(LuaEnvironment& env);
-extern void load_matrix4x4(LuaEnvironment& env);
-extern void load_mouse(LuaEnvironment& env);
-extern void load_physics_world(LuaEnvironment& env);
-extern void load_quaternion(LuaEnvironment& env);
-extern void load_raycast(LuaEnvironment& env);
-extern void load_resource_package(LuaEnvironment& env);
-extern void load_sound_world(LuaEnvironment& env);
-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_vector3(LuaEnvironment& env);
-extern void load_window(LuaEnvironment& env);
-extern void load_world(LuaEnvironment& env);
-extern void load_color4(LuaEnvironment& env);
-extern void load_material(LuaEnvironment& env);
-
-namespace lua_globals
-{
-	static lua_State* L;
-
-	static uint32_t _vec3_used = 0;
-	static Vector3 _vec3_buffer[CROWN_MAX_LUA_VECTOR3];
-	static uint32_t _mat4_used = 0;
-	static Matrix4x4 s_mat4_buffer[CROWN_MAX_LUA_MATRIX4X4];
-	static uint32_t _quat_used = 0;
-	static Quaternion _quat_buffer[CROWN_MAX_LUA_QUATERNION];
-
-	// When an error occurs, logs the error message and pauses the engine.
-	int error_handler(lua_State* L)
-	{
-		lua_getfield(L, LUA_GLOBALSINDEX, "debug");
-		if (!lua_istable(L, -1))
-		{
-			lua_pop(L, 1);
-			return 0;
-		}
-
-		lua_getfield(L, -1, "traceback");
-		if (!lua_isfunction(L, -1))
-		{
-			lua_pop(L, 2);
-			return 0;
-		}
-
-		lua_pushvalue(L, 1); // Pass error message
-		lua_pushinteger(L, 2);
-		lua_call(L, 2, 1); // Call debug.traceback
-
-		CE_LOGE(lua_tostring(L, -1)); // Print error message
-		lua_pop(L, 1); // Remove error message from stack
-		lua_pop(L, 1); // Remove debug.traceback from stack
-
-		device()->pause();
-		return 0;
-	}
-
-	// Redirects require to the resource manager.
-	static int require(lua_State* L)
-	{
-		using namespace lua_resource;
-		LuaStack stack(L);
-		const LuaResource* lr = (LuaResource*)device()->resource_manager()->get(LUA_TYPE, stack.get_resource_id(1));
-		luaL_loadbuffer(L, program(lr), size(lr), "");
-		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;
-	}
-
-	// Initializes lua subsystem
-	void init()
-	{
-		L = luaL_newstate();
-		CE_ASSERT(L, "Unable to create lua state");
-
-		// Open default libraries
-		luaL_openlibs(L);
-
-		// Register crown libraries
-		LuaEnvironment env(L);
-		load_actor(env);
-		load_camera(env);
-		load_controller(env);
-		load_debug_line(env);
-		load_device(env);
-		load_float_setting(env);
-		load_gui(env);
-		load_int_setting(env);
-		load_keyboard(env);
-		load_math(env);
-		load_matrix4x4(env);
-		load_mouse(env);
-		load_physics_world(env);
-		load_quaternion(env);
-		load_raycast(env);
-		load_resource_package(env);
-		load_sound_world(env);
-		load_sprite(env);
-		load_string_setting(env);
-		load_touch(env);
-		load_unit(env);
-		load_vector3(env);
-		load_window(env);
-		load_world(env);
-		load_color4(env);
-		load_material(env);
-
-		// Register custom loader
-		lua_getfield(L, LUA_GLOBALSINDEX, "package");
-		lua_getfield(L, -1, "loaders");
-		lua_remove(L, -2);
-
-		int num_loaders = 0;
-		lua_pushnil(L);
-		while (lua_next(L, -2) != 0)
-		{
-			lua_pop(L, 1);
-			num_loaders++;
-		}
-		lua_pushinteger(L, num_loaders + 1);
-		lua_pushcfunction(L, require);
-		lua_rawset(L, -3);
-		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
-
-		// Ensure stack is clean
-		CE_ASSERT(lua_gettop(L) == 0, "Stack not clean");
-	}
-
-	void shutdown()
-	{
-		lua_close(L);
-	}
-
-	lua_State* state()
-	{
-		return L;
-	}
-
-	Vector3* next_vector3(const Vector3& v)
-	{
-		CE_ASSERT(_vec3_used < CROWN_MAX_LUA_VECTOR3, "Maximum number of Vector3 reached");
-
-		return &(_vec3_buffer[_vec3_used++] = v);
-	}
-
-	Matrix4x4* next_matrix4x4(const Matrix4x4& m)
-	{
-		CE_ASSERT(_mat4_used < CROWN_MAX_LUA_MATRIX4X4, "Maximum number of Matrix4x4 reached");
-
-		return &(s_mat4_buffer[_mat4_used++] = m);
-	}
-
-	Quaternion* next_quaternion(const Quaternion& q)
-	{
-		CE_ASSERT(_quat_used < CROWN_MAX_LUA_QUATERNION, "Maximum number of Quaternion reached");
-		return &(_quat_buffer[_quat_used++] = q);
-	}
-
-	bool is_vector3(int32_t index)
-	{
-		void* type = lua_touserdata(L, index);
-		return (type >= &_vec3_buffer[0] && type <= &_vec3_buffer[CROWN_MAX_LUA_VECTOR3 - 1]);
-	}
-
-	bool is_matrix4x4(int32_t index)
-	{
-		void* type = lua_touserdata(L, index);
-		return (type >= &s_mat4_buffer[0] && type <= &s_mat4_buffer[CROWN_MAX_LUA_MATRIX4X4 - 1]);
-	}
-
-	bool is_quaternion(int32_t index)
-	{
-		void* type = lua_touserdata(L, index);
-		return (type >= &_quat_buffer[0] && type <= &_quat_buffer[CROWN_MAX_LUA_QUATERNION - 1]);
-	}
-
-	void clear_temporaries()
-	{
-		_vec3_used = 0;
-		_mat4_used = 0;
-		_quat_used = 0;
-	}
-
-} // namespace lua_globals
-} // namespace crown

+ 0 - 38
engine/lua/lua_system.h

@@ -1,38 +0,0 @@
-/*
- * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
- * License: https://github.com/taylor001/crown/blob/master/LICENSE
- */
-
-#include "types.h"
-#include "math_types.h"
-#include "lua.hpp"
-
-namespace crown
-{
-
-/// Global lua-related functions
-namespace lua_globals
-{
-	/// Initializes the lua system.
-	/// This is the place where to create and initialize per-application objects.
-	void init();
-
-	/// It should reverse the actions performed by lua_globals::init().
-	void shutdown();
-
-	lua_State* state();
-
-	/// Clears temporary objects (Vector3, Matrix4x4, ...).
-	void clear_temporaries();
-
-	/// Returns a new temporary Vector2, Vector3, Matrix4x4 or Quaternion
-	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 Vector3, Matrix4x4 or Quaternion
-	bool is_vector3(int32_t index);
-	bool is_matrix4x4(int32_t index);
-	bool is_quaternion(int32_t index);
-} // namespace lua_globals
-} // namespace crown