Kaynağa Gözat

Refactor Device to support resource compilation and to work with Android ApkFilesystem

Daniele Bartolini 12 yıl önce
ebeveyn
işleme
f4219971e1
2 değiştirilmiş dosya ile 138 ekleme ve 220 silme
  1. 131 188
      engine/Device.cpp
  2. 7 32
      engine/Device.h

+ 131 - 188
engine/Device.cpp

@@ -29,42 +29,45 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #include "Config.h"
 #include "Device.h"
-#include "Filesystem.h"
+#include "Accelerometer.h"
+#include "Args.h"
+#include "ConsoleServer.h"
+#include "DebugRenderer.h"
+#include "DiskFile.h"
+#include "DiskFilesystem.h"
 #include "InputManager.h"
+#include "JSONParser.h"
+#include "Keyboard.h"
 #include "Log.h"
+#include "LuaEnvironment.h"
+#include "Memory.h"
+#include "Mouse.h"
 #include "OS.h"
+#include "OsWindow.h"
 #include "Renderer.h"
-#include "DebugRenderer.h"
-#include "Types.h"
-#include "StringUtils.h"
-#include "Args.h"
-#include "ArchiveBundle.h"
-#include "FileBundle.h"
 #include "ResourceManager.h"
-#include "TextureResource.h"
-#include "Keyboard.h"
-#include "Mouse.h"
-#include "Touch.h"
-#include "Accelerometer.h"
-#include "OsWindow.h"
-#include "JSONParser.h"
-#include "DiskFile.h"
-#include "Memory.h"
-#include "LuaEnvironment.h"
-#include "ConsoleServer.h"
-#include "TextReader.h"
-#include "SoundResource.h"
 #include "StringSetting.h"
+#include "StringUtils.h"
+#include "TextReader.h"
+#include "Touch.h"
+#include "Types.h"
+#include "Bundle.h"
 
