فهرست منبع

Move BundleCompiler into LinuxDevice

Daniele Bartolini 12 سال پیش
والد
کامیت
1a98a717da
4فایلهای تغییر یافته به همراه229 افزوده شده و 260 حذف شده
  1. 30 219
      engine/Device.cpp
  2. 1 18
      engine/Device.h
  3. 197 22
      engine/os/linux/main.cpp
  4. 1 1
      engine/renderers/Renderer.h

+ 30 - 219
engine/Device.cpp

@@ -69,16 +69,6 @@ namespace crown
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 Device::Device() : 
 Device::Device() : 
 	m_allocator(default_allocator(), MAX_SUBSYSTEMS_HEAP),
 	m_allocator(default_allocator(), MAX_SUBSYSTEMS_HEAP),
-
-	m_preferred_window_width(1000),
-	m_preferred_window_height(625),
-	m_preferred_window_fullscreen(0),
-	m_parent_window_handle(0),
-	m_compile(0),
-	m_continue(0),
-
-	m_quit_after_init(0),
-
 	m_is_init(false),
 	m_is_init(false),
 	m_is_running(false),
 	m_is_running(false),
 	m_is_paused(false),
 	m_is_paused(false),
@@ -113,41 +103,6 @@ Device::~Device()
 {
 {
 }
 }
 
 
-//-----------------------------------------------------------------------------
-bool Device::init(int argc, char** argv)
-{
-	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);
-				m_allocator.clear();
-				Log::e("Exiting.");
-				exit(EXIT_FAILURE);
-			}
-
-			if (!m_continue)
-			{
-				CE_DELETE(m_allocator, m_bundle_compiler);
-				m_allocator.clear();
-				exit(EXIT_SUCCESS);
-			}
-		}
-	#endif
-
-	init();
-
-	return true;
-}
-
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 void Device::init()
 void Device::init()
 {
 {
@@ -210,12 +165,6 @@ void Device::init()
 	}
 	}
 
 
 	Log::d("Total allocated size: %llu", m_allocator.allocated_size());
 	Log::d("Total allocated size: %llu", m_allocator.allocated_size());
-
-	if (m_quit_after_init == 1)
-	{
-		stop();
-		shutdown();
-	}
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
@@ -269,16 +218,7 @@ void Device::shutdown()
 		CE_DELETE(m_allocator, m_filesystem);
 		CE_DELETE(m_allocator, m_filesystem);
 	}
 	}
 
 
-	#if (defined(LINUX) || defined(WINDOWS)) && (defined(CROWN_DEBUG) || defined(CROWN_DEVELOPMENT))
-		Log::i("Releasing BundleCompiler...");
-		if (m_bundle_compiler)
-		{
-			CE_DELETE(m_allocator, m_bundle_compiler);
-		}
-	#endif
-
 	m_allocator.clear();
 	m_allocator.clear();
-
 	m_is_init = false;
 	m_is_init = false;
 }
 }
 
 
@@ -464,182 +404,53 @@ void Device::reload(ResourceId name)
 	(void)name;
 	(void)name;
 }
 }
 
 
