浏览代码

Merge branch 'master' into rendering-2

Daniele Bartolini 12 年之前
父节点
当前提交
4125f299cb

+ 8 - 1
src/ArchiveResourceArchive.cpp

@@ -76,7 +76,7 @@ ArchiveResourceArchive::~ArchiveResourceArchive()
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-FileStream* ArchiveResourceArchive::find(ResourceId name)
+FileStream* ArchiveResourceArchive::open(ResourceId name)
 {
 {
 	// Search the resource in the archive
 	// Search the resource in the archive
 	for (uint32_t i = 0; i < m_entries_count; i++)
 	for (uint32_t i = 0; i < m_entries_count; i++)
@@ -93,4 +93,11 @@ FileStream* ArchiveResourceArchive::find(ResourceId name)
 	return NULL;
 	return NULL;
 }
 }
 
 
+//-----------------------------------------------------------------------------
+void ArchiveResourceArchive::close(FileStream* resource)
+{
+	// Does nothing, the stream is automatically closed at exit.
+	(void)resource;
+}
+
 } // namespace crown
 } // namespace crown

+ 5 - 1
src/ArchiveResourceArchive.h

@@ -61,7 +61,11 @@ public:
 					ArchiveResourceArchive(Filesystem& fs);
 					ArchiveResourceArchive(Filesystem& fs);
 					~ArchiveResourceArchive();
 					~ArchiveResourceArchive();
 
 
-	FileStream*		find(ResourceId name);
+	/// @copydoc ResourceArchive::open()
+	FileStream*		open(ResourceId name);
+
+	/// @copydoc ResourceArchive::close()
+	void			close(FileStream* resource);
 
 
 private:
 private:
 
 

+ 6 - 2
src/FileResourceArchive.cpp

@@ -46,7 +46,7 @@ FileResourceArchive::~FileResourceArchive()
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-FileStream* FileResourceArchive::find(ResourceId name)
+FileStream* FileResourceArchive::open(ResourceId name)
 {
 {
 	// Convert name/type into strings
 	// Convert name/type into strings
 	char resource_name[512];
 	char resource_name[512];
@@ -65,8 +65,12 @@ FileStream* FileResourceArchive::find(ResourceId name)
 	file->skip(sizeof(ResourceHeader));
 	file->skip(sizeof(ResourceHeader));
 
 
 	return file;
 	return file;
+}
 
 
-	// FIXME NEED TO RELEASE MEMORY FOR file
+//-----------------------------------------------------------------------------
+void FileResourceArchive::close(FileStream* resource)
+{
+	m_filesystem.close(resource);
 }
 }
 
 
 } // namespace crown
 } // namespace crown

+ 6 - 1
src/FileResourceArchive.h

@@ -53,7 +53,12 @@ public:
 					FileResourceArchive(Filesystem& fs);
 					FileResourceArchive(Filesystem& fs);
 					~FileResourceArchive();
 					~FileResourceArchive();
 
 
-	FileStream*		find(ResourceId name);
+	/// @copydoc ResourceArchive::open()
+	FileStream*		open(ResourceId name);
+
+	/// @copydoc ResourceArchive::close()
+	void			close(FileStream* resource);
+
 
 
 private:
 private:
 
 

+ 14 - 10
src/Resource.h

