Bläddra i källkod

Add basic .physics compiler

Daniele Bartolini 12 år sedan
förälder
incheckning
02414388b4

+ 4 - 0
engine/CMakeLists.txt

@@ -88,6 +88,7 @@ set (CROWN_INCLUDES
 	${CMAKE_SOURCE_DIR}/engine/compilers/package
 	${CMAKE_SOURCE_DIR}/engine/compilers/unit
 	${CMAKE_SOURCE_DIR}/engine/compilers/sprite
+	${CMAKE_SOURCE_DIR}/engine/compilers/physics
 	${CMAKE_SOURCE_DIR}/engine/physics
 )
 
@@ -326,6 +327,7 @@ set (RESOURCE_HEADERS
 	resource/UnitResource.h
 	resource/ResourcePackage.h
 	resource/SpriteResource.h
+	resource/PhysicsResource.h
 )
 
 set (OS_SRC
@@ -401,6 +403,7 @@ set (COMPILER_SRC
 	compilers/package/PackageCompiler.cpp
 	compilers/unit/UnitCompiler.cpp
 	compilers/sprite/SpriteCompiler.cpp
+	compilers/physics/PhysicsCompiler.cpp
 )
 
 set (COMPILER_HEADER
@@ -413,6 +416,7 @@ set (COMPILER_HEADER
 	compilers/package/PackageCompiler.h
 	compilers/unit/UnitCompiler.h
 	compilers/sprite/SpriteCompiler.h
+	compilers/physics/PhysicsCompiler.h
 )
 
 set (CROWN_LIBRARIES)

+ 4 - 0
engine/compilers/BundleCompiler.cpp

@@ -136,6 +136,10 @@ bool BundleCompiler::compile(const char* bundle_dir, const char* source_dir, con
 		{
 			result = m_unit.compile(source_dir, bundle_dir, filename, out_name);
 		}
+		else if (resource_type_hash == PHYSICS_TYPE)
+		{
+			result = m_physics.compile(source_dir, bundle_dir, filename, out_name);
+		}
 		else
 		{
 			Log::e("Oops, unknown resource type!");

+ 2 - 0
engine/compilers/BundleCompiler.h

@@ -36,6 +36,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Vector.h"
 #include "DiskFilesystem.h"
 #include "UnitCompiler.h"
+#include "PhysicsCompiler.h"
 
 namespace crown
 {
@@ -64,6 +65,7 @@ private:
 	SpriteCompiler	m_sprite;
 	PackageCompiler m_package;
 	UnitCompiler	m_unit;
+	PhysicsCompiler m_physics;
 };
 
 } // namespace crown

+ 104 - 0
engine/compilers/physics/PhysicsCompiler.cpp

@@ -0,0 +1,104 @@
+/*
+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 "Filesystem.h"
+#include "StringUtils.h"
+#include "PhysicsCompiler.h"
+#include "Hash.h"
+#include "JSONParser.h"
+
+namespace crown
+{
+
+//-----------------------------------------------------------------------------
+PhysicsCompiler::PhysicsCompiler()
+{
+}
+
+//-----------------------------------------------------------------------------
+PhysicsCompiler::~PhysicsCompiler()
+{
+}
+
+//-----------------------------------------------------------------------------
+size_t PhysicsCompiler::compile_impl(Filesystem& fs, const char* resource_path)
+{
+	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();
+
+	// Read controller
+	JSONElement controller = root.key_or_nil("controller");
+	if (controller.is_nil())
+	{
+		m_has_controller = false;
+	}
+	else
+	{
+		parse_controller(controller);
+		m_has_controller = true;
+	}
+
+	fs.close(file);
+	default_allocator().deallocate(buf);
+	return 1;
+}
+
+//-----------------------------------------------------------------------------
+void PhysicsCompiler::parse_controller(JSONElement controller)
+{
+	JSONElement name = controller.key("name");
+	JSONElement height = controller.key("height");
+	JSONElement radius = controller.key("radius");
+
+	m_controller.name = hash::murmur2_32(name.string_value(), name.size(), 0);
+	m_controller.height = height.float_value();
+	m_controller.radius = radius.float_value();
+}
+
+//-----------------------------------------------------------------------------
+void PhysicsCompiler::write_impl(File* out_file)
+{
+	PhysicsHeader h;
+	h.version = 1;
+	h.num_controllers = m_has_controller ? 1 : 0;
+
+	uint32_t offt = sizeof(PhysicsHeader);
+	h.controller_offset = offt;
+
+	out_file->write((char*) &h, sizeof(PhysicsHeader));
+
+	if (m_has_controller)
+	{
+		out_file->write((char*) &m_controller, sizeof(PhysicsController));
+	}
+}
+
+} // namespace crown

+ 57 - 0
engine/compilers/physics/PhysicsCompiler.h

@@ -0,0 +1,57 @@
+/*
+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 "PhysicsResource.h"
+#include "Compiler.h"
+#include "JSONParser.h"
+
+namespace crown
+{
+
+class Filesystem;
+
+//-----------------------------------------------------------------------------
+class PhysicsCompiler : public Compiler
+{
+public:
+							PhysicsCompiler();
+							~PhysicsCompiler();
+
+	size_t					compile_impl(Filesystem& fs, const char* resource_path);
+	void					write_impl(File* out_file);
+
+	void					parse_controller(JSONElement controller);
+
+private:
+
+	bool					m_has_controller;
+	PhysicsController		m_controller;
+};
+
+} // namespace crown

+ 108 - 0
engine/resource/PhysicsResource.h

@@ -0,0 +1,108 @@
+/*
+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 "Allocator.h"
+#include "File.h"
+#include "Bundle.h"
+#include "ResourceManager.h"
+
+namespace crown
+{
+
+//-----------------------------------------------------------------------------
+struct PhysicsHeader
+{
+	uint32_t version;
+	uint32_t num_controllers;		// 0 or 1, ATM
+	uint32_t controller_offset;
+};
+
+struct PhysicsController
+{
+	StringId32 name;
+	float height;
+	float radius;
+};
+
+//-----------------------------------------------------------------------------
+struct PhysicsResource
+{
+	//-----------------------------------------------------------------------------
+	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)
+	{
+	}
+
+	//-----------------------------------------------------------------------------
+	bool has_controller() const
+	{
+		PhysicsHeader* ph = (PhysicsHeader*) this;
+		return ph->num_controllers == 1;
+	}
+
+	//-----------------------------------------------------------------------------
+	PhysicsController controller() const
+	{
+		PhysicsHeader* ph = (PhysicsHeader*) this;
+		PhysicsController* controller = (PhysicsController*) (((char*) this) + ph->controller_offset);
+		return *controller;
+	}
+
+private:
+
+	// Disable construction
+	PhysicsResource();
+};
+
+} // namespace crown

+ 4 - 2
engine/resource/Resource.h

@@ -44,17 +44,19 @@ const char* const SPRITE_EXTENSION			= "sprite";
 const char* const CONFIG_EXTENSION			= "config";
 const char* const PACKAGE_EXTENSION			= "package";
 const char* const UNIT_EXTENSION			= "unit";
+const char* const PHYSICS_EXTENSION			= "physics";
 
-const uint32_t TEXTURE_TYPE					= 0xDEED4F7;
+const uint32_t TEXTURE_TYPE					= 0x0DEED4F7;
 const uint32_t MESH_TYPE					= 0x742FBC9A;
 const uint32_t LUA_TYPE						= 0xD96E7C37;
-const uint32_t TEXT_TYPE					= 0x45CC650;
+const uint32_t TEXT_TYPE					= 0x045CC650;
 const uint32_t MATERIAL_TYPE				= 0x46807A92;
 const uint32_t SOUND_TYPE					= 0xD196AB6E;
 const uint32_t SPRITE_TYPE					= 0x5DD272E5;
 const uint32_t CONFIG_TYPE					= 0x17DEA5E1;
 const uint32_t PACKAGE_TYPE					= 0xC0A2212C;
 const uint32_t UNIT_TYPE					= 0x516224CF;
+const uint32_t PHYSICS_TYPE					= 0xFA32C012;
 
 /// ResourceId uniquely identifies a resource by its name and type.
 /// In order to speed up the lookup by the manager, it also keeps

+ 2 - 0
engine/resource/ResourceRegistry.cpp

@@ -32,6 +32,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "SpriteResource.h"
 #include "PackageResource.h"
 #include "UnitResource.h"
+#include "PhysicsResource.h"
 
 namespace crown
 {
@@ -45,6 +46,7 @@ static const ResourceCallback RESOURCE_CALLBACK_REGISTRY[] =
 	{ UNIT_TYPE, UnitResource::load, UnitResource::unload, UnitResource::online, UnitResource::offline },
 	{ SPRITE_TYPE, SpriteResource::load, SpriteResource::unload, SpriteResource::online, SpriteResource::offline},
 	{ PACKAGE_TYPE, PackageResource::load, PackageResource::unload, PackageResource::online, PackageResource::offline },
+	{ PHYSICS_TYPE, PhysicsResource::load, PhysicsResource::unload, PhysicsResource::online, PhysicsResource::offline },
 	{ 0, NULL, NULL, NULL, NULL }
 };