Jelajahi Sumber

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

mikymod 12 tahun lalu
induk
melakukan
27ff636c89
63 mengubah file dengan 1252 tambahan dan 1427 penghapusan
  1. 3 8
      CMakeLists.txt
  2. 1 1
      engine/Android.mk
  3. 11 4
      engine/CMakeLists.txt
  4. 71 71
      engine/ConsoleServer.cpp
  5. 32 32
      engine/ConsoleServer.h
  6. 12 11
      engine/Crown.h
  7. 26 40
      engine/Device.cpp
  8. 0 5
      engine/Device.h
  9. 0 117
      engine/Glyph.h
  10. 0 65
      engine/core/math/Shape.h
  11. 9 0
      engine/core/mem/ProxyAllocator.cpp
  12. 6 3
      engine/core/mem/ProxyAllocator.h
  13. 2 5
      engine/lua/LuaAccelerometer.cpp
  14. 21 21
      engine/lua/LuaCamera.cpp
  15. 4 4
      engine/lua/LuaDevice.cpp
  16. 7 8
      engine/lua/LuaFloatSetting.cpp
  17. 7 8
      engine/lua/LuaIntSetting.cpp
  18. 5 5
      engine/lua/LuaKeyboard.cpp
  19. 139 173
      engine/lua/LuaMat4.cpp
  20. 16 16
      engine/lua/LuaMath.cpp
  21. 14 21
      engine/lua/LuaMouse.cpp
  22. 37 54
      engine/lua/LuaQuat.cpp
  23. 60 56
      engine/lua/LuaStack.cpp
  24. 15 18
      engine/lua/LuaStack.h
  25. 7 8
      engine/lua/LuaStringSetting.cpp
  26. 80 100
      engine/lua/LuaVec2.cpp
  27. 67 88
      engine/lua/LuaVec3.cpp
  28. 13 14
      engine/lua/LuaWindow.cpp
  29. 0 3
      engine/os/posix/Cond.cpp
  30. 6 3
      engine/os/posix/Cond.h
  31. 0 4
      engine/os/posix/Mutex.cpp
  32. 6 3
      engine/os/posix/Mutex.h
  33. 59 13
      engine/os/posix/Thread.cpp
  34. 48 7
      engine/os/posix/Thread.h
  35. 4 4
      engine/os/win/Cond.cpp
  36. 10 8
      engine/os/win/Cond.h
  37. 0 3
      engine/os/win/Mutex.cpp
  38. 5 3
      engine/os/win/Mutex.h
  39. 1 1
      engine/resource/FontResource.cpp
  40. 2 3
      engine/resource/FontResource.h
  41. 1 1
      engine/resource/MaterialResource.cpp
  42. 4 4
      engine/resource/MaterialResource.h
  43. 1 1
      engine/resource/MeshResource.cpp
  44. 2 2
      engine/resource/MeshResource.h
  45. 24 12
      engine/resource/Resource.h
  46. 102 0
      engine/resource/ResourceLoader.cpp
  47. 90 0
      engine/resource/ResourceLoader.h
  48. 56 260
      engine/resource/ResourceManager.cpp
  49. 29 90
      engine/resource/ResourceManager.h
  50. 101 0
      engine/resource/ResourceRegistry.cpp
  51. 8 18
      engine/resource/ResourceRegistry.h
  52. 1 1
      engine/resource/SoundResource.cpp
  53. 2 2
      engine/resource/SoundResource.h
  54. 1 1
      engine/resource/TextureResource.cpp
  55. 2 2
      engine/resource/TextureResource.h
  56. 0 2
      engine/tests/CMakeLists.txt
  57. 0 0
      engine/tests/allocators.cpp
  58. 1 1
      engine/tests/compressors.cpp
  59. 0 0
      engine/tests/containers.cpp
  60. 0 0
      engine/tests/messages.cpp
  61. 0 0
      engine/tests/paths.cpp
  62. 0 0
      engine/tests/strings.cpp
  63. 21 19
      samples/CMakeLists.txt

+ 3 - 8
CMakeLists.txt

@@ -4,11 +4,11 @@ project(crown)
 
 set (CROWN_VERSION_MAJOR 0)
 set (CROWN_VERSION_MINOR 1)
-set (CROWN_VERSION_MICRO 8)
+set (CROWN_VERSION_MICRO 9)
 
 option (CROWN_BUILD_SAMPLES "Whether to build the samples" ON)
 option (CROWN_BUILD_TOOLS "Whether to build the tools" ON)
-option (CROWN_BUILD_TESTS "Whether to build unit tests" OFF)
+option (CROWN_BUILD_TESTS "Whether to build unit tests" ON)
 
 # always debug mode for now
 set (CROWN_DEBUG 1)
@@ -29,12 +29,7 @@ add_subdirectory(engine)
 if (CROWN_BUILD_TOOLS)
 	add_subdirectory(tools)
 endif (CROWN_BUILD_TOOLS)
+
 if (CROWN_BUILD_SAMPLES)
 	add_subdirectory(samples)
 endif (CROWN_BUILD_SAMPLES)
-
-if (CROWN_BUILD_TESTS)
-	add_subdirectory(tests)
-endif (CROWN_BUILD_TESTS)
-
-

+ 1 - 1
engine/Android.mk

@@ -39,7 +39,6 @@ LOCAL_SRC_FILES :=\
 	core/math/Mat4.cpp\
 	core/math/Plane.cpp\
 	core/math/Quat.cpp\
-	core/math/Shape.cpp\
 	core/math/Vec2.cpp\
 	core/math/Vec3.cpp\
 	core/math/Vec4.cpp\
@@ -90,6 +89,7 @@ LOCAL_SRC_FILES :=\
 	resource/FontResource.cpp\
 	resource/MaterialResource.cpp\
 	resource/PixelShaderResource.cpp\
+	resource/ResourceLoader.cpp\
 	resource/ResourceManager.cpp\
 	resource/TextResource.cpp\
 	resource/TextureResource.cpp\

+ 11 - 4
engine/CMakeLists.txt

@@ -70,7 +70,6 @@ set (HEADERS
 	Config.h
 	Crown.h
 	Device.h
-	Glyph.h
 	ConsoleServer.h
 	FPSSystem.h
 )