@@ -33,17 +33,21 @@ namespace crown
 {
 {
 
 
 /// Hashed values for supported resource types
 /// Hashed values for supported resource types
-const char* const TEXTURE_EXTENSION		= "tga";
-const char* const MESH_EXTENSION		= "dae";
-const char* const SCRIPT_EXTENSION		= "lua";
-const char* const TEXT_EXTENSION		= "txt";
-const char* const MATERIAL_EXTENSION	= "material";
+const char* const TEXTURE_EXTENSION			= "tga";
+const char* const MESH_EXTENSION			= "dae";
+const char* const SCRIPT_EXTENSION			= "lua";
+const char* const TEXT_EXTENSION			= "txt";
+const char* const MATERIAL_EXTENSION		= "material";
+const char* const VERTEX_SHADER_EXTENSION	= "vs";
+const char* const PIXEL_SHADER_EXTENSION	= "ps";
 
 
-const uint32_t TEXTURE_TYPE		= 0x1410A16A;
-const uint32_t MESH_TYPE		= 0xE8239EEC;
-const uint32_t SCRIPT_TYPE		= 0xD96E7C37;
-const uint32_t TEXT_TYPE		= 0x9000BF0B;
-const uint32_t MATERIAL_TYPE	= 0x46807A92;
+const uint32_t TEXTURE_TYPE					= 0x1410A16A;
+const uint32_t MESH_TYPE					= 0xE8239EEC;
+const uint32_t SCRIPT_TYPE					= 0xD96E7C37;
+const uint32_t TEXT_TYPE					= 0x9000BF0B;
+const uint32_t MATERIAL_TYPE				= 0x46807A92;
+const uint32_t VERTEX_SHADER_TYPE			= 0xDC7F061F;
+const uint32_t PIXEL_SHADER_TYPE			= 0x2A461B45;
 
 
 /// Enumerates the loading states of a resource
 /// Enumerates the loading states of a resource
 enum ResourceState
 enum ResourceState

+ 10 - 1
src/ResourceArchive.h

@@ -57,7 +57,16 @@ public:
 
 
 	virtual					~ResourceArchive() {}
 	virtual					~ResourceArchive() {}
 
 
-	virtual FileStream*		find(ResourceId name) = 0;
+	/// Opens the resource file containing @name resource
+	/// and returns a stream from which read the data from.
+	/// @note
+	/// The resource stream points exactly at the start
+	/// of the useful resource data, so you do not have to
+	/// care about skipping headers, metadatas and so on.
+	virtual FileStream*		open(ResourceId name) = 0;
+
+	/// Closes the resource file.
+	virtual void			close(FileStream* resource) = 0;
 };
 };
 
 
 } // namespace crown
 } // namespace crown

+ 3 - 1
src/ScriptResource.cpp

