Daniele Bartolini 9 лет назад
Родитель
Сommit
e79012d7ed
4 измененных файлов с 97 добавлено и 39 удалено
  1. 57 0
      src/device/boot_config.cpp
  2. 28 0
      src/device/boot_config.h
  3. 11 38
      src/device/device.cpp
  4. 1 1
      src/device/device.h

+ 57 - 0
src/device/boot_config.cpp

@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2012-2016 Daniele Bartolini and individual contributors.
+ * License: https://github.com/taylor001/crown/blob/master/LICENSE
+ */
+
+#include "boot_config.h"
+#include "json_object.h"
+#include "map.h"
+#include "platform.h"
+#include "sjson.h"
+#include "temp_allocator.h"
+
+namespace crown
+{
+BootConfig::BootConfig()
+	: boot_script_name(u64(0))
+	, boot_package_name(u64(0))
+	, window_w(CROWN_DEFAULT_WINDOW_WIDTH)
+	, window_h(CROWN_DEFAULT_WINDOW_HEIGHT)
+	, vsync(true)
+{
+}
+
+bool BootConfig::parse(const char* json)
+{
+	TempAllocator4096 ta;
+	JsonObject cfg(ta);
+	sjson::parse(json, cfg);
+
+	// General configs
+	boot_script_name  = sjson::parse_resource_id(cfg["boot_script"]);
+	boot_package_name = sjson::parse_resource_id(cfg["boot_package"]);
+
+	// Platform-specific configs
+	if (json_object::has(cfg, CROWN_PLATFORM_NAME))
+	{
+		JsonObject platform(ta);
+		sjson::parse(cfg[CROWN_PLATFORM_NAME], platform);
+
+		if (json_object::has(platform, "renderer"))
+		{
+			JsonObject renderer(ta);
+			sjson::parse(platform["renderer"], renderer);
+
+			if (json_object::has(renderer, "window_width"))
+				window_w = (u16)sjson::parse_int(renderer["window_width"]);
+			if (json_object::has(renderer, "window_height"))
+				window_h = (u16)sjson::parse_int(renderer["window_height"]);
+			if (json_object::has(renderer, "vsync"))
+				vsync = sjson::parse_bool(renderer["vsync"]);
+		}
+	}
+
+	return true;
+}
+
+} // namespace crown

+ 28 - 0
src/device/boot_config.h

@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2012-2016 Daniele Bartolini and individual contributors.
+ * License: https://github.com/taylor001/crown/blob/master/LICENSE
+ */
+
+#pragma once
+
+#include "types.h"
+#include "string_id.h"
+
+namespace crown
+{
+/// Boot configuration.
+///
+/// @ingroup Device
+struct BootConfig
+{
+	StringId64 boot_script_name;
+	StringId64 boot_package_name;
+	u16 window_w;
+	u16 window_h;
+	bool vsync;
+
+	BootConfig();
+	bool parse(const char* json);
+};
+
+} // namespace crown

+ 11 - 38
src/device/device.cpp

@@ -5,6 +5,7 @@
 
 
 #include "array.h"
 #include "array.h"
 #include "audio.h"
 #include "audio.h"
+#include "boot_config.h"
 #include "config.h"
 #include "config.h"
 #include "config_resource.h"
 #include "config_resource.h"
 #include "console_api.h"
 #include "console_api.h"
@@ -169,7 +170,7 @@ Device::Device(const DeviceOptions& opts)
 {
 {
 }
 }
 
 
