Daniele Bartolini 10 лет назад
Родитель
Сommit
e9872cab66

+ 3 - 3
src/compilers/bundle_compiler.cpp

@@ -27,7 +27,7 @@ BundleCompiler::BundleCompiler(const char* source_dir, const char* bundle_dir)
 	temp.create_directory(bundle_dir);
 }
 
-bool BundleCompiler::compile(const char* type, const char* name, Platform::Enum platform)
+bool BundleCompiler::compile(const char* type, const char* name, const char* platform)
 {
 	StringId64 _type(type);
 	StringId64 _name(name);
@@ -57,7 +57,7 @@ bool BundleCompiler::compile(const char* type, const char* name, Platform::Enum
 	return true;
 }
 
-bool BundleCompiler::compile_all(Platform::Enum platform)
+bool BundleCompiler::compile_all(const char* platform)
 {
 	Vector<DynamicString> files(default_allocator());
 	BundleCompiler::scan("", files);
@@ -132,7 +132,7 @@ void BundleCompiler::scan(const char* cur_dir, Vector<DynamicString>& files)
 
 namespace bundle_compiler
 {
-	bool main(bool do_compile, bool do_continue, Platform::Enum platform)
+	bool main(bool do_compile, bool do_continue, const char* platform)
 	{
 		if (do_compile)
 		{

+ 3 - 3
src/compilers/bundle_compiler.h

@@ -18,11 +18,11 @@ public:
 
 	BundleCompiler(const char* source_dir, const char* bundle_dir);
 
-	bool compile(const char* type, const char* name, Platform::Enum platform);
+	bool compile(const char* type, const char* name, const char* platform);
 
 	/// Compiles all the resources found in @a source_dir and puts them in @a bundle_dir.
 	/// Returns true on success, false otherwise.
-	bool compile_all(Platform::Enum platform);
+	bool compile_all(const char* platform);
 
 	void scan(const char* cur_dir, Vector<DynamicString>& files);
 
@@ -34,7 +34,7 @@ private:
 
 namespace bundle_compiler
 {
-	bool main(bool do_compile, bool do_continue, Platform::Enum platform);
+	bool main(bool do_compile, bool do_continue, const char* platform);
 } // namespace bundle_compiler
 
 namespace bundle_compiler_globals

+ 3 - 3
src/compilers/compile_options.h

@@ -16,7 +16,7 @@ typedef Array<char> Buffer;
 
 struct CompileOptions
 {
-	CompileOptions(Filesystem& fs, File* out, Platform::Enum platform)
+	CompileOptions(Filesystem& fs, File* out, const char* platform)
 		: _fs(fs)
 		, _bw(*out)
 		, _platform(platform)
@@ -65,14 +65,14 @@ struct CompileOptions
 		return _bw;
 	}
 
-	Platform::Enum platform() const
+	const char* platform() const
 	{
 		return _platform;
 	}
 
 	Filesystem& _fs;
 	BinaryWriter _bw;
-	Platform::Enum _platform;
+	const char* _platform;
 };
 
 } // namespace crown

+ 0 - 6
src/core/strings/path.cpp

@@ -12,12 +12,6 @@ namespace crown
 {
 namespace path
 {
-#if CROWN_PLATFORM_POSIX
-	const char SEPARATOR = '/';
-#elif CROWN_PLATFORM_WINDOWS
-	const char SEPARATOR = '\\';
-#endif // CROWN_PLATFORM_POSIX
-
 	bool is_absolute_path(const char* path)
 	{
 		CE_ASSERT(path != NULL, "Path must be != NULL");

+ 6 - 0
src/core/strings/path.h

@@ -17,6 +17,12 @@ namespace crown
 /// @ingroup Path
 namespace path
 {
+#if CROWN_PLATFORM_POSIX
+	const char SEPARATOR = '/';
+#elif CROWN_PLATFORM_WINDOWS
+	const char SEPARATOR = '\\';
+#endif // CROWN_PLATFORM_POSIX
+
 	/// Returns whether the @a path is absolute.
 	bool is_absolute_path(const char* path);
 

+ 2 - 126
src/crown.cpp

@@ -23,29 +23,6 @@
 namespace crown
 {
 
-struct PlatformInfo
-{
-	const char* name;
-	Platform::Enum target;
-};
-
-static const PlatformInfo s_platform[Platform::COUNT] =
-{
-	{ "linux",   Platform::LINUX   },
-	{ "windows", Platform::WINDOWS },
-	{ "android", Platform::ANDROID }
-};
-
-static Platform::Enum string_to_platform(const char* platform)
-{
-	for (uint32_t i = 0; platform != NULL && i < Platform::COUNT; i++)
-	{
-		if (strcmp(platform, s_platform[i].name) == 0)
-			return s_platform[i].target;
-	}
-	return Platform::COUNT;
-}
-
 static void help(const char* msg = NULL)
 {
 	if (msg)
@@ -79,114 +56,13 @@ static void help(const char* msg = NULL)
 	);
 }
 
-void parse_command_line(int argc, char** argv, ConfigSettings& cs)
-{
-	CommandLine cmd(argc, argv);
-
-	if (cmd.has_argument("help", 'h'))
-	{
-		help();
-		exit(EXIT_SUCCESS);
-	}
-
-	if (cmd.has_argument("version", 'v'))
-	{
-		printf(CROWN_PLATFORM_NAME "-" CROWN_CPU_NAME " (" CROWN_ARCH_NAME ")" " (" CROWN_COMPILER_NAME ")\n");
-		exit(EXIT_SUCCESS);
-	}
-
-	cs.source_dir = cmd.get_parameter("source-dir");
-	if (!cs.source_dir)
-	{
-		help("Source directory must be specified.");
-		exit(EXIT_FAILURE);
-	}
-
-	cs.bundle_dir = cmd.get_parameter("bundle-dir");
-	if (!cs.bundle_dir)
-	{
-		help("Bundle directory must be specified.");
-		exit(EXIT_FAILURE);
-	}
-
-	if (strcmp(cs.source_dir, cs.bundle_dir) == 0)
-	{
-		help("Source and Bundle directories must differ.");
-		exit(EXIT_FAILURE);
-	}
-
-	cs.project = cmd.get_parameter("project");
-
-	cs.wait_console = cmd.has_argument("wait-console");
-	cs.do_compile = cmd.has_argument("compile");
-	cs.do_continue = cmd.has_argument("continue");
-
-	cs.platform = string_to_platform(cmd.get_parameter("platform"));
-	if (cs.do_compile && cs.platform == Platform::COUNT)
-	{
-		help("Platform must be specified.");
-		exit(EXIT_FAILURE);
-	}
-
-	const char* parent = cmd.get_parameter("parent-window");
-	if (parent)
-	{
-		cs.parent_window = parse_uint(parent);
-	}
-
-	const char* port = cmd.get_parameter("console-port");
-	if (port)
-	{
-		cs.console_port = parse_uint(port);
-	}
-}
-
-void parse_config_file(Filesystem& fs, ConfigSettings& cs)
-{
-	TempAllocator512 alloc;
-	DynamicString project_path(alloc);
-
-	if (cs.project != NULL)
-	{
-		project_path += cs.project;
-		project_path += "/";
-	}
-	project_path += "crown.config";
-
-	File* tmpfile = fs.open(project_path.c_str(), FOM_READ);
-	JSONParser config(*tmpfile);
-	fs.close(tmpfile);
-	JSONElement root = config.root();
-
-	JSONElement cport = root.key_or_nil("console_port");
-	if (!cport.is_nil())
-	{
-		cs.console_port = (int16_t) cport.to_int();
-	}
-
-	JSONElement window_width = root.key_or_nil("window_width");
-	if (!window_width.is_nil())
-	{
-		cs.window_width = max((uint16_t)1, (uint16_t)window_width.to_int());
-	}
-
-	JSONElement window_height = root.key_or_nil("window_height");
-	if (!window_height.is_nil())
-	{
-		cs.window_height = max((uint16_t)1, (uint16_t)window_height.to_int());
-	}
-
-	cs.boot_script = root.key("boot_script").to_resource_id();
-	cs.boot_package = root.key("boot_package").to_resource_id();
-}
-
-bool init(Filesystem& fs, const ConfigSettings& cs)
+bool init(const DeviceOptions& opts, Filesystem& fs)
 {
 	profiler_globals::init();
 	audio_globals::init();
 	physics_globals::init();
 	bgfx::init();
-	device_globals::init(cs, fs);
+	device_globals::init(opts, fs);
 	device()->init();
 	return true;
 }

+ 2 - 52
src/crown.h

@@ -7,62 +7,12 @@
 
 #include "types.h"
 #include "filesystem_types.h"
+#include "device_options.h"
 
 namespace crown
 {
-	struct Platform
-	{
-		enum Enum
-		{
-			LINUX,
-			WINDOWS,
-			ANDROID,
-
-			COUNT
-		};
-	};
-
-	struct ConfigSettings
-	{
-		ConfigSettings()
-			: source_dir(NULL)
-			, bundle_dir(NULL)
-			, project(NULL)
-			, platform(Platform::COUNT)
-			, wait_console(false)
-			, do_compile(false)
-			, do_continue(false)
-			, parent_window(0)
-			, console_port(CROWN_DEFAULT_CONSOLE_PORT)
-			, boot_package(uint64_t(0))
-			, boot_script(uint64_t(0))
-			, window_width(CROWN_DEFAULT_WINDOW_WIDTH)
-			, window_height(CROWN_DEFAULT_WINDOW_HEIGHT)
-		{
-		}
-
-		const char* source_dir;
-		const char* bundle_dir;
-		const char* project;
-		Platform::Enum platform;
-		bool wait_console;
-		bool do_compile;
-		bool do_continue;
-		uint32_t parent_window;
-		uint16_t console_port;
-		StringId64 boot_package;
-		StringId64 boot_script;
-		uint16_t window_width;
-		uint16_t window_height;
-	};
-
-	void parse_command_line(int argc, char** argv, ConfigSettings& cs);
-
-	/// Read configuration file from @a fs.
-	void parse_config_file(Filesystem& fs, ConfigSettings& cs);
-
 	/// Initializes the engine.
-	bool init(Filesystem& fs, const ConfigSettings& cs);
+	bool init(const DeviceOptions& opts, Filesystem& fs);
 
 	/// Updates all the subsystems.
 	void update();

+ 61 - 21
src/device.cpp

@@ -17,12 +17,16 @@
 #include "resource_package.h"
 #include "types.h"
 #include "world.h"
+#include "json_parser.h"
+#include "filesystem.h"
+#include "path.h"
 
 #define MAX_SUBSYSTEMS_HEAP 8 * 1024 * 1024
 
 namespace crown
 {
-Device::Device(const ConfigSettings& cs, Filesystem& fs)
+
+Device::Device(const DeviceOptions& opts, Filesystem& fs)
 	: _allocator(default_allocator(), MAX_SUBSYSTEMS_HEAP)
 	, _width(0)
 	, _height(0)
@@ -34,10 +38,10 @@ Device::Device(const ConfigSettings& cs, Filesystem& fs)
 	, _current_time(0)
 	, _last_delta_time(0.0f)
 	, _time_since_start(0.0)
-	, _cs(cs)
+	, _device_options(opts)
 	, _fs(fs)
-	, _boot_package_id(cs.boot_package)
-	, _boot_script_id(cs.boot_script)
+	, _boot_package_id(uint64_t(0))
+	, _boot_script_id(uint64_t(0))
 	, _boot_package(NULL)
 	, _lua_environment(NULL)
 	, _resource_manager(NULL)
@@ -51,6 +55,8 @@ void Device::init()
 	// Initialize
 	CE_LOGI("Initializing Crown Engine %s...", version());
 
+	read_config();
+
 	// Create resource manager
 	CE_LOGD("Creating resource manager...");
 	_resource_manager = CE_NEW(_allocator, ResourceManager)(_fs);
@@ -104,21 +110,6 @@ void Device::shutdown()
 	_is_init = false;
 }
 
-ResourceManager* Device::resource_manager()
-{
-	return _resource_manager;
-}
-
-LuaEnvironment* Device::lua_environment()
-{
-	return _lua_environment;
-}
-
-InputManager* Device::input_manager()
-{
-	return _input_manager;
-}
-
 void Device::quit()
 {
 	_is_running = false;
@@ -136,6 +127,18 @@ void Device::unpause()
 	CE_LOGI("Engine unpaused.");
 }
 
+void Device::update_resolution(uint16_t width, uint16_t height)
+{
+	_width = width;
+	_height = height;
+}
+
+void Device::resolution(uint16_t& width, uint16_t& height)
+{
+	width = _width;
+	height = _height;
+}
+
 bool Device::is_running() const
 {
 	return _is_running;
@@ -228,15 +231,52 @@ void Device::reload(StringId64 type, StringId64 name)
 	}
 }
 
+ResourceManager* Device::resource_manager()
+{
+	return _resource_manager;
+}
+
+LuaEnvironment* Device::lua_environment()
+{
+	return _lua_environment;
+}
+
+InputManager* Device::input_manager()
+{
+	return _input_manager;
+}
+
+void Device::read_config()
+{
+	TempAllocator1024 ta;
+	DynamicString project_path(ta);
+
+	if (_device_options.project() != NULL)
+	{
+		project_path += _device_options.project();
+		project_path += path::SEPARATOR;
+	}
+
+	project_path += "crown.config";
+
+	File* tmpfile = _fs.open(project_path.c_str(), FOM_READ);
+	JSONParser config(*tmpfile);
+	_fs.close(tmpfile);
+	JSONElement root = config.root();
+
+	_boot_script_id = root.key("boot_script").to_resource_id();
+	_boot_package_id = root.key("boot_package").to_resource_id();
+}
+
 namespace device_globals
 {
 	char _buffer[sizeof(Device)];
 	Device* _device = NULL;
 
-	void init(const ConfigSettings& cs, Filesystem& fs)
+	void init(const DeviceOptions& opts, Filesystem& fs)
 	{
 		CE_ASSERT(_device == NULL, "Crown already initialized");
-		_device = new (_buffer) Device(cs, fs);
+		_device = new (_buffer) Device(opts, fs);
 	}
 
 	void shutdown()

+ 10 - 14
src/device.h

@@ -14,7 +14,7 @@
 #include "filesystem_types.h"
 #include "container_types.h"
 #include "input_types.h"
-#include "crown.h"
+#include "device_options.h"
 
 namespace crown
 {
@@ -27,7 +27,7 @@ namespace crown
 /// @ingroup Device
 struct Device
 {
-	Device(const ConfigSettings& cs, Filesystem& fs);
+	Device(const DeviceOptions& opts, Filesystem& fs);
 
 	void init();
 
@@ -65,18 +65,10 @@ struct Device
 	/// Unpauses the engine.
 	void unpause();
 
-	void update_resolution(uint16_t width, uint16_t height)
-	{
-		_width = width;
-		_height = height;
-	}
+	void update_resolution(uint16_t width, uint16_t height);
 
 	/// Returns the main window resolution.
-	void resolution(uint16_t& width, uint16_t& height)
-	{
-		width = _width;
-		height = _height;
-	}
+	void resolution(uint16_t& width, uint16_t& height);
 
 	/// Updates all the subsystems.
 	void update();
@@ -111,6 +103,10 @@ struct Device
 	/// Returns the input manager.
 	InputManager* input_manager();
 
+private:
+
+	void read_config();
+
 private:
 
 	// Used to allocate all subsystems
@@ -129,7 +125,7 @@ private:
 	float _last_delta_time;
 	double _time_since_start;
 
-	const ConfigSettings& _cs;
+	const DeviceOptions& _device_options;
 	Filesystem& _fs;
 	StringId64 _boot_package_id;
 	StringId64 _boot_script_id;
@@ -150,7 +146,7 @@ private:
 
 namespace device_globals
 {
-	void init(const ConfigSettings& cs, Filesystem& fs);
+	void init(const DeviceOptions& opts, Filesystem& fs);
 	void shutdown();
 } // namespace device_globals
 

+ 48 - 0
src/device_options.cpp

@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
+ * License: https://github.com/taylor001/crown/blob/master/LICENSE
+ */
+
+#include "device_options.h"
+#include "command_line.h"
+
+namespace crown
+{
+
+DeviceOptions::DeviceOptions(int argc, char** argv)
+	: _source_dir(NULL)
+	, _bundle_dir(NULL)
+	, _project(NULL)
+	, _platform(NULL)
+	, _wait_console(false)
+	, _do_compile(false)
+	, _do_continue(false)
+	, _parent_window(0)
+	, _console_port(CROWN_DEFAULT_CONSOLE_PORT)
+	, _window_x(0)
+	, _window_y(0)
+	, _window_width(CROWN_DEFAULT_WINDOW_WIDTH)
+	, _window_height(CROWN_DEFAULT_WINDOW_HEIGHT)
+{
+	CommandLine cmd(argc, argv);
+
+	_source_dir = cmd.get_parameter("source-dir");
+	_bundle_dir = cmd.get_parameter("bundle-dir");
+	_project = cmd.get_parameter("project");
+	_platform = cmd.get_parameter("platform");
+
+	_wait_console = cmd.has_argument("wait-console");
+	_do_compile = cmd.has_argument("compile");
+	_do_continue = cmd.has_argument("continue");
+
+	const char* parent = cmd.get_parameter("parent-window");
+	if (parent)
+		_parent_window = parse_uint(parent);
+
+	const char* port = cmd.get_parameter("console-port");
+	if (port)
+		_console_port = parse_uint(port);
+}
+
+}
+

+ 48 - 0
src/device_options.h

@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
+ * License: https://github.com/taylor001/crown/blob/master/LICENSE
+ */
+
+#pragma once
+
+#include "types.h"
+
+namespace crown
+{
+
+struct DeviceOptions
+{
+	DeviceOptions(int argc, char** argv);
+
+	const char* source_dir() const { return _source_dir; }
+	const char* bundle_dir() const { return _bundle_dir; }
+	const char* project() const { return _project; }
+	const char* platform() const { return _platform; }
+	bool wait_console() const { return _wait_console; }
+	bool do_compile() const { return _do_compile; }
+	bool do_continue() const { return _do_continue; }
+	uint32_t parent_window() const { return _parent_window; }
+	uint16_t console_port() const { return _console_port; }
+	uint16_t window_x() const { return _window_x; }
+	uint16_t window_y() const { return _window_y; }
+	uint16_t window_width() const { return _window_width; }
+	uint16_t window_height() const { return _window_height; }
+
+private:
+
+	const char* _source_dir;
+	const char* _bundle_dir;
+	const char* _project;
+	const char* _platform;
+	bool _wait_console;
+	bool _do_compile;
+	bool _do_continue;
+	uint32_t _parent_window;
+	uint16_t _console_port;
+	uint16_t _window_x;
+	uint16_t _window_y;
+	uint16_t _window_width;
+	uint16_t _window_height;
+};
+
+} // namespace crown

+ 16 - 23
src/main/main_linux.cpp

@@ -166,13 +166,13 @@ static bool s_exit = false;
 struct MainThreadArgs
 {
 	Filesystem* fs;
-	ConfigSettings* cs;
+	DeviceOptions* ds;
 };
 
 int32_t func(void* data)
 {
 	MainThreadArgs* args = (MainThreadArgs*) data;
-	crown::init(*args->fs, *args->cs);
+	crown::init(*args->ds, *args->fs);
 	crown::update();
 	crown::shutdown();
 	s_exit = true;
@@ -190,7 +190,7 @@ struct LinuxDevice
 	{
 	}
 
-	int32_t run(Filesystem* fs, ConfigSettings* cs)
+	int32_t run(Filesystem* fs, DeviceOptions* ds)
 	{
 		// http://tronche.com/gui/x/xlib/display/XInitThreads.html
 		Status xs = XInitThreads();
@@ -204,8 +204,8 @@ struct LinuxDevice
 		int depth = DefaultDepth(_x11_display, screen);
 		Visual* visual = DefaultVisual(_x11_display, screen);
 
-		_x11_parent_window = (cs->parent_window == 0) ? RootWindow(_x11_display, screen) :
-			(Window) cs->parent_window;
+		_x11_parent_window = (ds->parent_window() == 0) ? RootWindow(_x11_display, screen) :
+			(Window)ds->parent_window();
 
 		// Create main window
 		XSetWindowAttributes win_attribs;
@@ -221,10 +221,10 @@ struct LinuxDevice
 
 		_x11_window = XCreateWindow(_x11_display
 			, _x11_parent_window
-			, 0
-			, 0
-			, cs->window_width
-			, cs->window_height
+			, ds->window_x()
+			, ds->window_y()
+			, ds->window_width()
+			, ds->window_height()
 			, 0
 			, depth
 			, InputOutput
@@ -264,7 +264,7 @@ struct LinuxDevice
 		// Start main thread
 		MainThreadArgs mta;
 		mta.fs = fs;
-		mta.cs = cs;
+		mta.ds = ds;
 
 		Thread main_thread;
 		main_thread.start(func, &mta);
@@ -401,29 +401,22 @@ bool next_event(OsEvent& ev)
 int main(int argc, char** argv)
 {
 	using namespace crown;
-
-	ConfigSettings cs;
-	parse_command_line(argc, argv, cs);
-
 	memory_globals::init();
-	{
-		DiskFilesystem fs(cs.source_dir);
-		parse_config_file(fs, cs);
-	}
 
-	console_server_globals::init(cs.console_port, cs.wait_console);
+	DeviceOptions opts(argc, argv);
 
-	bundle_compiler_globals::init(cs.source_dir, cs.bundle_dir);
+	console_server_globals::init(opts.console_port(), opts.wait_console());
+	bundle_compiler_globals::init(opts.source_dir(), opts.bundle_dir());
 
 	bool do_continue = true;
 	int exitcode = EXIT_SUCCESS;
 
-	do_continue = bundle_compiler::main(cs.do_compile, cs.do_continue, cs.platform);
+	do_continue = bundle_compiler::main(opts.do_compile(), opts.do_continue(), opts.platform());
 
 	if (do_continue)
 	{
-		DiskFilesystem dst_fs(cs.bundle_dir);
-		exitcode = crown::s_ldvc.run(&dst_fs, &cs);
+		DiskFilesystem dst_fs(opts.bundle_dir());
+		exitcode = crown::s_ldvc.run(&dst_fs, &opts);
 	}
 
 	bundle_compiler_globals::shutdown();

+ 2 - 9
src/renderers/shader.cpp

@@ -32,13 +32,6 @@ namespace crown
 {
 namespace shader_resource
 {
-	static const char* _scplatform[Platform::COUNT] =
-	{
-		"linux",
-		"windows",
-		"android"
-	};
-
 	void compile(const char* path, CompileOptions& opts)
 	{
 		Buffer buf = opts.read(path);
@@ -92,7 +85,7 @@ namespace shader_resource
 			"-o", tmpvs_path.c_str(),
 			"--varyingdef", varying_def_path.c_str(),
 			"--type", "vertex",
-			"--platform", _scplatform[opts.platform()],
+			"--platform", opts.platform(),
 			"--profile",
 #if CROWN_PLATFORM_LINUX
 			"120",
@@ -111,7 +104,7 @@ namespace shader_resource
 			"-o", tmpfs_path.c_str(),
 			"--varyingdef", varying_def_path.c_str(),
 			"--type", "fragment",
-			"--platform", _scplatform[opts.platform()],
+			"--platform", opts.platform(),
 			"--profile",
 #if CROWN_PLATFORM_LINUX
 			"120",