@@ -12,7 +12,7 @@ namespace crown
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 void* ScriptResource::load(Allocator& allocator, ResourceArchive& archive, ResourceId id)
 void* ScriptResource::load(Allocator& allocator, ResourceArchive& archive, ResourceId id)
 {
 {
-	FileStream* stream = archive.find(id);
+	FileStream* stream = archive.open(id);
 
 
 	if (stream != NULL)
 	if (stream != NULL)
 	{
 	{
@@ -24,6 +24,8 @@ void* ScriptResource::load(Allocator& allocator, ResourceArchive& archive, Resou
 
 
 		stream->read(resource->m_data, size);
 		stream->read(resource->m_data, size);
 
 
+		archive.close(stream);
+
 		return resource;
 		return resource;
 	}
 	}
 
 

+ 3 - 1
src/TextResource.cpp

@@ -10,7 +10,7 @@ namespace crown
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 void* TextResource::load(Allocator& allocator, ResourceArchive& archive, ResourceId id)
 void* TextResource::load(Allocator& allocator, ResourceArchive& archive, ResourceId id)
 {
 {
-	FileStream* stream = archive.find(id);
+	FileStream* stream = archive.open(id);
 
 
 	if (stream != NULL)
 	if (stream != NULL)
 	{
 	{
@@ -24,6 +24,8 @@ void* TextResource::load(Allocator& allocator, ResourceArchive& archive, Resourc
 		
 		
 		resource->data[resource->length] = '\0';
 		resource->data[resource->length] = '\0';
 
 
+		archive.close(stream);
+
 		return resource;
 		return resource;
 	}
 	}
 
 

+ 3 - 1
src/TextureResource.cpp

@@ -13,7 +13,7 @@ namespace crown
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
 void* TextureResource::load(Allocator& allocator, ResourceArchive& archive, ResourceId id)
 void* TextureResource::load(Allocator& allocator, ResourceArchive& archive, ResourceId id)
 {
 {
-	FileStream* stream = archive.find(id);
+	FileStream* stream = archive.open(id);
 
 
 	if (stream != NULL)
 	if (stream != NULL)
 	{
 	{
@@ -29,6 +29,8 @@ void* TextureResource::load(Allocator& allocator, ResourceArchive& archive, Reso
 
 
 		stream->read(resource->m_data, size);
 		stream->read(resource->m_data, size);
 
 
+		archive.close(stream);
+
 		return resource;
 		return resource;
 	}
 	}
 
 

+ 2 - 0
tools/compilers/CMakeLists.txt

@@ -24,6 +24,8 @@ add_subdirectory(txt)
 add_subdirectory(tga)
 add_subdirectory(tga)
 add_subdirectory(lua)
 add_subdirectory(lua)
 add_subdirectory(mat)
 add_subdirectory(mat)
+add_subdirectory(vs)
+add_subdirectory(ps)
 
 
 install (TARGETS crown-compiler-utils DESTINATION lib/${CMAKE_PROJECT_NAME})
 install (TARGETS crown-compiler-utils DESTINATION lib/${CMAKE_PROJECT_NAME})
 
 

+ 16 - 0
tools/compilers/ps/CMakeLists.txt

@@ -0,0 +1,16 @@
+cmake_minimum_required(VERSION 2.8)
+
+# ps-compiler
+set (SRC
+	PSCompiler.cpp
+	main.cpp
+)
+
+set (HEADERS
+	PSCompiler.h
+)
+
+add_executable(ps-compiler ${SRC} ${HEADERS})
+target_link_libraries(ps-compiler crown-compiler-utils)
+
+install (TARGETS ps-compiler DESTINATION bin)

+ 85 - 0
tools/compilers/ps/PSCompiler.cpp

@@ -0,0 +1,85 @@
+/*
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include "PSCompiler.h"
+#include "FileStream.h"
+#include "Resource.h"
+
+namespace crown
+{
+
+//-----------------------------------------------------------------------------
+PSCompiler::PSCompiler(const char* root_path, const char* dest_path, const char* resource, uint32_t seed) :
+	Compiler(root_path, dest_path, resource, PIXEL_SHADER_TYPE, seed),
+	m_file_size(0),
+	m_file_data(NULL)
+{
+}
+
+//-----------------------------------------------------------------------------
+PSCompiler::~PSCompiler()
+{
+	if (m_file_data)
+	{
+		delete[] m_file_data;
+	}
+}
+
+//-----------------------------------------------------------------------------
+bool PSCompiler::compile()
+{
+	FileStream* file = Compiler::source_file();
+
+	m_file_size = file->size();
+
+	if (m_file_size == 0)
+	{
+		return false;
+	}
+
+	m_file_data = new char[m_file_size];
+	
+	// Copy the entire file into the buffer
+	file->read(m_file_data, m_file_size);
+
+	// Prepare for writing
+	Compiler::prepare_header(m_file_size + sizeof(uint32_t));
+
+	return true;
+}
+
+//-----------------------------------------------------------------------------
+void PSCompiler::write()
+{
+	Compiler::write_header();
+
+	FileStream* file = Compiler::destination_file();
+
+	file->write(&m_file_size, sizeof(uint32_t));
+	file->write(m_file_data, m_file_size);
+}
+
+} // namespace crown
+

+ 50 - 0
tools/compilers/ps/PSCompiler.h

@@ -0,0 +1,50 @@
+/*
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#pragma once
+
+#include "Compiler.h"
+
+namespace crown
+{
+
+class PSCompiler : public Compiler
+{
+public:
+
+					PSCompiler(const char* root_path, const char* dest_path, const char* resource, uint32_t seed);
+					~PSCompiler();
+
+	bool			compile();
+	void			write();
+
+private:
+
+	uint32_t		m_file_size;
+	char*			m_file_data;
+};
+
+} // namespace crown
+

+ 181 - 0
tools/compilers/ps/main.cpp

@@ -0,0 +1,181 @@
+/*
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "Crown.h"
+#include "PSCompiler.h"
+
+using namespace crown;
+
+const char* root_path = NULL;
+const char* dest_path = NULL;
+const char* resource_in = NULL;
+uint32_t hash_seed = 0;
+
+void print_help_message(const char* program_name);
+void parse_command_line(int argc, char** argv);
+
+/// UTF-8 compiler for "txt" resource types
+int main(int argc, char** argv)
+{
+	parse_command_line(argc, argv);
+
+	if (root_path == NULL)
+	{
+		printf("%s: ERROR: you have to specify the root path with `--root-path`\n", argv[0]);
+		exit(-1);
+	}
+
+	if (dest_path == NULL)
+	{
+		printf("%s: ERROR: you have to specify the destination path with `--dest-path`\n", argv[0]);
+		exit(-1);
+	}
+
+	if (resource_in == NULL)
+	{
+		printf("%s: ERROR: you have to specify the resource name with `--resource-in`\n", argv[0]);
+		exit(-1);
+	}
+	
+	// FIXME: validate input
+
+	PSCompiler compiler(root_path, dest_path, resource_in, hash_seed);
+
+	if (compiler.compile() == false)
+	{
+		printf("%s: ERROR: compilation failed for resource %s\n", argv[0], compiler.resource_path());
+		exit(-1);
+	}
+
+	compiler.write();
+
+	return 0;
+}
+
+void parse_command_line(int argc, char** argv)
+{
+	// Parse arguments
+	ArgsOption options[] = 
+	{
+		"help",         AOA_NO_ARGUMENT,       NULL,        'h',
+		"root-path",    AOA_REQUIRED_ARGUMENT, NULL,        'r',
+		"dest-path",    AOA_REQUIRED_ARGUMENT, NULL,        'd',
+		"resource-in",  AOA_REQUIRED_ARGUMENT, NULL,        'i',
+		"seed",         AOA_REQUIRED_ARGUMENT, NULL,        's',
+		NULL, 0, NULL, 0
+	};
+
+	Args args(argc, argv, "", options);
+
+	while (1)
+	{
+		int32_t ret = args.next_option();
+		
+		switch (ret)
+		{
+			case -1:
+			{
+				return;
+			}
+			// Help message
+			case 'h':
+			{
+				print_help_message(argv[0]);
+				exit(0);
+			}
+			// Root path
+			case 'r':
+			{
+				if (args.option_argument() == NULL)
+				{
+					printf("%s: ERROR: missing path after `--root-path`\n", argv[0]);
+					exit(-1);
+				}
+				
+				root_path = args.option_argument();
+				
+				break;
+			}
+			// Dest path
+			case 'd':
+			{
+				if (args.option_argument() == NULL)
+				{
+					printf("%s: ERROR: missing path after `--dest-path`\n", argv[0]);
+					exit(-1);
+				}
+				
+				dest_path = args.option_argument();
+				
+				break;
+			}
+			// Resource in
+			case 'i':
+			{
+				if (args.option_argument() == NULL)
+				{
+					printf("%s: ERROR: missing path after `--resource-in`\n", argv[0]);
+					exit(-1);
+				}
+				
+				resource_in = args.option_argument();
+				
+				break;
+			}
+			case 's':
+			{
+				if (args.option_argument() == NULL)
+				{
+					printf("%s: ERROR: missing seed value after `--seed`\n", argv[0]);
+					exit(-1);
+				}
+
+				hash_seed = atoi(args.option_argument());
+
+				break;
+			}
+			default:
+			{
+				break;
+			}
+		}
+	}
+}
+
+void print_help_message(const char* program_name)
+{
+	printf("Usage: %s [options]\n", program_name);
+	printf("Options:\n\n");
+
+	printf("  --help                  Show this help.\n");
+	printf("  --root-path <path>      The _absolute_ <path> whether to look for the input resource.\n");
+	printf("  --dest-path <path>      The _absolute_ <path> whether to put the compiled resource.\n");
+	printf("  --resource-in <path>    The _relative_ <path> of the input resource.\n");
+	printf("  --seed <value>          The unsigned integer <value> of the hash seed.\n");
+}
+

+ 3 - 1
tools/compilers/resource-hash.cpp

@@ -35,7 +35,9 @@ int main(int argc, char** argv)
 		resource_extension_hash != MESH_TYPE &&
 		resource_extension_hash != MESH_TYPE &&
 		resource_extension_hash != SCRIPT_TYPE &&
 		resource_extension_hash != SCRIPT_TYPE &&
 		resource_extension_hash != TEXT_TYPE &&
 		resource_extension_hash != TEXT_TYPE &&
-		resource_extension_hash != MATERIAL_TYPE)
+		resource_extension_hash != MATERIAL_TYPE &&
+		resource_extension_hash != VERTEX_SHADER_TYPE &&
+		resource_extension_hash != PIXEL_SHADER_TYPE)
 	{
 	{
 		printf("%s: ERROR: cannot generate hash for resource '%s': Unknown type.\n", argv[0], resource_in);
 		printf("%s: ERROR: cannot generate hash for resource '%s': Unknown type.\n", argv[0], resource_in);
 		exit(-1);
 		exit(-1);

+ 16 - 0
tools/compilers/vs/CMakeLists.txt

@@ -0,0 +1,16 @@
+cmake_minimum_required(VERSION 2.8)
+
+# vs-compiler
+set (SRC
+	VSCompiler.cpp
+	main.cpp
+)
+
+set (HEADERS
+	VSCompiler.h
+)
+
+add_executable(vs-compiler ${SRC} ${HEADERS})
+target_link_libraries(vs-compiler crown-compiler-utils)
+
+install (TARGETS vs-compiler DESTINATION bin)

+ 85 - 0
tools/compilers/vs/VSCompiler.cpp

@@ -0,0 +1,85 @@
+/*
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include "VSCompiler.h"
+#include "FileStream.h"
+#include "Resource.h"
+
+namespace crown
+{
+
+//-----------------------------------------------------------------------------
+VSCompiler::VSCompiler(const char* root_path, const char* dest_path, const char* resource, uint32_t seed) :
+	Compiler(root_path, dest_path, resource, VERTEX_SHADER_TYPE, seed),
+	m_file_size(0),
+	m_file_data(NULL)
+{
+}
+
+//-----------------------------------------------------------------------------
+VSCompiler::~VSCompiler()
+{
+	if (m_file_data)
+	{
+		delete[] m_file_data;
+	}
+}
+
+//-----------------------------------------------------------------------------
+bool VSCompiler::compile()
+{
+	FileStream* file = Compiler::source_file();
+
+	m_file_size = file->size();
+
+	if (m_file_size == 0)
+	{
+		return false;
+	}
+
+	m_file_data = new char[m_file_size];
+	
+	// Copy the entire file into the buffer
+	file->read(m_file_data, m_file_size);
+
+	// Prepare for writing
+	Compiler::prepare_header(m_file_size + sizeof(uint32_t));
+
+	return true;
+}
+
+//-----------------------------------------------------------------------------
+void VSCompiler::write()
+{
+	Compiler::write_header();
+
+	FileStream* file = Compiler::destination_file();
+
+	file->write(&m_file_size, sizeof(uint32_t));
+	file->write(m_file_data, m_file_size);
+}
+
+} // namespace crown
+

+ 50 - 0
tools/compilers/vs/VSCompiler.h

@@ -0,0 +1,50 @@
+/*
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#pragma once
+
+#include "Compiler.h"
+
+namespace crown
+{
+
+class VSCompiler : public Compiler
+{
+public:
+
+					VSCompiler(const char* root_path, const char* dest_path, const char* resource, uint32_t seed);
+					~VSCompiler();
+
+	bool			compile();
+	void			write();
+
+private:
+
+	uint32_t		m_file_size;
+	char*			m_file_data;
+};
+
+} // namespace crown
+

+ 181 - 0
tools/compilers/vs/main.cpp

@@ -0,0 +1,181 @@
+/*
+Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "Crown.h"
+#include "VSCompiler.h"
+
+using namespace crown;
+
+const char* root_path = NULL;
+const char* dest_path = NULL;
+const char* resource_in = NULL;
+uint32_t hash_seed = 0;
+
+void print_help_message(const char* program_name);
+void parse_command_line(int argc, char** argv);
+
+/// UTF-8 compiler for "txt" resource types
+int main(int argc, char** argv)
+{
+	parse_command_line(argc, argv);
+
+	if (root_path == NULL)
+	{
+		printf("%s: ERROR: you have to specify the root path with `--root-path`\n", argv[0]);
+		exit(-1);
+	}
+
+	if (dest_path == NULL)
+	{
+		printf("%s: ERROR: you have to specify the destination path with `--dest-path`\n", argv[0]);
+		exit(-1);
+	}
+
+	if (resource_in == NULL)
+	{
+		printf("%s: ERROR: you have to specify the resource name with `--resource-in`\n", argv[0]);
+		exit(-1);
+	}
+	
+	// FIXME: validate input
+
+	VSCompiler compiler(root_path, dest_path, resource_in, hash_seed);
+
+	if (compiler.compile() == false)
+	{
+		printf("%s: ERROR: compilation failed for resource %s\n", argv[0], compiler.resource_path());
+		exit(-1);
+	}
+
+	compiler.write();
+
+	return 0;
+}
+
+void parse_command_line(int argc, char** argv)
+{
+	// Parse arguments
+	ArgsOption options[] = 
+	{
+		"help",         AOA_NO_ARGUMENT,       NULL,        'h',
+		"root-path",    AOA_REQUIRED_ARGUMENT, NULL,        'r',
+		"dest-path",    AOA_REQUIRED_ARGUMENT, NULL,        'd',
+		"resource-in",  AOA_REQUIRED_ARGUMENT, NULL,        'i',
+		"seed",         AOA_REQUIRED_ARGUMENT, NULL,        's',
+		NULL, 0, NULL, 0
+	};
+
+	Args args(argc, argv, "", options);
+
+	while (1)
+	{
+		int32_t ret = args.next_option();
+		
+		switch (ret)
+		{
+			case -1:
+			{
+				return;
+			}
+			// Help message
+			case 'h':
+			{
+				print_help_message(argv[0]);
+				exit(0);
+			}
+			// Root path
+			case 'r':
+			{
+				if (args.option_argument() == NULL)
+				{
+					printf("%s: ERROR: missing path after `--root-path`\n", argv[0]);
+					exit(-1);
+				}
+				
+				root_path = args.option_argument();
+				
+				break;
+			}
+			// Dest path
+			case 'd':
+			{
+				if (args.option_argument() == NULL)
+				{
+					printf("%s: ERROR: missing path after `--dest-path`\n", argv[0]);
+					exit(-1);
+				}
+				
+				dest_path = args.option_argument();
+				
+				break;
+			}
+			// Resource in
+			case 'i':
+			{
+				if (args.option_argument() == NULL)
+				{
+					printf("%s: ERROR: missing path after `--resource-in`\n", argv[0]);
+					exit(-1);
+				}
+				
+				resource_in = args.option_argument();
+				
+				break;
+			}
+			case 's':
+			{
+				if (args.option_argument() == NULL)
+				{
+					printf("%s: ERROR: missing seed value after `--seed`\n", argv[0]);
+					exit(-1);
+				}
+
+				hash_seed = atoi(args.option_argument());
+
+				break;
+			}
+			default:
+			{
+				break;
+			}
+		}
+	}
+}
+
+void print_help_message(const char* program_name)
+{
+	printf("Usage: %s [options]\n", program_name);
+	printf("Options:\n\n");
+
+	printf("  --help                  Show this help.\n");
+	printf("  --root-path <path>      The _absolute_ <path> whether to look for the input resource.\n");
+	printf("  --dest-path <path>      The _absolute_ <path> whether to put the compiled resource.\n");
+	printf("  --resource-in <path>    The _relative_ <path> of the input resource.\n");
+	printf("  --seed <value>          The unsigned integer <value> of the hash seed.\n");
+}
+

+ 30 - 1
tools/pycrown/Compiler.py

@@ -30,6 +30,8 @@ BC_G = "bytecode-generator.lua"
 TXT_C = "txt-compiler"
 TXT_C = "txt-compiler"
 TGA_C = "tga-compiler"
 TGA_C = "tga-compiler"
 LUA_C = "lua-compiler"
 LUA_C = "lua-compiler"
+VS_C = "vs-compiler"
+PS_C = "ps-compiler"
 RES_H = "resource-hash"
 RES_H = "resource-hash"
 
 
 ROOT_P = "--root-path"
 ROOT_P = "--root-path"
@@ -107,6 +109,20 @@ class Compiler:
 		for res in scripts:
 		for res in scripts:
 			self.compile(res)
 			self.compile(res)
 
 
+	# Compiles all the vertex shader resources in the repository
+	def compile_vertex_shaders(self):
+		vss = self.m_repository.vertex_shader_resources();
+
+		for res in vss:
+			self.compile(res)
+
+	# Compiles all the vertex shader resources in the repository
+	def compile_pixel_shaders(self):
+		pss = self.m_repository.pixel_shader_resources();
+
+		for res in pss:
+			self.compile(res)
+
 	# Compiles all the resources in the repository
 	# Compiles all the resources in the repository
 	def compile_all(self):
 	def compile_all(self):
 		print("Compiling textures...")
 		print("Compiling textures...")
@@ -121,6 +137,12 @@ class Compiler:
 		print("Compiling scripts...")
 		print("Compiling scripts...")
 		self.compile_scripts()
 		self.compile_scripts()
 
 
+		print("Compiling vertex shaders...")
+		self.compile_vertex_shaders()
+
+		print("Compiling pixel shaders...")
+		self.compile_pixel_shaders()
+
 	# Compile a single resource from the repository
 	# Compile a single resource from the repository
 	def compile(self, resource):
 	def compile(self, resource):
 		# Compute perfect seed if necessary
 		# Compute perfect seed if necessary
@@ -134,12 +156,19 @@ class Compiler:
 		# Call appropriate compiler based on resource extension
 		# Call appropriate compiler based on resource extension
 		if resource.endswith('.txt'):
 		if resource.endswith('.txt'):
 			p = subprocess.call([TXT_C, ROOT_P, root_path, DEST_P, self.m_dest_path, RES_IN, resource, SEED, str(self.m_perfect_seed)]);
 			p = subprocess.call([TXT_C, ROOT_P, root_path, DEST_P, self.m_dest_path, RES_IN, resource, SEED, str(self.m_perfect_seed)]);
+		
 		if resource.endswith('.tga'):
 		if resource.endswith('.tga'):
 			p = subprocess.call([TGA_C, ROOT_P, root_path, DEST_P, self.m_dest_path, RES_IN, resource, SEED, str(self.m_perfect_seed)]);
 			p = subprocess.call([TGA_C, ROOT_P, root_path, DEST_P, self.m_dest_path, RES_IN, resource, SEED, str(self.m_perfect_seed)]);
+		
 		if resource.endswith('.lua'):
 		if resource.endswith('.lua'):
 			path = os.path.normpath(root_path + "/" + resource)
 			path = os.path.normpath(root_path + "/" + resource)
 			f = subprocess.call([LUAJIT, BC_G, path]);
 			f = subprocess.call([LUAJIT, BC_G, path]);
 			p = subprocess.call([LUA_C, ROOT_P, root_path, DEST_P, self.m_dest_path, RES_IN, resource, SEED, str(self.m_perfect_seed)]);
 			p = subprocess.call([LUA_C, ROOT_P, root_path, DEST_P, self.m_dest_path, RES_IN, resource, SEED, str(self.m_perfect_seed)]);
+		
+		if resource.endswith('.vs'):
+			p = subprocess.call([VS_C, ROOT_P, root_path, DEST_P, self.m_dest_path, RES_IN, resource, SEED, str(self.m_perfect_seed)]);
+		
+		if resource.endswith('.ps'):
+			p = subprocess.call([PS_C, ROOT_P, root_path, DEST_P, self.m_dest_path, RES_IN, resource, SEED, str(self.m_perfect_seed)]);
 
 
-	
 
 

+ 27 - 5
tools/pycrown/Repository.py

@@ -24,12 +24,14 @@
 
 
 import os
 import os
 
 
-TEXTURE_EXTENSION = ('.tga')
-TEXT_EXTENSION = ('.txt')
-MESH_EXTENSION = ('.dae')
-LUA_EXTENSION = ('.lua')
+TEXTURE_EXTENSION			= ('.tga')
+TEXT_EXTENSION				= ('.txt')
+MESH_EXTENSION				= ('.dae')
+LUA_EXTENSION				= ('.lua')
+VERTEX_SHADER_EXTENSION		= ('.vs')
+PIXEL_SHADER_EXTENSION		= ('.ps')
 
 
-resource_extensions = ('.txt', '.tga', '.dae', '.lua')
+resource_extensions = ('.txt', '.tga', '.dae', '.lua', '.vs', '.ps')
 
 
 # Represents the folder containing the resources
 # Represents the folder containing the resources
 # Can filter resources by type and other useful stuff
 # Can filter resources by type and other useful stuff
@@ -86,6 +88,26 @@ class Repository:
 
 
 		return scripts
 		return scripts
 
 
+	# Returns a list of all the vertex shader resources found
+	def vertex_shader_resources(self):
+		vss = []
+
+		for res in self.m_resources:
+			if (res.endswith(VERTEX_SHADER_EXTENSION)):
+				vss.append(res)
+
+		return vss
+
+	# Returns a list of all the pixel shader resources found
+	def pixel_shader_resources(self):
+		pss = []
+
+		for res in self.m_resources:
+			if (res.endswith(PIXEL_SHADER_EXTENSION)):
+				pss.append(res)
+
+		return pss
+
 	# Scans the root path to find resources
 	# Scans the root path to find resources
 	def scan(self):
 	def scan(self):
 		# Clear the resources
 		# Clear the resources