-bool Device::process_events(s16& mouse_x, s16& mouse_y, s16& mouse_last_x, s16& mouse_last_y)
+bool Device::process_events(s16& mouse_x, s16& mouse_y, s16& mouse_last_x, s16& mouse_last_y, bool vsync)
 {
 {
 	OsEvent event;
 	OsEvent event;
 	bool exit = false;
 	bool exit = false;
@@ -258,7 +259,7 @@ bool Device::process_events(s16& mouse_x, s16& mouse_y, s16& mouse_last_x, s16&
 				const OsMetricsEvent& ev = event.metrics;
 				const OsMetricsEvent& ev = event.metrics;
 				_width = ev.width;
 				_width = ev.width;
 				_height = ev.height;
 				_height = ev.height;
-				bgfx::reset(ev.width, ev.height, BGFX_RESET_VSYNC);
+				bgfx::reset(ev.width, ev.height, (vsync ? BGFX_RESET_VSYNC : BGFX_RESET_NONE));
 				break;
 				break;
 			}
 			}
 			case OsEvent::EXIT:
 			case OsEvent::EXIT:
@@ -405,13 +406,9 @@ void Device::run()
 		_resource_manager->register_type(RESOURCE_TYPE_CONFIG,           cor::load, cor::unload, NULL,        NULL        );
 		_resource_manager->register_type(RESOURCE_TYPE_CONFIG,           cor::load, cor::unload, NULL,        NULL        );
 
 
 		// Read config
 		// Read config
-		StringId64 boot_package_name(u64(0));
-		StringId64 boot_script_name(u64(0));
-		u16 window_w = CROWN_DEFAULT_WINDOW_WIDTH;
-		u16 window_h = CROWN_DEFAULT_WINDOW_HEIGHT;
-
+		BootConfig boot_config;
 		{
 		{
-			TempAllocator4096 ta;
+			TempAllocator512 ta;
 			DynamicString boot_dir(ta);
 			DynamicString boot_dir(ta);
 
 
 			if (_device_options._boot_dir != NULL)
 			if (_device_options._boot_dir != NULL)
@@ -422,33 +419,9 @@ void Device::run()
 			boot_dir += CROWN_BOOT_CONFIG;
 			boot_dir += CROWN_BOOT_CONFIG;
 
 
 			const StringId64 config_name(boot_dir.c_str());
 			const StringId64 config_name(boot_dir.c_str());
-
 			_resource_manager->load(RESOURCE_TYPE_CONFIG, config_name);
 			_resource_manager->load(RESOURCE_TYPE_CONFIG, config_name);
 			_resource_manager->flush();
 			_resource_manager->flush();
-			const char* cfile = (const char*)_resource_manager->get(RESOURCE_TYPE_CONFIG, config_name);
-
-			JsonObject config(ta);
-			sjson::parse(cfile, config);
-
-			boot_script_name  = sjson::parse_resource_id(config["boot_script"]);
-			boot_package_name = sjson::parse_resource_id(config["boot_package"]);
-
-			// Platform-specific configs
-			if (json_object::has(config, CROWN_PLATFORM_NAME))
-			{
-				JsonObject platform(ta);
-				sjson::parse(config[CROWN_PLATFORM_NAME], platform);
-
-				if (json_object::has(platform, "window_width"))
-				{
-					window_w = (u16)sjson::parse_int(platform["window_width"]);
-				}
-				if (json_object::has(platform, "window_height"))
-				{
-					window_h = (u16)sjson::parse_int(platform["window_height"]);
-				}
-			}
-
+			boot_config.parse((const char*)_resource_manager->get(RESOURCE_TYPE_CONFIG, config_name));
 			_resource_manager->unload(RESOURCE_TYPE_CONFIG, config_name);
 			_resource_manager->unload(RESOURCE_TYPE_CONFIG, config_name);
 		}
 		}
 
 
@@ -460,8 +433,8 @@ void Device::run()
 		_window = window::create(_allocator);
 		_window = window::create(_allocator);
 		_window->open(_device_options._window_x
 		_window->open(_device_options._window_x
 			, _device_options._window_y
 			, _device_options._window_y
-			, window_w
-			, window_h
+			, boot_config.window_w
+			, boot_config.window_h
 			, _device_options._parent_window
 			, _device_options._parent_window
 			);
 			);
 		_window->bgfx_setup();
 		_window->bgfx_setup();
@@ -482,12 +455,12 @@ void Device::run()
 		audio_globals::init();
 		audio_globals::init();
 		physics_globals::init(_allocator);
 		physics_globals::init(_allocator);
 
 
-		ResourcePackage* boot_package = create_resource_package(boot_package_name);
+		ResourcePackage* boot_package = create_resource_package(boot_config.boot_package_name);
 		boot_package->load();
 		boot_package->load();
 		boot_package->flush();
 		boot_package->flush();
 
 
 		_lua_environment->load_libs();
 		_lua_environment->load_libs();
-		_lua_environment->execute((LuaResource*)_resource_manager->get(RESOURCE_TYPE_SCRIPT, boot_script_name));
+		_lua_environment->execute((LuaResource*)_resource_manager->get(RESOURCE_TYPE_SCRIPT, boot_config.boot_script_name));
 		_lua_environment->call_global("init", 0);
 		_lua_environment->call_global("init", 0);
 
 
 		logd("Engine initialized");
 		logd("Engine initialized");
@@ -500,7 +473,7 @@ void Device::run()
 		s64 last_time = os::clocktime();
 		s64 last_time = os::clocktime();
 		s64 curr_time;
 		s64 curr_time;
 
 
-		while (!process_events(mouse_x, mouse_y, mouse_last_x, mouse_last_y) && !_quit)
+		while (!process_events(mouse_x, mouse_y, mouse_last_x, mouse_last_y, boot_config.vsync) && !_quit)
 		{
 		{
 			curr_time = os::clocktime();
 			curr_time = os::clocktime();
 			const s64 time = curr_time - last_time;
 			const s64 time = curr_time - last_time;

+ 1 - 1
src/device/device.h

@@ -65,7 +65,7 @@ class Device
 	f32 _last_delta_time;
 	f32 _last_delta_time;
 	f64 _time_since_start;
 	f64 _time_since_start;
 
 
-	bool process_events(s16& mouse_x, s16& mouse_y, s16& mouse_last_x, s16& mouse_last_y);
+	bool process_events(s16& mouse_x, s16& mouse_y, s16& mouse_last_x, s16& mouse_last_y, bool vsync);
 
 
 public:
 public: