Explorar el Código

Add very basic .unit compiler

Daniele Bartolini hace 12 años
padre
commit
fd1a68f732

+ 4 - 0
engine/CMakeLists.txt

@@ -68,6 +68,7 @@ set (CROWN_INCLUDES
 	${CMAKE_SOURCE_DIR}/engine/compilers/sound
 	${CMAKE_SOURCE_DIR}/engine/compilers/mesh
 	${CMAKE_SOURCE_DIR}/engine/compilers/package
+	${CMAKE_SOURCE_DIR}/engine/compilers/unit
 )
 
 set (SRC
@@ -299,6 +300,7 @@ set (RESOURCE_HEADERS
 	resource/SoundResource.h
 	resource/MaterialResource.h
 	resource/PackageResource.h
+	resource/UnitResource.h
 	resource/ResourcePackage.h
 )
 
@@ -365,6 +367,7 @@ set (COMPILER_SRC
 	compilers/texture/TextureCompiler.cpp
 	compilers/sound/SoundCompiler.cpp
 	compilers/package/PackageCompiler.cpp
+	compilers/unit/UnitCompiler.cpp
 )
 
 set (COMPILER_HEADER
@@ -375,6 +378,7 @@ set (COMPILER_HEADER
 	compilers/texture/TextureCompiler.h
 	compilers/sound/SoundCompiler.h
 	compilers/package/PackageCompiler.h
+	compilers/unit/UnitCompiler.h
 )
 
 set (CROWN_LIBRARIES)

+ 105 - 0
engine/compilers/unit/UnitCompiler.cpp

@@ -0,0 +1,105 @@
+/*
+Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+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 "Allocator.h"
+#include "File.h"
+#include "Filesystem.h"
+#include "Hash.h"
+#include "JSONParser.h"
+#include "UnitCompiler.h"
+#include "TempAllocator.h"
+#include "Log.h"
+
+namespace crown
+{
+
+//-----------------------------------------------------------------------------
+UnitCompiler::UnitCompiler()
+	: m_renderable(default_allocator())
+{
+}
+
+//-----------------------------------------------------------------------------
+size_t UnitCompiler::compile_impl(Filesystem& fs, const char* resource_path)
+{
+	File* file = fs.open(resource_path, FOM_READ);
+
+	char file_buf[4096];
+	file->read(file_buf, file->size());
+	fs.close(file);
+
+	JSONParser json(file_buf);
+	JSONElement root = json.root();
+
+	// Check for renderable
+	if (root.has_key("renderable"))
+	{
+		Log::d("Reading renderables");
+
+		JSONElement renderable_array = root.key("renderable");
+		uint32_t renderable_array_size = renderable_array.size();
+
+		for (uint32_t i = 0; i < renderable_array_size; i++)
+		{
+			DynamicString mesh_resource(default_allocator());
+			mesh_resource += renderable_array[i].key("resource").string_value();
+			mesh_resource += ".mesh";
+
+			DynamicString mesh_name(default_allocator());
+			mesh_name = renderable_array[i].key("name").string_value();
+
+			UnitRenderable ur;
+			ur.resource.id = hash::murmur2_64(mesh_resource.c_str(), string::strlen(mesh_resource.c_str()), 0);
+			ur.name = hash::murmur2_32(mesh_name.c_str(), string::strlen(mesh_name.c_str()), 0);
+			ur.visible = renderable_array[i].key("visible").bool_value();
+
+			m_renderable.push_back(ur);
+		}
+	}
+
+	return sizeof(UnitHeader) + m_renderable.size() * sizeof(UnitRenderable);
+}
+
+//-----------------------------------------------------------------------------
+void UnitCompiler::write_impl(File* out_file)
+{
+	UnitHeader header;
+	header.num_renderables = m_renderable.size();
+
+	header.renderables_offset = sizeof(UnitHeader);
+
+	out_file->write((char*) &header, sizeof(UnitHeader));
+
+	if (m_renderable.size() > 0)
+	{
+		out_file->write((char*) m_renderable.begin(), sizeof(ResourceId) * header.num_renderables);
+	}
+
+	// Cleanup
+	m_renderable.clear();
+}
+
+} // namespace crown

+ 56 - 0
engine/compilers/unit/UnitCompiler.h

@@ -0,0 +1,56 @@
+/*
+Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+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"
+#include "Resource.h"
+#include "UnitResource.h"
+#include "List.h"
+
+namespace crown
+{
+
+struct UnitCamera
+{
+
+};
+
+class CE_EXPORT UnitCompiler : public Compiler
+{
+public:
+
+	UnitCompiler();
+
+	size_t compile_impl(Filesystem& fs, const char* resource_path);
+	void write_impl(File* out_file);
+
+private:
+
+	List<UnitRenderable> m_renderable;
+};
+
+} // namespace crown

+ 2 - 1
engine/resource/Resource.h

@@ -42,6 +42,7 @@ const char* const MATERIAL_EXTENSION		= "material";
 const char* const SOUND_EXTENSION			= "sound";
 const char* const CONFIG_EXTENSION			= "config";
 const char* const PACKAGE_EXTENSION			= "package";
+const char* const UNIT_EXTENSION			= "unit";
 
 const uint32_t TEXTURE_TYPE					= 0xDEED4F7;
 const uint32_t MESH_TYPE					= 0x742FBC9A;
@@ -51,7 +52,7 @@ const uint32_t MATERIAL_TYPE				= 0x46807A92;
 const uint32_t SOUND_TYPE					= 0xD196AB6E;
 const uint32_t CONFIG_TYPE					= 0x17DEA5E1;
 const uint32_t PACKAGE_TYPE					= 0xC0A2212C;
-
+const uint32_t UNIT_TYPE					= 0x516224CF;
 
 /// ResourceId uniquely identifies a resource by its name and type.
 /// In order to speed up the lookup by the manager, it also keeps

+ 3 - 1
engine/resource/ResourceRegistry.cpp

@@ -30,6 +30,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "MeshResource.h"
 #include "SoundResource.h"
 #include "PackageResource.h"
+#include "UnitResource.h"
 
 namespace crown
 {
@@ -40,7 +41,8 @@ static const ResourceCallback RESOURCE_CALLBACK_REGISTRY[] =
 	{ TEXTURE_TYPE, TextureResource::load, TextureResource::unload, TextureResource::online, TextureResource::offline },
 	{ MESH_TYPE, MeshResource::load, MeshResource::unload, MeshResource::online, MeshResource::offline },
 	{ SOUND_TYPE, SoundResource::load, SoundResource::unload, SoundResource::online, SoundResource::offline },
-	{ PACKAGE_TYPE, PackageResource::load, PackageResource::unload, PackageResource::online, PackageResource::offline },	
+	{ PACKAGE_TYPE, PackageResource::load, PackageResource::unload, PackageResource::online, PackageResource::offline },
+	{ UNIT_TYPE, UnitResource::load, UnitResource::unload, UnitResource::online, UnitResource::offline },
 	{ 0, NULL, NULL, NULL, NULL }
 };
 

+ 114 - 0
engine/resource/UnitResource.h

@@ -0,0 +1,114 @@
+/*
+Copyright (c) 2013 Daniele Bartolini, Michele Rossi
+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 "Types.h"
+#include "Resource.h"
+#include "Bundle.h"
+#include "Allocator.h"
+#include "File.h"
+
+namespace crown
+{
+
+// All offsets are absolute
+struct UnitHeader
+{
+	uint32_t num_renderables;
+	uint32_t renderables_offset;
+};
+
+struct UnitRenderable
+{
+	ResourceId resource;
+	uint32_t name;
+	bool visible;
+};
+
+class UnitResource
+{
+public:
+
+	//-----------------------------------------------------------------------------
+	static void* load(Allocator& allocator, Bundle& bundle, ResourceId id)
+	{
+		File* file = bundle.open(id);
+
+		const size_t file_size = file->size() - 12;
+		UnitResource* res = (UnitResource*) allocator.allocate(sizeof(UnitResource));
+		res->m_data = (char*) allocator.allocate(file_size);
+		file->read(res->m_data, file_size);
+
+		bundle.close(file);
+
+		return res;
+	}
+
+	//-----------------------------------------------------------------------------
+	static void online(void* /*resource*/)
+	{
+	}
+
+	//-----------------------------------------------------------------------------
+	static void unload(Allocator& allocator, void* resource)
+	{
+		CE_ASSERT_NOT_NULL(resource);
+
+		allocator.deallocate(((UnitResource*)resource)->m_data);
+		allocator.deallocate(resource);
+	}
+
+	//-----------------------------------------------------------------------------
+	static void offline(void* /*resource*/)
+	{
+	}
+
+public:
+
+	//-----------------------------------------------------------------------------
+	uint32_t num_renderables() const
+	{
+		CE_ASSERT_NOT_NULL(m_data);
+
+		return ((UnitHeader*)m_data)->num_renderables;
+	}
+
+	//-----------------------------------------------------------------------------
+	const UnitRenderable& get_texture_id(uint32_t i) const
+	{
+		CE_ASSERT(i < num_renderables(), "Index out of bounds");
+
+		UnitRenderable* begin = (UnitRenderable*) (m_data + ((UnitHeader*)m_data)->renderables_offset);
+		return begin[i];
+	}
+
+private:
+
+	char* m_data;
+};
+
+} // namespace crown