Panagiotis Christopoulos Charitos 12 rokov pred
rodič
commit
7fdfc96fc1

+ 1 - 1
build/clean

@@ -1,2 +1,2 @@
 #!/bin/bash
-ls | xargs -I % echo % | grep -v clean | grep -v genmakefiledbg | grep -v genmakefilemali | grep -v genmakefilemalimodel | xargs -I % rm -rf %
+ls | xargs -I % echo % | grep -v clean | grep -v genmakefile | xargs -I % rm -rf %

+ 0 - 6
include/anki/core/App.h

@@ -65,11 +65,6 @@ public:
 	{
 		return cachePath;
 	}
-
-	const StringList& getDataPaths() const
-	{
-		return dataPaths;
-	}
 	/// @}
 
 private:
@@ -77,7 +72,6 @@ private:
 	std::string settingsPath;
 	/// This is used as a cache
 	std::string cachePath;
-	StringList dataPaths;
 	F32 timerTick;
 
 	void parseCommandLineArgs(int argc, char* argv[]);

+ 1 - 9
include/anki/misc/Xml.h

@@ -75,15 +75,7 @@ private:
 class XmlDocument
 {
 public:
-	void loadFile(const char* filename)
-	{
-		if(doc.LoadFile(filename))
-		{
-			throw ANKI_EXCEPTION("Cannot parse file. Reason: "
-				+ ((doc.GetErrorStr1() == nullptr)
-				? "unknown" : doc.GetErrorStr1()));
-		}
-	}
+	void loadFile(const char* filename);
 
 	XmlElement getChildElement(const char* name)
 	{

+ 1 - 1
include/anki/resource/Resource.h

@@ -9,7 +9,7 @@ namespace anki {
 
 #define ANKI_RESOURCE_TYPEDEFS(rsrc, name) \
 	class rsrc; \
-	typedef ResourceManager<rsrc> rsrc ## ResourceManager; \
+	typedef TypeResourceManager<rsrc> rsrc ## ResourceManager; \
 	typedef Singleton<rsrc ## ResourceManager> \
 		rsrc ## ResourceManagerSingleton; \
 	typedef ResourcePointer<rsrc, rsrc ## ResourceManagerSingleton> name;

+ 30 - 5
include/anki/resource/ResourceManager.h

@@ -3,6 +3,7 @@
 
 #include "anki/util/Vector.h"
 #include "anki/util/StdTypes.h"
+#include "anki/util/Singleton.h"
 #include <string>
 
 namespace anki {
@@ -12,8 +13,8 @@ template<typename Type>
 struct ResourceHook
 {
 	std::string uuid; ///< Unique identifier
-	U32 referenceCounter;
-	Type* resource;
+	U32 referenceCounter = 0;
+	Type* resource = nullptr;
 
 	~ResourceHook()
 	{}
@@ -26,18 +27,42 @@ struct ResourceHook
 	}
 };
 
+/// Resource manager. It holds a few global variables
+class ResourceManager
+{
+public:
+	ResourceManager();
+
+	const std::string& getDataPath() const
+	{
+		return dataPath;
+	}
+
+	std::string fixResourcePath(const char* filename) const;
+
+private:
+	std::string dataPath;
+};
+
+/// The singleton of resource manager
+typedef Singleton<ResourceManager> ResourceManagerSingleton;
+
+/// XXX
+#define ANKI_R(x_) \
+	ResourceManagerSingleton::get().fixResourcePath(x_).c_str()
+
 /// Manage resources of a certain type
 template<typename Type>
-class ResourceManager
+class TypeResourceManager
 {
 public:
-	typedef ResourceManager<Type> Self;
+	typedef TypeResourceManager<Type> Self;
 	typedef ResourceHook<Type> Hook;
 	typedef PtrVector<Hook> Container;
 	typedef typename Container::iterator Iterator;
 	typedef typename Container::const_iterator ConstIterator;
 
-	virtual ~ResourceManager()
+	virtual ~TypeResourceManager()
 	{}
 
 	Hook& load(const char* filename);

+ 18 - 18
include/anki/resource/ResourceManager.inl.h

@@ -1,15 +1,16 @@
 #include "anki/resource/ResourceManager.h"
-#include "anki/util/Exception.h"
 #include "anki/util/Assert.h"
+#include "anki/util/Exception.h"
 
 namespace anki {
 
 //==============================================================================
 template<typename Type>
-void ResourceManager<Type>::
+void TypeResourceManager<Type>::
 	allocAndLoadRsrc(const char* filename, Type*& newInstance)
 {
-	newInstance = NULL;
+	newInstance = nullptr;
+	std::string newFname;
 
 	// Alloc
 	try
@@ -24,17 +25,20 @@ void ResourceManager<Type>::
 	// Load
 	try
 	{
-		newInstance->load(filename);
+		newFname = 
+			ResourceManagerSingleton::get().fixResourcePath(filename);
+
+		newInstance->load(newFname.c_str());
 	}
 	catch(std::exception& e)
 	{
-		throw ANKI_EXCEPTION("Cannot load: " + filename) << e;
+		throw ANKI_EXCEPTION("Cannot load: " + newFname) << e;
 	}
 }
 
 //==============================================================================
 template<typename Type>
-typename ResourceManager<Type>::Hook& ResourceManager<Type>::
+typename TypeResourceManager<Type>::Hook& TypeResourceManager<Type>::
 	load(const char* filename)
 {
 	Iterator it = find(filename);
@@ -48,7 +52,7 @@ typename ResourceManager<Type>::Hook& ResourceManager<Type>::
 	// else create new, load it and update the container
 	else
 	{
-		Hook* hook = NULL;
+		Hook* hook = nullptr;
 		hook = new Hook;
 		hook->uuid = filename;
 		hook->referenceCounter = 1;
@@ -59,11 +63,7 @@ typename ResourceManager<Type>::Hook& ResourceManager<Type>::
 		}
 		catch(std::exception& e)
 		{
-			if(hook != NULL)
-			{
-				delete hook;
-			}
-
+			delete hook;
 			throw ANKI_EXCEPTION("Cannot load: " + filename) << e;
 		}
 
@@ -74,14 +74,14 @@ typename ResourceManager<Type>::Hook& ResourceManager<Type>::
 
 //==============================================================================
 template<typename Type>
-void ResourceManager<Type>::deallocRsrc(Type* rsrc)
+void TypeResourceManager<Type>::deallocRsrc(Type* rsrc)
 {
 	propperDelete(rsrc);
 }
 
 //==============================================================================
 template<typename Type>
-void ResourceManager<Type>::unload(const Hook& hook)
+void TypeResourceManager<Type>::unload(const Hook& hook)
 {
 	// Find
 	Iterator it = find(hook.uuid.c_str());
@@ -89,11 +89,11 @@ void ResourceManager<Type>::unload(const Hook& hook)
 	// If not found
 	if(it == hooks.end())
 	{
-		throw ANKI_EXCEPTION("Resource hook incorrect (\"" 
-			+ hook.uuid + "\")");
+		throw ANKI_EXCEPTION("Resource hook not found: " + hook.uuid);
 	}
 
 	ANKI_ASSERT(*(*it) == hook);
+	ANKI_ASSERT((*it)->referenceCounter > 0);
 
 	--(*it)->referenceCounter;
 
@@ -107,7 +107,7 @@ void ResourceManager<Type>::unload(const Hook& hook)
 
 //==============================================================================
 template<typename Type>
-typename ResourceManager<Type>::Iterator ResourceManager<Type>::
+typename TypeResourceManager<Type>::Iterator TypeResourceManager<Type>::
 	find(const char* filename)
 {
 	Iterator it = hooks.begin();
@@ -122,4 +122,4 @@ typename ResourceManager<Type>::Iterator ResourceManager<Type>::
 	return it;
 }
 
-} // end namespace
+} // end namespace anki

+ 3 - 3
include/anki/util/File.h

@@ -49,12 +49,12 @@ public:
 
 	/// Default constructor
 	File()
-		: flags(0), file(nullptr)
+		: file(nullptr), flags(0)
 	{}
 
 	/// Open file
 	File(const char* filename, U8 openMask)
-		: flags(0), file(nullptr)
+		: file(nullptr), flags(0)
 	{
 		open(filename, openMask);
 	}
@@ -120,8 +120,8 @@ public:
 	/// @}
 
 private:
-	U8 flags; ///< All the flags. Initialy zero and set on open
 	void* file; ///< A native type
+	U8 flags; ///< All the flags. Initialy zero and set on open
 
 	/// Get the current machine's endianness
 	static Endianness getMachineEndianness();

+ 5 - 0
pack_data.sh

@@ -0,0 +1,5 @@
+#!/bin/sh
+rm data.ankizip
+cd data
+zip -r ../data.ankizip . -i '*.mtl' '*.mdl' '*.mesh' '*.scene' '*.ankitex' '*.tga' '*.png' '*.ttf' '*.ogg'
+cd -

+ 0 - 10
src/core/App.cpp

@@ -120,16 +120,6 @@ void App::initDirs()
 
 	ANKI_LOGI("Creating cache dir: %s", cachePath.c_str());
 	createDirectory(cachePath.c_str());
-
-	// Data
-	if(getenv("ANKI_DATA_PATH"))
-	{
-		dataPaths = StringList::splitString(getenv("ANKI_DATA_PATH"), ':');
-	}
-	else
-	{
-		dataPaths.push_back("./");
-	}
 }
 
 //==============================================================================

+ 24 - 0
src/misc/Xml.cpp

@@ -1,8 +1,13 @@
 #include "anki/misc/Xml.h"
 #include "anki/util/StringList.h"
+#include "anki/util/File.h"
 
 namespace anki {
 
+//==============================================================================
+// XmlElement                                                                  =
+//==============================================================================
+
 //==============================================================================
 I XmlElement::getInt() const
 {
@@ -80,4 +85,23 @@ XmlElement XmlElement::getNextSiblingElement(const char* name) const
 	return out;
 }
 
+//==============================================================================
+// XmlDocument                                                                 =
+//==============================================================================
+
+//==============================================================================
+void XmlDocument::loadFile(const char* filename)
+{
+	File file(filename, File::OF_READ);
+	std::string text;
+	file.readAllText(text);
+
+	if(doc.Parse(text.c_str()))
+	{
+		throw ANKI_EXCEPTION("Cannot parse file. Reason: "
+			+ ((doc.GetErrorStr1() == nullptr)
+			? "unknown" : doc.GetErrorStr1()));
+	}
+}
+
 } // end namespace anki

+ 51 - 0
src/resource/ResourceManager.cpp

@@ -0,0 +1,51 @@
+#include "anki/resource/ResourceManager.h"
+#include "anki/core/Logger.h"
+#include "anki/core/App.h"
+#include <cstring>
+
+namespace anki {
+
+//==============================================================================
+ResourceManager::ResourceManager()
+{
+	if(getenv("ANKI_DATA_PATH"))
+	{
+		dataPath = getenv("ANKI_DATA_PATH");
+#if ANKI_POSIX
+		dataPath += "/";
+#else
+		dataPath = "\\";
+#endif
+		ANKI_LOGI("Data path: %s", dataPath.c_str());
+	}
+	else
+	{
+		// Assume working directory
+#if ANKI_POSIX
+		dataPath = "./";
+#else
+		dataPath = ".\\";
+#endif
+	}
+}
+
+//==============================================================================
+std::string ResourceManager::fixResourcePath(const char* filename) const
+{
+	std::string newFname;
+
+	// If the filename is in cache then dont append the data path
+	const char* cachePath = AppSingleton::get().getCachePath().c_str();
+	if(strstr(filename, cachePath) != nullptr)
+	{
+		newFname = filename;
+	}
+	else
+	{
+		newFname = ResourceManagerSingleton::get().getDataPath() + filename;
+	}
+
+	return newFname;
+}
+
+} // end namespace anki

+ 1 - 1
src/scene/SceneGraph.cpp

@@ -228,7 +228,7 @@ void SceneGraph::load(const char* filename)
 	try
 	{
 		XmlDocument doc;
-		doc.loadFile(filename);
+		doc.loadFile(ANKI_R(filename));
 
 		XmlElement rootEl = doc.getChildElement("scene");
 

+ 38 - 7
src/util/File.cpp

@@ -178,7 +178,7 @@ void File::openZipFile(const char* archive, const char* archived, U8 flags_)
 
 	static_assert(sizeof(file) == sizeof(zfile), "See file");
 	file = (void*)zfile;
-	flags = flags_ | FT_C;
+	flags = flags_ | FT_ZIP;
 }
 
 //==============================================================================
@@ -192,7 +192,7 @@ void File::close()
 	}
 	else if(flags & FT_ZIP)
 	{
-		ANKI_ASSERT(0 && "Not implemented");
+		unzClose(file);
 	}
 	else
 	{
@@ -219,7 +219,7 @@ void File::read(void* buff, PtrSize size)
 	}
 	else if(flags & FT_ZIP)
 	{
-		ANKI_ASSERT(0 && "Not implemented");
+		readSize = unzReadCurrentFile(file, buff, size);
 	}
 	else
 	{
@@ -255,7 +255,28 @@ void File::readAllText(std::string& txt)
 	}
 	else if(flags & FT_ZIP)
 	{
-		ANKI_ASSERT(0 && "Not implemented");
+		char buff[256];
+		I readSize;
+
+		while(true)
+		{
+			readSize = unzReadCurrentFile(file, buff, sizeof(buff) - 1);
+
+			if(readSize > 0)
+			{
+				buff[readSize] = '\0';
+				txt += buff;
+			}
+			else
+			{
+				break;
+			}
+		}
+
+		if(readSize < 0)
+		{
+			throw ANKI_EXCEPTION("unzReadCurrentFile() failed");
+		}
 	}
 	else
 	{
@@ -370,7 +391,7 @@ void File::write(void* buff, PtrSize size)
 	}
 	else if(flags & FT_ZIP)
 	{
-		ANKI_ASSERT(0 && "Not implemented");
+		throw ANKI_EXCEPTION("Writting to archives is not supported");
 	}
 	else
 	{
@@ -396,7 +417,7 @@ void File::writeText(const char* format, ...)
 	}
 	else if(flags & FT_ZIP)
 	{
-		ANKI_ASSERT(0 && "Not implemented");
+		throw ANKI_EXCEPTION("Writting to archives is not supported");
 	}
 	else
 	{
@@ -421,7 +442,17 @@ void File::seek(PtrSize offset, SeekOrigin origin)
 	}
 	else if(flags & FT_ZIP)
 	{
-		ANKI_ASSERT(0 && "Not implemented");
+		// Rewind if needed
+		if(origin == SO_BEGINNING)
+		{
+			if(unzCloseCurrentFile(file)
+				|| unzOpenCurrentFile(file))
+			{
+				throw ANKI_EXCEPTION("Rewinde failed");
+			}
+			
+			// XXX
+		}
 	}
 	else
 	{

+ 10 - 7
testapp/Main.cpp

@@ -95,7 +95,7 @@ void initPhysics()
 
 				ModelNode* mnode = new ModelNode(
 					name.c_str(), &SceneGraphSingleton::get(), nullptr,
-					Movable::MF_NONE, "data/models/crate0/crate0.mdl");
+					Movable::MF_NONE, "models/crate0/crate0.mdl");
 
 				init.movable = mnode;
 				ANKI_ASSERT(init.movable);
@@ -202,7 +202,7 @@ void init()
 		PointLight* point =
 			new PointLight(("vase_plight" + std::to_string(i)).c_str(),
 			&scene, nullptr, Movable::MF_NONE, 
-			(i != 100) ? "data/textures/lens_flare/flares0.ankitex" : nullptr);
+			(i != 100) ? "textures/lens_flare/flares0.ankitex" : nullptr);
 		point->setRadius(2.0);
 		point->setLocalOrigin(lightPos);
 		point->setDiffuseColor(Vec4(3.0, 0.2, 0.0, 0.0));
@@ -227,12 +227,12 @@ void init()
 
 		ParticleEmitter* pe = new ParticleEmitter(
 			("pe" + std::to_string(i)).c_str(), &scene, nullptr,
-			Movable::MF_NONE, "data/particles/smoke.particles");
+			Movable::MF_NONE, "particles/smoke.ankipart");
 		pe->setLocalOrigin(lightPos);
 
 		pe = new ParticleEmitter(
 			("pef" + std::to_string(i)).c_str(), &scene, nullptr,
-			Movable::MF_NONE, "data/particles/fire.particles");
+			Movable::MF_NONE, "particles/fire.ankipart");
 		pe->setLocalOrigin(lightPos);
 	}
 #endif
@@ -240,14 +240,14 @@ void init()
 #if 1
 	// horse
 	horse = new ModelNode("horse", &scene, nullptr,
-		Movable::MF_NONE, "data/models/horse/horse.mdl");
+		Movable::MF_NONE, "models/horse/horse.mdl");
 	horse->setLocalTransform(Transform(Vec3(-2, 0, 0), Mat3::getIdentity(),
 		0.7));
 
 	// barrel
 	ModelNode* redBarrel = new ModelNode(
 		"red_barrel", &scene, nullptr, Movable::MF_NONE, 
-		"data/models/red_barrel/red_barrel.mdl");
+		"models/red_barrel/red_barrel.mdl");
 	redBarrel->setLocalTransform(Transform(Vec3(+2, 0, 0), Mat3::getIdentity(),
 		0.7));
 #endif
@@ -260,7 +260,7 @@ void init()
 
 	(void)sponzaModel;
 #endif
-	scene.load("data/maps/sponza/master.scene");
+	scene.load("maps/sponza/master.scene");
 
 	//initPhysics();
 
@@ -573,6 +573,9 @@ void initSubsystems(int argc, char* argv[])
 	initializer.samples = 1;
 	initializer.pps.enabled = false;
 	initializer.is.maxPointLights = 64;
+	initializer.is.maxPointLightsPerTile = 4;
+	initializer.is.maxSpotLightsPerTile = 4;
+	initializer.is.maxSpotTexLightsPerTile = 4;
 #endif
 
 	MainRendererSingleton::get().init(initializer);