Explorar el Código

Prepare for rewriting windows main

Daniele Bartolini hace 11 años
padre
commit
de08f28929
Se han modificado 1 ficheros con 15 adiciones y 397 borrados
  1. 15 397
      engine/main/main_windows.cpp

+ 15 - 397
engine/main/main_windows.cpp

@@ -34,12 +34,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #define WM_USER_TOGGLE_WINDOW_FRAME (WM_USER+1)
 #define WM_USER_MOUSE_LOCK          (WM_USER+2)
 
-#include "args.h"
-#include "bundle_compiler.h"
 #include "device.h"
-#include "json_parser.h"
-#include "log.h"
-#include "os.h"
 #include "os_event_queue.h"
 #include "os_window_windows.h"
 #include "thread.h"
@@ -53,18 +48,6 @@ namespace crown
 
 extern void set_win_handle_window(HWND hwnd);
 
-//-----------------------------------------------------------------------------
-void init()
-{
-	memory::init();
-}
-
-//-----------------------------------------------------------------------------
-void shutdown()
-{
-	memory::shutdown();
-}
-
 //-----------------------------------------------------------------------------
 static KeyboardButton::Enum win_translate_key(int32_t winKey)
 {
@@ -122,94 +105,18 @@ static KeyboardButton::Enum win_translate_key(int32_t winKey)
 	}
 }
 
