Sfoglia il codice sorgente

Create BundleCompiler in Device

Daniele Bartolini 10 anni fa
parent
commit
beb4d9ecb1

+ 1 - 1
src/device/console_server.cpp

@@ -165,7 +165,7 @@ void ConsoleServer::process(TCPSocket client, const char* json)
 		sjson::parse_string(root["resource_name"], rname);
 		sjson::parse_string(root["platform"], platform);
 
-		bool succ = bundle_compiler_globals::compiler()->compile(rtype.c_str()
+		bool succ = device()->bundle_compiler()->compile(rtype.c_str()
 			, rname.c_str()
 			, platform.c_str()
 			);

+ 110 - 87
src/device/device.cpp

@@ -6,6 +6,7 @@
 #include "apk_filesystem.h"
 #include "array.h"
 #include "audio.h"
+#include "bundle_compiler.h"
 #include "config.h"
 #include "console_server.h"
 #include "device.h"
@@ -128,6 +129,7 @@ Device::Device(const DeviceOptions& opts)
 	: _allocator(default_allocator(), MAX_SUBSYSTEMS_HEAP)
 	, _device_options(opts)
 	, _console_server(NULL)
+	, _bundle_compiler(NULL)
 	, _bundle_filesystem(NULL)
 	, _last_log(NULL)
 	, _resource_loader(NULL)
@@ -172,125 +174,141 @@ void Device::init()
 	_console_server = CE_NEW(_allocator, ConsoleServer)(default_allocator());
 	_console_server->listen(_device_options._console_port, _device_options._wait_console);
 
+	bool do_continue = true;
+
+#if CROWN_PLATFORM_LINUX || CROWN_PLATFORM_WINDOWS
+	_bundle_compiler = CE_NEW(_allocator, BundleCompiler)(_device_options._source_dir, _device_options._bundle_dir);
+	if (_device_options._do_compile)
+	{
+		bool success = _bundle_compiler->compile_all(_device_options._platform);
+		do_continue = success && _device_options._do_continue;
+	}
+#endif // CROWN_PLATFORM_LINUX || CROWN_PLATFORM_WINDOWS
+
+	if (do_continue)
+	{
 #if CROWN_PLATFORM_ANDROID
-	_bundle_filesystem = CE_NEW(_allocator, ApkFilesystem)(default_allocator(), const_cast<AAssetManager*>((AAssetManager*)_device_options._asset_manager));
+		_bundle_filesystem = CE_NEW(_allocator, ApkFilesystem)(default_allocator(), const_cast<AAssetManager*>((AAssetManager*)_device_options._asset_manager));
 #else
-	_bundle_filesystem = CE_NEW(_allocator, DiskFilesystem)(default_allocator(), _device_options._bundle_dir);
-	_last_log = _bundle_filesystem->open(CROWN_LAST_LOG, FileOpenMode::WRITE);
+		_bundle_filesystem = CE_NEW(_allocator, DiskFilesystem)(default_allocator(), _device_options._bundle_dir);
+		_last_log = _bundle_filesystem->open(CROWN_LAST_LOG, FileOpenMode::WRITE);
 #endif // CROWN_PLATFORM_ANDROID
 
-	CE_LOGI("Initializing Crown Engine %s...", version());
+		CE_LOGI("Initializing Crown Engine %s...", version());
 
-	_resource_loader  = CE_NEW(_allocator, ResourceLoader)(*_bundle_filesystem);
-	_resource_manager = CE_NEW(_allocator, ResourceManager)(*_resource_loader);
+		_resource_loader  = CE_NEW(_allocator, ResourceLoader)(*_bundle_filesystem);
+		_resource_manager = CE_NEW(_allocator, ResourceManager)(*_resource_loader);
 
-	read_config();
+		read_config();
 
-	_bgfx_allocator = CE_NEW(_allocator, BgfxAllocator)(default_allocator());
-	_bgfx_callback  = CE_NEW(_allocator, BgfxCallback)();
+		_bgfx_allocator = CE_NEW(_allocator, BgfxAllocator)(default_allocator());
+		_bgfx_callback  = CE_NEW(_allocator, BgfxCallback)();
 
-	_display = display::create(_allocator);
-	_window = window::create(_allocator);
-	_window->open(_device_options._window_x
-		, _device_options._window_y
-		, _config_window_w
-		, _config_window_h
-		, _device_options._parent_window
-		);
-	_window->bgfx_setup();
+		_display = display::create(_allocator);
+		_window = window::create(_allocator);
+		_window->open(_device_options._window_x
+			, _device_options._window_y
+			, _config_window_w
+			, _config_window_h
+			, _device_options._parent_window
+			);
+		_window->bgfx_setup();
 
-	bgfx::init(bgfx::RendererType::Count
-		, BGFX_PCI_ID_NONE
-		, 0
-		, _bgfx_callback
-		, _bgfx_allocator
-		);
+		bgfx::init(bgfx::RendererType::Count
+			, BGFX_PCI_ID_NONE
+			, 0
+			, _bgfx_callback
+			, _bgfx_allocator
+			);
 
-	_shader_manager   = CE_NEW(_allocator, ShaderManager)(default_allocator());
-	_material_manager = CE_NEW(_allocator, MaterialManager)(default_allocator(), *_resource_manager);
-	_input_manager    = CE_NEW(_allocator, InputManager)(default_allocator());
-	_unit_manager     = CE_NEW(_allocator, UnitManager)(default_allocator());
-	_lua_environment  = CE_NEW(_allocator, LuaEnvironment)();
+		_shader_manager   = CE_NEW(_allocator, ShaderManager)(default_allocator());
+		_material_manager = CE_NEW(_allocator, MaterialManager)(default_allocator(), *_resource_manager);
+		_input_manager    = CE_NEW(_allocator, InputManager)(default_allocator());
+		_unit_manager     = CE_NEW(_allocator, UnitManager)(default_allocator());
+		_lua_environment  = CE_NEW(_allocator, LuaEnvironment)();
 
-	audio_globals::init();
-	physics_globals::init(_allocator);
+		audio_globals::init();
+		physics_globals::init(_allocator);
 
-	_boot_package = create_resource_package(_boot_package_name);
-	_boot_package->load();
-	_boot_package->flush();
+		_boot_package = create_resource_package(_boot_package_name);
+		_boot_package->load();
+		_boot_package->flush();
 
-	_lua_environment->load_libs();
-	_lua_environment->execute((LuaResource*)_resource_manager->get(RESOURCE_TYPE_SCRIPT, _boot_script_name));
-	_lua_environment->call_global("init", 0);
+		_lua_environment->load_libs();
+		_lua_environment->execute((LuaResource*)_resource_manager->get(RESOURCE_TYPE_SCRIPT, _boot_script_name));
+		_lua_environment->call_global("init", 0);
 
-	_last_time = os::clocktime();
+		_last_time = os::clocktime();
 
-	CE_LOGD("Engine initialized");
+		CE_LOGD("Engine initialized");
 
-	while (!process_events() && !_quit)
-	{
-		_current_time = os::clocktime();
-		const s64 time = _current_time - _last_time;
-		_last_time = _current_time;
-		const f64 freq = (f64) os::clockfrequency();
-		_last_delta_time = f32(time * (1.0 / freq));
-		_time_since_start += _last_delta_time;
+		while (!process_events() && !_quit)
+		{
+			_current_time = os::clocktime();
+			const s64 time = _current_time - _last_time;
+			_last_time = _current_time;
+			const f64 freq = (f64) os::clockfrequency();
+			_last_delta_time = f32(time * (1.0 / freq));
+			_time_since_start += _last_delta_time;
 
-		profiler_globals::clear();
-		_console_server->update();
+			profiler_globals::clear();
+			_console_server->update();
 
-		RECORD_FLOAT("device.dt", _last_delta_time);
-		RECORD_FLOAT("device.fps", 1.0f/_last_delta_time);
+			RECORD_FLOAT("device.dt", _last_delta_time);
+			RECORD_FLOAT("device.fps", 1.0f/_last_delta_time);
 
-		if (!_paused)
-		{
-			_resource_manager->complete_requests();
-			_lua_environment->call_global("update", 1, ARGUMENT_FLOAT, last_delta_time());
-			_lua_environment->call_global("render", 1, ARGUMENT_FLOAT, last_delta_time());
-		}
+			if (!_paused)
+			{
+				_resource_manager->complete_requests();
+				_lua_environment->call_global("update", 1, ARGUMENT_FLOAT, last_delta_time());
+				_lua_environment->call_global("render", 1, ARGUMENT_FLOAT, last_delta_time());
+			}
 
-		_input_manager->update();
+			_input_manager->update();
 
-		const bgfx::Stats* stats = bgfx::getStats();
-		RECORD_FLOAT("bgfx.gpu_time", f64(stats->gpuTimeEnd - stats->gpuTimeBegin)*1000.0/stats->gpuTimerFreq);
-		RECORD_FLOAT("bgfx.cpu_time", f64(stats->cpuTimeEnd - stats->cpuTimeBegin)*1000.0/stats->cpuTimerFreq);
+			const bgfx::Stats* stats = bgfx::getStats();
+			RECORD_FLOAT("bgfx.gpu_time", f64(stats->gpuTimeEnd - stats->gpuTimeBegin)*1000.0/stats->gpuTimerFreq);
+			RECORD_FLOAT("bgfx.cpu_time", f64(stats->cpuTimeEnd - stats->cpuTimeBegin)*1000.0/stats->cpuTimerFreq);
 
-		bgfx::frame();
-		profiler_globals::flush();
+			bgfx::frame();
+			profiler_globals::flush();
 
-		_lua_environment->reset_temporaries();
+			_lua_environment->reset_temporaries();
 
-		_frame_count++;
-	}
+			_frame_count++;
+		}
 
-	_lua_environment->call_global("shutdown", 0);
+		_lua_environment->call_global("shutdown", 0);
 
-	_boot_package->unload();
-	destroy_resource_package(*_boot_package);
+		_boot_package->unload();
+		destroy_resource_package(*_boot_package);
 
-	physics_globals::shutdown(_allocator);
-	audio_globals::shutdown();
+		physics_globals::shutdown(_allocator);
+		audio_globals::shutdown();
 
-	CE_DELETE(_allocator, _lua_environment);
-	CE_DELETE(_allocator, _unit_manager);
-	CE_DELETE(_allocator, _input_manager);
-	CE_DELETE(_allocator, _material_manager);
-	CE_DELETE(_allocator, _shader_manager);
-	CE_DELETE(_allocator, _resource_manager);
-	CE_DELETE(_allocator, _resource_loader);
+		CE_DELETE(_allocator, _lua_environment);
+		CE_DELETE(_allocator, _unit_manager);
+		CE_DELETE(_allocator, _input_manager);
+		CE_DELETE(_allocator, _material_manager);
+		CE_DELETE(_allocator, _shader_manager);
+		CE_DELETE(_allocator, _resource_manager);
+		CE_DELETE(_allocator, _resource_loader);
 
-	bgfx::shutdown();
-	window::destroy(_allocator, *_window);
-	display::destroy(_allocator, *_display);
-	CE_DELETE(_allocator, _bgfx_callback);
-	CE_DELETE(_allocator, _bgfx_allocator);
+		bgfx::shutdown();
+		window::destroy(_allocator, *_window);
+		display::destroy(_allocator, *_display);
+		CE_DELETE(_allocator, _bgfx_callback);
+		CE_DELETE(_allocator, _bgfx_allocator);
 
-	if (_last_log)
-	{
-		_bundle_filesystem->close(*_last_log);
+		if (_last_log)
+		{
+			_bundle_filesystem->close(*_last_log);
+		}
+
+		CE_DELETE(_allocator, _bundle_filesystem);
 	}
 
-	CE_DELETE(_allocator, _bundle_filesystem);
+	CE_DELETE(_allocator, _bundle_compiler);
 
 	_console_server->shutdown();
 	CE_DELETE(_allocator, _console_server);
@@ -463,6 +481,11 @@ ConsoleServer* Device::console_server()
 	return _console_server;
 }
 
+BundleCompiler* Device::bundle_compiler()
+{
+	return _bundle_compiler;
+}
+
 ResourceManager* Device::resource_manager()
 {
 	return _resource_manager;

+ 5 - 3
src/device/device.h

@@ -6,6 +6,7 @@
 #pragma once
 
 #include "allocator.h"
+#include "compiler_types.h"
 #include "config.h"
 #include "console_server.h"
 #include "container_types.h"
@@ -38,6 +39,7 @@ class Device
 
 	const DeviceOptions& _device_options;
 	ConsoleServer* _console_server;
+	BundleCompiler* _bundle_compiler;
 	Filesystem* _bundle_filesystem;
 	File* _last_log;
 	ResourceLoader* _resource_loader;
@@ -125,9 +127,6 @@ public:
 	/// Returns the main window resolution.
 	void resolution(u16& width, u16& height);
 
-	/// Updates all the subsystems.
-	void update();
-
 	/// Renders @a world using @a camera.
 	void render(World& world, CameraInstance camera);
 
@@ -155,6 +154,9 @@ public:
 	/// Returns the console server.
 	ConsoleServer* console_server();
 
+	/// Returns the bundle compiler.
+	BundleCompiler* bundle_compiler();
+
 	/// Returns the resource manager.
 	ResourceManager* resource_manager();
 

+ 4 - 17
src/device/main_linux.cpp

@@ -7,13 +7,14 @@
 
 #if CROWN_PLATFORM_LINUX
 
-#include "bundle_compiler.h"
+#include "array.h"
 #include "device.h"
 #include "display.h"
 #include "os_event_queue.h"
 #include "thread.h"
 #include "window.h"
 #include <stdlib.h>
+#include <string.h> // memset
 #include <X11/extensions/Xrandr.h>
 #include <X11/Xatom.h>
 #include <X11/XKBlib.h>
@@ -719,26 +720,12 @@ int main(int argc, char** argv)
 	memory_globals::init();
 
 	DeviceOptions opts(argc, argv);
-
 	int exitcode = opts.parse();
+
 	if (exitcode == EXIT_FAILURE)
-	{
 		return exitcode;
-	}
-
-	bool do_continue = true;
-
-	if (opts._do_compile)
-	{
-		bundle_compiler_globals::init(opts._source_dir, opts._bundle_dir);
-		do_continue = bundle_compiler::main(opts._do_compile, opts._do_continue, opts._platform);
-	}
-
-	if (do_continue)
-		exitcode = crown::s_ldvc.run(&opts);
 
-	if (opts._do_compile)
-		bundle_compiler_globals::shutdown();
+	exitcode = crown::s_ldvc.run(&opts);
 
 	memory_globals::shutdown();
 	return exitcode;

+ 2 - 17
src/device/main_windows.cpp

@@ -7,7 +7,6 @@
 
 #if CROWN_PLATFORM_WINDOWS
 
-#include "bundle_compiler.h"
 #include "device.h"
 #include "os_event_queue.h"
 #include "thread.h"
@@ -620,26 +619,12 @@ int main(int argc, char** argv)
 	CE_UNUSED(err);
 
 	DeviceOptions opts(argc, argv);
-
 	int exitcode = opts.parse();
+
 	if (exitcode == EXIT_FAILURE)
-	{
 		return exitcode;
-	}
-
-	bool do_continue = true;
-
-	if (opts._do_compile)
-	{
-		bundle_compiler_globals::init(opts._source_dir, opts._bundle_dir);
-		do_continue = bundle_compiler::main(opts._do_compile, opts._do_continue, opts._platform);
-	}
-
-	if (do_continue)
-		exitcode = crown::s_wdvc.run(&opts);
 
-	if (opts._do_compile)
-		bundle_compiler_globals::shutdown();
+	exitcode = crown::s_ldvc.run(&opts);
 
 	memory_globals::shutdown();
 	return exitcode;

+ 0 - 39
src/resource/bundle_compiler.cpp

@@ -289,43 +289,4 @@ void BundleCompiler::scan_source_dir(const char* cur_dir)
 	}
 }
 
