Prechádzať zdrojové kódy

Map string platform to enum

Daniele Bartolini 11 rokov pred
rodič
commit
80a298abc1

+ 2 - 2
engine/compilers/bundle_compiler.cpp

@@ -51,7 +51,7 @@ BundleCompiler::BundleCompiler(const char* source_dir, const char* bundle_dir)
 }
 
 //-----------------------------------------------------------------------------
-bool BundleCompiler::compile(const char* type, const char* name, const char* platform)
+bool BundleCompiler::compile(const char* type, const char* name, Platform::Enum platform)
 {
 	const ResourceId id(type, name);
 	char out_name[512];
@@ -68,7 +68,7 @@ bool BundleCompiler::compile(const char* type, const char* name, const char* pla
 }
 
 //-----------------------------------------------------------------------------
-bool BundleCompiler::compile_all(const char* platform)
+bool BundleCompiler::compile_all(Platform::Enum platform)
 {
 	Vector<DynamicString> files(default_allocator());
 	BundleCompiler::scan("", files);

+ 2 - 2
engine/compilers/bundle_compiler.h

@@ -39,11 +39,11 @@ public:
 
 	BundleCompiler(const char* source_dir, const char* bundle_dir);
 
-	bool compile(const char* type, const char* name, const char* platform = "linux");
+	bool compile(const char* type, const char* name, Platform::Enum 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(const char* platform);
+	bool compile_all(Platform::Enum platform);
 
 	void scan(const char* cur_dir, Vector<DynamicString>& files);
 

+ 14 - 4
engine/compilers/compile_options.h

@@ -28,6 +28,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #include "filesystem.h"
 #include "reader_writer.h"
+#include "crown.h"
 
 namespace crown
 {
@@ -36,7 +37,7 @@ typedef Array<char> Buffer;
 
 struct CompileOptions
 {
-	CompileOptions(Filesystem& fs, File* out, const char* platform)
+	CompileOptions(Filesystem& fs, File* out, Platform::Enum platform)
 		: _fs(fs)
 		, _bw(*out)
 		, _platform(platform)
@@ -64,9 +65,10 @@ struct CompileOptions
 		_fs.delete_file(path);
 	}
 
-	void write(const void* data, size_t size)
+	BinaryWriter& write(const void* data, size_t size)
 	{
 		_bw.write(data, size);
+		return _bw;
 	}
 
 	template <typename T>
@@ -76,14 +78,22 @@ struct CompileOptions
 		return _bw;
 	}
 
-	const char* platform() const
+	BinaryWriter& write(const Buffer& data)
+	{
+		if (array::size(data))
+			_bw.write(array::begin(data), array::size(data));
+
+		return _bw;
+	}
+
+	Platform::Enum platform() const
 	{
 		return _platform;
 	}
 
 	Filesystem& _fs;
 	BinaryWriter _bw;
-	const char* _platform;
+	Platform::Enum _platform;
 };
 
 } // namespace crown

+ 28 - 5
engine/crown.cpp

@@ -45,6 +45,30 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 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 (string::strcmp(platform, s_platform[i].name) == 0)
+			return s_platform[i].target;
+	}
+	return Platform::COUNT;
+}
+
 static void help(const char* msg = NULL)
 {
 	if (msg)
@@ -68,8 +92,8 @@ static void help(const char* msg = NULL)
 		"  --platform <platform>      Compile resources for the given <platform>.\n"
 		"      Possible values for <platform> are:\n"
 		"          linux\n"
-		"          android\n"
 		"          windows\n"
+		"          android\n"
 		"  --continue                 Continue the execution after the resource compilation step.\n"
 		"  --host                     Read resources from a remote engine instance.\n"
 		"  --wait-console             Wait for a console connection before starting up.\n"
@@ -81,7 +105,7 @@ CommandLineSettings parse_command_line(int argc, char** argv)
 	CommandLineSettings cls;
 	cls.source_dir = NULL;
 	cls.bundle_dir = NULL;
-	cls.platform = NULL;
+	cls.platform = Platform::COUNT;
 	cls.wait_console = false;
 	cls.do_compile = false;
 	cls.do_continue = false;
@@ -109,13 +133,12 @@ CommandLineSettings parse_command_line(int argc, char** argv)
 		exit(EXIT_FAILURE);
 	}
 
-	cls.platform = cmd.get_parameter("platform");
 	cls.wait_console = cmd.has_argument("wait-console");
 	cls.do_compile = cmd.has_argument("compile");
 	cls.do_continue = cmd.has_argument("continue");
 
-	cls.platform = cmd.get_parameter("platform");
-	if (cls.do_compile && !cls.platform)
+	cls.platform = string_to_platform(cmd.get_parameter("platform"));
+	if (cls.do_compile && cls.platform == Platform::COUNT)
 	{
 		help("Platform must be specified.");
 		exit(EXIT_FAILURE);

+ 13 - 1
engine/crown.h

@@ -30,6 +30,18 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 namespace crown
 {
+	struct Platform
+	{
+		enum Enum
+		{
+			LINUX = 0,
+			WINDOWS = 1,
+			ANDROID = 2,
+
+			COUNT
+		};
+	};
+
 	struct ConfigSettings
 	{
 		uint16_t console_port;
@@ -43,7 +55,7 @@ namespace crown
 	{
 		const char* source_dir;
 		const char* bundle_dir;
-		const char* platform;
+		Platform::Enum platform;
 		bool wait_console;
 		bool do_compile;
 		bool do_continue;

+ 9 - 2
engine/renderers/shader.cpp

@@ -35,6 +35,13 @@ namespace crown
 {
 namespace shader_resource
 {
+	static const char* s_scplatform[Platform::COUNT] =
+	{
+		"linux",
+		"windows",
+		"android"
+	};
+
 	//-----------------------------------------------------------------------------
 	void compile(const char* path, CompileOptions& opts)
 	{
@@ -69,7 +76,7 @@ namespace shader_resource
 			"-o", tmpvs_path.c_str(),
 			"--varyingdef", varying_def_path.c_str(),
 			"--type", "vertex",
-			"--platform", opts.platform(),
+			"--platform", s_scplatform[opts.platform()],
 #if CROWN_PLATFORM_WINDOWS
 			"--profile", "vs_3_0",
 #endif
@@ -84,7 +91,7 @@ namespace shader_resource
 			"-o", tmpfs_path.c_str(),
 			"--varyingdef", varying_def_path.c_str(),
 			"--type", "fragment",
-			"--platform", opts.platform(),
+			"--platform", s_scplatform[opts.platform()],
 #if CROWN_PLATFORM_WINDOWS
 			"--profile", "ps_3_0",
 #endif