-//-----------------------------------------------------------------------------
-void Device::parse_command_line(int argc, char** argv)
-{
-	static ArgsOption options[] = 
-	{
-		{ "help",             AOA_NO_ARGUMENT,       NULL,        'i' },
-		{ "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 },
-		{ "parent-window",    AOA_REQUIRED_ARGUMENT, NULL,        'p' },
-		{ "quit-after-init",  AOA_NO_ARGUMENT,       &m_quit_after_init, 1 },
-		{ NULL, 0, NULL, 0 }
-	};
-
-	Args args(argc, argv, "", options);
-
-	int32_t opt;
-
-	while ((opt = args.getopt()) != -1)
-	{
-		switch (opt)
-		{
-			case 0:
-			{
-				break;
-			}
-			// Source directory
-			case 's':
-			{
-				string::strncpy(m_source_dir, args.optarg(), MAX_PATH_LENGTH);
-				break;
-			}
-			// Bundle directory
-			case 'b':
-			{
-				string::strncpy(m_bundle_dir, args.optarg(), MAX_PATH_LENGTH);
-				break;
-			}
-			// Window width
-			case 'w':
-			{
-				m_preferred_window_width = atoi(args.optarg());
-				break;
-			}
-			// Window height
-			case 'h':
-			{
-				m_preferred_window_height = atoi(args.optarg());
-				break;
-			}
-			// Parent window
-			case 'p':
-			{
-				m_parent_window_handle = string::parse_uint(args.optarg());
-				break;
-			}
-			case 'i':
-			case '?':
-			default:
-			{
-				print_help_message();
-				exit(EXIT_FAILURE);
-			}
-		}
-	}
-}
-
-//-----------------------------------------------------------------------------
-void Device::check_preferred_settings()
-{
-	if (m_compile == 1)
-	{
-		if (string::strcmp(m_source_dir, "") == 0)
-		{
-			Log::e("You have to specify the source directory when running in compile mode.");
-			exit(EXIT_FAILURE);
-		}
-
-		if (!os::is_absolute_path(m_source_dir))
-		{
-			Log::e("The source directory must be absolute.");
-			exit(EXIT_FAILURE);
-		}
-	}
-
-	if (!os::is_absolute_path(m_bundle_dir))
-	{
-		Log::e("The bundle directory must be absolute.");
-		exit(EXIT_FAILURE);
-	}
-
-	if (m_preferred_window_width == 0 || m_preferred_window_height == 0)
-	{
-		Log::e("Window width and height must be greater than zero.");
-		exit(EXIT_FAILURE);
-	}
-}
-
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 void Device::read_engine_settings()
 void Device::read_engine_settings()
 {
 {
-	// Check crown.config existance
-	CE_ASSERT(m_filesystem->is_file("crown.config"), "Unable to open crown.config");
+	// // Check crown.config existance
+	// CE_ASSERT(m_filesystem->is_file("crown.config"), "Unable to open crown.config");
 
 
-	// Copy crown config in a buffer
-	TempAllocator4096 allocator;
+	// // Copy crown config in a buffer
+	// TempAllocator4096 allocator;
 
 
-	File* config_file = m_filesystem->open("crown.config", FOM_READ);
+	// File* config_file = m_filesystem->open("crown.config", FOM_READ);
 
 
-	char* json_string = (char*)allocator.allocate(config_file->size());
+	// char* json_string = (char*)allocator.allocate(config_file->size());
 
 
-	config_file->read(json_string, config_file->size());
+	// config_file->read(json_string, config_file->size());
 
 
-	m_filesystem->close(config_file);
+	// m_filesystem->close(config_file);
 
 
-	// Parse crown.config
-	JSONParser parser(json_string);
+	// // Parse crown.config
+	// JSONParser parser(json_string);
 
 
-	JSONElement root = parser.root();
+	// JSONElement root = parser.root();
 
 
-	// Boot
-	if (root.has_key("boot"))
-	{
-		const char* boot = root.key("boot").string_value();
-		const size_t boot_length = string::strlen(boot) + 1;
+	// // Boot
+	// if (root.has_key("boot"))
+	// {
+	// 	const char* boot = root.key("boot").string_value();
+	// 	const size_t boot_length = string::strlen(boot) + 1;
 
 
-		string::strncpy(m_boot_file, boot, boot_length);
-	}
-	// Window width
-	if (root.has_key("window_width"))
-	{
-		m_preferred_window_width = root.key("window_width").int_value();
-	}
-	// Window height
-	if (root.has_key("window_height"))
-	{
-		m_preferred_window_height = root.key("window_height").int_value();
-	}
+	// 	string::strncpy(m_boot_file, boot, boot_length);
+	// }
+	// // Window width
+	// if (root.has_key("window_width"))
+	// {
+	// 	m_preferred_window_width = root.key("window_width").int_value();
+	// }
+	// // Window height
+	// if (root.has_key("window_height"))
+	// {
+	// 	m_preferred_window_height = root.key("window_height").int_value();
+	// }
 
 
-	allocator.deallocate(json_string);
+	// allocator.deallocate(json_string);
 
 
-	Log::i("Configuration set");
-}
-
-//-----------------------------------------------------------------------------
-void Device::print_help_message()
-{
-	os::printf(
-	"Usage: crown [options]\n"
-	"Options:\n\n"
-
-	"All of the following options take precedence over\n"
-	"environment variables and configuration files.\n\n"
-
-	"  --help                     Show this help.\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"
-
-	"\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");
+	// Log::i("Configuration set");
 }
 }
 
 
 static Device* g_device;
 static Device* g_device;
-
 void set_device(Device* device)
 void set_device(Device* device)
 {
 {
 	g_device = device;
 	g_device = device;

+ 1 - 18
engine/Device.h

@@ -60,9 +60,7 @@ public:
 							Device();
 							Device();
 							~Device();
 							~Device();
 
 
-	/// Initializes the engine allowing to pass command line
-	/// parameters to configure some parameters.
-	bool					init(int argc, char** argv);
+	void					init();
 
 
 	/// Shutdowns the engine freeing all the allocated resources
 	/// Shutdowns the engine freeing all the allocated resources
 	void					shutdown();
 	void					shutdown();
@@ -135,11 +133,7 @@ public:
 
 
 private:
 private:
 
 
-	void					init();
-	void					parse_command_line(int argc, char** argv);
-	void					check_preferred_settings();
 	void					read_engine_settings();
 	void					read_engine_settings();
-	void					print_help_message();
 
 
 protected:
 protected:
 
 
@@ -147,17 +141,9 @@ protected:
 	LinearAllocator			m_allocator;
 	LinearAllocator			m_allocator;
 
 
 	// Preferred settings
 	// Preferred settings
-	int32_t					m_preferred_window_width;
-	int32_t					m_preferred_window_height;
-	int32_t					m_preferred_window_fullscreen;
-	uint32_t				m_parent_window_handle;
 	char					m_source_dir[MAX_PATH_LENGTH];
 	char					m_source_dir[MAX_PATH_LENGTH];
 	char 					m_bundle_dir[MAX_PATH_LENGTH];
 	char 					m_bundle_dir[MAX_PATH_LENGTH];
 	char 					m_boot_file[MAX_PATH_LENGTH];
 	char 					m_boot_file[MAX_PATH_LENGTH];
-	int32_t					m_compile;
-	int32_t					m_continue;
-
-	int32_t					m_quit_after_init;
 
 
 	bool					m_is_init		: 1;
 	bool					m_is_init		: 1;
 	bool					m_is_running	: 1;
 	bool					m_is_running	: 1;
@@ -202,6 +188,3 @@ CE_EXPORT Device* device();
 CE_EXPORT void set_device(Device* device);
 CE_EXPORT void set_device(Device* device);
 
 
 } // namespace crown
 } // namespace crown
-
-
-

+ 197 - 22
engine/os/linux/main.cpp

@@ -32,6 +32,8 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Device.h"
 #include "Device.h"
 #include "OsTypes.h"
 #include "OsTypes.h"
 #include "EventQueue.h"
 #include "EventQueue.h"
+#include "Config.h"
+#include "BundleCompiler.h"
 
 
 namespace crown
 namespace crown
 {
 {
@@ -125,7 +127,17 @@ public:
 		, m_x11_window(None)
 		, m_x11_window(None)
 		, m_x11_parent_window(None)
 		, m_x11_parent_window(None)
 		, m_x11_hidden_cursor(None)
 		, m_x11_hidden_cursor(None)
-		, m_exit(false), m_x(0), m_y(0), m_width(1000), m_height(625), m_alloc(m_event_buffer, 1024 * 4), m_queue(m_alloc)
+		, m_exit(false)
+		, m_x(0)
+		, m_y(0)
+		, m_width(1000)
+		, m_height(625)
+		, m_parent_window_handle(0)
+		, m_fullscreen(0)
+		, m_compile(0)
+		, m_continue(0)
+		, m_alloc(m_event_buffer, 1024 * 4)
+		, m_queue(m_alloc)
 	{
 	{
 		for (uint32_t i = 0; i < 1024; i++)
 		for (uint32_t i = 0; i < 1024; i++)
 		{
 		{
@@ -133,11 +145,46 @@ public:
 		}
 		}
 	}
 	}
 
 
+	//-----------------------------------------------------------------------------
+	void init(int argc, char** argv)
+	{
+		parse_command_line(argc, argv);
+		check_preferred_settings();
+
+		#if defined(CROWN_DEBUG) || defined(CROWN_DEVELOPMENT)
+			if (m_compile == 1)
+			{
+				m_bundle_compiler = CE_NEW(default_allocator(), BundleCompiler);
+				if (!m_bundle_compiler->compile(m_bundle_dir, m_source_dir))
+				{
+					CE_DELETE(default_allocator(), m_bundle_compiler);
+					Log::e("Exiting.");
+					exit(EXIT_FAILURE);
+				}
+
+				if (!m_continue)
+				{
+					CE_DELETE(default_allocator(), m_bundle_compiler);
+					exit(EXIT_SUCCESS);
+				}
+			}
+		#endif
+	}
+
+	//-----------------------------------------------------------------------------
+	void shutdown()
+	{
+		#if defined(CROWN_DEBUG) || defined(CROWN_DEVELOPMENT)
+			CE_DELETE(default_allocator(), m_bundle_compiler);
+		#endif
+	}
+
 	//-----------------------------------------------------------------------------
 	//-----------------------------------------------------------------------------
 	int32_t run(int argc, char** argv)
 	int32_t run(int argc, char** argv)
 	{
 	{
-		XInitThreads();
+		init(argc, argv);
 
 
+		XInitThreads();
 		CE_ASSERT(m_width != 0 || m_height != 0, "Width and height must differ from zero");
 		CE_ASSERT(m_width != 0 || m_height != 0, "Width and height must differ from zero");
 
 
 		m_x11_display = XOpenDisplay(NULL);
 		m_x11_display = XOpenDisplay(NULL);
@@ -148,11 +195,11 @@ public:
 		int depth = DefaultDepth(m_x11_display, screen);
 		int depth = DefaultDepth(m_x11_display, screen);
 		Visual* visual = DefaultVisual(m_x11_display, screen);
 		Visual* visual = DefaultVisual(m_x11_display, screen);
 
 
-		// if (parent != 0)
-		// {
-		// 	m_x11_parent_window = (Window) parent;
-		// }
-		// else
+		if (m_parent_window_handle != 0)
+		{
+			m_x11_parent_window = (Window) m_parent_window_handle;
+		}
+		else
 		{
 		{
 			m_x11_parent_window = RootWindow(m_x11_display, screen);
 			m_x11_parent_window = RootWindow(m_x11_display, screen);
 		}
 		}
@@ -201,13 +248,8 @@ public:
 
 
 		set_x11_display_and_window(m_x11_display, m_x11_window);
 		set_x11_display_and_window(m_x11_display, m_x11_window);
 
 
-		MainArgs args;
-		args.argc = argc;
-		args.argv = argv;
-		args.device = this;
-
 		OsThread game_thread("game-thread");
 		OsThread game_thread("game-thread");
-		game_thread.start(main_loop, (void*)&args);
+		game_thread.start(main_loop, (void*)this);
 
 
 		while (!m_exit)
 		while (!m_exit)
 		{
 		{
@@ -217,6 +259,8 @@ public:
 			}
 			}
 		}
 		}
 
 
+		Log::d("Stopping game thread");
+
 		game_thread.stop();
 		game_thread.stop();
 
 
 		XDestroyWindow(m_x11_display, m_x11_window);
 		XDestroyWindow(m_x11_display, m_x11_window);
@@ -226,22 +270,28 @@ public:
 	}
 	}
 
 
 	//-----------------------------------------------------------------------------
 	//-----------------------------------------------------------------------------
-	static int32_t main_loop(void* user_data)
+	int32_t loop()
 	{
 	{
-		MainArgs* args = (MainArgs*)user_data;
+		Device::init();
 
 
-		device()->init(args->argc, args->argv);
-
-		while(!args->device->process_events())
+		while(!process_events() && is_running())
 		{
 		{
-			device()->frame();
+			Device::frame();
 		}
 		}
 
 
-		device()->shutdown();
+		Device::shutdown();
+
+		m_exit = true;
 
 
 		return 0;
 		return 0;
 	}
 	}
 
 
+	//-----------------------------------------------------------------------------
+	static int32_t main_loop(void* thiz)
+	{
+		return ((LinuxDevice*)thiz)->loop();
+	}
+
 	//-----------------------------------------------------------------------------
 	//-----------------------------------------------------------------------------
 	bool process_events()
 	bool process_events()
 	{
 	{
@@ -266,8 +316,6 @@ public:
 					}
 					}
 					case OsEvent::EXIT:
 					case OsEvent::EXIT:
 					{
 					{
-						Log::d("Exiting");
-						m_exit = true;
 						return true;
 						return true;
 					}
 					}
 					default:
 					default:
@@ -405,6 +453,128 @@ public:
 		}
 		}
 	}
 	}
 
 
+	//-----------------------------------------------------------------------------
+	void parse_command_line(int argc, char** argv)
+	{
+		static const char* help_message =
+			"Usage: crown [options]\n"
+			"Options:\n\n"
+
+			"All of the following options take precedence over\n"
+			"environment variables and configuration files.\n\n"
+
+			"  --help                     Show this help.\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"
+
+			"\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";
+
+		static ArgsOption options[] = 
+		{
+			{ "help",             AOA_NO_ARGUMENT,       NULL,        'i' },
+			{ "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_fullscreen, 1 },
+			{ "parent-window",    AOA_REQUIRED_ARGUMENT, NULL,        'p' },
+			{ NULL, 0, NULL, 0 }
+		};
+
+		Args args(argc, argv, "", options);
+
+		int32_t opt;
+		while ((opt = args.getopt()) != -1)
+		{
+			switch (opt)
+			{
+				case 0:
+				{
+					break;
+				}
+				// Source directory
+				case 's':
+				{
+					string::strncpy(m_source_dir, args.optarg(), MAX_PATH_LENGTH);
+					break;
+				}
+				// Bundle directory
+				case 'b':
+				{
+					string::strncpy(m_bundle_dir, args.optarg(), MAX_PATH_LENGTH);
+					break;
+				}
+				// Window width
+				case 'w':
+				{
+					m_width = atoi(args.optarg());
+					break;
+				}
+				// Window height
+				case 'h':
+				{
+					m_height = atoi(args.optarg());
+					break;
+				}
+				// Parent window
+				case 'p':
+				{
+					m_parent_window_handle = string::parse_uint(args.optarg());
+					break;
+				}
+				case 'i':
+				case '?':
+				default:
+				{
+					os::printf(help_message);
+					exit(EXIT_FAILURE);
+				}
+			}
+		}
+	}
+
+	//-----------------------------------------------------------------------------
+	void check_preferred_settings()
+	{
+		if (m_compile == 1)
+		{
+			if (string::strcmp(m_source_dir, "") == 0)
+			{
+				Log::e("You have to specify the source directory when running in compile mode.");
+				exit(EXIT_FAILURE);
+			}
+
+			if (!os::is_absolute_path(m_source_dir))
+			{
+				Log::e("The source directory must be absolute.");
+				exit(EXIT_FAILURE);
+			}
+		}
+
+		if (!os::is_absolute_path(m_bundle_dir))
+		{
+			Log::e("The bundle directory must be absolute.");
+			exit(EXIT_FAILURE);
+		}
+
+		if (m_width == 0 || m_height == 0)
+		{
+			Log::e("Window width and height must be greater than zero.");
+			exit(EXIT_FAILURE);
+		}
+	}
+
+
 private:
 private:
 
 
 	Display* m_x11_display;
 	Display* m_x11_display;
@@ -420,6 +590,11 @@ private:
 	uint32_t m_height;
 	uint32_t m_height;
 	bool m_x11_detectable_autorepeat;
 	bool m_x11_detectable_autorepeat;
 
 
+	uint32_t m_parent_window_handle;
+	int32_t m_fullscreen;
+	int32_t m_compile;
+	int32_t m_continue;
+
 	char m_event_buffer[1024 * 4];
 	char m_event_buffer[1024 * 4];
 	LinearAllocator m_alloc;
 	LinearAllocator m_alloc;
 	EventQueue m_queue;
 	EventQueue m_queue;

+ 1 - 1
engine/renderers/Renderer.h

@@ -95,7 +95,6 @@ public:
 			m_submit->m_commands.write(COMMAND_SHUTDOWN_RENDERER);
 			m_submit->m_commands.write(COMMAND_SHUTDOWN_RENDERER);
 			frame();
 			frame();
 
 
-			m_should_run = false;
 			m_thread.stop();		
 			m_thread.stop();		
 		}
 		}
 	}
 	}
@@ -314,6 +313,7 @@ public:
 				{
 				{
 					shutdown_impl();
 					shutdown_impl();
 					m_is_initialized = false;
 					m_is_initialized = false;
+					m_should_run = false;
 					break;
 					break;
 				}
 				}
 				case COMMAND_CREATE_VERTEX_BUFFER:
 				case COMMAND_CREATE_VERTEX_BUFFER: