Daniele Bartolini 11 anni fa
parent
commit
3e62e340dd

+ 25 - 23
engine/CMakeLists.txt

@@ -215,42 +215,44 @@ set (RENDERERS_HEADERS
 )
 
 set (RESOURCE_SRC
+	resource/FileBundle.cpp
+	resource/FontResource.cpp
+	resource/GuiResource.cpp
+	resource/LevelResource.cpp
+	resource/LuaResource.cpp
+	resource/MaterialResource.cpp
+	resource/MeshResource.cpp
+	resource/PackageResource.cpp
+	resource/PhysicsResource.cpp
 	resource/ResourceLoader.cpp
 	resource/ResourceManager.cpp
 	resource/ResourceRegistry.cpp
-	resource/UnitResource.cpp
-	resource/TextureResource.cpp
-	resource/SpriteResource.cpp
 	resource/SoundResource.cpp
-	resource/MaterialResource.cpp
-	resource/PhysicsResource.cpp
-	resource/PackageResource.cpp
-	resource/MeshResource.cpp
-	resource/LuaResource.cpp
-	resource/GuiResource.cpp
-	resource/FontResource.cpp
-	resource/FileBundle.cpp
+	resource/SpriteResource.cpp
+	resource/TextureResource.cpp
+	resource/UnitResource.cpp
 )
 
 set (RESOURCE_HEADERS
-	resource/Resource.h
-	resource/ResourceLoader.h
-	resource/ResourceManager.h
-	resource/ResourceRegistry.h
 	resource/Bundle.h
-	resource/LuaResource.h
-	resource/TextureResource.h
-	resource/MeshResource.h
 	resource/FontResource.h
-	resource/SoundResource.h
+	resource/FontResource.h
+	resource/GuiResource.h
+	resource/LevelResource.h
+	resource/LuaResource.h
 	resource/MaterialResource.h
+	resource/MeshResource.h
 	resource/PackageResource.h
-	resource/UnitResource.h
+	resource/PhysicsResource.h
+	resource/Resource.h
+	resource/ResourceLoader.h
+	resource/ResourceManager.h
 	resource/ResourcePackage.h
+	resource/ResourceRegistry.h
+	resource/SoundResource.h
 	resource/SpriteResource.h
-	resource/PhysicsResource.h
-	resource/GuiResource.h
-	resource/FontResource.h
+	resource/TextureResource.h
+	resource/UnitResource.h
 )
 
 set (OS_SRC

+ 5 - 0
engine/compilers/BundleCompiler.cpp

@@ -51,6 +51,7 @@ namespace sprite_resource { extern void compile(Filesystem&, const char*, File*)
 namespace material_resource { extern void compile(Filesystem&, const char*, File*); }
 namespace gui_resource { extern void compile(Filesystem&, const char*, File*); }
 namespace font_resource { extern void compile(Filesystem&, const char*, File*); }
+namespace level_resource { extern void compile(Filesystem&, const char*, File*); }
 
 //-----------------------------------------------------------------------------
 BundleCompiler::BundleCompiler()
@@ -176,6 +177,10 @@ bool BundleCompiler::compile(const char* bundle_dir, const char* source_dir, con
 			{
 				font_resource::compile(root_fs, filename, out_file);
 			}
+			else if (resource_type_hash == LEVEL_TYPE)
+			{
+				level_resource::compile(root_fs, filename, out_file);
+			}
 			else
 			{
 				Log::e("Oops, unknown resource type!");

+ 92 - 0
engine/resource/LevelResource.cpp

@@ -0,0 +1,92 @@
+/*
+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 "LevelResource.h"
+#include "Array.h"
+#include "Memory.h"
+#include "JSONParser.h"
+#include "Filesystem.h"
+
+namespace crown
+{
+namespace level_resource
+{
+
+void parse_units(JSONElement root, Array<LevelUnit>& units)
+{
+	JSONElement units_arr = root.key("units");
+	const uint32_t size = units_arr.size();
+
+	for (uint32_t i = 0; i < size; i++)
+	{
+		JSONElement e = units_arr[i];
+
+		LevelUnit lu;
+
+		DynamicString name;
+		e.key("name").to_string(name);
+		name += ".unit";
+		ResourceId id; id.id = string::murmur2_64(name.c_str(), name.length());
+		lu.name = id;
+		lu.position = e.key("position").to_vector3();
+		lu.rotation = e.key("rotation").to_quaternion();
+
+		array::push_back(units, lu);
+	}
+}
+
+//-----------------------------------------------------------------------------
+void compile(Filesystem& fs, const char* resource_path, File* out_file)
+{
+	File* file = fs.open(resource_path, FOM_READ);
+	char* buf = (char*)default_allocator().allocate(file->size());
+	file->read(buf, file->size());
+
+	JSONParser json(buf);
+	JSONElement root = json.root();
+
+	Array<LevelUnit> units(default_allocator());
+
+	parse_units(root, units);
+
+	fs.close(file);
+	default_allocator().deallocate(buf);
+
+	LevelHeader lh;
+	lh.num_units = array::size(units);
+	uint32_t offt = sizeof(LevelHeader);
+	lh.units_offset = offt;
+
+	out_file->write((char*) &lh, sizeof(LevelHeader));
+
+	if (lh.num_units)
+	{
+		out_file->write((char*) array::begin(units), sizeof(LevelUnit) * lh.num_units);
+	}
+}
+
+} // namespace level_resource
+} // namespace crown

+ 103 - 0
engine/resource/LevelResource.h

@@ -0,0 +1,103 @@
+/*
+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 "Allocator.h"
+#include "Assert.h"
+#include "Bundle.h"
+#include "File.h"
+#include "MathTypes.h"
+#include "Resource.h"
+#include "Types.h"
+
+namespace crown
+{
+
+struct LevelHeader
+{
+	uint32_t num_units;
+	uint32_t units_offset;
+};
+
+struct LevelUnit
+{
+	ResourceId name;
+	Vector3 position;
+	Quaternion rotation;
+};
+
+struct LevelResource
+{
+	//-----------------------------------------------------------------------------
+	static void* load(Allocator& allocator, Bundle& bundle, ResourceId id)
+	{
+		File* file = bundle.open(id);
+		const size_t file_size = file->size();
+
+		void* res = allocator.allocate(file_size);
+		file->read(res, 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(resource);
+	}
+
+	//-----------------------------------------------------------------------------
+	static void offline(void* /*resource*/)
+	{
+	}
+
+	//-----------------------------------------------------------------------------
+	uint32_t num_units() const
+	{
+		return ((LevelHeader*) this)->num_units;
+	}
+
+	//-----------------------------------------------------------------------------
+	const LevelUnit* get_unit(uint32_t i) const
+	{
+		CE_ASSERT(i < num_units(), "Index out of bounds");
+
+		const LevelHeader* h = (LevelHeader*) this;
+		const LevelUnit* begin = (LevelUnit*) (((char*) this) + h->units_offset);
+		return &begin[i];
+	}
+};
+
+} // namespace crown

+ 30 - 0
engine/resource/PackageResource.cpp

@@ -60,6 +60,7 @@ void compile(Filesystem& fs, const char* resource_path, File* out_file)
 	Array<ResourceId> m_materials(default_allocator());
 	Array<ResourceId> m_guis(default_allocator());
 	Array<ResourceId> m_fonts(default_allocator());
+	Array<ResourceId> m_levels(default_allocator());
 
 	// Check for resource types
 	if (root.has_key("texture"))
@@ -291,6 +292,29 @@ void compile(Filesystem& fs, const char* resource_path, File* out_file)
 		}
 	}
 
+	// Check for fonts
+	if (root.has_key("level"))
+	{
+		JSONElement levels_array = root.key("level");
+		uint32_t levels_array_size = levels_array.size();
+
+		for (uint32_t i = 0; i < levels_array_size; i++)
+		{
+			DynamicString level_name;
+			levels_array[i].to_string(level_name); level_name += ".level";
+
+			if (!fs.is_file(level_name.c_str()))
+			{
+				Log::e("level '%s' does not exist.", level_name.c_str());
+				return;				
+			}
+
+			ResourceId id;
+			id.id = string::murmur2_64(level_name.c_str(), level_name.length(), 0);
+			array::push_back(m_levels, id);
+		}
+	}
+
 	PackageHeader header;
 	header.num_textures = array::size(m_texture);
 	header.num_scripts = array::size(m_script);
@@ -302,6 +326,7 @@ void compile(Filesystem& fs, const char* resource_path, File* out_file)
 	header.num_materials = array::size(m_materials);
 	header.num_guis = array::size(m_guis);
 	header.num_fonts = array::size(m_fonts);
+	header.num_levels = array::size(m_levels);
 
 	header.textures_offset = sizeof(PackageHeader);
 	header.scripts_offset  = header.textures_offset + sizeof(ResourceId) * header.num_textures;
@@ -313,6 +338,7 @@ void compile(Filesystem& fs, const char* resource_path, File* out_file)
 	header.materials_offset = header.physics_offset + sizeof(ResourceId) * header.num_physics;
 	header.guis_offset = header.materials_offset + sizeof(ResourceId) * header.num_materials;
 	header.fonts_offset = header.guis_offset + sizeof(ResourceId) * header.num_guis;
+	header.levels_offset = header.fonts_offset + sizeof(ResourceId) * header.num_fonts;
 
 	out_file->write((char*) &header, sizeof(PackageHeader));
 
@@ -356,6 +382,10 @@ void compile(Filesystem& fs, const char* resource_path, File* out_file)
 	{
 		out_file->write((char*) array::begin(m_fonts), sizeof(ResourceId) * header.num_fonts);
 	}
+	if (array::size(m_levels) > 0)
+	{
+		out_file->write((char*) array::begin(m_levels), sizeof(ResourceId) * header.num_levels);
+	}
 }
 
 } // namespace package_resource

+ 17 - 0
engine/resource/PackageResource.h

@@ -58,6 +58,8 @@ struct PackageHeader
 	uint32_t guis_offset;
 	uint32_t num_fonts;
 	uint32_t fonts_offset;
+	uint32_t num_levels;
+	uint32_t levels_offset;
 };
 
 struct PackageResource
@@ -153,6 +155,12 @@ struct PackageResource
 		return ((PackageHeader*) this)->num_fonts;
 	}
 
+	//-----------------------------------------------------------------------------
+	uint32_t num_levels() const
+	{
+		return ((PackageHeader*) this)->num_levels;
+	}
+
 	//-----------------------------------------------------------------------------
 	ResourceId get_texture_id(uint32_t i) const
 	{
@@ -243,6 +251,15 @@ struct PackageResource
 		return begin[i];
 	}
 
+	//-----------------------------------------------------------------------------
+	ResourceId get_level_id(uint32_t i) const
+	{
+		CE_ASSERT(i < num_levels(), "Index out of bounds");
+
+		ResourceId* begin = (ResourceId*) ((char*) this + ((PackageHeader*) this)->levels_offset);
+		return begin[i];
+	}
+
 private:
 
 	// Disable construction

+ 2 - 0
engine/resource/Resource.h

@@ -48,6 +48,7 @@ namespace crown
 #define GUI_EXTENSION				"gui"
 #define PHYSICS_CONFIG_EXTENSION	"physics_config"
 #define FONT_EXTENSION				"font"
+#define LEVEL_EXTENSION				"level"
 
 #define TEXTURE_TYPE				0x0DEED4F7
 #define MESH_TYPE					0x742FBC9A
@@ -63,6 +64,7 @@ namespace crown
 #define GUI_TYPE					0x2C56149A
 #define PHYSICS_CONFIG_TYPE			0x514F14A1
 #define FONT_TYPE					0x536DC7D4
+#define LEVEL_TYPE					0x349657F7
 
 /// ResourceId uniquely identifies a resource by its name and type.
 /// In order to speed up the lookup by the manager, it also keeps

+ 10 - 0
engine/resource/ResourcePackage.h

@@ -102,11 +102,21 @@ public:
 		{
 			m_resource_manager->load(FONT_TYPE, m_package->get_font_id(i));
 		}
+
+		for (uint32_t i = 0; i < m_package->num_levels(); i++)
+		{
+			m_resource_manager->load(LEVEL_TYPE, m_package->get_level_id(i));
+		}
 	}
 
 	/// Unloads all the resources in the package.
 	void unload()
 	{
+		for (uint32_t i = 0; i < m_package->num_levels(); i++)
+		{
+			m_resource_manager->unload(m_package->get_level_id(i));
+		}
+
 		for (uint32_t i = 0; i < m_package->num_fonts(); i++)
 		{
 			m_resource_manager->unload(m_package->get_font_id(i));

+ 2 - 0
engine/resource/ResourceRegistry.cpp

@@ -36,6 +36,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "MaterialResource.h"
 #include "GuiResource.h"
 #include "FontResource.h"
+#include "LevelResource.h"
 
 namespace crown
 {
@@ -54,6 +55,7 @@ static const ResourceCallback RESOURCE_CALLBACK_REGISTRY[] =
 	{ GUI_TYPE, GuiResource::load, GuiResource::unload, GuiResource::online, GuiResource::offline },
 	{ PHYSICS_CONFIG_TYPE, PhysicsConfigResource::load, PhysicsConfigResource::unload, PhysicsConfigResource::online, PhysicsConfigResource::offline },
 	{ FONT_TYPE, FontResource::load, FontResource::unload, FontResource::online, FontResource::offline },
+	{ LEVEL_TYPE, LevelResource::load, LevelResource::unload, LevelResource::online, LevelResource::offline },
 	{ 0, NULL, NULL, NULL, NULL }
 };