@@ -121,7 +120,6 @@ set (MATH_SRC
 	core/math/Plane.cpp
 	core/math/Point2.cpp
 	core/math/Quat.cpp
-	core/math/Shape.cpp
 	core/math/Vec2.cpp
 	core/math/Vec3.cpp
 	core/math/Vec4.cpp
@@ -139,7 +137,6 @@ set (MATH_HEADERS
 	core/math/Quat.h
 	core/math/Random.h
 	core/math/Ray.h
-	core/math/Shape.h
 	core/math/Triangle.h
 	core/math/Vec2.h
 	core/math/Vec3.h
@@ -273,7 +270,9 @@ set (RENDERERS_HEADERS
 
 set (RESOURCE_SRC
 	resource/MaterialResource.cpp
+	resource/ResourceLoader.cpp
 	resource/ResourceManager.cpp
+	resource/ResourceRegistry.cpp
 	resource/TextureResource.cpp
 	resource/MeshResource.cpp
 	resource/FontResource.cpp
@@ -285,7 +284,9 @@ set (RESOURCE_SRC
 set (RESOURCE_HEADERS
 	resource/MaterialResource.h
 	resource/Resource.h
+	resource/ResourceLoader.h
 	resource/ResourceManager.h
+	resource/ResourceRegistry.h
 	resource/Bundle.h
 	resource/TextureResource.h
 	resource/MeshResource.h
@@ -394,7 +395,7 @@ if (LINUX)
 		-g
 		-pg
 		-fPIC
-		-fvisibility=hidden
+		#-fvisibility=hidden
 	)
 
 	set (CROWN_EXECUTABLE_NAME crown-linux)
@@ -492,6 +493,8 @@ set (CROWN_HEADERS
 
 	${RESOURCE_HEADERS}
 
+	${RPC_HEADERS}
+
 #	${NETWORK_HEADERS}
 
 	${OS_HEADERS}
@@ -513,6 +516,10 @@ set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_RPATH}:$ORIGIN")
 add_executable(${CROWN_EXECUTABLE_NAME} ${CROWN_MAIN_SRC})
 target_link_libraries(${CROWN_EXECUTABLE_NAME} crown)
 
+if (CROWN_BUILD_TESTS)
+	add_subdirectory(tests)
+endif (CROWN_BUILD_TESTS)
+
 install (TARGETS crown DESTINATION bin)
 install (TARGETS ${CROWN_EXECUTABLE_NAME} DESTINATION bin)
 

+ 71 - 71
engine/ConsoleServer.cpp

@@ -38,93 +38,93 @@ namespace crown
 
 static IntSetting g_port("read_port", "port used for reading", 10000, 9999, 65535);
 
-//-----------------------------------------------------------------------------
-ConsoleServer::ConsoleServer() :
-	m_thread(ConsoleServer::background_thread, (void*)this, "console-thread"),
-	m_active(false)
-{
-	string::strncpy(m_cmd_buffer, "", 1024);
-	string::strncpy(m_err_buffer, "", 1024);
-}
-
-//-----------------------------------------------------------------------------
-void ConsoleServer::init()
-{
-	m_active = true;
-}
-
-//-----------------------------------------------------------------------------
-void ConsoleServer::shutdown()
-{
-	m_active = false;
-
-	m_socket.close();
-}
-
-//-----------------------------------------------------------------------------
-void ConsoleServer::read_eval_loop()
-{
-	m_socket.open(g_port);
+// //-----------------------------------------------------------------------------
+// ConsoleServer::ConsoleServer() :
+// 	m_thread("console-thread"),
+// 	m_active(false)
+// {
+// 	// string::strncpy(m_cmd_buffer, "", 1024);
+// 	// string::strncpy(m_err_buffer, "", 1024);
+// }
+
+// //-----------------------------------------------------------------------------
+// void ConsoleServer::init()
+// {
+// 	// m_active = true;
+// }
+
+// //-----------------------------------------------------------------------------
+// void ConsoleServer::shutdown()
+// {
+// 	// m_active = false;
+
+// 	// m_socket.close();
+// }
+
+// //-----------------------------------------------------------------------------
+// void ConsoleServer::read_eval_loop()
+// {
+// 	// m_socket.open(g_port);
 	
-	char cmd[1024];
+// 	// char cmd[1024];
 
-	while (m_active)
-	{
-		string::strncpy(cmd, "", 1024);
-		receive((char*)cmd, 1024);
+// 	// while (m_active)
+// 	// {
+// 	// 	string::strncpy(cmd, "", 1024);
+// 	// 	receive((char*)cmd, 1024);
 
-		// Fill command buffer
-		string::strncpy(m_cmd_buffer, cmd, 1024);
-	}
+// 	// 	// Fill command buffer
+// 	// 	string::strncpy(m_cmd_buffer, cmd, 1024);
+// 	// }
 
-	Log::i("End read-eval loop");
-	m_socket.close();
-}
+// 	// Log::i("End read-eval loop");
+// 	// m_socket.close();
+// }
 
-//-----------------------------------------------------------------------------
-void ConsoleServer::execute()
-{
-	m_command_mutex.lock();
+// //-----------------------------------------------------------------------------
+// void ConsoleServer::execute()
+// {
+// 	// m_command_mutex.lock();
 
-	LuaEnvironment* lua = device()->lua_environment();
+// 	// LuaEnvironment* lua = device()->lua_environment();
 
-	lua->load_buffer(m_cmd_buffer, string::strlen(m_cmd_buffer));
-	lua->execute(0, 0);
+// 	// lua->load_buffer(m_cmd_buffer, string::strlen(m_cmd_buffer));
+// 	// lua->execute(0, 0);
 
-	string::strncpy(m_cmd_buffer, "", 1024);
+// 	// string::strncpy(m_cmd_buffer, "", 1024);
 
-	string::strncpy(m_err_buffer, lua->error(),  1024);
+// 	// string::strncpy(m_err_buffer, lua->error(),  1024);
 
-	if (string::strcmp(m_err_buffer, "") != 0)
-	{
-		// Fill error buffer
-		send((char*)m_err_buffer, 1024);
-	}
+// 	// if (string::strcmp(m_err_buffer, "") != 0)
+// 	// {
+// 	// 	// Fill error buffer
+// 	// 	send((char*)m_err_buffer, 1024);
+// 	// }
 
-	string::strncpy(m_err_buffer, "", 1024);
+// 	// string::strncpy(m_err_buffer, "", 1024);
 
-	m_command_mutex.unlock();
-}
+// 	// m_command_mutex.unlock();
+// }
 
-//-----------------------------------------------------------------------------
-void ConsoleServer::send(const void* data, size_t size)
-{
-	m_socket.send(data, size);
-}
+// //-----------------------------------------------------------------------------
+// void ConsoleServer::send(const void* data, size_t size)
+// {
+// 	// m_socket.send(data, size);
+// }
 
-//-----------------------------------------------------------------------------
-void ConsoleServer::receive(char* data, size_t size)
-{
-	m_socket.receive(data, size);
-}
+// //-----------------------------------------------------------------------------
+// void ConsoleServer::receive(char* data, size_t size)
+// {
+// 	// m_socket.receive(data, size);
+// }
 
-//-----------------------------------------------------------------------------
-void* ConsoleServer::background_thread(void* thiz)
-{
-	((ConsoleServer*)thiz)->read_eval_loop();
+// //-----------------------------------------------------------------------------
+// void* ConsoleServer::background_thread(void* thiz)
+// {
+// 	// ((ConsoleServer*)thiz)->read_eval_loop();
 
-	return NULL;
-}
+// 	// return NULL;
+// }
 
 
 } // namespace crown

+ 32 - 32
engine/ConsoleServer.h

@@ -38,38 +38,38 @@ class ConsoleServer
 {
 public:
 
-	/// Constructor
-							ConsoleServer();
-	/// Start listening on @port
-	void					init();
-	/// Stop listening
-	void					shutdown();
-	/// Read-evaluation loop, executed on a different thread
-	void					read_eval_loop();
-	/// Execute commands, executed on main thread
-	void					execute();
-	/// Send data to client
-	void					send(const void* data, size_t size = 1024);
-	/// Receive data to client
-	void					receive(char* data, size_t size = 1024);
-
-private:
-
-	static void*			background_thread(void* thiz);
-
-private:
-
-	os::TCPSocket			m_socket;
-
-	os::Thread				m_thread;
-	os::Mutex				m_command_mutex;
-
-	// Is console active?
-	bool					m_active;
-	// Commands buffer
-	char					m_cmd_buffer[1024];
-
-	char 					m_err_buffer[1024];
+// 	/// Constructor
+// 							ConsoleServer();
+// 	/// Start listening on @port
+// 	void					init();
+// 	/// Stop listening
+// 	void					shutdown();
+// 	/// Read-evaluation loop, executed on a different thread
+// 	void					read_eval_loop();
+// 	/// Execute commands, executed on main thread
+// 	void					execute();
+// 	/// Send data to client
+// 	void					send(const void* data, size_t size = 1024);
+// 	/// Receive data to client
+// 	void					receive(char* data, size_t size = 1024);
+
+// private:
+
+// 	static void*			background_thread(void* thiz);
+
+// private:
+
+// 	os::TCPSocket			m_socket;
+
+// 	Thread					m_thread;
+// 	Mutex					m_command_mutex;
+
+// 	// Is console active?
+// 	bool					m_active;
+// 	// Commands buffer
+// 	char					m_cmd_buffer[1024];
+
+// 	char 					m_err_buffer[1024];
 
 };
 

+ 12 - 11
engine/Crown.h

@@ -44,7 +44,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Quat.h"
 #include "Random.h"
 #include "Ray.h"
-#include "Shape.h"
 #include "Triangle.h"
 #include "Vec2.h"
 #include "Vec3.h"
@@ -100,16 +99,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 // Engine
 #include "Camera.h"
 #include "Device.h"
-#include "Glyph.h"
-#include "Bundle.h"
-#include "ArchiveBundle.h"
-#include "FileBundle.h"
-#include "ResourceManager.h"
-#include "TextureResource.h"
-#include "MeshResource.h"
-#include "SoundResource.h"
-#include "MaterialResource.h"
-#include "FontResource.h"
 
 // Engine/Input
 #include "EventDispatcher.h"
@@ -141,3 +130,15 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "DebugRenderer.h"
 #include "PixelFormat.h"
 #include "VertexFormat.h"
+
+// Engine/Resource
+#include "Bundle.h"
+#include "ArchiveBundle.h"
+#include "FileBundle.h"
+#include "ResourceLoader.h"
+#include "ResourceManager.h"
+#include "TextureResource.h"
+#include "MeshResource.h"
+#include "SoundResource.h"
+#include "MaterialResource.h"
+#include "FontResource.h"

+ 26 - 40
engine/Device.cpp

@@ -51,6 +51,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Memory.h"
 #include "LuaEnvironment.h"
 #include "ConsoleServer.h"
+#include "TextReader.h"
 
 namespace crown
 {
@@ -162,7 +163,7 @@ void Device::shutdown()
 	Log::i("Releasing ConsoleServer...");
 	if (m_console_server)
 	{
-		m_console_server->shutdown();
+		//m_console_server->shutdown();
 
 		CE_DELETE(m_allocator, m_console_server);
 	}
@@ -350,15 +351,14 @@ void Device::frame()
 	m_last_delta_time = (m_current_time - m_last_time) / 1000000.0f;
 	m_last_time = m_current_time;
 
-	m_resource_manager->check_load_queue();
-	m_resource_manager->bring_loaded_online();
+	m_resource_manager->poll_resource_loader();
 
 	m_window->frame();
 	m_input_manager->frame(frame_count());
 
 	m_lua_environment->game_frame(last_delta_time());
 
-	m_console_server->execute();
+	//m_console_server->execute();
 
 	m_debug_renderer->draw_all();
 	m_renderer->frame();
@@ -366,36 +366,12 @@ void Device::frame()
 	m_frame_count++;
 }
 
-//-----------------------------------------------------------------------------
-ResourceId Device::load(const char* name)
-{
-	return m_resource_manager->load(name);
-}
-
-//-----------------------------------------------------------------------------
-void Device::unload(ResourceId name)
-{
-	m_resource_manager->unload(name);
-}
-
 //-----------------------------------------------------------------------------
 void Device::reload(ResourceId name)
 {
 	(void)name;
 }
 
-//-----------------------------------------------------------------------------
-bool Device::is_loaded(ResourceId name)
-{
-	return m_resource_manager->is_loaded(name);
-}
-
-//-----------------------------------------------------------------------------
-const void* Device::data(ResourceId name)
-{
-	return m_resource_manager->data(name);
-}
-
 //-----------------------------------------------------------------------------
 void Device::create_filesystem()
 {
@@ -418,8 +394,19 @@ void Device::create_resource_manager()
 		m_resource_bundle = CE_NEW(m_allocator, ArchiveBundle)(*m_filesystem);
 	}
 
+	// Read resource seed
+	DiskFile* seed_file = filesystem()->open("seed.ini", FOM_READ);
+	TextReader reader(*seed_file);
+
+	char tmp_buf[32];
+	reader.read_string(tmp_buf, 32);
+
+	filesystem()->close(seed_file);
+
+	uint32_t seed = string::parse_uint(tmp_buf);
+
 	// Create resource manager
-	m_resource_manager = CE_NEW(m_allocator, ResourceManager)(*m_resource_bundle);
+	m_resource_manager = CE_NEW(m_allocator, ResourceManager)(*m_resource_bundle, seed);
 
 	Log::d("Resource manager created.");
 	Log::d("Resource seed: %d", m_resource_manager->seed());
@@ -477,9 +464,9 @@ void Device::create_lua_environment()
 
 void Device::create_console_server()
 {
-	m_console_server = CE_NEW(m_allocator, ConsoleServer)();
+	m_console_server = NULL;//CE_NEW(m_allocator, ConsoleServer)();
 
-	m_console_server->init();
+	//m_console_server->init();
 
 	Log::d("Console server created.");
 }
@@ -489,15 +476,14 @@ void Device::parse_command_line(int argc, char** argv)
 {
 	static ArgsOption options[] = 
 	{
-
-		"help",             AOA_NO_ARGUMENT,       NULL,        'i',
-		"root-path",        AOA_REQUIRED_ARGUMENT, NULL,        'r',
-		"width",            AOA_REQUIRED_ARGUMENT, NULL,        'w',
-		"height",           AOA_REQUIRED_ARGUMENT, NULL,        'h',
-		"fullscreen",       AOA_NO_ARGUMENT,       &m_preferred_window_fullscreen, 1,
-		"dev",              AOA_NO_ARGUMENT,       &m_preferred_mode, MODE_DEVELOPMENT,
-		"quit-after-init",  AOA_NO_ARGUMENT,       &m_quit_after_init, 1,
-		NULL, 0, NULL, 0
+		{ "help",             AOA_NO_ARGUMENT,       NULL,        'i' },
+		{ "root-path",        AOA_REQUIRED_ARGUMENT, NULL,        'r' },
+		{ "width",            AOA_REQUIRED_ARGUMENT, NULL,        'w' },
+		{ "height",           AOA_REQUIRED_ARGUMENT, NULL,        'h' },
+		{ "fullscreen",       AOA_NO_ARGUMENT,       &m_preferred_window_fullscreen, 1 },
+		{ "dev",              AOA_NO_ARGUMENT,       &m_preferred_mode, MODE_DEVELOPMENT },
+		{ "quit-after-init",  AOA_NO_ARGUMENT,       &m_quit_after_init, 1 },
+		{ NULL, 0, NULL, 0 }
 	};
 
 	Args args(argc, argv, "", options);

+ 0 - 5
engine/Device.h

@@ -93,12 +93,7 @@ public:
 	/// Updates all the subsystems
 	void					frame();
 
-	/// Loads a resource and returns its unique identifier.
-	ResourceId				load(const char* name);
-	void					unload(ResourceId name);
 	void					reload(ResourceId name);
-	bool					is_loaded(ResourceId name);
-	const void*				data(ResourceId name);
 
 	Filesystem*				filesystem();
 	ResourceManager*		resource_manager();

+ 0 - 117
engine/Glyph.h

@@ -1,117 +0,0 @@
-/*
-Copyright (c) 2013 Daniele Bartolini, Michele Rossi
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-#pragma once
-
-#include "Types.h"
-
-namespace crown
-{
-
-class Glyph
-{
-public:
-
-	/// Constructor
-	Glyph() :
-		m_code_point(0),
-		m_left(0),
-		m_right(0),
-		m_bottom(0),
-		m_top(0),
-		m_width(0),
-		m_height(0),
-		m_advance(0),
-		m_baseline(0)
-	{
-	}
-
-	/// Constructor
-	Glyph(uint32_t code, float left, float right, float bottom, float top, float width, float height, float advance, float baseline) :
-		m_code_point(code),
-		m_left(left),
-		m_right(right),
-		m_bottom(bottom),
-		m_top(top),
-		m_width(width),
-		m_height(height),
-		m_advance(advance),
-		m_baseline(baseline)
-	{
-	}
-
-	/// Destructor
-	~Glyph()
-	{
-	}
-
-	/// Returns the glyph's metrics
-	void metrics(float& left, float& right, float& bottom, float& top, float& width, float& height, float& advance, float& baseline) const
-	{
-		left = m_left;
-		right = m_right;
-		bottom = m_bottom;
-		top = m_top;
-		width = m_width;
-		height = m_height;
-		advance = m_advance;
-		baseline = m_baseline;
-	}
-
-	/// Sets the glyph's metrics
-	void set_metrics(float left, float right, float bottom, float top, float width, float height, float advance, float baseline)
-	{
-		m_left = left;
-		m_right = right;
-		m_bottom = bottom;
-		m_top = top;
-		m_width = width;
-		m_height = height;
-		m_advance = advance;
-		m_baseline = baseline;
-	}
-
-	/// Returns the glyph's code point
-	uint32_t code_point() const
-	{
-		return m_code_point;
-	}
-
-private:
-
-	uint32_t m_code_point;
-	float m_left;
-	float m_right;
-	float m_bottom;
-	float m_top;
-	float m_width;
-	float m_height;
-	float m_advance;
-	float m_baseline;
-};
-
-} // namespace crown
-

+ 0 - 65
engine/core/math/Shape.h

@@ -1,65 +0,0 @@
-/*
-Copyright (c) 2013 Daniele Bartolini, Michele Rossi
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-#pragma once
-
-#include "Plane.h"
-#include "Sphere.h"
-#include "Box.h"
-#include "Ray.h"
-
-namespace crown
-{
-
-enum ShapeType
-{
-	ST_PLANE				= BIT(0),
-	ST_SPHERE				= BIT(1),
-	ST_BOX					= BIT(2),
-	ST_RAY					= BIT(3),
-	ST_CONVEX				= BIT(4)
-//	ST_RECT					= BIT(4),
-//	ST_CIRCLE				= BIT(5),
-//	ST_ORIENTED_RECTANGLE	= BIT(6),
-//	ST_RAY_2D				= BIT(7),
-//	ST_SEGMENT				= BIT(8)
-};
-
-class Shape
-{
-public:
-					Shape(ShapeType type);
-					~Shape();
-
-	ShapeType		type();
-
-private:
-
-	ShapeType		m_type;
-	void*			m_shape;
-};
-
-} // namespace crown

+ 9 - 0
engine/core/mem/ProxyAllocator.cpp

@@ -38,6 +38,7 @@ static ProxyAllocator* g_proxy_allocators_head = NULL;
 ProxyAllocator::ProxyAllocator(const char* name, Allocator& allocator) :
 	m_allocator(allocator),
 	m_name(name),
+	m_total_allocated(0),
 	m_next(NULL)
 {
 	CE_ASSERT(name != NULL, "Name must be != NULL");
@@ -53,6 +54,8 @@ ProxyAllocator::ProxyAllocator(const char* name, Allocator& allocator) :
 //-----------------------------------------------------------------------------
 void* ProxyAllocator::allocate(size_t size, size_t align)
 {
+	m_total_allocated += size;
+
 	return m_allocator.allocate(size, align);
 }
 
@@ -62,6 +65,12 @@ void ProxyAllocator::deallocate(void* data)
 	m_allocator.deallocate(data);
 }
 
+//-----------------------------------------------------------------------------
+size_t ProxyAllocator::allocated_size()
+{
+	return m_total_allocated;
+}
+
 //-----------------------------------------------------------------------------
 const char* ProxyAllocator::name() const
 {

+ 6 - 3
engine/core/mem/ProxyAllocator.h

@@ -28,16 +28,15 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #include "Types.h"
 #include "Memory.h"
+#include "Allocator.h"
 
 namespace crown
 {
 
-class Allocator;
-
 /// Offers the facility to tag allocators by a string identifier.
 /// Proxy allocator is appended to a global linked list when instantiated
 /// so that it is possible to later visit that list for debugging purposes.
-class ProxyAllocator
+class ProxyAllocator : public Allocator
 {
 public:
 
@@ -50,6 +49,9 @@ public:
 	/// @copydoc Allocator::deallocate()
 	void					deallocate(void* data);
 
+	/// @copydoc Allocator::allocated_size()
+	size_t					allocated_size();
+
 	/// Returns the name of the proxy allocator
 	const char*				name() const;
 
@@ -74,6 +76,7 @@ private:
 
 	Allocator&				m_allocator;
 	const char*				m_name;
+	size_t					m_total_allocated;
 
 	ProxyAllocator*			m_next;
 };

+ 2 - 5
engine/lua/LuaAccelerometer.cpp

@@ -33,14 +33,11 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t accelerometer_orientation(lua_State* L)
+CE_EXPORT int accelerometer_orientation(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Vec3* orientation = next_vec3();
-	*orientation = device()->accelerometer()->orientation();
-
-	stack.push_vec3(orientation);
+	stack.push_vec3(device()->accelerometer()->orientation());
 
 	return 1;
 }

+ 21 - 21
engine/lua/LuaCamera.cpp

@@ -33,128 +33,128 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t camera(lua_State* L)
+CE_EXPORT int camera(lua_State* L)
 {
 	LuaStack stack(L);	
 
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t camera_position(lua_State* L)
+CE_EXPORT int camera_position(lua_State* L)
 {
 	LuaStack stack(L);	
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t camera_set_position(lua_State* L)
+CE_EXPORT int camera_set_position(lua_State* L)
 {
 	LuaStack stack(L);	
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t camera_look_at(lua_State* L)
+CE_EXPORT int camera_look_at(lua_State* L)
 {
 	LuaStack stack(L);	
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t camera_set_look_at(lua_State* L)
+CE_EXPORT int camera_set_look_at(lua_State* L)
 {
 	LuaStack stack(L);	
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t camera_set_rotation(lua_State* L)
+CE_EXPORT int camera_set_rotation(lua_State* L)
 {
 	LuaStack stack(L);	
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t camera_up(lua_State* L)
+CE_EXPORT int camera_up(lua_State* L)
 {
 	LuaStack stack(L);	
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t camera_fov(lua_State* L)
+CE_EXPORT int camera_fov(lua_State* L)
 {
 	LuaStack stack(L);	
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t camera_set_fov(lua_State* L)
+CE_EXPORT int camera_set_fov(lua_State* L)
 {
 	LuaStack stack(L);	
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t camera_aspect(lua_State* L)
+CE_EXPORT int camera_aspect(lua_State* L)
 {
 	LuaStack stack(L);	
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t camera_set_aspect(lua_State* L)
+CE_EXPORT int camera_set_aspect(lua_State* L)
 {
 	LuaStack stack(L);	
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t camera_near_clip_distance(lua_State* L)
+CE_EXPORT int camera_near_clip_distance(lua_State* L)
 {
 	LuaStack stack(L);	
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t camera_set_near_clip_distance(lua_State* L)
+CE_EXPORT int camera_set_near_clip_distance(lua_State* L)
 {
 	LuaStack stack(L);	
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t camera_far_clip_distance(lua_State* L)
+CE_EXPORT int camera_far_clip_distance(lua_State* L)
 {
 	LuaStack stack(L);	
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t camera_set_far_clip_distance(lua_State* L)
+CE_EXPORT int camera_set_far_clip_distance(lua_State* L)
 {
 	LuaStack stack(L);	
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t camera_projection_matrix(lua_State* L)
+CE_EXPORT int camera_projection_matrix(lua_State* L)
 {
 	LuaStack stack(L);	
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t camera_view_matrix(lua_State* L)
+CE_EXPORT int camera_view_matrix(lua_State* L)
 {
 	LuaStack stack(L);	
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t camera_move_forward(lua_State* L)
+CE_EXPORT int camera_move_forward(lua_State* L)
 {
 	LuaStack stack(L);	
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t camera_move_backward(lua_State* L)
+CE_EXPORT int camera_move_backward(lua_State* L)
 {
 	LuaStack stack(L);	
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t camera_strafe_left(lua_State* L)
+CE_EXPORT int camera_strafe_left(lua_State* L)
 {
 	LuaStack stack(L);	
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t camera_strafe_right(lua_State* L)
+CE_EXPORT int camera_strafe_right(lua_State* L)
 {
 	LuaStack stack(L);	
 }

+ 4 - 4
engine/lua/LuaDevice.cpp

@@ -32,7 +32,7 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t device_frame_count(lua_State* L)
+CE_EXPORT int device_frame_count(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -44,7 +44,7 @@ CE_EXPORT int32_t device_frame_count(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t device_last_delta_time(lua_State* L)
+CE_EXPORT int device_last_delta_time(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -56,7 +56,7 @@ CE_EXPORT int32_t device_last_delta_time(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t device_start(lua_State* /*L*/)
+CE_EXPORT int device_start(lua_State* /*L*/)
 {
 	device()->start();
 
@@ -64,7 +64,7 @@ CE_EXPORT int32_t device_start(lua_State* /*L*/)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t device_stop(lua_State* /*L*/)
+CE_EXPORT int device_stop(lua_State* /*L*/)
 {
 	device()->stop();
 

+ 7 - 8
engine/lua/LuaFloatSetting.cpp

@@ -27,13 +27,12 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "FloatSetting.h"
 #include "LuaStack.h"
 #include "LuaEnvironment.h"
-#include "StringUtils.h"
 
 namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t float_setting_value(lua_State* L)
+CE_EXPORT int float_setting_value(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -54,7 +53,7 @@ CE_EXPORT int32_t float_setting_value(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t float_setting_synopsis(lua_State* L)
+CE_EXPORT int float_setting_synopsis(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -64,18 +63,18 @@ CE_EXPORT int32_t float_setting_synopsis(lua_State* L)
 
 	if (setting != NULL)
 	{
-		stack.push_string(setting->synopsis(), string::strlen(setting->synopsis()));
+		stack.push_string(setting->synopsis());
 	}
 	else
 	{
-		stack.push_string("", string::strlen(""));
+		stack.push_string("");
 	}
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t float_setting_min(lua_State* L)
+CE_EXPORT int float_setting_min(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -96,7 +95,7 @@ CE_EXPORT int32_t float_setting_min(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t float_setting_max(lua_State* L)
+CE_EXPORT int float_setting_max(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -117,7 +116,7 @@ CE_EXPORT int32_t float_setting_max(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t float_setting_update(lua_State* L)
+CE_EXPORT int float_setting_update(lua_State* L)
 {
 	LuaStack stack(L);
 

+ 7 - 8
engine/lua/LuaIntSetting.cpp

@@ -27,13 +27,12 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "IntSetting.h"
 #include "LuaStack.h"
 #include "LuaEnvironment.h"
-#include "StringUtils.h"
 
 namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t int_setting_query_value(lua_State* L)
+CE_EXPORT int int_setting_query_value(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -54,7 +53,7 @@ CE_EXPORT int32_t int_setting_query_value(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t int_setting_query_synopsis(lua_State* L)
+CE_EXPORT int int_setting_query_synopsis(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -64,18 +63,18 @@ CE_EXPORT int32_t int_setting_query_synopsis(lua_State* L)
 
 	if (setting != NULL)
 	{
-		stack.push_string(setting->synopsis(), string::strlen(setting->synopsis()));
+		stack.push_string(setting->synopsis());
 	}
 	else
 	{
-		stack.push_string("", string::strlen(""));
+		stack.push_string("");
 	}
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t int_setting_query_min(lua_State* L)
+CE_EXPORT int int_setting_query_min(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -96,7 +95,7 @@ CE_EXPORT int32_t int_setting_query_min(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t int_setting_query_max(lua_State* L)
+CE_EXPORT int int_setting_query_max(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -117,7 +116,7 @@ CE_EXPORT int32_t int_setting_query_max(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t int_setting_update(lua_State* L)
+CE_EXPORT int int_setting_update(lua_State* L)
 {
 	LuaStack stack(L);
 

+ 5 - 5
engine/lua/LuaKeyboard.cpp

@@ -33,7 +33,7 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t keyboard_modifier_pressed(lua_State* L)
+CE_EXPORT int keyboard_modifier_pressed(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -45,7 +45,7 @@ CE_EXPORT int32_t keyboard_modifier_pressed(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t keyboard_key_pressed(lua_State* L)
+CE_EXPORT int keyboard_key_pressed(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -57,7 +57,7 @@ CE_EXPORT int32_t keyboard_key_pressed(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t keyboard_key_released(lua_State* L)
+CE_EXPORT int keyboard_key_released(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -69,7 +69,7 @@ CE_EXPORT int32_t keyboard_key_released(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t keyboard_any_pressed(lua_State* L)
+CE_EXPORT int keyboard_any_pressed(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -79,7 +79,7 @@ CE_EXPORT int32_t keyboard_any_pressed(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t keyboard_any_released(lua_State* L)
+CE_EXPORT int keyboard_any_released(lua_State* L)
 {
 	LuaStack stack(L);
 

+ 139 - 173
engine/lua/LuaMat4.cpp

@@ -34,508 +34,474 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mat4(lua_State* L)
+CE_EXPORT int mat4(lua_State* L)
 {
 	LuaStack stack(L);
 
 	float m0 = stack.get_float(1);
 	float m1 = stack.get_float(2);
 	float m2 = stack.get_float(3);
-	float m4 = stack.get_float(4);	
-	float m5 = stack.get_float(5);
-	float m6 = stack.get_float(6);	
-	float m8 = stack.get_float(7);
-	float m9 = stack.get_float(8);
-	float m10 = stack.get_float(9);
-
-	Mat4* mat = next_mat4();
-
-	mat->m[0] = m0;
-	mat->m[1] = m1;
-	mat->m[2] = m2;
-	mat->m[4] = m4;
-	mat->m[5] = m5;
-	mat->m[6] = m6;
-	mat->m[8] = m8;
-	mat->m[9] = m9;
-	mat->m[10] = m10;
-
-	stack.push_mat4(mat);
+	float m3 = stack.get_float(4);
+	float m4 = stack.get_float(5);	
+	float m5 = stack.get_float(6);
+	float m6 = stack.get_float(7);
+	float m7 = stack.get_float(8);
+	float m8 = stack.get_float(9);
+	float m9 = stack.get_float(10);
+	float m10 = stack.get_float(11);
+	float m11 = stack.get_float(12);
+	float m12 = stack.get_float(13);
+	float m13 = stack.get_float(14);
+	float m14 = stack.get_float(15);
+	float m15 = stack.get_float(16);
+
+	stack.push_mat4(Mat4(m0, m1, m2, m3, m4, m5, m6, m7, m8, m9, m10, m11, m12, m13, m14, m15));
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------					
-CE_EXPORT int32_t mat4_add(lua_State* L)
+CE_EXPORT int mat4_add(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Mat4* a = stack.get_mat4(1);
-	Mat4* b = stack.get_mat4(2);
+	Mat4& a = stack.get_mat4(1);
+	Mat4& b = stack.get_mat4(2);
 
-	*a += *b;
-
-	stack.push_mat4(a);
+	stack.push_mat4(a + b);
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mat4_subtract(lua_State* L)
+CE_EXPORT int mat4_subtract(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Mat4* a = (Mat4*)stack.get_mat4(1);
-	Mat4* b = (Mat4*)stack.get_mat4(2);
-
-	*a -= *b;
+	Mat4& a = stack.get_mat4(1);
+	Mat4& b = stack.get_mat4(2);
 
-	stack.push_mat4(a);
+	stack.push_mat4(a - b);
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mat4_multiply(lua_State* L)
+CE_EXPORT int mat4_multiply(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Mat4* a = stack.get_mat4(1);
-	Mat4* b = stack.get_mat4(2);
-
-	*a *= *b;
+	Mat4& a = stack.get_mat4(1);
+	Mat4& b = stack.get_mat4(2);
 
-	stack.push_mat4(a);
+	stack.push_mat4(a * b);
 
 	return 1;
 }	
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mat4_multiply_by_scalar(lua_State* L)
+CE_EXPORT int mat4_multiply_by_scalar(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Mat4* a = (Mat4*)stack.get_mat4(1);
+	Mat4& a = stack.get_mat4(1);
 	float k = stack.get_float(2);
 
-	*a *= k;
-
-	stack.push_mat4(a);
+	stack.push_mat4(a * k);
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mat4_divide_by_scalar(lua_State* L)
+CE_EXPORT int mat4_divide_by_scalar(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Mat4* a = (Mat4*)stack.get_mat4(1);
+	Mat4& a = stack.get_mat4(1);
 	float k = stack.get_float(2);
 
-	*a /= k;
-
-	stack.push_mat4(a);
+	stack.push_mat4(a / k);
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mat4_build_rotation_x(lua_State* L)
+CE_EXPORT int mat4_build_rotation_x(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Mat4* a = (Mat4*)stack.get_mat4(1);
+	Mat4& a = stack.get_mat4(1);
 	float k = stack.get_float(2);
 
-	a->build_rotation_x(k);
+	a.build_rotation_x(k);
 
 	return 0;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mat4_build_rotation_y(lua_State* L)
+CE_EXPORT int mat4_build_rotation_y(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Mat4* a = stack.get_mat4(1);
+	Mat4& a = stack.get_mat4(1);
 	float k = stack.get_float(2);
 
-	a->build_rotation_y(k);
+	a.build_rotation_y(k);
 
 	return 0;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mat4_build_rotation_z(lua_State* L)
+CE_EXPORT int mat4_build_rotation_z(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Mat4* a = (Mat4*)stack.get_mat4(1);
+	Mat4& a = stack.get_mat4(1);
 	float k = stack.get_float(2);
 
-	a->build_rotation_z(k);
+	a.build_rotation_z(k);
 
 	return 0;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mat4_build_rotation(lua_State* L)
+CE_EXPORT int mat4_build_rotation(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Mat4* a = (Mat4*)stack.get_mat4(1);
-	Vec3* d = (Vec3*)stack.get_vec3(2);
+	Mat4& a = stack.get_mat4(1);
+	Vec3& d = stack.get_vec3(2);
 	float k = stack.get_float(3);
 
-	a->build_rotation(*d, k);
+	a.build_rotation(d, k);
 
 	return 0;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mat4_build_projection_perspective_rh(lua_State* L)
+CE_EXPORT int mat4_build_projection_perspective_rh(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Mat4* a = stack.get_mat4(1);
+	Mat4& a = stack.get_mat4(1);
 	float fovy = stack.get_float(2);
 	float aspect = stack.get_float(3);
 	float near = stack.get_float(4);
 	float far = stack.get_float(5);
 
-	a->build_projection_perspective_rh(fovy, aspect, near, far);
+	a.build_projection_perspective_rh(fovy, aspect, near, far);
 
 	return 0;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mat4_build_projection_perspective_lh(lua_State* L)
+CE_EXPORT int mat4_build_projection_perspective_lh(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Mat4* a = stack.get_mat4(1);
+	Mat4& a = stack.get_mat4(1);
 	float fovy = stack.get_float(2);
 	float aspect = stack.get_float(3);
 	float near = stack.get_float(4);
 	float far = stack.get_float(5);
 
-	a->build_projection_perspective_lh(fovy, aspect, near, far);
+	a.build_projection_perspective_lh(fovy, aspect, near, far);
 
 	return 0;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mat4_build_projection_ortho_rh(lua_State* L)
+CE_EXPORT int mat4_build_projection_ortho_rh(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Mat4* a = stack.get_mat4(1);
+	Mat4& a = stack.get_mat4(1);
 	float width = stack.get_float(2);
 	float height = stack.get_float(3);
 	float near = stack.get_float(4);
 	float far = stack.get_float(5);
 
-	a->build_projection_ortho_rh(width, height, near, far);
+	a.build_projection_ortho_rh(width, height, near, far);
 
 	return 0;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mat4_build_projection_ortho_lh(lua_State* L)
+CE_EXPORT int mat4_build_projection_ortho_lh(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Mat4* a = stack.get_mat4(1);
+	Mat4& a = stack.get_mat4(1);
 	float width = stack.get_float(2);
 	float height = stack.get_float(3);
 	float near = stack.get_float(4);
 	float far = stack.get_float(5);
 
-	a->build_projection_ortho_lh(width, height, near, far);
+	a.build_projection_ortho_lh(width, height, near, far);
 
 	return 0;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mat4_build_projection_ortho_2d_rh(lua_State* L)
+CE_EXPORT int mat4_build_projection_ortho_2d_rh(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Mat4* a = stack.get_mat4(1);
+	Mat4& a = stack.get_mat4(1);
 	float width = stack.get_float(2);
 	float height = stack.get_float(3);
 	float near = stack.get_float(4);
 	float far = stack.get_float(5);
 
-	a->build_projection_ortho_2d_rh(width, height, near, far);
+	a.build_projection_ortho_2d_rh(width, height, near, far);
 
 	return 0;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mat4_build_look_at_rh(lua_State* L)
+CE_EXPORT int mat4_build_look_at_rh(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Mat4* a = stack.get_mat4(1);
-	Vec3* pos = stack.get_vec3(2);
-	Vec3* target = stack.get_vec3(3);
-	Vec3* up = stack.get_vec3(4);
+	Mat4& a = stack.get_mat4(1);
+	Vec3& pos = stack.get_vec3(2);
+	Vec3& target = stack.get_vec3(3);
+	Vec3& up = stack.get_vec3(4);
 
-	a->build_look_at_rh(*pos, *target, *up);
+	a.build_look_at_rh(pos, target, up);
 
 	return 0;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mat4_build_look_at_lh(lua_State* L)
+CE_EXPORT int mat4_build_look_at_lh(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Mat4* a = stack.get_mat4(1);
-	Vec3* pos = stack.get_vec3(2);
-	Vec3* target = stack.get_vec3(3);
-	Vec3* up = stack.get_vec3(4);
+	Mat4& a = stack.get_mat4(1);
+	Vec3& pos = stack.get_vec3(2);
+	Vec3& target = stack.get_vec3(3);
+	Vec3& up = stack.get_vec3(4);
 
-	a->build_look_at_lh(*pos, *target, *up);
+	a.build_look_at_lh(pos, target, up);
 
 	return 0;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mat4_build_viewpoint_billboard(lua_State* L)
+CE_EXPORT int mat4_build_viewpoint_billboard(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Mat4* a = stack.get_mat4(1);
-	Vec3* pos = stack.get_vec3(2);
-	Vec3* target = stack.get_vec3(3);
-	Vec3* up = stack.get_vec3(4);
+	Mat4& a = stack.get_mat4(1);
+	Vec3& pos = stack.get_vec3(2);
+	Vec3& target = stack.get_vec3(3);
+	Vec3& up = stack.get_vec3(4);
 
-	a->build_viewpoint_billboard(*pos, *target, *up);
+	a.build_viewpoint_billboard(pos, target, up);
 
 	return 0;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mat4_build_axis_billboard(lua_State* L)
+CE_EXPORT int mat4_build_axis_billboard(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Mat4* a = stack.get_mat4(1);
-	Vec3* pos = stack.get_vec3(2);
-	Vec3* target = stack.get_vec3(3);
-	Vec3* up = stack.get_vec3(4);
+	Mat4& a = stack.get_mat4(1);
+	Vec3& pos = stack.get_vec3(2);
+	Vec3& target = stack.get_vec3(3);
+	Vec3& up = stack.get_vec3(4);
 
-	a->build_axis_billboard(*pos, *target, *up);
+	a.build_axis_billboard(pos, target, up);
 
 	return 0;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mat4_transpose(lua_State* L)
+CE_EXPORT int mat4_transpose(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Mat4* a = stack.get_mat4(1);
-
-	a->transpose();
+	Mat4& a = stack.get_mat4(1);
 
-	stack.push_mat4(a);
+	stack.push_mat4(a.transpose());
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mat4_determinant(lua_State* L)
+CE_EXPORT int mat4_determinant(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Mat4* a = stack.get_mat4(1);
+	Mat4& a = stack.get_mat4(1);
 
-	stack.push_float(a->get_determinant());
+	stack.push_float(a.get_determinant());
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mat4_invert(lua_State* L)
+CE_EXPORT int mat4_invert(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Mat4* a = stack.get_mat4(1);
+	Mat4& a = stack.get_mat4(1);
 
-	a->invert();
-
-	stack.push_mat4(a);
+	stack.push_mat4(a.invert());
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mat4_load_identity(lua_State* L)
+CE_EXPORT int mat4_load_identity(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Mat4* a = stack.get_mat4(1);
+	Mat4& a = stack.get_mat4(1);
 
-	a->load_identity();
+	a.load_identity();
 
 	return 0;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mat4_x(lua_State* L)
+CE_EXPORT int mat4_x(lua_State* L)
 {	
 	LuaStack stack(L);
 
-	Mat4* a = stack.get_mat4(1);
-
-	Vec3* x = next_vec3();
-	*x = a->x();
+	Mat4& a = stack.get_mat4(1);
 
-	stack.push_vec3(x);
+	stack.push_vec3(a.x());
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mat4_y(lua_State* L)
+CE_EXPORT int mat4_y(lua_State* L)
 {	
 	LuaStack stack(L);
 
-	Mat4* a = stack.get_mat4(1);
-
-	Vec3* y = next_vec3();
-	*y = a->y();
+	Mat4& a = stack.get_mat4(1);
 
-	stack.push_vec3(y);
+	stack.push_vec3(a.y());
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mat4_z(lua_State* L)
+CE_EXPORT int mat4_z(lua_State* L)
 {	
 	LuaStack stack(L);
 
-	Mat4* a = stack.get_mat4(1);
+	Mat4& a = stack.get_mat4(1);
 
-	Vec3* z = next_vec3();
-	*z = a->z();
-
-	stack.push_vec3(z);
+	stack.push_vec3(a.z());
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mat4_set_x(lua_State* L)
+CE_EXPORT int mat4_set_x(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Mat4* a = stack.get_mat4(1);
-	Vec3* x = stack.get_vec3(2);
+	Mat4& a = stack.get_mat4(1);
+	Vec3& x = stack.get_vec3(2);
 
-	a->set_x(*x);
+	a.set_x(x);
 
 	return 0;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mat4_set_y(lua_State* L)
+CE_EXPORT int mat4_set_y(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Mat4* a = stack.get_mat4(1);
-	Vec3* y = stack.get_vec3(2);
+	Mat4& a = stack.get_mat4(1);
+	Vec3& y = stack.get_vec3(2);
 
-	a->set_y(*y);
+	a.set_y(y);
 
 	return 0;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mat4_set_z(lua_State* L)
+CE_EXPORT int mat4_set_z(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Mat4* a = stack.get_mat4(1);
-	Vec3* z = stack.get_vec3(2);
+	Mat4& a = stack.get_mat4(1);
+	Vec3& z = stack.get_vec3(2);
 
-	a->set_z(*z);
+	a.set_z(z);
 
 	return 0;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mat4_translation(lua_State* L)
+CE_EXPORT int mat4_translation(lua_State* L)
 {	
 	LuaStack stack(L);
 
-	Mat4* a = stack.get_mat4(1);
-
-	Vec3* translation = next_vec3();
-	*translation = a->translation();
+	Mat4& a = stack.get_mat4(1);
 
-	stack.push_vec3(translation);
+	stack.push_vec3(a.translation());
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mat4_set_translation(lua_State* L)
+CE_EXPORT int mat4_set_translation(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Mat4* a = stack.get_mat4(1);
-	Vec3* trans = stack.get_vec3(2);
+	Mat4& a = stack.get_mat4(1);
+	Vec3& trans = stack.get_vec3(2);
 
-	a->set_translation(*trans);
+	a.set_translation(trans);
 
 	return 0;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mat4_get_scale(lua_State* L)
+CE_EXPORT int mat4_get_scale(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Mat4* a = stack.get_mat4(1);
-
-	Vec3* scale = next_vec3();
-	*scale = a->get_scale();
+	Mat4& a = stack.get_mat4(1);
 
-	stack.push_vec3(scale);
+	stack.push_vec3(a.get_scale());
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mat4_set_scale(lua_State* L)
+CE_EXPORT int mat4_set_scale(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Mat4* a = stack.get_mat4(1);
-	Vec3* scale = stack.get_vec3(2);
+	Mat4& a = stack.get_mat4(1);
+	Vec3& scale = stack.get_vec3(2);
 
-	a->set_scale(*scale);
+	a.set_scale(scale);
 
 	return 0;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mat4_print(lua_State* L)
+CE_EXPORT int mat4_print(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Mat4* a = stack.get_mat4(1);
+	Mat4& a = stack.get_mat4(1);
 
-	os::printf("|%.1f|%.1f|%.1f|%.1f|\n", a->m[0], a->m[4], a->m[8], a->m[12]);
-	os::printf("|%.1f|%.1f|%.1f|%.1f|\n", a->m[1], a->m[5], a->m[9], a->m[13]);
-	os::printf("|%.1f|%.1f|%.1f|%.1f|\n", a->m[2], a->m[6], a->m[10], a->m[14]);
-	os::printf("|%.1f|%.1f|%.1f|%.1f|\n", a->m[3], a->m[7], a->m[11], a->m[15]);
+	os::printf("|%.1f|%.1f|%.1f|%.1f|\n", a.m[0], a.m[4], a.m[8], a.m[12]);
+	os::printf("|%.1f|%.1f|%.1f|%.1f|\n", a.m[1], a.m[5], a.m[9], a.m[13]);
+	os::printf("|%.1f|%.1f|%.1f|%.1f|\n", a.m[2], a.m[6], a.m[10], a.m[14]);
+	os::printf("|%.1f|%.1f|%.1f|%.1f|\n", a.m[3], a.m[7], a.m[11], a.m[15]);
 
 	return 0;
 }

+ 16 - 16
engine/lua/LuaMath.cpp

@@ -32,7 +32,7 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t math_deg_to_rad(lua_State* L)
+CE_EXPORT int math_deg_to_rad(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -44,7 +44,7 @@ CE_EXPORT int32_t math_deg_to_rad(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t math_rad_to_deg(lua_State* L)
+CE_EXPORT int math_rad_to_deg(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -56,7 +56,7 @@ CE_EXPORT int32_t math_rad_to_deg(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t math_next_pow_2(lua_State* L)
+CE_EXPORT int math_next_pow_2(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -68,7 +68,7 @@ CE_EXPORT int32_t math_next_pow_2(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t math_is_pow_2(lua_State* L)
+CE_EXPORT int math_is_pow_2(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -80,7 +80,7 @@ CE_EXPORT int32_t math_is_pow_2(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t math_ceil(lua_State* L)
+CE_EXPORT int math_ceil(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -92,7 +92,7 @@ CE_EXPORT int32_t math_ceil(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t math_floor(lua_State* L)
+CE_EXPORT int math_floor(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -104,7 +104,7 @@ CE_EXPORT int32_t math_floor(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t math_sqrt(lua_State* L)
+CE_EXPORT int math_sqrt(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -116,7 +116,7 @@ CE_EXPORT int32_t math_sqrt(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t math_inv_sqrt(lua_State* L)
+CE_EXPORT int math_inv_sqrt(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -128,7 +128,7 @@ CE_EXPORT int32_t math_inv_sqrt(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t math_sin(lua_State* L)
+CE_EXPORT int math_sin(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -140,7 +140,7 @@ CE_EXPORT int32_t math_sin(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t math_cos(lua_State* L)
+CE_EXPORT int math_cos(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -152,7 +152,7 @@ CE_EXPORT int32_t math_cos(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t math_asin(lua_State* L)
+CE_EXPORT int math_asin(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -164,7 +164,7 @@ CE_EXPORT int32_t math_asin(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t math_acos(lua_State* L)
+CE_EXPORT int math_acos(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -176,7 +176,7 @@ CE_EXPORT int32_t math_acos(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t math_tan(lua_State* L)
+CE_EXPORT int math_tan(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -188,7 +188,7 @@ CE_EXPORT int32_t math_tan(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t math_atan2(lua_State* L)
+CE_EXPORT int math_atan2(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -201,7 +201,7 @@ CE_EXPORT int32_t math_atan2(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t math_abs(lua_State* L)
+CE_EXPORT int math_abs(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -213,7 +213,7 @@ CE_EXPORT int32_t math_abs(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t math_fmod(lua_State* L)
+CE_EXPORT int math_fmod(lua_State* L)
 {
 	LuaStack stack(L);
 

+ 14 - 21
engine/lua/LuaMouse.cpp

@@ -34,7 +34,7 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mouse_button_pressed(lua_State* L)
+CE_EXPORT int mouse_button_pressed(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -46,7 +46,7 @@ CE_EXPORT int32_t mouse_button_pressed(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mouse_button_released(lua_State* L)
+CE_EXPORT int mouse_button_released(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -58,7 +58,7 @@ CE_EXPORT int32_t mouse_button_released(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mouse_any_pressed(lua_State* L)
+CE_EXPORT int mouse_any_pressed(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -68,7 +68,7 @@ CE_EXPORT int32_t mouse_any_pressed(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mouse_any_released(lua_State* L)
+CE_EXPORT int mouse_any_released(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -78,52 +78,45 @@ CE_EXPORT int32_t mouse_any_released(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mouse_cursor_xy(lua_State* L)
+CE_EXPORT int mouse_cursor_xy(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Vec2* xy = next_vec2();
-
-	*xy = device()->mouse()->cursor_xy();
-
-	stack.push_vec2(xy);
+	stack.push_vec2(device()->mouse()->cursor_xy());
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mouse_set_cursor_xy(lua_State* L)
+CE_EXPORT int mouse_set_cursor_xy(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Vec2* xy = stack.get_vec2(1);
+	Vec2& xy = stack.get_vec2(1);
 
-	device()->mouse()->set_cursor_xy(*xy);
+	device()->mouse()->set_cursor_xy(xy);
 
 	return 0;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mouse_cursor_relative_xy(lua_State* L)
+CE_EXPORT int mouse_cursor_relative_xy(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Vec2* xy = next_vec2();
-	*xy = device()->mouse()->cursor_relative_xy();
-
-	stack.push_vec2(xy);
+	stack.push_vec2(device()->mouse()->cursor_relative_xy());
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t mouse_set_cursor_relative_xy(lua_State* L)
+CE_EXPORT int mouse_set_cursor_relative_xy(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Vec2* xy = (Vec2*) stack.get_vec2(1);
+	Vec2& xy = stack.get_vec2(1);
 
-	device()->mouse()->set_cursor_relative_xy(*xy);
+	device()->mouse()->set_cursor_relative_xy(xy);
 
 	return 0;
 }

+ 37 - 54
engine/lua/LuaQuat.cpp

@@ -34,130 +34,113 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t quat(lua_State* L)
+CE_EXPORT int quat(lua_State* L)
 {
 	LuaStack stack(L);
 
 	float w = stack.get_float(1);
-	Vec3* v = stack.get_vec3(2);
+	Vec3& v = stack.get_vec3(2);
 
-	Quat* quat = next_quat();
-
-	quat->w = w;
-	quat->v = *v;
-
-	stack.push_quat(quat);
+	stack.push_quat(Quat(v, w));
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t quat_negate(lua_State* L)
+CE_EXPORT int quat_negate(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Quat* q = stack.get_quat(1);
+	Quat& q = stack.get_quat(1);
 
-	q->negate();
+	q.negate();
 
 	return 0;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t quat_load_identity(lua_State* L)
+CE_EXPORT int quat_load_identity(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Quat* q = stack.get_quat(1);
+	Quat& q = stack.get_quat(1);
 
-	q->load_identity();
+	q.load_identity();
 
 	return 0;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t quat_length(lua_State* L)
+CE_EXPORT int quat_length(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Quat* q = stack.get_quat(1);
+	Quat& q = stack.get_quat(1);
 
-	stack.push_float(q->length());
+	stack.push_float(q.length());
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t quat_conjugate(lua_State* L)
+CE_EXPORT int quat_conjugate(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Quat* q = stack.get_quat(1);
+	Quat& q = stack.get_quat(1);
 
-	Quat* conjugate = next_quat();
-	*conjugate = q->get_conjugate();
-
-	stack.push_quat(conjugate);
+	stack.push_quat(q.get_conjugate());
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t quat_inverse(lua_State* L)
+CE_EXPORT int quat_inverse(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Quat* q = stack.get_quat(1);
-
-	Quat* inverse = next_quat();
-	*inverse = q->get_inverse();
+	Quat& q = stack.get_quat(1);
 
-	stack.push_quat(inverse);
+	stack.push_quat(q.get_inverse());
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t quat_cross(lua_State* L)
+CE_EXPORT int quat_cross(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Quat* q1 = stack.get_quat(1);
-	Quat* q2 = stack.get_quat(2);
+	Quat& q1 = stack.get_quat(1);
+	Quat& q2 = stack.get_quat(2);
 
-	*q1 = (*q1) * (*q2);
-
-	stack.push_quat(q1);
+	stack.push_quat(q1 * q2);
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t quat_multiply(lua_State* L)
+CE_EXPORT int quat_multiply(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Quat* q = stack.get_quat(1);
+	Quat& q = stack.get_quat(1);
 	float k = stack.get_float(2);
 
-	*q = (*q) * k;
-
-	stack.push_quat(q);
+	stack.push_quat(q * k);
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t quat_power(lua_State* L)
+CE_EXPORT int quat_power(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Quat* q = stack.get_quat(1);
+	Quat& q = stack.get_quat(1);
 	float k = stack.get_float(2);
 
-	q->power(k);
-
-	stack.push_quat(q);
+	stack.push_quat(q.power(k));
 
 	return 1;
 }
@@ -165,15 +148,15 @@ CE_EXPORT int32_t quat_power(lua_State* L)
 //-----------------------------------------------------------------------------
 void load_quat(LuaEnvironment& env)
 {
-	env.load_module_function("Quat", "new", quat);
-	env.load_module_function("Quat", "negate", quat_negate);
-	env.load_module_function("Quat", "load_identity", quat_load_identity);
-	env.load_module_function("Quat", "length", quat_length);
-	env.load_module_function("Quat", "conjugate", quat_conjugate);
-	env.load_module_function("Quat", "inverse", quat_inverse);
-	env.load_module_function("Quat", "cross", quat_cross);
-	env.load_module_function("Quat", "mul", quat_multiply);
-	env.load_module_function("Quat", "pow", quat_power);
+	env.load_module_function("Quat", "new",				quat);
+	env.load_module_function("Quat", "negate",			quat_negate);
+	env.load_module_function("Quat", "load_identity",	quat_load_identity);
+	env.load_module_function("Quat", "length",			quat_length);
+	env.load_module_function("Quat", "conjugate",		quat_conjugate);
+	env.load_module_function("Quat", "inverse",			quat_inverse);
+	env.load_module_function("Quat", "cross",			quat_cross);
+	env.load_module_function("Quat", "mul",				quat_multiply);
+	env.load_module_function("Quat", "pow",				quat_power);
 }
 
 } //namespace crown

+ 60 - 56
engine/lua/LuaStack.cpp

@@ -51,35 +51,35 @@ static Quat 			quat_buffer[LUA_QUAT_BUFFER_SIZE];
 static uint32_t 		quat_used = 0;
 
 //-----------------------------------------------------------------------------
-Vec2* next_vec2()
+static Vec2* next_vec2(const Vec2& v)
 {
 	CE_ASSERT(vec2_used < LUA_VEC2_BUFFER_SIZE, "Maximum number of Vec2 reached");
 
-	return &vec2_buffer[vec2_used++];
+	return &(vec2_buffer[vec2_used++] = v);
 }
 
 //-----------------------------------------------------------------------------
-Vec3* next_vec3()
+static Vec3* next_vec3(const Vec3& v)
 {
 	CE_ASSERT(vec3_used < LUA_VEC3_BUFFER_SIZE, "Maximum number of Vec3 reached");
 
-	return &vec3_buffer[vec3_used++];
+	return &(vec3_buffer[vec3_used++] = v);
 }
 
 //-----------------------------------------------------------------------------
-Mat4* next_mat4()
+static Mat4* next_mat4(const Mat4& m)
 {
 	CE_ASSERT(mat4_used < LUA_MAT4_BUFFER_SIZE, "Maximum number of Mat4 reached");
 
-	return &mat4_buffer[mat4_used++];
+	return &(mat4_buffer[mat4_used++] = m);
 }
 
 //-----------------------------------------------------------------------------
-Quat* next_quat()
+static Quat* next_quat(const Quat& q)
 {
 	CE_ASSERT(quat_used < LUA_QUAT_BUFFER_SIZE, "Maximum number of Quat reached");
 
-	return &quat_buffer[quat_used++];
+	return &(quat_buffer[quat_used++] = q);
 }
 
 //-----------------------------------------------------------------------------	
@@ -94,6 +94,12 @@ lua_State* LuaStack::state()
 	return m_state;
 }
 
+//-----------------------------------------------------------------------------
+int32_t LuaStack::num_args()
+{
+	return lua_gettop(m_state);
+}
+
 //-----------------------------------------------------------------------------
 void LuaStack::push_bool(bool value)
 {
@@ -131,9 +137,15 @@ void LuaStack::push_float(float value)
 }
 
 //-----------------------------------------------------------------------------
-void LuaStack::push_string(const char* str, size_t len)
+void LuaStack::push_string(const char* s)
 {
-	lua_pushlstring(m_state, str, len);
+	lua_pushstring(m_state, s);
+}
+
+//-----------------------------------------------------------------------------
+void LuaStack::push_literal(const char* s, size_t len)
+{
+	lua_pushlstring(m_state, s, len);
 }
 
 //-----------------------------------------------------------------------------
@@ -143,35 +155,27 @@ void LuaStack::push_lightdata(void* data)
 }
 
 //-----------------------------------------------------------------------------
-void LuaStack::push_vec2(Vec2* v)
+void LuaStack::push_vec2(const Vec2& v)
 {
-	CE_ASSERT(v >= &vec2_buffer[0] && v <= &vec2_buffer[LUA_VEC2_BUFFER_SIZE-1], "Vec2 type error");
-
-	lua_pushlightuserdata(m_state, v);
+	lua_pushlightuserdata(m_state, next_vec2(v));
 }
 
 //-----------------------------------------------------------------------------
-void LuaStack::push_vec3(Vec3* v)
+void LuaStack::push_vec3(const Vec3& v)
 {
-	CE_ASSERT(v >= &vec3_buffer[0] && v <= &vec3_buffer[LUA_VEC3_BUFFER_SIZE-1], "Vec3 type error");
-
-	lua_pushlightuserdata(m_state, v);
+	lua_pushlightuserdata(m_state, next_vec3(v));
 }
 
 //-----------------------------------------------------------------------------
-void LuaStack::push_mat4(Mat4* m)
+void LuaStack::push_mat4(const Mat4& m)
 {
-	CE_ASSERT(m >= &mat4_buffer[0] && m <= &mat4_buffer[LUA_MAT4_BUFFER_SIZE-1], "Mat4 type error");
-
-	lua_pushlightuserdata(m_state, m);
+	lua_pushlightuserdata(m_state, next_mat4(m));
 }
 
 //-----------------------------------------------------------------------------
-void LuaStack::push_quat(Quat* q)
+void LuaStack::push_quat(const Quat& q)
 {
-	CE_ASSERT(q >= &quat_buffer[0] && q <= &quat_buffer[LUA_MAT4_BUFFER_SIZE-1], "Quat type error");
-
-	lua_pushlightuserdata(m_state, q);
+	lua_pushlightuserdata(m_state, next_quat(q));
 }
 
 //-----------------------------------------------------------------------------
@@ -201,59 +205,59 @@ const char* LuaStack::get_string(int32_t index)
 //-----------------------------------------------------------------------------
 void* LuaStack::get_lightdata(int32_t index)
 {
-	CE_ASSERT(lua_islightuserdata(m_state, index), "Not a lightuserdata object");
-
-	void* data = lua_touserdata(m_state, index);
-
-	return data;	
+	return lua_touserdata(m_state, index);	
 }
 
 //-----------------------------------------------------------------------------
-Vec2* LuaStack::get_vec2(int32_t index)
+Vec2& LuaStack::get_vec2(int32_t index)
 {
-	CE_ASSERT(lua_islightuserdata(m_state, index), "Not a lightuserdata object");
+	void* v = lua_touserdata(m_state, index);
 
-	Vec2* v = (Vec2*)lua_touserdata(m_state, index);
+	if (v < &vec2_buffer[0] || v > &vec2_buffer[LUA_VEC2_BUFFER_SIZE-1])
+	{
+		luaL_typerror(m_state, index, "Vec2");
+	}
 
-	CE_ASSERT(v >= &vec2_buffer[0] && v <= &vec2_buffer[LUA_VEC2_BUFFER_SIZE-1], "Vec2 type error");
-
-	return v;
+	return *(Vec2*)v;
 }
 
 //-----------------------------------------------------------------------------
-Vec3* LuaStack::get_vec3(int32_t index)
+Vec3& LuaStack::get_vec3(int32_t index)
 {
-	CE_ASSERT(lua_islightuserdata(m_state, index), "Not a lightuserdata object");
-
-	Vec3* v = (Vec3*)lua_touserdata(m_state, index);
+	void* v = lua_touserdata(m_state, index);
 
-	CE_ASSERT(v >= &vec3_buffer[0] && v <= &vec3_buffer[LUA_VEC3_BUFFER_SIZE-1], "Vec3 type error");
+	if (v < &vec3_buffer[0] || v > &vec3_buffer[LUA_VEC3_BUFFER_SIZE-1])
+	{
+		luaL_typerror(m_state, index, "Vec3");
+	}
 
-	return v;
+	return *(Vec3*)v;
 }
 
 //-----------------------------------------------------------------------------
-Mat4* LuaStack::get_mat4(int32_t index)
+Mat4& LuaStack::get_mat4(int32_t index)
 {
-	CE_ASSERT(lua_islightuserdata(m_state, index), "Not a lightuserdata object");
+	void* m = lua_touserdata(m_state, index);
 
-	Mat4* m = (Mat4*)lua_touserdata(m_state, index);
+	if (m < &mat4_buffer[0] || m > &mat4_buffer[LUA_MAT4_BUFFER_SIZE-1])
+	{
+		luaL_typerror(m_state, index, "Mat4");
+	}
 
-	CE_ASSERT(m >= &mat4_buffer[0] && m <= &mat4_buffer[LUA_MAT4_BUFFER_SIZE-1], "Mat4 type error");
-
-	return m;
+	return *(Mat4*)m;
 }
 
 //-----------------------------------------------------------------------------
-Quat* LuaStack::get_quat(int32_t index)
+Quat& LuaStack::get_quat(int32_t index)
 {
-	CE_ASSERT(lua_islightuserdata(m_state, index), "Not a lightuserdata object");
-
-	Quat* q = (Quat*)lua_touserdata(m_state, index);
+	void* q = lua_touserdata(m_state, index);
 
-	CE_ASSERT(q >= &quat_buffer[0] && q <= &quat_buffer[LUA_QUAT_BUFFER_SIZE-1], "Quat type error");
+	if (q < &quat_buffer[0] || q > &quat_buffer[LUA_QUAT_BUFFER_SIZE-1])
+	{
+		luaL_typerror(m_state, index, "Quat");
+	}
 
-	return q;
+	return *(Quat*)q;
 }
 
-} // namespace crown
+} // namespace crown

+ 15 - 18
engine/lua/LuaStack.h

@@ -37,12 +37,6 @@ class Vec3;
 class Mat4;
 class Quat;
 
-Vec2* next_vec2();
-Vec3* next_vec3();
-
-Mat4* next_mat4();
-Quat* next_quat();
-
 class LuaStack
 {
 public:
@@ -51,6 +45,11 @@ public:
 
 	lua_State*				state();
 
+	/// Returns the number of elements in the stack.
+	/// When called inside a function, it can be used to count
+	/// the number of arguments passed to the function itself.
+	int32_t					num_args();
+
 	void					push_bool(bool value);
 
 	void					push_int32(int32_t value);
@@ -63,17 +62,18 @@ public:
 
 	void 					push_float(float value);
 
-	void 					push_string(const char* str, size_t len);
+	void 					push_string(const char* s);
+	void					push_literal(const char* s, size_t len);
 
 	void					push_lightdata(void* data);
 
-	void					push_vec2(Vec2* v);
+	void					push_vec2(const Vec2& v);
 
-	void					push_vec3(Vec3* v);
+	void					push_vec3(const Vec3& v);
 
-	void					push_mat4(Mat4* m);
+	void					push_mat4(const Mat4& m);
 
-	void					push_quat(Quat* q);
+	void					push_quat(const Quat& q);
 
 	bool 					get_bool(int32_t index);
 
@@ -85,13 +85,10 @@ public:
 
 	void*					get_lightdata(int32_t index);
 
-	Vec2*					get_vec2(int32_t index);
-
-	Vec3*					get_vec3(int32_t index);
-
-	Mat4*					get_mat4(int32_t index);
-
-	Quat*					get_quat(int32_t index);
+	Vec2&					get_vec2(int32_t index);
+	Vec3&					get_vec3(int32_t index);
+	Mat4&					get_mat4(int32_t index);
+	Quat&					get_quat(int32_t index);
 
 private:
 

+ 7 - 8
engine/lua/LuaStringSetting.cpp

@@ -27,13 +27,12 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "StringSetting.h"
 #include "LuaStack.h"
 #include "LuaEnvironment.h"
-#include "StringUtils.h"
 
 namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t string_setting_value(lua_State* L)
+CE_EXPORT int string_setting_value(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -43,18 +42,18 @@ CE_EXPORT int32_t string_setting_value(lua_State* L)
 
 	if (setting != NULL)
 	{
-		stack.push_string(setting->value(), string::strlen(setting->value()));
+		stack.push_string(setting->value());
 	}
 	else
 	{
-		stack.push_string("", string::strlen(""));
+		stack.push_string("");
 	}
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t string_setting_synopsis(lua_State* L)
+CE_EXPORT int string_setting_synopsis(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -64,18 +63,18 @@ CE_EXPORT int32_t string_setting_synopsis(lua_State* L)
 
 	if (setting != NULL)
 	{
-		stack.push_string(setting->synopsis(), string::strlen(setting->synopsis()));
+		stack.push_string(setting->synopsis());
 	}
 	else
 	{
-		stack.push_string("", string::strlen(""));
+		stack.push_string("");
 	}
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t string_setting_update(lua_State* L)
+CE_EXPORT int string_setting_update(lua_State* L)
 {
 	LuaStack stack(L);
 

+ 80 - 100
engine/lua/LuaVec2.cpp

@@ -32,250 +32,230 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t vec2(lua_State* L)
+CE_EXPORT int vec2(lua_State* L)
 {
 	LuaStack stack(L);
 
 	float x = stack.get_float(1);
 	float y = stack.get_float(2);
 
-	Vec2* vec = next_vec2();
-
-	vec->x = x;
-	vec->y = y;
-
-	stack.push_vec2(vec);
+	stack.push_vec2(Vec2(x, y));
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t vec2_values(lua_State* L)
+CE_EXPORT int vec2_values(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Vec2* a = (Vec2*)stack.get_vec2(1);
-
-	float x = a->x;
-	float y = a->y;
+	Vec2& a = stack.get_vec2(1);
 
-	stack.push_float(x);
-	stack.push_float(y);
+	stack.push_float(a.x);
+	stack.push_float(a.y);
 
 	return 2;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t vec2_add(lua_State* L)
+CE_EXPORT int vec2_add(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Vec2* a = (Vec2*)stack.get_vec2(1);
-	Vec2* b = (Vec2*)stack.get_vec2(2);
+	Vec2& a = stack.get_vec2(1);
+	Vec2& b = stack.get_vec2(2);
 
-	*a += *b;
-
-	stack.push_vec2(a);
+	stack.push_vec2(a + b);
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t vec2_subtract(lua_State* L)
+CE_EXPORT int vec2_subtract(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Vec2* a = (Vec2*) stack.get_vec2(1);
-	Vec2* b = (Vec2*) stack.get_vec2(2);
-
-	*a -= *b;
+	Vec2& a = stack.get_vec2(1);
+	Vec2& b = stack.get_vec2(2);
 
-	stack.push_vec2(a);
+	stack.push_vec2(a - b);
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t vec2_multiply(lua_State* L)
+CE_EXPORT int vec2_multiply(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Vec2* a = (Vec2*) stack.get_vec2(1);
+	Vec2& a = stack.get_vec2(1);
 	float k = stack.get_float(2);
 
-	*a *= k;
-
-	stack.push_vec2(a);
+	stack.push_vec2(a * k);
 
 	return 1;
 }			
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t vec2_divide(lua_State* L)
+CE_EXPORT int vec2_divide(lua_State* L)
 {
 	LuaStack stack(L);
 	
-	Vec2* a = (Vec2*) stack.get_vec2(1);
+	Vec2& a = stack.get_vec2(1);
 	float k = stack.get_float(2);
 
-	*a /= k;
-
-	stack.push_vec2(a);
+	stack.push_vec2(a / k);
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t vec2_dot(lua_State* L)
+CE_EXPORT int vec2_dot(lua_State* L)
 {
 	LuaStack stack(L);
 	
-	Vec2* a = (Vec2*) stack.get_vec2(1);
-	Vec2* b = (Vec2*) stack.get_vec2(2);
+	Vec2& a = stack.get_vec2(1);
+	Vec2& b = stack.get_vec2(2);
 
-	stack.push_float(a->dot(*b));
+	stack.push_float(a.dot(b));
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t vec2_equals(lua_State* L)
+CE_EXPORT int vec2_equals(lua_State* L)
 {
 	LuaStack stack(L);
 	
-	Vec2* a = (Vec2*) stack.get_vec2(1);
-	Vec2* b = (Vec2*) stack.get_vec2(2);
+	Vec2& a = stack.get_vec2(1);
+	Vec2& b = stack.get_vec2(2);
 
-	stack.push_bool(*a == *b);
+	stack.push_bool(a == b);
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t vec2_lower(lua_State* L)
+CE_EXPORT int vec2_lower(lua_State* L)
 {
 	LuaStack stack(L);
 	
-	Vec2* a = (Vec2*) stack.get_vec2(1);
-	Vec2* b = (Vec2*) stack.get_vec2(2);
+	Vec2& a = stack.get_vec2(1);
+	Vec2& b = stack.get_vec2(2);
 
-	stack.push_bool(*a < *b);
+	stack.push_bool(a < b);
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t vec2_greater(lua_State* L)
+CE_EXPORT int vec2_greater(lua_State* L)
 {
 	LuaStack stack(L);
 	
-	Vec2* a = (Vec2*) stack.get_vec2(1);
-	Vec2* b = (Vec2*) stack.get_vec2(2);
+	Vec2& a = stack.get_vec2(1);
+	Vec2& b = stack.get_vec2(2);
 	
-	stack.push_bool(*a > *b);
+	stack.push_bool(a > b);
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t vec2_length(lua_State* L)
+CE_EXPORT int vec2_length(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Vec2* a = (Vec2*) stack.get_vec2(1);
+	Vec2& a = stack.get_vec2(1);
 
-	stack.push_float(a->length());
+	stack.push_float(a.length());
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t vec2_squared_length(lua_State* L)
+CE_EXPORT int vec2_squared_length(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Vec2* a = (Vec2*) stack.get_vec2(1);
+	Vec2& a = stack.get_vec2(1);
 
-	stack.push_float(a->squared_length());
+	stack.push_float(a.squared_length());
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t vec2_set_length(lua_State* L)
+CE_EXPORT int vec2_set_length(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Vec2* a = (Vec2*) stack.get_vec2(1);
+	Vec2& a = stack.get_vec2(1);
 	float len = stack.get_float(2);
 
-	a->set_length(len);
+	a.set_length(len);
 
 	return 0;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t vec2_normalize(lua_State* L)
+CE_EXPORT int vec2_normalize(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Vec2* a = (Vec2*) stack.get_vec2(1);
+	Vec2& a = stack.get_vec2(1);
 
-	a->normalize();
-
-	stack.push_vec2(a);
+	stack.push_vec2(a.normalize());
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t vec2_negate(lua_State* L)
+CE_EXPORT int vec2_negate(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Vec2* a = (Vec2*) stack.get_vec2(1);
-
-	a->negate();
+	Vec2& a = stack.get_vec2(1);
 
-	stack.push_vec2(a);
+	stack.push_vec2(a.negate());
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t vec2_get_distance_to(lua_State* L)
+CE_EXPORT int vec2_get_distance_to(lua_State* L)
 {
 	LuaStack stack(L);
 	
-	Vec2* a = (Vec2*) stack.get_vec2(1);
-	Vec2* b = (Vec2*) stack.get_vec2(2);
+	Vec2& a = stack.get_vec2(1);
+	Vec2& b = stack.get_vec2(2);
 
-	stack.push_float(a->get_distance_to(*b));
+	stack.push_float(a.get_distance_to(b));
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t vec2_get_angle_between(lua_State* L)
+CE_EXPORT int vec2_get_angle_between(lua_State* L)
 {
 	LuaStack stack(L);
 	
-	Vec2* a = (Vec2*) stack.get_vec2(1);
-	Vec2* b = (Vec2*) stack.get_vec2(2);
+	Vec2& a = stack.get_vec2(1);
+	Vec2& b = stack.get_vec2(2);
 
-	stack.push_float(a->get_angle_between(*b));
+	stack.push_float(a.get_angle_between(b));
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t vec2_zero(lua_State* L)
+CE_EXPORT int vec2_zero(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Vec2* a = (Vec2*) stack.get_vec2(1);
+	Vec2& a = stack.get_vec2(1);
 
-	a->zero();
+	a.zero();
 
 	return 0;
 }
@@ -283,24 +263,24 @@ CE_EXPORT int32_t vec2_zero(lua_State* L)
 //-----------------------------------------------------------------------------
 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);
+	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

+ 67 - 88
engine/lua/LuaVec3.cpp

@@ -32,7 +32,7 @@ namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t vec3(lua_State* L)
+CE_EXPORT int vec3(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -40,258 +40,237 @@ CE_EXPORT int32_t vec3(lua_State* L)
 	float y = stack.get_float(2);
 	float z = stack.get_float(3);
 
-	Vec3* vec = next_vec3();
-
-	vec->x = x;
-	vec->y = y;
-	vec->z = z;
-
-	stack.push_vec3(vec);
+	stack.push_vec3(Vec3(x, y, z));
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t vec3_values(lua_State* L)
+CE_EXPORT int vec3_values(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Vec3* a = stack.get_vec3(1);
+	Vec3& a = stack.get_vec3(1);
 
-	float x = a->x;
-	float y = a->y;
-	float z = a->z;
-
-	stack.push_float(x);
-	stack.push_float(y);
-	stack.push_float(z);
+	stack.push_float(a.x);
+	stack.push_float(a.y);
+	stack.push_float(a.z);
 
 	return 3;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t vec3_add(lua_State* L)
+CE_EXPORT int vec3_add(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Vec3* a = stack.get_vec3(1);
-	Vec3* b = stack.get_vec3(2);
-
-	*a += *b;
+	Vec3& a = stack.get_vec3(1);
+	Vec3& b = stack.get_vec3(2);
 
-	stack.push_vec3(a);
+	stack.push_vec3(a + b);
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t vec3_subtract(lua_State* L)
+CE_EXPORT int vec3_subtract(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Vec3* a = stack.get_vec3(1);
-	Vec3* b = stack.get_vec3(2);
+	Vec3& a = stack.get_vec3(1);
+	Vec3& b = stack.get_vec3(2);
 
-	*a -= *b;
-
-	stack.push_vec3(a);
+	stack.push_vec3(a - b);
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t vec3_multiply(lua_State* L)
+CE_EXPORT int vec3_multiply(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Vec3* a = stack.get_vec3(1);
+	Vec3& a = stack.get_vec3(1);
 	float b = stack.get_float(2);
 
-	*a *= b;
-
-	stack.push_vec3(a);
+	stack.push_vec3(a * b);
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t vec3_divide(lua_State* L)
+CE_EXPORT int vec3_divide(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Vec3* a = stack.get_vec3(1);
+	Vec3& a = stack.get_vec3(1);
 	float b = stack.get_float(2);
 
-	*a /= b;
-
-	stack.push_vec3(a);
+	stack.push_vec3(a / b);
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t vec3_dot(lua_State* L)
+CE_EXPORT int vec3_dot(lua_State* L)
 {
 	LuaStack stack(L);
 	
-	Vec3* a = stack.get_vec3(1);
-	Vec3* b = stack.get_vec3(2);
+	Vec3& a = stack.get_vec3(1);
+	Vec3& b = stack.get_vec3(2);
 
-	stack.push_float(a->dot(*b));
+	stack.push_float(a.dot(b));
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t vec3_cross(lua_State* L)
+CE_EXPORT int vec3_cross(lua_State* L)
 {
 	LuaStack stack(L);
 	
-	Vec3* a = stack.get_vec3(1);
-	Vec3* b = stack.get_vec3(2);
-
-	/// CHECK CHECK CHECK
-	*a = a->cross(*b);
+	Vec3& a = stack.get_vec3(1);
+	Vec3& b = stack.get_vec3(2);
 
-	stack.push_vec3(a);
+	stack.push_vec3(a.cross(b));
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t vec3_equals(lua_State* L)
+CE_EXPORT int vec3_equals(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Vec3* a = stack.get_vec3(1);
-	Vec3* b = stack.get_vec3(2);
+	Vec3& a = stack.get_vec3(1);
+	Vec3& b = stack.get_vec3(2);
 
-	stack.push_bool(*a == *b);
+	stack.push_bool(a == b);
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t vec3_lower(lua_State* L)
+CE_EXPORT int vec3_lower(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Vec3* a = stack.get_vec3(1);
-	Vec3* b = stack.get_vec3(2);
+	Vec3& a = stack.get_vec3(1);
+	Vec3& b = stack.get_vec3(2);
 
-	stack.push_bool(*a < *b);
+	stack.push_bool(a < b);
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t vec3_greater(lua_State* L)
+CE_EXPORT int vec3_greater(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Vec3* a = stack.get_vec3(1);
-	Vec3* b = stack.get_vec3(2);
+	Vec3& a = stack.get_vec3(1);
+	Vec3& b = stack.get_vec3(2);
 
-	stack.push_bool(*a > *b);
+	stack.push_bool(a > b);
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t vec3_length(lua_State* L)
+CE_EXPORT int vec3_length(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Vec3* a = stack.get_vec3(1);
+	Vec3& a = stack.get_vec3(1);
 
-	stack.push_float(a->length());
+	stack.push_float(a.length());
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t vec3_squared_length(lua_State* L)
+CE_EXPORT int vec3_squared_length(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Vec3* a = stack.get_vec3(1);
+	Vec3& a = stack.get_vec3(1);
 
-	stack.push_float(a->squared_length());
+	stack.push_float(a.squared_length());
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t vec3_set_length(lua_State* L)
+CE_EXPORT int vec3_set_length(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Vec3* a = stack.get_vec3(1);
+	Vec3& a = stack.get_vec3(1);
 	float len = stack.get_float(2);
 
-	a->set_length(len);
+	a.set_length(len);
 
 	return 0;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t vec3_normalize(lua_State* L)
+CE_EXPORT int vec3_normalize(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Vec3* a = stack.get_vec3(1);
+	Vec3& a = stack.get_vec3(1);
 
-	a->normalize();
+	a.normalize();
 
 	return 0;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t vec3_negate(lua_State* L)
+CE_EXPORT int vec3_negate(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Vec3* a = stack.get_vec3(1);
+	Vec3& a = stack.get_vec3(1);
 
-	a->negate();
+	a.negate();
 
 	return 0;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t vec3_get_distance_to(lua_State* L)
+CE_EXPORT int vec3_get_distance_to(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Vec3* a = stack.get_vec3(1);
-	Vec3* b = stack.get_vec3(2);
+	Vec3& a = stack.get_vec3(1);
+	Vec3& b = stack.get_vec3(2);
 
-	stack.push_float(a->get_distance_to(*b));
+	stack.push_float(a.get_distance_to(b));
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t vec3_get_angle_between(lua_State* L)
+CE_EXPORT int vec3_get_angle_between(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Vec3* a = stack.get_vec3(1);
-	Vec3* b = stack.get_vec3(2);
+	Vec3& a = stack.get_vec3(1);
+	Vec3& b = stack.get_vec3(2);
 
-	stack.push_float(a->get_angle_between(*b));
+	stack.push_float(a.get_angle_between(b));
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t vec3_zero(lua_State* L)
+CE_EXPORT int vec3_zero(lua_State* L)
 {
 	LuaStack stack(L);
 
-	Vec3* a = stack.get_vec3(1);
+	Vec3& a = stack.get_vec3(1);
 
-	a->zero();
+	a.zero();
 
 	return 0;
 }	

+ 13 - 14
engine/lua/LuaWindow.cpp

@@ -28,13 +28,12 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "OsWindow.h"
 #include "LuaStack.h"
 #include "LuaEnvironment.h"
-#include "StringUtils.h"
 
 namespace crown
 {
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t window_show(lua_State* L)
+CE_EXPORT int window_show(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -44,7 +43,7 @@ CE_EXPORT int32_t window_show(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t window_hide(lua_State* L)
+CE_EXPORT int window_hide(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -54,7 +53,7 @@ CE_EXPORT int32_t window_hide(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t window_get_size(lua_State* L)
+CE_EXPORT int window_get_size(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -69,7 +68,7 @@ CE_EXPORT int32_t window_get_size(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t window_get_position(lua_State* L)
+CE_EXPORT int window_get_position(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -84,7 +83,7 @@ CE_EXPORT int32_t window_get_position(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t window_resize(lua_State* L)
+CE_EXPORT int window_resize(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -97,7 +96,7 @@ CE_EXPORT int32_t window_resize(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t window_move(lua_State* L)
+CE_EXPORT int window_move(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -110,7 +109,7 @@ CE_EXPORT int32_t window_move(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t window_show_cursor(lua_State* L)
+CE_EXPORT int window_show_cursor(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -120,7 +119,7 @@ CE_EXPORT int32_t window_show_cursor(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t window_hide_cursor(lua_State* L)
+CE_EXPORT int window_hide_cursor(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -130,7 +129,7 @@ CE_EXPORT int32_t window_hide_cursor(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t window_get_cursor_xy(lua_State* L)
+CE_EXPORT int window_get_cursor_xy(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -145,7 +144,7 @@ CE_EXPORT int32_t window_get_cursor_xy(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t window_set_cursor_xy(lua_State* L)
+CE_EXPORT int window_set_cursor_xy(lua_State* L)
 {
 	LuaStack stack(L);
 
@@ -158,19 +157,19 @@ CE_EXPORT int32_t window_set_cursor_xy(lua_State* L)
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t window_title(lua_State* L)
+CE_EXPORT int window_title(lua_State* L)
 {
 	LuaStack stack(L);
 
 	const char* title = device()->window()->title();
 
-	stack.push_string(title, string::strlen(title));
+	stack.push_string(title);
 
 	return 1;
 }
 
 //-----------------------------------------------------------------------------
-CE_EXPORT int32_t window_set_title(lua_State* L)
+CE_EXPORT int window_set_title(lua_State* L)
 {
 	LuaStack stack(L);
 

+ 0 - 3
engine/os/posix/Cond.cpp

@@ -30,8 +30,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 namespace crown
 {
-namespace os
-{
 
 //-----------------------------------------------------------------------------
 Cond::Cond()
@@ -59,5 +57,4 @@ void Cond::wait(Mutex& mutex)
 	pthread_cond_wait(&m_cond, &(mutex.m_mutex));
 }
 
-} // namespace os
 } // namespace crown

+ 6 - 3
engine/os/posix/Cond.h

@@ -34,8 +34,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 namespace crown
 {
-namespace os
-{
 
 class Cond
 {
@@ -50,7 +48,12 @@ public:
 private:
 
 	pthread_cond_t	m_cond;
+
+private:
+
+	// Disable copying.
+					Cond(const Cond&);
+	Cond&			operator=(const Cond&);
 };
 
-} // namespace os
 } // namespace crown

+ 0 - 4
engine/os/posix/Mutex.cpp

@@ -30,8 +30,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 namespace crown
 {
-namespace os
-{
 
 //-----------------------------------------------------------------------------
 Mutex::Mutex()
@@ -59,6 +57,4 @@ void Mutex::unlock()
 	pthread_mutex_unlock(&m_mutex);
 }
 
-} // namespace os
 } // namespace crown
-

+ 6 - 3
engine/os/posix/Mutex.h

@@ -33,8 +33,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 namespace crown
 {
-namespace os
-{
 
 class Mutex
 {
@@ -50,8 +48,13 @@ private:
 
 	pthread_mutex_t		m_mutex;
 
+private:
+
+	// Disable copying.
+						Mutex(const Mutex&);
+	Mutex&				operator=(const Mutex&);
+
 	friend class		Cond;
 };
 
-} // namespace os
 } // namespace crown

+ 59 - 13
engine/os/posix/Thread.cpp

@@ -28,17 +28,36 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include <string.h>
 
 #include "Thread.h"
+#include "Assert.h"
 
 namespace crown
 {
-namespace os
-{
 
 //-----------------------------------------------------------------------------
-Thread::Thread(os::ThreadFunction f, void* params, const char* name) :
-	m_name(name)
+Thread::Thread(const char* name) :
+	m_name(name),
+	m_is_running(false),
+	m_is_terminating(false),
+	m_thread(0)
 {
 	memset(&m_thread, 0, sizeof(pthread_t));
+}
+
+//-----------------------------------------------------------------------------
+Thread::~Thread()
+{
+}
+
+//-----------------------------------------------------------------------------
+const char* Thread::name() const
+{
+	return m_name;
+}
+
+//-----------------------------------------------------------------------------
+void Thread::start()
+{
+	m_is_terminating = false;
 
 	// Make thread joinable
 	pthread_attr_t attr;
@@ -46,21 +65,49 @@ Thread::Thread(os::ThreadFunction f, void* params, const char* name) :
 	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
 
 	// Create thread
-	int rc = pthread_create(&m_thread, &attr, f, (void*)params);
-
-	if (rc != 0)
-	{
-		os::printf("Unable to create the thread '%s' Error code: %d\n", name, rc);
-		exit(-1);
-	}
+	int rc = pthread_create(&m_thread, &attr, Thread::background_proc, (void*) this);
+	CE_ASSERT(rc == 0, "Failed to create the thread '%s': errno: %d", m_name, rc);
 
 	// Free attr memory
 	pthread_attr_destroy(&attr);
+
+	m_is_running = true;
 }
 
 //-----------------------------------------------------------------------------
-Thread::~Thread()
+bool Thread::is_running() const
+{
+	return m_is_running;
+}
+
+//-----------------------------------------------------------------------------
+bool Thread::is_terminating() const
+{
+	return m_is_terminating;
+}
+
+//-----------------------------------------------------------------------------
+void Thread::stop()
+{
+	m_is_terminating = true;
+}
+
+//-----------------------------------------------------------------------------
+int32_t Thread::run()
 {
+	return 0;
+}
+
+//-----------------------------------------------------------------------------
+void* Thread::background_proc(void* thiz)
+{
+	Thread* thread = ((Thread*) thiz);
+
+	thread->run();
+
+	thread->m_is_running = false;
+
+	return NULL;
 }
 
 //-----------------------------------------------------------------------------
@@ -75,5 +122,4 @@ void Thread::detach()
 	pthread_detach(m_thread);
 }
 
-} // namespace os
 } // namespace crown

+ 48 - 7
engine/os/posix/Thread.h

@@ -29,12 +29,9 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include <pthread.h>
 
 #include "Types.h"
-#include "OS.h"
 
 namespace crown
 {
-namespace os
-{
 
 typedef void* (*ThreadFunction)(void*);
 
@@ -42,17 +39,61 @@ class Thread
 {
 public:
 
-					Thread(os::ThreadFunction f, void* params, const char* name);
-					~Thread();
+	/// Constructs the thread and gives it a @a name.
+	/// @note
+	/// The actual OS thread creation and execution is
+	/// deferred to the first call to Thread::start().
+					Thread(const char* name);
+
+	/// Does not stop the thread. The user must call
+	/// Thread::stop() to effectively stop the thread. 
+	virtual			~Thread();
+
+	/// Returns the name of the thread.
+	const char*		name() const;
 
 	void			join();
 	void			detach();
 
+	/// Returns whether the thread is currently running.
+	bool			is_running() const;
+
+	/// Returns whether the thread is being asked to stop running.
+	/// @note
+	/// The implementer tipically polls this function to
+	/// determine whether to stop the execution or not.
+	bool			is_terminating() const;
+
+	/// Starts the execution of the thread.
+	/// The function creates the OS thread and starts
+	/// its execution.
+	void			start();
+
+	/// Stops the execution of the thread if it is running.
+	/// The function releases the OS thread causing its
+	/// termination.
+	void			stop();
+
+	/// Executes in background when the thead is running.
+	/// The thread has to be started with Thread::start()
+	virtual int32_t	run();
+
+private:
+
+	static void*	background_proc(void* thiz);
+
 private:
 
-	pthread_t		m_thread;
 	const char*		m_name;
+	bool			m_is_running;
+	bool			m_is_terminating;
+	pthread_t		m_thread;
+
+private:
+
+	// Disable copying
+					Thread(const Thread&);
+	Thread&			operator=(const Thread&);
 };
 
-} // namespace os
 } // namespace crown

+ 4 - 4
engine/os/win/Cond.cpp

@@ -28,28 +28,28 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 namespace crown
 {
-namespace os
-{
 
+//-----------------------------------------------------------------------------
 Cond::Cond()
 {
 	InitializeConditionVariable(&m_cond);
 }
 
+//-----------------------------------------------------------------------------
 Cond::~Cond()
 {
-	
 }
 
+//-----------------------------------------------------------------------------
 void Cond::signal()
 {
 	WakeConditionVariable(&m_cond);
 }
 
+//-----------------------------------------------------------------------------
 void Cond::wait(Mutex& mutex)
 {
 	SleepConditionVariableCS(&m_cond, &mutex.m_cs, INFINITE);
 }
 
-} // namespace os
 } // namespace crown

+ 10 - 8
engine/os/win/Cond.h

@@ -33,23 +33,25 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 namespace crown
 {
-namespace os
-{
 
 class Cond
 {
 public:
 
-							Cond();
-							~Cond();
+						Cond();
+						~Cond();
+
+	void				signal();
+	void				wait(Mutex& mutex);
+
+private:
 
-	void					signal();
-	void					wait(Mutex& mutex);
+						Cond(const Cond&);
+	Cond&				operator=(const Cond&);
 
 private:
 
-	CONDITION_VARIABLE		m_cond;
+	CONDITION_VARIABLE	m_cond;
 };
 
-} // namespace os
 } // namespace crown

+ 0 - 3
engine/os/win/Mutex.cpp

@@ -29,8 +29,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 namespace crown
 {
-namespace os
-{
 
 //-----------------------------------------------------------------------------
 Mutex::Mutex()
@@ -56,5 +54,4 @@ void Mutex::unlock()
     LeaveCriticalSection(&m_cs);
 }
 
-} // namespace os
 } // namespace crown

+ 5 - 3
engine/os/win/Mutex.h

@@ -32,8 +32,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 namespace crown
 {
-namespace os
-{
 
 class Mutex
 {
@@ -49,8 +47,12 @@ private:
 
 	CRITICAL_SECTION	m_cs;
 
+private:
+
+						Mutex(const Mutex&);
+	Mutex&				operator=(const Mutex&);
+
 	friend class		Cond;
 };
 
-} // namespace os
 } // namespace crown

+ 1 - 1
engine/resource/FontResource.cpp

@@ -49,7 +49,7 @@ void FontResource::unload(Allocator& /*allocator*/, void* /*resource*/)
 }
 
 //-----------------------------------------------------------------------------
-void FontResource::offline()
+void FontResource::offline(void* /*resource*/)
 {
 }
 

+ 2 - 3
engine/resource/FontResource.h

@@ -27,7 +27,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 #pragma once
 
 #include "Types.h"
-#include "Glyph.h"
 #include "Resource.h"
 
 namespace crown
@@ -41,9 +40,9 @@ class FontResource
 public:
 
 	static void*		load(Allocator& allocator, Bundle& bundle, ResourceId id);
-	static void			online(void* resource);
 	static void			unload(Allocator& allocator, void* resource);
-	static void			offline();
+	static void			online(void* resource);
+	static void			offline(void* resource);
 
 public:
 

+ 1 - 1
engine/resource/MaterialResource.cpp

@@ -51,7 +51,7 @@ void MaterialResource::unload(Allocator& /*allocator*/, void* /*material*/)
 }
 
 //-----------------------------------------------------------------------------
-void MaterialResource::offline()
+void MaterialResource::offline(void* /*resource*/)
 {
 	// TODO
 }

+ 4 - 4
engine/resource/MaterialResource.h

@@ -51,10 +51,10 @@ class MaterialResource
 {
 public:
 
-	static void*	load(Allocator& allocator, Bundle& bundle, ResourceId id);
-	static void		online(void* resource);
-	static void		unload(Allocator& allocator, void* texture);
-	static void		offline();
+	static void*		load(Allocator& allocator, Bundle& bundle, ResourceId id);
+	static void			unload(Allocator& allocator, void* resource);
+	static void			online(void* resource);
+	static void			offline(void* resource);
 
 private:
 

+ 1 - 1
engine/resource/MeshResource.cpp

@@ -72,7 +72,7 @@ void MeshResource::unload(Allocator& , void* )
 }
 
 //-----------------------------------------------------------------------------
-void MeshResource::offline()
+void MeshResource::offline(void* /*resource*/)
 {
 
 }

+ 2 - 2
engine/resource/MeshResource.h

@@ -53,9 +53,9 @@ class MeshResource
 public:
 
 	static void*		load(Allocator& allocator, Bundle& bundle, ResourceId id);
-	static void			online(void* resource);
 	static void			unload(Allocator& allocator, void* resource);
-	static void			offline();
+	static void			online(void* resource);
+	static void			offline(void* resource);
 
 public:
 

+ 24 - 12
engine/resource/Resource.h

@@ -48,13 +48,12 @@ const uint32_t TEXT_TYPE					= 0x9000BF0B;
 const uint32_t MATERIAL_TYPE				= 0x46807A92;
 const uint32_t SOUND_TYPE					= 0x8E128AA1;
 
-
 /// Enumerates the loading states of a resource
 enum ResourceState
 {
-	RS_UNLOADED		= 0,		//< The resource is not loaded, so it cannot be used
-	RS_LOADING		= 1,		//< The resource loader started to load the resource but it is yet not ready to use
-	RS_LOADED		= 2			//< The resource loader finished to load the texture meaning it can be used
+	RS_UNLOADED		= 0,		// The resource is not loaded, so it cannot be used
+	RS_LOADING		= 1,		// The resource loader started to load the resource but it is yet not ready to use
+	RS_LOADED		= 2			// The resource loader finished to load the texture meaning it can be used
 };
 
 /// ResourceId uniquely identifies a resource by its name and type.
@@ -62,14 +61,27 @@ enum ResourceState
 /// the index to the resource list where it is stored.
 struct ResourceId
 {
-	uint32_t		name;		// Hashed resource name
-	uint32_t		type;		// Hashed resource type
-	uint32_t		index;		// Index into the ResourceManager internal list
-
-	bool			operator==(const ResourceId& b)
-	{
-		return name == b.name && type == b.type && index == b.index;
-	}
+	bool operator==(const ResourceId& b) const { return name == b.name && type == b.type; }
+
+	uint32_t		name;
+	uint32_t		type;
+};
+
+class Allocator;
+class Bundle;
+
+typedef void*	(*ResourceLoadCallback)(Allocator& a, Bundle& b, ResourceId id);
+typedef void	(*ResourceUnloadCallback)(Allocator& a, void* resource);
+typedef void	(*ResourceOnlineCallback)(void* resource);
+typedef void	(*ResourceOfflineCallback)(void* resource);
+
+struct ResourceCallback
+{
+	uint32_t					type;
+	ResourceLoadCallback		on_load;
+	ResourceUnloadCallback		on_unload;
+	ResourceOnlineCallback		on_online;
+	ResourceOfflineCallback		on_offline;
 };
 
 } // namespace crown

+ 102 - 0
engine/resource/ResourceLoader.cpp

@@ -0,0 +1,102 @@
+/*
+Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include "ResourceLoader.h"
+#include "ResourceRegistry.h"
+
+namespace crown
+{
+
+//-----------------------------------------------------------------------------
+ResourceLoader::ResourceLoader(Bundle& bundle, Allocator& resource_heap) :
+	Thread("resource-loader"),
+	m_bundle(bundle),
+	m_resource_heap(resource_heap),
+	m_load_queue(default_allocator()),
+	m_done_queue(default_allocator())
+{
+}
+
+//-----------------------------------------------------------------------------
+void ResourceLoader::load(ResourceId resource)
+{
+	m_load_mutex.lock();
+	m_load_queue.push_back(resource);
+	m_load_requests.signal();
+	m_load_mutex.unlock();
+}
+
+//-----------------------------------------------------------------------------
+uint32_t ResourceLoader::remaining() const
+{
+	return m_load_queue.size();
+}
+
+//-----------------------------------------------------------------------------
+uint32_t ResourceLoader::num_loaded() const
+{
+	return m_done_queue.size();
+}
+
+//-----------------------------------------------------------------------------
+void ResourceLoader::get_loaded(List<LoadedResource>& l)
+{
+	m_done_mutex.lock();
+	for (uint32_t i = 0; i < m_done_queue.size(); i++)
+	{
+		l.push_back(m_done_queue[i]);
+	}
+
+	m_done_queue.clear();
+	m_done_mutex.unlock();
+}
+
+//-----------------------------------------------------------------------------
+int32_t ResourceLoader::run()
+{
+	while (!is_terminating())
+	{
+		m_load_mutex.lock();
+		while (m_load_queue.size() == 0)
+		{
+			m_load_requests.wait(m_load_mutex);
+		}
+
+		ResourceId resource = m_load_queue.front();
+		m_load_queue.pop_front();
+		m_load_mutex.unlock();
+
+		void* data = resource_on_load(resource.type, m_resource_heap, m_bundle, resource);
+
+		m_done_mutex.lock();
+		m_done_queue.push_back(LoadedResource(resource, data));
+		m_done_mutex.unlock();
+	}
+
+	return 0;
+}
+
+} // namespace crown

+ 90 - 0
engine/resource/ResourceLoader.h

@@ -0,0 +1,90 @@
+/*
+Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#pragma once
+
+#include "Types.h"
+#include "Resource.h"
+#include "Thread.h"
+#include "Queue.h"
+#include "List.h"
+#include "Mutex.h"
+#include "Cond.h"
+
+namespace crown
+{
+
+struct LoadedResource
+{
+	LoadedResource(ResourceId r, void* d) : resource(r), data(d) {}
+
+	ResourceId	resource;
+	void*		data;
+};
+
+class Bundle;
+class Allocator;
+
+/// Loads resources in a background thread.
+class ResourceLoader : public Thread
+{
+public:
+
+	/// Reads the resources data from the given @a bundle using
+	/// @a resource_heap to allocate memory for them.
+				ResourceLoader(Bundle& bundle, Allocator& resource_heap);
+
+	/// Loads the @a resource in a background thread.
+	void		load(ResourceId resource);
+
+	/// Returns the number of resources still in the loading queue.
+	uint32_t	remaining() const;
+
+	/// Returns the number of resources already loaded.
+	uint32_t	num_loaded() const;
+
+	/// Returns a list of the last loaded resources.
+	void		get_loaded(List<LoadedResource>& l);
+
+	// Loads resources in the loading queue.
+	int32_t		run();
+
+private:
+
+	// Whether to look for resources
+	Bundle&					m_bundle;
+
+	// Used to strore resource memory
+	Allocator&				m_resource_heap;
+
+	Queue<ResourceId>		m_load_queue;
+	List<LoadedResource>	m_done_queue;
+	Mutex					m_load_mutex;
+	Mutex					m_done_mutex;
+	Cond					m_load_requests;
+};
+
+} // namespace crown

+ 56 - 260
engine/resource/ResourceManager.cpp

@@ -25,143 +25,71 @@ OTHER DEALINGS IN THE SOFTWARE.
 */
 
 #include <algorithm>
-#include <cstdio>
 
 #include "Types.h"
 #include "ResourceManager.h"
+#include "ResourceRegistry.h"
 #include "StringUtils.h"
 #include "Hash.h"
-#include "Path.h"
-#include "Log.h"
-#include "Device.h"
-#include "Filesystem.h"
-#include "TextReader.h"
-#include "DiskFile.h"
-#include "TextureResource.h"
-#include "MeshResource.h"
-#include "SoundResource.h"
+#include "TempAllocator.h"
 
 namespace crown
 {
 
 //-----------------------------------------------------------------------------
-ResourceManager::ResourceManager(Bundle& bundle) :
-	m_resource_bundle(bundle),
-	m_resources(m_allocator),
-	m_loading_queue(m_allocator),
-	m_loaded_queue(m_allocator),
-	m_seed(0),
-	m_background_thread_should_run(true),
-	m_thread(ResourceManager::background_thread, (void*)this, "resource-loader-thread")
+ResourceManager::ResourceManager(Bundle& bundle, uint32_t seed) :
+	m_resource_heap("resource", default_allocator()),
+	m_loader(bundle, m_resource_heap),
+	m_seed(seed),
+	m_resources(default_allocator())
 {
-	DiskFile* seed_file = device()->filesystem()->open("seed.ini", FOM_READ);
-	TextReader reader(*seed_file);
-
-	char tmp_buf[32];
-	reader.read_string(tmp_buf, 32);
-
-	device()->filesystem()->close(seed_file);
-
-	sscanf(tmp_buf, "%u", &m_seed);
+	m_loader.start();
 }
 
 //-----------------------------------------------------------------------------
 ResourceManager::~ResourceManager()
 {
-	m_background_thread_should_run = false;
+	m_loader.stop();
 }
 
 //-----------------------------------------------------------------------------
-ResourceId ResourceManager::load(const char* name)
+ResourceId ResourceManager::load(const char* type, const char* name)
 {
-	char basename[512];
-	char extension[512];
-
-	path::filename_without_extension(name, basename, 512);
-	path::extension(name, extension, 512);
-
-	uint32_t name_hash = hash::murmur2_32(basename, string::strlen(basename), m_seed);
-	uint32_t type_hash = hash::murmur2_32(extension, string::strlen(extension), 0);
-
-	return load(name_hash, type_hash);
+	return load(resource_id(type, name));
 }
 
 //-----------------------------------------------------------------------------
 void ResourceManager::unload(ResourceId name)
 {
 	CE_ASSERT(has(name), "Resource not loaded: %.8X%.8X", name.name, name.type);
-	
-	m_resources_mutex.lock();
 
-	ResourceEntry& entry = m_resources[name.index];
+	ResourceEntry* entry = find(name);
 	
-	entry.references--;
+	entry->references--;
 	
-	if (entry.references == 0 && entry.state == RS_LOADED)
+	if (entry->references == 0 && entry->state == RS_LOADED)
 	{
-		unload_by_type(name, entry.resource);
-
-		entry.state = RS_UNLOADED;
-		entry.resource = NULL;
-
+		resource_on_unload(name.type, m_resource_heap, entry->resource);
 
+		entry->state = RS_UNLOADED;
+		entry->resource = NULL;
 	}
-
-	m_resources_mutex.unlock();
-}
-
-//-----------------------------------------------------------------------------
-void ResourceManager::reload(ResourceId name)
-{
-	CE_ASSERT(has(name), "Resource not loaded: %.8X%.8X", name.name, name.type);
-	
-	m_resources_mutex.lock();
-
-	ResourceEntry& entry = m_resources[name.index];
-	
-	if (entry.state == RS_LOADED)
-	{
-		unload_by_type(name, entry.resource);
-
-		entry.state = RS_UNLOADED;
-		entry.resource = NULL;
-
-		entry.resource = load_by_type(name);
-		entry.state = RS_LOADED;
-	}
-
-	m_resources_mutex.unlock();
 }
 
 //-----------------------------------------------------------------------------
 bool ResourceManager::has(ResourceId name) const
 {
-	bool has_resource = false;
-
-	m_resources_mutex.lock();
+	ResourceEntry* entry = find(name);
 
-	if (m_resources.size() > name.index)
-	{
-		 has_resource = (m_resources[name.index].id.name == name.name);
-	}
-
-	m_resources_mutex.unlock();
-	
-	return has_resource;
+	return entry != NULL;
 }
 
 //-----------------------------------------------------------------------------
 const void* ResourceManager::data(ResourceId name) const
 {
 	CE_ASSERT(has(name), "Resource not loaded: %.8X%.8X", name.name, name.type);
-	
-	m_resources_mutex.lock();
 
-	void* resource = m_resources[name.index].resource;
-
-	m_resources_mutex.unlock();
-
-	return resource;
+	return find(name)->resource;
 }
 
 //-----------------------------------------------------------------------------
@@ -169,13 +97,7 @@ bool ResourceManager::is_loaded(ResourceId name) const
 {
 	CE_ASSERT(has(name), "Resource not loaded: %.8X%.8X", name.name, name.type);
 
-	m_resources_mutex.lock();
-
-	bool loaded = m_resources[name.index].state == RS_LOADED;
-
-	m_resources_mutex.unlock();
-
-	return loaded;
+	return find(name)->state == RS_LOADED;
 }
 
 //-----------------------------------------------------------------------------
@@ -183,50 +105,15 @@ uint32_t ResourceManager::references(ResourceId name) const
 {
 	CE_ASSERT(has(name), "Resource not loaded: %.8X%.8X", name.name, name.type);
 
-	m_resources_mutex.lock();
-
-	bool loaded = m_resources[name.index].references;
-
-	m_resources_mutex.unlock();
-
-	return loaded;
-}
-
-//-----------------------------------------------------------------------------
-uint32_t ResourceManager::remaining() const
-{
-	uint32_t count = 0;
-
-	m_loading_mutex.lock();
-
-	count = m_loading_queue.size();
-
-	m_loading_mutex.unlock();
-
-	return count;
+	return find(name)->references;
 }
 
 //-----------------------------------------------------------------------------
 void ResourceManager::flush()
 {
-	check_load_queue();
-
-	while (true)
-	{
-		// Wait for all the resources to be loaded
-		// by the background thread
-		m_loading_mutex.lock();
-		while (m_loading_queue.size() > 0)
-		{
-			m_all_loaded.wait(m_loading_mutex);
-		}
-		m_loading_mutex.unlock();
-
-		// When all loaded, bring them online
-		bring_loaded_online();
+	while (m_loader.remaining() > 0) ;
 
-		return;
-	}
+	poll_resource_loader();
 }
 
 //-----------------------------------------------------------------------------
@@ -236,63 +123,61 @@ uint32_t ResourceManager::seed() const
 }
 
 //-----------------------------------------------------------------------------
-void ResourceManager::check_load_queue()
+ResourceId ResourceManager::resource_id(const char* type, const char* name) const
 {
-	m_loading_mutex.lock();
-
-	if (m_loading_queue.size() > 0)
-	{
-		m_loading_requests.signal();
-	}
+	ResourceId id;
+	id.type = hash::murmur2_32(type, string::strlen(type), 0);
+	id.name = hash::murmur2_32(name, string::strlen(name), m_seed);
 
-	m_loading_mutex.unlock();
+	return id;
 }
 
 //-----------------------------------------------------------------------------
-void ResourceManager::bring_loaded_online()
+ResourceEntry* ResourceManager::find(ResourceId id) const
 {
-	m_loaded_mutex.lock();
+	const ResourceEntry* entry = std::find(m_resources.begin(), m_resources.end(), id);
+
+	return entry != m_resources.end() ? const_cast<ResourceEntry*>(entry) : NULL;
+}
 
-	while (m_loaded_queue.size() > 0)
+//-----------------------------------------------------------------------------
+void ResourceManager::poll_resource_loader()
+{
+	if (m_loader.num_loaded() != 0)
 	{
-		LoadedResource lr = m_loaded_queue.front();
-		m_loaded_queue.pop_front();
+		TempAllocator1024 alloc;
+		List<LoadedResource> loaded(alloc);
+		m_loader.get_loaded(loaded);
 
-		online(lr.resource, lr.data);
+		for (uint32_t i = 0; i < loaded.size(); i++)
+		{
+			online(loaded[i].resource, loaded[i].data);
+		}
 	}
-
-	m_loaded_mutex.unlock();
 }
 
 //-----------------------------------------------------------------------------
-ResourceId ResourceManager::load(uint32_t name, uint32_t type)
+ResourceId ResourceManager::load(ResourceId name)
 {
-	ResourceId id;
-	id.name = name;
-	id.type = type;
-
 	// Search for an already existent resource
-	ResourceEntry* entry = std::find(m_resources.begin(), m_resources.end(), id);
+	ResourceEntry* entry = find(name);
 
 	// If resource not found, create a new one
-	if (entry == m_resources.end())
+	if (entry == NULL)
 	{
-		id.index = m_resources.size();
-
 		ResourceEntry entry;
 
-		entry.id = id;
+		entry.id = name;
 		entry.state = RS_UNLOADED;
 		entry.references = 1;
 		entry.resource = NULL;
 
 		m_resources.push_back(entry);
 
-		m_loading_mutex.lock();
-		m_loading_queue.push_back(id);
-		m_loading_mutex.unlock();
+		// Issue request to resource loader
+		m_loader.load(name);
 
-		return id;
+		return name;
 	}
 
 	// Else, increment its reference count
@@ -301,103 +186,14 @@ ResourceId ResourceManager::load(uint32_t name, uint32_t type)
 	return entry->id;
 }
 
-//-----------------------------------------------------------------------------
-void ResourceManager::background_load()
-{
-	while (m_background_thread_should_run)
-	{
-		m_loading_mutex.lock();
-		while (m_loading_queue.size() == 0)
-		{
-			m_loading_requests.wait(m_loading_mutex);
-		}
-
-		ResourceId resource = m_loading_queue.front();
-		m_loading_queue.pop_front();
-
-		m_loading_mutex.unlock();
-
-		void* data = load_by_type(resource);
-
-		LoadedResource lr;
-		lr.resource = resource;
-		lr.data = data;
-
-		m_loaded_mutex.lock();
-		m_loaded_queue.push_back(lr);
-		m_loaded_mutex.unlock();
-
-		m_loading_mutex.lock();
-		if (m_loading_queue.size() == 0)
-		{
-			m_all_loaded.signal();
-		}
-		m_loading_mutex.unlock();
-	}
-}
-
-//-----------------------------------------------------------------------------
-void* ResourceManager::load_by_type(ResourceId name)
-{
-	switch (name.type)
-	{
-		case TEXTURE_TYPE: return TextureResource::load(m_resource_allocator, m_resource_bundle, name);
-		case MESH_TYPE: return MeshResource::load(m_resource_allocator, m_resource_bundle, name);
-		case SOUND_TYPE: return SoundResource::load(m_resource_allocator, m_resource_bundle, name);
-		default: return NULL;
-	}
-}
-
-//-----------------------------------------------------------------------------
-void ResourceManager::unload_by_type(ResourceId name, void* resource)
-{
-	switch (name.type)
-	{
-		case TEXTURE_TYPE: return TextureResource::unload(m_resource_allocator, resource);
-		case MESH_TYPE: return MeshResource::unload(m_resource_allocator, resource);
-		case SOUND_TYPE: return SoundResource::unload(m_resource_allocator, resource);
-	}
-
-	return;
-}
-
 //-----------------------------------------------------------------------------
 void ResourceManager::online(ResourceId name, void* resource)
 {
-	switch (name.type)
-	{
-		case TEXTURE_TYPE:
-		{
-			TextureResource::online(resource);
-			break;
-		}
-		case MESH_TYPE:
-		{
-			MeshResource::online(resource);
-			break;
-		}
-		case SOUND_TYPE:
-		{
-			SoundResource::online(resource);
-			break;
-		}
-	}
-
-	m_resources_mutex.lock();
-
-	ResourceEntry& entry = m_resources[name.index];
-	entry.resource = resource;
-	entry.state = RS_LOADED;
-
-	m_resources_mutex.unlock();
-}
-
-//-----------------------------------------------------------------------------
-void* ResourceManager::background_thread(void* thiz)
-{
-	((ResourceManager*)thiz)->background_load();
+	resource_on_online(name.type, resource);
 
-	return NULL;
+	ResourceEntry* entry = find(name);
+	entry->resource = resource;
+	entry->state = RS_LOADED;
 }
 
 } // namespace crown

+ 29 - 90
engine/resource/ResourceManager.h

@@ -28,96 +28,63 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #include "Types.h"
 #include "List.h"
-#include "Queue.h"
 #include "Resource.h"
-#include "HeapAllocator.h"
-#include "Thread.h"
-#include "Mutex.h"
-#include "Cond.h"
+#include "ProxyAllocator.h"
+#include "ResourceLoader.h"
 
 namespace crown
 {
 
 struct ResourceEntry
 {
+	bool operator==(const ResourceId& resource) const { return id == resource; }
+	bool operator==(const ResourceEntry& b) const { return id == b.id; }
+
 	ResourceId		id;
 	ResourceState	state;
-
 	uint32_t		references;
-
 	void*			resource;
-
-	bool			operator==(const ResourceId& resource)
-					{
-						return id == resource;
-					}
-
-	bool			operator==(const ResourceEntry& b)
-					{
-						return id == b.id;
-					}
-};
-
-struct LoadedResource
-{
-	ResourceId	resource;
-	void*		data;
 };
 
 class Bundle;
 
-/// Resource manager.
+/// Keeps track and manages resources loaded by ResourceLoader.
 class ResourceManager
 {
 public:
 
-	/// Read resources from @a bundle and store resource data using @a allocator.
-							ResourceManager(Bundle& bundle);
+	/// The resources will be loaded from @a bundle.
+							ResourceManager(Bundle& bundle, uint32_t seed);
 							~ResourceManager();
 
-	/// Loads the resource by @a name and returns its ResourceId.
+	/// Loads the resource by @a type and @a name and returns its ResourceId.
 	/// @note
-	/// The resource data may be not immediately available,
-	/// the resource gets pushed in a queue of load requests and loadead as
-	/// soon as possible by the ResourceLoader.
-	/// You have to explicitly call is_loaded() method to check if the
-	/// loading process is actually completed.
-	ResourceId				load(const char* name);
-
-	/// Unloads the @a resource, freeing up all the memory associated by it
+	/// You have to call is_loaded() to check if the loading process is actually completed.
+	ResourceId				load(const char* type, const char* name);
+
+	/// Unloads the resource @a name, freeing up all the memory associated by it
 	/// and eventually any global object associated with it.
-	/// (Such as texture objects, vertex buffers etc.)
 	void					unload(ResourceId name);
 
-	/// Reloads the @a resource
-	void					reload(ResourceId name);
-
 	/// Returns whether the manager has the @a name resource into
 	/// its list of resources.
-	/// @note
+	/// @warning
 	/// Having a resource does not mean that the resource is
-	/// available for using; instead, you have to check is_loaded() to
-	/// obtain the resource availability status.
+	/// ready to be used; See is_loaded().
 	bool					has(ResourceId name) const;
 
 	/// Returns the data associated with the @a name resource.
-	/// The resource data contains resource-specific metadata
-	/// and the actual resource data. In order to correctly use
-	/// it, you have to know which type of data @a name refers to
-	/// and cast accordingly.
+	/// You will have to cast the returned pointer accordingly.
 	const void*				data(ResourceId name) const;
 	
 	/// Returns whether the @a name resource is loaded (i.e. whether
 	/// you can use the data associated with it).
 	bool					is_loaded(ResourceId name) const;
 
-	/// Returns the number of references to the @a resource
+	/// Returns the number of references to the resource @a name;
 	uint32_t				references(ResourceId name) const;
 
-	/// Returns the number of resources still waiting to load.
-	uint32_t				remaining() const;
-
-	/// Forces all the loading requests to complete before preceeding.
+	/// Forces all of the loading requests to complete before preceeding.
 	void					flush();
 
 	/// Returns the seed used to generate resource name hashes.
@@ -125,54 +92,26 @@ public:
 
 private:
 
-	// Checks the load queue and signal the backgroud about pending
-	// requests. It is normally called only by the Device.
-	void					check_load_queue();
-	// Calls online() on loaded resources. Must be called only
-	// in the main thread and generally only by Device.
-	void					bring_loaded_online();
+	ResourceId				resource_id(const char* type, const char* name) const;
 
-	// Loads the resource by name and type and returns its ResourceId.
-	ResourceId				load(uint32_t name, uint32_t type);
+	// Returns the entry of the given id.
+	ResourceEntry*			find(ResourceId id) const;
 
-	void					background_load();
+	// Polls the resource loader for loaded resources.
+	void					poll_resource_loader();
 
-	void*					load_by_type(ResourceId name);
-	void					unload_by_type(ResourceId name, void* resource);
+	// Loads the resource by name and type and returns its ResourceId.
+	ResourceId				load(ResourceId name);
 	void					online(ResourceId name, void* resource);
 
 private:
 
-	static void*			background_thread(void* thiz);
-
-private:
-
-	// Archive whether to look for resources
-	Bundle&					m_resource_bundle;
-	// Used to strore resource memory
-	HeapAllocator			m_resource_allocator;
-
-	HeapAllocator			m_allocator;
-	// The master lookup table
-	List<ResourceEntry>		m_resources;
-
-	// Resources waiting for loading
-	Queue<ResourceId>		m_loading_queue;
-	// Resources already loaded, ready to bring online
-	Queue<LoadedResource>	m_loaded_queue;
-
+	ProxyAllocator			m_resource_heap;
+	ResourceLoader			m_loader;
 	uint32_t				m_seed;
+	List<ResourceEntry>		m_resources;
 
-	// Background loading thread
-	bool 					m_background_thread_should_run;
-	os::Thread				m_thread;
-
-	mutable os::Mutex		m_loading_mutex;
-	os::Cond 				m_loading_requests;
-	os::Cond 				m_all_loaded;
-
-	os::Mutex				m_loaded_mutex;
-	mutable os::Mutex		m_resources_mutex;
+private:
 
 	friend class			Device;
 };

+ 101 - 0
engine/resource/ResourceRegistry.cpp

@@ -0,0 +1,101 @@
+/*
+Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include "ResourceRegistry.h"
+#include "TextureResource.h"
+#include "MeshResource.h"
+#include "SoundResource.h"
+
+namespace crown
+{
+
+static const ResourceCallback RESOURCE_CALLBACK_REGISTRY[] =
+{
+	{ TEXTURE_TYPE, TextureResource::load, TextureResource::unload, TextureResource::online, TextureResource::offline },
+	{ MESH_TYPE, MeshResource::load, MeshResource::unload, MeshResource::online, MeshResource::offline },
+	{ SOUND_TYPE, SoundResource::load, SoundResource::unload, SoundResource::online, SoundResource::offline },
+	{ 0, NULL, NULL, NULL, NULL }
+};
+
+//-----------------------------------------------------------------------------
+static const ResourceCallback* find_callback(uint32_t type)
+{
+	const ResourceCallback* c = RESOURCE_CALLBACK_REGISTRY;
+
+	while (c->type != 0)
+	{
+		if (c->type == type)
+		{
+			return c;
+		}
+
+		c++;
+	}
+
+	return NULL;
+}
+
+//-----------------------------------------------------------------------------
+void* resource_on_load(uint32_t type, Allocator& allocator, Bundle& bundle, ResourceId id)
+{
+	const ResourceCallback* c = find_callback(type);
+
+	CE_ASSERT_NOT_NULL(c);
+
+	return c->on_load(allocator, bundle, id);
+}
+
+//-----------------------------------------------------------------------------
+void resource_on_unload(uint32_t type, Allocator& allocator, void* resource)
+{
+	const ResourceCallback* c = find_callback(type);
+
+	CE_ASSERT_NOT_NULL(c);
+
+	return c->on_unload(allocator, resource);
+}
+
+//-----------------------------------------------------------------------------
+void resource_on_online(uint32_t type, void* resource)
+{
+	const ResourceCallback* c = find_callback(type);
+
+	CE_ASSERT_NOT_NULL(c);
+
+	return c->on_online(resource);
+}
+
+//-----------------------------------------------------------------------------
+void resource_on_offline(uint32_t type, void* resource)
+{
+	const ResourceCallback* c = find_callback(type);
+
+	CE_ASSERT_NOT_NULL(c);
+
+	return c->on_offline(resource);
+}
+
+} // namespace crown

+ 8 - 18
engine/core/math/Shape.cpp → engine/resource/ResourceRegistry.h

@@ -24,26 +24,16 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 */
 
-#include "Shape.h"
+#pragma once
 
-namespace crown
-{
-
-//-----------------------------------------------------------------------------
-Shape::Shape(ShapeType type):
-	m_type(type)
-{
-}
+#include "Resource.h"
 
-//-----------------------------------------------------------------------------
-Shape::~Shape()
+namespace crown
 {
-}
 
-//-----------------------------------------------------------------------------
-ShapeType Shape::type()
-{
-	return m_type;
-}
+void* resource_on_load(uint32_t type, Allocator& allocator, Bundle& bundle, ResourceId id);
+void resource_on_online(uint32_t type, void* resource);
+void resource_on_offline(uint32_t type, void* resource);
+void resource_on_unload(uint32_t type, Allocator& allocator, void* resource);
 
-}
+} // namespace crown

+ 1 - 1
engine/resource/SoundResource.cpp

@@ -73,7 +73,7 @@ void SoundResource::unload(Allocator& allocator, void* resource)
 }
 
 //-----------------------------------------------------------------------------
-void SoundResource::offline()
+void SoundResource::offline(void* /*resource*/)
 {
 
 }

+ 2 - 2
engine/resource/SoundResource.h

@@ -50,9 +50,9 @@ class SoundResource
 public:
 
 	static void*		load(Allocator& allocator, Bundle& bundle, ResourceId id);
-	static void			online(void* resource);
 	static void			unload(Allocator& allocator, void* resource);
-	static void			offline();
+	static void			online(void* resource);
+	static void			offline(void* resource);
 
 public:
 

+ 1 - 1
engine/resource/TextureResource.cpp

@@ -74,7 +74,7 @@ void TextureResource::unload(Allocator& allocator, void* resource)
 }
 
 //-----------------------------------------------------------------------------
-void TextureResource::offline()
+void TextureResource::offline(void* /*resource*/)
 {
 
 }

+ 2 - 2
engine/resource/TextureResource.h

@@ -53,9 +53,9 @@ class TextureResource
 public:
 
 	static void*		load(Allocator& allocator, Bundle& bundle, ResourceId id);
-	static void			online(void* resource);
 	static void			unload(Allocator& allocator, void* resource);
-	static void			offline();
+	static void			online(void* resource);
+	static void			offline(void* resource);
 
 public:
 

+ 0 - 2
tests/CMakeLists.txt → engine/tests/CMakeLists.txt

@@ -2,8 +2,6 @@ cmake_minimum_required(VERSION 2.8)
 
 project(crown-tests)
 
-link_directories(${CROWN_BINARY_DIR})
-
 enable_testing()
 
 add_executable(allocators allocators.cpp)

+ 0 - 0
tests/allocators.cpp → engine/tests/allocators.cpp


+ 1 - 1
tests/compressors.cpp → engine/tests/compressors.cpp

@@ -5,7 +5,7 @@
 
 using namespace crown;
 
-int main(int argc, char** argv)
+int main()
 {
 	
 	HeapAllocator allocator;

+ 0 - 0
tests/containers.cpp → engine/tests/containers.cpp


+ 0 - 0
tests/messages.cpp → engine/tests/messages.cpp


+ 0 - 0
tests/paths.cpp → engine/tests/paths.cpp


+ 0 - 0
tests/strings.cpp → engine/tests/strings.cpp


+ 21 - 19
samples/CMakeLists.txt

@@ -4,21 +4,23 @@ project(crown-samples)
 
 # platform independent includes
 set (CROWN_INCLUDES
-	${CMAKE_SOURCE_DIR}/src
-	${CMAKE_SOURCE_DIR}/src/core
-	${CMAKE_SOURCE_DIR}/src/core/bv
-	${CMAKE_SOURCE_DIR}/src/core/containers
-	${CMAKE_SOURCE_DIR}/src/core/math
-	${CMAKE_SOURCE_DIR}/src/core/mem
-	${CMAKE_SOURCE_DIR}/src/core/compressors
-	${CMAKE_SOURCE_DIR}/src/core/filesystem
-	${CMAKE_SOURCE_DIR}/src/core/strings
-	${CMAKE_SOURCE_DIR}/src/core/settings
-	${CMAKE_SOURCE_DIR}/src/os
-	${CMAKE_SOURCE_DIR}/src/input
-	${CMAKE_SOURCE_DIR}/src/renderers
-	${CMAKE_SOURCE_DIR}/src/network
-	${CMAKE_SOURCE_DIR}/src/lua
+	${CMAKE_SOURCE_DIR}/engine
+	${CMAKE_SOURCE_DIR}/engine/core
+	${CMAKE_SOURCE_DIR}/engine/core/bv
+	${CMAKE_SOURCE_DIR}/engine/core/containers
+	${CMAKE_SOURCE_DIR}/engine/core/math
+	${CMAKE_SOURCE_DIR}/engine/core/mem
+	${CMAKE_SOURCE_DIR}/engine/core/compressors
+	${CMAKE_SOURCE_DIR}/engine/core/filesystem
+	${CMAKE_SOURCE_DIR}/engine/core/json
+	${CMAKE_SOURCE_DIR}/engine/core/strings
+	${CMAKE_SOURCE_DIR}/engine/core/settings
+	${CMAKE_SOURCE_DIR}/engine/os
+	${CMAKE_SOURCE_DIR}/engine/input
+	${CMAKE_SOURCE_DIR}/engine/renderers
+	${CMAKE_SOURCE_DIR}/engine/resource
+	${CMAKE_SOURCE_DIR}/engine/network
+	${CMAKE_SOURCE_DIR}/engine/lua
 )
 
 # detect operating system
@@ -26,8 +28,8 @@ if (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
 	set (LINUX 1)
 
 	list (APPEND CROWN_INCLUDES
-		${CMAKE_SOURCE_DIR}/src/os/linux
-		${CMAKE_SOURCE_DIR}/src/renderers/glx
+		${CMAKE_SOURCE_DIR}/engine/os/linux
+		${CMAKE_SOURCE_DIR}/engine/renderers/glx
 	)
 endif (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
 
@@ -35,8 +37,8 @@ if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
 	set(WINDOWS 1)
 
 	list (APPEND CROWN_INCLUDES
-		${CMAKE_SOURCE_DIR}/src/os/win
-		${CMAKE_SOURCE_DIR}/src/renderers/gl/wgl
+		${CMAKE_SOURCE_DIR}/engine/os/win
+		${CMAKE_SOURCE_DIR}/engine/renderers/gl/wgl
 	)
 endif (${CMAKE_SYSTEM_NAME} MATCHES "Windows")