-namespace crown
-{
+#undef LINUX
+#undef WINDOWS
+#define ANDROID
+
+#if defined(LINUX) || defined(WINDOWS)
+	#include "BundleCompiler.h"
+#endif
 
-#ifdef ANDROID
-	StringSetting g_default_mountpoint("default_mountpoint", "define default mount point for filesystem", "android");
-#else
-	StringSetting g_default_mountpoint("default_mountpoint", "define default mount point for filesystem", "disk");
+#if defined(ANDROID)
+	#include "ApkFilesystem.h"
 #endif
 
+namespace crown
+{
+
 //-----------------------------------------------------------------------------
 Device::Device() : 
 	m_allocator(m_subsystems_heap, MAX_SUBSYSTEMS_HEAP),
@@ -73,7 +76,8 @@ Device::Device() :
 	m_preferred_window_height(625),
 	m_preferred_window_fullscreen(0),
 	m_parent_window_handle(0),
-	m_preferred_mode(MODE_RELEASE),
+	m_compile(0),
+	m_continue(0),
 
 	m_quit_after_init(0),
 
@@ -97,8 +101,9 @@ Device::Device() :
 
 	m_console_server(NULL)
 {
-	// Select executable dir by default
-	string::strncpy(m_resource_path, os::get_cwd(), MAX_PATH_LENGTH);
+	// Bundle dir is current dir by default.
+	string::strncpy(m_bundle_dir, os::get_cwd(), MAX_PATH_LENGTH);
+	string::strncpy(m_source_dir, "", MAX_PATH_LENGTH);
 }
 
 //-----------------------------------------------------------------------------
@@ -109,58 +114,98 @@ Device::~Device()
 //-----------------------------------------------------------------------------
 bool Device::init(int argc, char** argv)
 {
-	if (is_init())
-	{
-		Log::e("Crown Engine is already initialized.");
-		return false;
-	}
+	CE_ASSERT(!is_init(), "Engine already initialized");
 
 	parse_command_line(argc, argv);
 	check_preferred_settings();
 
+	// Resource compilation only in debug or development mode and only on linux or windows builds
+	#if (defined(LINUX) || defined(WINDOWS)) && (defined(CROWN_DEBUG) || defined(CROWN_DEVELOPMENT))
+		if (m_compile == 1)
+		{
+			m_bundle_compiler = CE_NEW(m_allocator, BundleCompiler);
+			if (!m_bundle_compiler->compile(m_bundle_dir, m_source_dir))
+			{
+				CE_DELETE(m_allocator, m_bundle_compiler);
+				Log::e("Exiting.");
+				exit(EXIT_FAILURE);
+			}
+
+			if (!m_continue)
+			{
+				CE_DELETE(m_allocator, m_bundle_compiler);
+				exit(EXIT_SUCCESS);
+			}
+		}
+	#endif
+
 	// Initialize
 	Log::i("Initializing Crown Engine %d.%d.%d...", CROWN_VERSION_MAJOR, CROWN_VERSION_MINOR, CROWN_VERSION_MICRO);
 
-	if (m_compile == 1)
-	{
-		string::strncpy(m_resource_path, m_dest_path, MAX_PATH_LENGTH);
-	}
-	else
-	{
-		string::strncpy(m_resource_path, m_root_path, MAX_PATH_LENGTH);
-	}
+	// Default bundle filesystem
+	#if defined (LINUX) || defined(WINDOWS)
+		m_filesystem = CE_NEW(m_allocator, DiskFilesystem)(m_bundle_dir);
+	#elif defined(ANDROID)
+		m_filesystem = CE_NEW(m_allocator, ApkFilesystem)();
+	#endif
+	Log::d("Filesystem created.");
 
-	create_filesystem();
+	m_resource_bundle = Bundle::create(m_allocator, *m_filesystem);
 
-	create_resource_manager();
+	// // Read resource seed
+	// DiskFile* seed_file = (DiskFile*)filesystem()->open(g_default_mountpoint.value(), "seed.ini", FOM_READ);
+	// TextReader reader(*seed_file);
 
-	create_input_manager();
+	// char tmp_buf[32];
+	// reader.read_string(tmp_buf, 32);
 
-	create_window();
+	// filesystem()->close(seed_file);
 
-	create_renderer();
+	// uint32_t seed = string::parse_uint(tmp_buf);
 
-	create_debug_renderer();
+	// Create resource manager
+	m_resource_manager = CE_NEW(m_allocator, ResourceManager)(*m_resource_bundle, 0);
+	Log::d("Resource manager created.");
+	Log::d("Resource seed: %d", m_resource_manager->seed());
 
-	create_lua_environment();
+	// Create input manager
+	m_input_manager = CE_NEW(m_allocator, InputManager)();
+	Log::d("Input manager created.");
 
-	// create_console_server();
+	m_window = CE_NEW(m_allocator, OsWindow)(m_preferred_window_width, m_preferred_window_height, m_parent_window_handle);
 
-	read_engine_settings();
+	CE_ASSERT(m_window != NULL, "Unable to create the window");
 
-	Log::i("Crown Engine initialized.");
+	m_window->set_title("Crown Game Engine");
+	m_window->show();
+	Log::d("Window created.");
 
-	Log::i("Initializing Game...");
+	m_renderer = Renderer::create(m_allocator);
+	m_renderer->init();
+	Log::d("Renderer created.");
 
-	// Initialize the game through init game function
-	m_lua_environment->game_init();
+	// Create debug renderer
+	m_debug_renderer = CE_NEW(m_allocator, DebugRenderer)(*m_renderer);
+	Log::d("Debug renderer created.");
 
-	m_is_init = true;
+	m_lua_environment = CE_NEW(m_allocator, LuaEnvironment)();
+	m_lua_environment->init();
+	Log::d("Lua environment created.");
+
+	Log::i("Crown Engine initialized.");
+	Log::i("Initializing Game...");
 
+	m_is_init = true;
 	start();
 
+	ResourceId luagame_id = m_resource_manager->load("lua", "game");
+	m_resource_manager->flush();
+	m_lua_environment->load((LuaResource*) m_resource_manager->data(luagame_id));
+	m_lua_environment->call_global("init", 0);
+
 	if (m_quit_after_init == 1)
 	{
+		stop();
 		shutdown();
 	}
 
@@ -177,7 +222,7 @@ void Device::shutdown()
 	}
 
 	// Shutdowns the game
-	m_lua_environment->game_shutdown();
+	m_lua_environment->call_global("shutdown", 0);
 
 	Log::i("Releasing ConsoleServer...");
 	if (m_console_server)
@@ -222,14 +267,14 @@ void Device::shutdown()
 	}
 
 	Log::i("Releasing ResourceManager...");
-	if (m_resource_bundle)
+	if (m_resource_manager)
 	{
-		CE_DELETE(m_allocator, m_resource_bundle);
+		CE_DELETE(m_allocator, m_resource_manager);
 	}
 
-	if (m_resource_manager)
+	if (m_resource_bundle)
 	{
-		CE_DELETE(m_allocator, m_resource_manager);
+		Bundle::destroy(m_allocator, m_resource_bundle);
 	}
 
 	Log::i("Releasing Filesystem...");
@@ -322,25 +367,16 @@ ConsoleServer* Device::console_server()
 //-----------------------------------------------------------------------------
 void Device::start()
 {
-	if (is_init() == false)
-	{
-		Log::e("Cannot start uninitialized engine.");
-		return;
-	}
+	CE_ASSERT(m_is_init, "Cannot start uninitialized engine.");
 
 	m_is_running = true;
-
 	m_last_time = os::milliseconds();
 }
 
 //-----------------------------------------------------------------------------
 void Device::stop()
 {
-	if (is_init() == false)
-	{
-		Log::e("Cannot stop uninitialized engine.");
-		return;
-	}
+	CE_ASSERT(m_is_init, "Cannot stop uninitialized engine.");
 
 	m_is_running = false;
 }
@@ -374,7 +410,7 @@ void Device::frame()
 
 	m_window->frame();
 	m_input_manager->frame(frame_count());
-	m_lua_environment->game_frame(last_delta_time());
+	m_lua_environment->call_global("frame", 1, ARGUMENT_FLOAT, last_delta_time());
 
 	// m_console_server->execute();
 
@@ -385,112 +421,14 @@ void Device::frame()
 }
 
 //-----------------------------------------------------------------------------