-namespace bundle_compiler
-{
-	bool main(bool do_compile, bool do_continue, const char* platform)
-	{
-		bool can_proceed = true;
-
-		if (do_compile)
-		{
-			bool success = bundle_compiler_globals::compiler()->compile_all(platform);
-			can_proceed = !(!success || !do_continue);
-		}
-
-		return can_proceed;
-	}
-} // namespace bundle_compiler
-
-namespace bundle_compiler_globals
-{
-	char _buffer[sizeof(BundleCompiler)];
-	BundleCompiler* _compiler = NULL;
-
-	void init(const char* source_dir, const char* bundle_dir)
-	{
-#if CROWN_PLATFORM_LINUX || CROWN_PLATFORM_WINDOWS
-		_compiler = new (_buffer) BundleCompiler(source_dir, bundle_dir);
-#endif
-	}
-
-	void shutdown()
-	{
-		_compiler->~BundleCompiler();
-		_compiler = NULL;
-	}
-
-	BundleCompiler* compiler()
-	{
-		return _compiler;
-	}
-} // namespace bundle_compiler_globals
 } // namespace crown

+ 0 - 18
src/resource/bundle_compiler.h

@@ -47,22 +47,4 @@ public:
 	void scan_source_dir(const char* path);
 };
 
-namespace bundle_compiler
-{
-	bool main(bool do_compile, bool do_continue, const char* platform);
-} // namespace bundle_compiler
-
-namespace bundle_compiler_globals
-{
-	/// Creates the global resource compiler.
-	void init(const char* source_dir, const char* bundle_dir);
-
-	/// Destroys the global resource compiler.
-	void shutdown();
-
-	/// Returns the global resource compiler.
-	/// Returns NULL if the compiler is not available on the
-	/// running platform.
-	BundleCompiler* compiler();
-} // namespace bundle_compiler_globals
 } // namespace crown

+ 1 - 0
src/resource/compiler_types.h

@@ -7,6 +7,7 @@
 
 namespace crown
 {
+class BundleCompiler;
 class CompileOptions;
 
 } // namespace crown