-//-----------------------------------------------------------------------------
-class WindowsDevice : public Device
-{
-public:
+static bool s_exit = false;
 
+struct WindowsDevice
+{
 	WindowsDevice()
 		: m_hwnd(0)
-		, m_width(0)
-		, m_height(0)
-		, m_x(0)
-		, m_y(0)
-		, m_exit(false)
-		, m_wait_console(0)
-		, m_platform("windows")
-	{
-	}
-
-	//-----------------------------------------------------------------------------
-	void init(int argc, char** argv)
-	{
-		parse_command_line(argc, argv);
-		check_preferred_settings();
-
-		#if defined(CROWN_DEBUG)
-			m_console = CE_NEW(default_allocator(), ConsoleServer)();
-			m_console->init(m_console_port, m_wait_console == 1);
-
-			if (m_compile == 1)
-			{
-				m_bundle_compiler = CE_NEW(default_allocator(), BundleCompiler);
-				if (!m_bundle_compiler->compile(m_bundle_dir, m_source_dir, m_platform))
-				{
-					CE_DELETE(default_allocator(), m_bundle_compiler);
-					CE_LOGE("Exiting.");
-					exit(EXIT_FAILURE);
-				}
-
-				if (!m_continue)
-				{
-					CE_DELETE(default_allocator(), m_bundle_compiler);
-					exit(EXIT_SUCCESS);
-				}
-			}
-		#endif
-
-		read_configuration();
-	}
-
-	//-----------------------------------------------------------------------------
-	void shutdown()
-	{
-		#if defined(CROWN_DEBUG)
-			CE_DELETE(default_allocator(), m_bundle_compiler);
-
-			m_console->shutdown();
-			CE_DELETE(default_allocator(), m_console);
-		#endif
-	}
-
-	//-----------------------------------------------------------------------------
-	void display_modes(Array<DisplayMode>& /*modes*/)
-	{
-		// #error "Implement me"
-	}
-
-	//-----------------------------------------------------------------------------
-	void set_display_mode(uint32_t /*id*/)
 	{
-		// #error "Implement me"
-	}
-
-	//-----------------------------------------------------------------------------
-	void set_fullscreen(bool full)
-	{
-		// #error "Implement me"
 	}
 
 	//-----------------------------------------------------------------------------
 	int32_t	run(int argc, char** argv)
 	{
-		WSADATA WsaData;
-		int res = WSAStartup(MAKEWORD(2,2), &WsaData);
-		CE_ASSERT(res == 0, "Unable to initialize socket");
-		CE_UNUSED(WsaData);
-		CE_UNUSED(res);
-
-		init(argc, argv);
-
 		HINSTANCE instance = (HINSTANCE)GetModuleHandle(NULL);
 
 		WNDCLASSEX wnd;
@@ -241,9 +148,6 @@ public:
 		oswindow_set_window(m_hwnd);
 		bgfx::winSetHwnd(m_hwnd);
 
-		m_width = ENTRY_DEFAULT_WIDTH;
-		m_height = ENTRY_DEFAULT_HEIGHT;
-
 		m_argc = argc;
 		m_argv = argv;
 
@@ -253,7 +157,7 @@ public:
 		MSG msg;
 		msg.message = WM_NULL;
 
-		while (!m_exit)
+		while (!s_exit)
 		{
 			while (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE) != 0)
 			{
@@ -267,96 +171,9 @@ public:
 		shutdown();
 
 		DestroyWindow(m_hwnd);
-		WSACleanup();
-		return 0;
-	}
-
-	//-----------------------------------------------------------------------------
-	int32_t loop()
-	{
-		Device::init();
-
-		while(!process_events() && is_running())
-		{
-			#if defined(CROWN_DEBUG)
-				m_console->update();
-			#endif
-
-			Device::frame();
-
-			m_keyboard->update();
-			m_mouse->update();
-		}
-
-		Device::shutdown();
-
-		m_exit = true;
-
 		return 0;
 	}
 
-	//-----------------------------------------------------------------------------
-	static int32_t main_loop(void* data)
-	{
-		return ((WindowsDevice*)data)->loop();
-	}
-
-	//-----------------------------------------------------------------------------
-	bool process_events()
-	{
-		OsEvent event;
-		bool exit = false;
-
-		while(m_queue.pop_event(event))
-		{
-			if (event.type == OsEvent::NONE) continue;
-
-			switch (event.type)
-			{
-				case OsEvent::MOUSE:
-				{
-					const OsMouseEvent& ev = event.mouse;
-					switch (ev.type)
-					{
-						case OsMouseEvent::BUTTON: m_mouse->set_button_state(ev.x, ev.y, ev.button, ev.pressed); break;
-						case OsMouseEvent::MOVE: m_mouse->set_position(ev.x, ev.y); break;
-						default: CE_FATAL("Oops, unknown mouse event type"); break;
-					}
-
-					break;
-				}
-				case OsEvent::KEYBOARD:
-				{
-					const OsKeyboardEvent& ev = event.keyboard;
-					m_keyboard->set_button_state(ev.button, ev.pressed);
-					break;
-				}
-				case OsEvent::METRICS:
-				{
-					const OsMetricsEvent& ev = event.metrics;
-					m_mouse->set_metrics(ev.width, ev.height);
-					m_window->m_x = ev.x;
-					m_window->m_y = ev.y;
-					m_window->m_width = ev.width;
-					m_window->m_height = ev.height;
-					break;
-				}
-				case OsEvent::EXIT:
-				{
-					exit = true;
-					break;
-				}
-				default:
-				{
-					CE_FATAL("Unknown Os Event");
-					break;
-				}
-			}
-		}
-
-		return exit;
-	}
-
 	//-----------------------------------------------------------------------------
 	LRESULT pump_events(HWND hwnd, UINT id, WPARAM wparam, LPARAM lparam)
 	{
@@ -388,12 +205,7 @@ public:
 			{
 				uint32_t width = GET_X_LPARAM(lparam);
 				uint32_t height = GET_Y_LPARAM(lparam);
-
-				m_width = width;
-				m_height = height;
-
 				m_queue.push_metrics_event(m_x, m_y, m_width, m_height);
-
 				break;
 			}
 			case WM_SYSCOMMAND:
@@ -474,186 +286,6 @@ public:
 		return DefWindowProc(hwnd, id, wparam, lparam);
 	}
 
-	//-----------------------------------------------------------------------------
-	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                  Do a full compile of the resources.\n"
-			"  --platform <platform>      Compile resources for the given <platform>.\n"
-			"      Possible values for <platform> are:\n"
-			"          linux\n"
-			"          android\n"
-			"          windows\n"
-			"  --continue                 Continue the execution after the resource compilation step.\n"
-			"  --file-server              Read resources from a remote engine instance.\n"
-			"  --console-port             Set the network port of the console server.\n"
-			"  --wait-console             Wait for a console connection before starting up.\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 },
-			{ "platform",         AOA_REQUIRED_ARGUMENT, NULL,           'r' },
-			{ "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' },
-			{ "file-server",      AOA_NO_ARGUMENT,       &m_fileserver,    1 },
-			{ "console-port",     AOA_REQUIRED_ARGUMENT, NULL,           'c' },
-			{ "wait-console",     AOA_NO_ARGUMENT,       &m_wait_console,  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_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;
-				}
-				// Console port
-				case 'c':
-				{
-					m_console_port = string::parse_uint(args.optarg());
-					break;
-				}
-				// Platform
-				case 'r':
-				{
-					m_platform = args.optarg();
-					break;
-				}
-				case 'i':
-				case '?':
-				default:
-				{
-					printf(help_message);
-					exit(EXIT_FAILURE);
-				}
-			}
-		}
-	}
-
-	//-----------------------------------------------------------------------------
-	void check_preferred_settings()
-	{
-		if (m_compile == 1)
-		{
-			if (string::strcmp(m_source_dir, "") == 0)
-			{
-				CE_LOGE("You have to specify the source directory when running in compile mode.");
-				exit(EXIT_FAILURE);
-			}
-
-			if (!os::is_absolute_path(m_source_dir))
-			{
-				CE_LOGE("The source directory must be absolute.");
-				exit(EXIT_FAILURE);
-			}
-		}
-
-		if (!os::is_absolute_path(m_bundle_dir))
-		{
-			CE_LOGE("The bundle directory must be absolute.");
-			exit(EXIT_FAILURE);
-		}
-	}
-
-	//-------------------------------------------------------------------------
-	void read_configuration()
-	{
-		DiskFilesystem fs(m_bundle_dir);
-
-		// crown.config is mandatory
-		CE_ASSERT(fs.is_file("crown.config"), "Unable to open crown.config");
-
-		File* config_file = fs.open("crown.config", FOM_READ);
-
-		TempAllocator4096 alloc;
-		char* json_string = (char*)alloc.allocate(config_file->size());
-		config_file->read(json_string, config_file->size());
-		fs.close(config_file);
-
-		// Parse crown.config
-		JSONParser parser(json_string);
-		JSONElement root = parser.root();
-
-		// Boot
-		if (root.has_key("boot"))
-		{
-			DynamicString boot;
-			root.key("boot").to_string(boot);
-
-			string::strncpy(m_boot_file, boot.c_str(), (boot.length() > MAX_PATH_LENGTH) ? MAX_PATH_LENGTH : boot.length() + 1);
-		}
-
-		// Window width
-		if (root.has_key("window_width"))
-		{
-			m_width = root.key("window_width").to_int();
-		}
-
-		// Window height
-		if (root.has_key("window_height"))
-		{
-			m_height = root.key("window_height").to_int();
-		}
-	}
-
 private:
 
 	static LRESULT CALLBACK window_proc(HWND hwnd, UINT id, WPARAM wparam, LPARAM lparam);
@@ -662,46 +294,32 @@ public:
 
 	HWND m_hwnd;
 	HDC m_hdc;
-	uint32_t m_width;
-	uint32_t m_height;
-	int32_t m_x;
-	int32_t m_y;
-	bool m_exit;
-
 	uint32_t m_parent_window_handle;
-	int32_t m_fullscreen;
-	int32_t m_compile;
-	int32_t m_continue;
-	int32_t m_wait_console;
-	const char* m_platform;
-
 	OsEventQueue m_queue;
 };
 
-WindowsDevice* engine;
+static WindowsDevice s_wdvc;
 
 LRESULT CALLBACK WindowsDevice::window_proc(HWND hwnd, UINT id, WPARAM wparam, LPARAM lparam)
 {
-	return ((WindowsDevice*)engine)->pump_events(hwnd, id, wparam, lparam);
+	return s_wdvc.pump_events(hwnd, id, wparam, lparam);
 }
 
 } // namespace crown
 
-
 int main(int argc, char** argv)
 {
-	using namespace crown;
-	init();
-
-	engine = CE_NEW(default_allocator(), WindowsDevice)();
-	set_device(engine);
-
-	int32_t ret = ((WindowsDevice*)engine)->run(argc, argv);
-
-	CE_DELETE(default_allocator(), engine);
+	WSADATA WsaData;
+	int res = WSAStartup(MAKEWORD(2,2), &WsaData);
+	CE_ASSERT(res == 0, "Unable to initialize socket");
+	CE_UNUSED(WsaData);
+	CE_UNUSED(res);
 
-	shutdown();
+	crown::init();
+	int32_t ret = crown::s_wdvc.run(argc, argv);
+	crown::shutdown();
 
+	WSACleanup();
 	return ret;
 }