-void Device::reload(ResourceId name)
-{
-	(void)name;
-}
-
-//-----------------------------------------------------------------------------
-void Device::create_filesystem()
-{
-	m_filesystem = CE_NEW(m_allocator, Filesystem)();
-
-	m_root_mountpoint.set_root_path(m_resource_path);
-
-	m_filesystem->mount(m_root_mountpoint);
-
-	Log::d("Filesystem created.");
-	Log::d("Filesystem root path: %s", m_root_mountpoint.root_path());
-}
-
-//-----------------------------------------------------------------------------
-void Device::create_resource_manager()
+void Device::compile(const char* , const char* , const char* )
 {
-	// Select appropriate resource archive
-	if (m_preferred_mode == MODE_DEVELOPMENT)
-	{
-		m_resource_bundle = CE_NEW(m_allocator, FileBundle)(*m_filesystem);
-	}
-	else
-	{
-		m_resource_bundle = CE_NEW(m_allocator, ArchiveBundle)(*m_filesystem);
-	}
-
-	// Read resource seed
-	DiskFile* seed_file = (DiskFile*)filesystem()->open(g_default_mountpoint.value(), "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, seed);
-
-	Log::d("Resource manager created.");
-	Log::d("Resource seed: %d", m_resource_manager->seed());
 }
 
 //-----------------------------------------------------------------------------
-void Device::create_input_manager()
-{
-	// Create input manager
-	m_input_manager = CE_NEW(m_allocator, InputManager)();
-
-	Log::d("Input manager created.");
-}
-
-//-----------------------------------------------------------------------------
-void Device::create_window()
-{
-	m_window = CE_NEW(m_allocator, OsWindow)(m_preferred_window_width, m_preferred_window_height, m_parent_window_handle);
-
-	CE_ASSERT(m_window != NULL, "Unable to create the window");
-
-	m_window->set_title("Crown Game Engine");
-	m_window->show();
-
-	Log::d("Window created.");
-}
-
-//-----------------------------------------------------------------------------
-void Device::create_renderer()
-{
-	m_renderer = Renderer::create(m_allocator);
-	m_renderer->init();
-
-	Log::d("Renderer created.");
-}
-
-//-----------------------------------------------------------------------------
-void Device::create_debug_renderer()
-{
-	// Create debug renderer
-	m_debug_renderer = CE_NEW(m_allocator, DebugRenderer)(*m_renderer);
-
-	Log::d("Debug renderer created.");
-}
-
-//-----------------------------------------------------------------------------
-void Device::create_lua_environment()
-{
-	m_lua_environment = CE_NEW(m_allocator, LuaEnvironment)();
-
-	m_lua_environment->init();
-
-	Log::d("Lua environment created.");
-}
-
-void Device::create_console_server()
+void Device::reload(ResourceId name)
 {
-	m_console_server = NULL;//CE_NEW(m_allocator, ConsoleServer)();
-
-	//m_console_server->init();
-
-	Log::d("Console server created.");
+	(void)name;
 }
 
 //-----------------------------------------------------------------------------
@@ -499,14 +437,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' },
-		{ "dest-path",        AOA_REQUIRED_ARGUMENT, NULL,        'd' },
+		{ "source-dir",       AOA_REQUIRED_ARGUMENT, NULL,        's' },
+		{ "bundle-dir",       AOA_REQUIRED_ARGUMENT, NULL,        'b' },
+		{ "compile",          AOA_NO_ARGUMENT,       &m_compile,   1 },
+		{ "continue",         AOA_NO_ARGUMENT,       &m_continue,  1 },
 		{ "width",            AOA_REQUIRED_ARGUMENT, NULL,        'w' },
 		{ "height",           AOA_REQUIRED_ARGUMENT, NULL,        'h' },
 		{ "fullscreen",       AOA_NO_ARGUMENT,       &m_preferred_window_fullscreen, 1 },
-		{ "compile",          AOA_NO_ARGUMENT,       &m_compile,   1 },
 		{ "parent-window",    AOA_REQUIRED_ARGUMENT, NULL,        'p' },
-		{ "dev",              AOA_NO_ARGUMENT,       &m_preferred_mode, MODE_DEVELOPMENT },
 		{ "quit-after-init",  AOA_NO_ARGUMENT,       &m_quit_after_init, 1 },
 		{ NULL, 0, NULL, 0 }
 	};
@@ -523,15 +461,16 @@ void Device::parse_command_line(int argc, char** argv)
 			{
 				break;
 			}
-			case 'r':
+			// Source directory
+			case 's':
 			{
-				string::strncpy(m_root_path, args.optarg(), MAX_PATH_LENGTH);
+				string::strncpy(m_source_dir, args.optarg(), MAX_PATH_LENGTH);
 				break;
 			}
-			// Resource path
-			case 'd':
+			// Bundle directory
+			case 'b':
 			{
-				string::strncpy(m_dest_path, args.optarg(), MAX_PATH_LENGTH);
+				string::strncpy(m_bundle_dir, args.optarg(), MAX_PATH_LENGTH);
 				break;
 			}
 			// Window width
@@ -566,7 +505,7 @@ void Device::parse_command_line(int argc, char** argv)
 //-----------------------------------------------------------------------------
 void Device::check_preferred_settings()
 {
-	if (!os::is_absolute_path(m_resource_path))
+	if (!os::is_absolute_path(m_bundle_dir))
 	{
 		Log::e("The root path must be absolute.");
 		exit(EXIT_FAILURE);
@@ -595,15 +534,19 @@ void Device::print_help_message()
 	"environment variables and configuration files.\n\n"
 
 	"  --help                     Show this help.\n"
-	"  --root-path <path>         Use <path> as the filesystem root path.\n"
+	"  --bundle-dir <path>        Use <path> as the source directory for compiled resources.\n"
 	"  --width <width>            Set the <width> of the main window.\n"
 	"  --height <width>           Set the <height> of the main window.\n"
 	"  --fullscreen               Start in fullscreen.\n"
 	"  --parent-window <handle>   Set the parent window <handle> of the main window.\n"
 	"                             Used only by tools.\n"
-	"  --dev                      Run the engine in development mode\n"
-	"  --quit-after-init          Quit the engine immediately after the initialization.\n"
-	"                             Used only for debugging.\n");
+
+	"\nAvailable only in debug and development builds:\n\n"
+
+	"  --source-dir <path>        Use <path> as the source directory for resource compilation.\n"
+	"  --compile                  Run the engine as resource compiler.\n"
+	"  --continue                 Do a full compile of the resources and continue the execution.\n"
+	"  --quit-after-init          Quit the engine immediately after the initialization.\n");
 }
 
 Device g_device;

+ 7 - 32
engine/Device.h

@@ -31,11 +31,6 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "OS.h"
 #include "LinearAllocator.h"
 #include "Resource.h"
-#include "DiskMountPoint.h"
-
-#ifdef ANDROID
-#include "AndroidMountPoint.h"
-#endif
 
 #define MAX_SUBSYSTEMS_HEAP 1024 * 1024
 
@@ -55,6 +50,7 @@ class Touch;
 class Accelerometer;
 class LuaEnvironment;
 class ConsoleServer;
+class BundleCompiler;
 
 /// The Engine.
 /// It is the place where to look for accessing all of
@@ -98,6 +94,8 @@ public:
 	/// Updates all the subsystems
 	void					frame();
 
+	void					compile(const char* bundle_dir, const char* source_dir, const char* resource);
+
 	void					reload(ResourceId name);
 
 	Filesystem*				filesystem();
@@ -118,17 +116,6 @@ public:
 
 private:
 
-	void					create_filesystem();
-	void					create_resource_manager();
-	void					create_input_manager();
-	void 					create_lua_environment();
-
-	void					create_window();
-	void					create_renderer();
-	void					create_debug_renderer();
-
-	void					create_console_server();
-
 	void					parse_command_line(int argc, char** argv);
 	void					check_preferred_settings();
 	void					read_engine_settings();
@@ -145,11 +132,10 @@ private:
 	int32_t					m_preferred_window_height;
 	int32_t					m_preferred_window_fullscreen;
 	uint32_t				m_parent_window_handle;
-	int32_t					m_preferred_mode;
-	char					m_root_path[MAX_PATH_LENGTH];
-	char 					m_dest_path[MAX_PATH_LENGTH];
-	char					m_resource_path[MAX_PATH_LENGTH];
+	char					m_source_dir[MAX_PATH_LENGTH];
+	char 					m_bundle_dir[MAX_PATH_LENGTH];
 	int32_t					m_compile;
+	int32_t					m_continue;
 
 	int32_t					m_quit_after_init;
 
@@ -172,26 +158,15 @@ private:
 	DebugRenderer*			m_debug_renderer;
 
 	// Private subsystems
+	BundleCompiler*			m_bundle_compiler;
 	ResourceManager*		m_resource_manager;
 	Bundle*					m_resource_bundle;
 
 	// Debug subsystems
 	ConsoleServer*			m_console_server;
 
-	#ifdef ANDROID
-	AndroidMountPoint		m_root_mountpoint;
-	#else
-	DiskMountPoint			m_root_mountpoint;
-	#endif
-
 private:
 
-	enum
-	{
-		MODE_RELEASE,
-		MODE_DEVELOPMENT
-	};
-
 	// Disable copying
 	Device(const Device&);
 	Device& operator=(const Device&);