Added resource managers

Former-commit-id: 9c071230c1e325ec672c9dfe30ee7e498baa1d08
This commit is contained in:
Lynix
2015-01-28 20:53:40 +01:00
parent 6324d21c67
commit 7a73314ef5
16 changed files with 282 additions and 56 deletions

View File

@@ -17,6 +17,7 @@
#include <Nazara/Core/RefCounted.hpp>
#include <Nazara/Core/Resource.hpp>
#include <Nazara/Core/ResourceLoader.hpp>
#include <Nazara/Core/ResourceManager.hpp>
struct NzSoundBufferParams
{
@@ -33,6 +34,7 @@ using NzSoundBufferConstRef = NzObjectRef<const NzSoundBuffer>;
using NzSoundBufferLibrary = NzObjectLibrary<NzSoundBuffer>;
using NzSoundBufferListener = NzObjectListenerWrapper<NzSoundBuffer>;
using NzSoundBufferLoader = NzResourceLoader<NzSoundBuffer, NzSoundBufferParams>;
using NzSoundBufferManager = NzResourceManager<NzSoundBuffer, NzSoundBufferParams>;
using NzSoundBufferRef = NzObjectRef<NzSoundBuffer>;
struct NzSoundBufferImpl;
@@ -42,6 +44,7 @@ class NAZARA_API NzSoundBuffer : public NzRefCounted, public NzResource, NzNonCo
friend NzSound;
friend NzSoundBufferLibrary;
friend NzSoundBufferLoader;
friend NzSoundBufferManager;
friend class NzAudio;
public:
@@ -77,6 +80,8 @@ class NAZARA_API NzSoundBuffer : public NzRefCounted, public NzResource, NzNonCo
static NzSoundBufferLibrary::LibraryMap s_library;
static NzSoundBufferLoader::LoaderList s_loaders;
static NzSoundBufferManager::ManagerMap s_managerMap;
static NzSoundBufferManager::ManagerParams s_managerParameters;
};
#include <Nazara/Audio/SoundBuffer.inl>

View File

@@ -0,0 +1,43 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_RESOURCEMANAGER_HPP
#define NAZARA_RESOURCEMANAGER_HPP
#include <Nazara/Core/ObjectRef.hpp>
#include <Nazara/Core/String.hpp>
#include <unordered_map>
template<typename Type, typename Parameters>
class NzResourceManager
{
friend Type;
public:
NzResourceManager() = delete;
~NzResourceManager() = delete;
static void Clear();
static NzObjectRef<Type> Get(const NzString& filePath);
static const Parameters& GetDefaultParameters();
static void Purge();
static void Register(const NzString& filePath, NzObjectRef<Type> resource);
static void SetDefaultParameters(const Parameters& params);
static void Unregister(const NzString& filePath);
private:
static bool Initialize();
static void Uninitialize();
using ManagerMap = std::unordered_map<NzString, NzObjectRef<Type>>;
using ManagerParams = Parameters;
};
#include <Nazara/Core/ResourceManager.inl>
#endif // NAZARA_RESOURCEMANAGER_HPP

View File

@@ -0,0 +1,100 @@
// Copyright (C) 2015 Jérôme Leclercq
// This file is part of the "Nazara Engine - Core module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/File.hpp>
#include <Nazara/Core/Log.hpp>
#include <Nazara/Core/Debug.hpp>
template<typename Type, typename Parameters>
void NzResourceManager<Type, Parameters>::Clear()
{
Type::s_managerMap.clear();
}
template<typename Type, typename Parameters>
NzObjectRef<Type> NzResourceManager<Type, Parameters>::Get(const NzString& filePath)
{
NzObjectRef<Type> ref;
NzString absolutePath = NzFile::AbsolutePath(filePath);
auto it = Type::s_managerMap.find(absolutePath);
if (it == Type::s_managerMap.end())
{
NzObjectRef<Type> resource = Type::New();
if (!resource)
{
NazaraError("Failed to create resource");
return ref;
}
if (!resource->LoadFromFile(absolutePath, GetDefaultParameters()))
{
NazaraError("Failed to load resource from file: " + absolutePath);
return ref;
}
NazaraDebug("Loaded resource from file " + absolutePath);
it = Type::s_managerMap.insert(std::make_pair(absolutePath, resource)).first;
}
return it->second;
}
template<typename Type, typename Parameters>
const Parameters& NzResourceManager<Type, Parameters>::GetDefaultParameters()
{
return Type::s_managerParameters;
}
template<typename Type, typename Parameters>
void NzResourceManager<Type, Parameters>::Purge()
{
auto it = Type::s_managerMap.begin();
while (it != Type::s_managerMap.end())
{
const NzObjectRef<Type>& ref = it->second;
if (ref.GetReferenceCount() == 1) // Sommes-nous les seuls à détenir la ressource ?
Type::s_managerMap.erase(it++); // Alors on la supprime
else
++it;
}
}
template<typename Type, typename Parameters>
void NzResourceManager<Type, Parameters>::Register(const NzString& filePath, NzObjectRef<Type> resource)
{
NzString absolutePath = NzFile::AbsolutePath(filePath);
Type::s_managerMap[absolutePath] = resource;
}
template<typename Type, typename Parameters>
void NzResourceManager<Type, Parameters>::SetDefaultParameters(const Parameters& params)
{
Type::s_managerParameters = params;
}
template<typename Type, typename Parameters>
void NzResourceManager<Type, Parameters>::Unregister(const NzString& filePath)
{
NzString absolutePath = NzFile::AbsolutePath(filePath);
Type::s_managerMap.erase(absolutePath);
}
template<typename Type, typename Parameters>
bool NzResourceManager<Type, Parameters>::Initialize()
{
return true;
}
template<typename Type, typename Parameters>
void NzResourceManager<Type, Parameters>::Uninitialize()
{
Clear();
}
#include <Nazara/Core/DebugOff.hpp>

View File

@@ -15,6 +15,7 @@
#include <Nazara/Core/RefCounted.hpp>
#include <Nazara/Core/Resource.hpp>
#include <Nazara/Core/ResourceLoader.hpp>
#include <Nazara/Core/ResourceManager.hpp>
#include <Nazara/Core/String.hpp>
#include <Nazara/Graphics/Enums.hpp>
#include <Nazara/Renderer/RenderStates.hpp>
@@ -42,12 +43,14 @@ using NzMaterialConstRef = NzObjectRef<const NzMaterial>;
using NzMaterialLibrary = NzObjectLibrary<NzMaterial>;
using NzMaterialListener = NzObjectListenerWrapper<NzMaterial>;
using NzMaterialLoader = NzResourceLoader<NzMaterial, NzMaterialParams>;
using NzMaterialManager = NzResourceManager<NzMaterial, NzMaterialParams>;
using NzMaterialRef = NzObjectRef<NzMaterial>;
class NAZARA_API NzMaterial : public NzRefCounted, public NzResource
{
friend NzMaterialLibrary;
friend NzMaterialLoader;
friend NzMaterialManager;
friend class NzGraphics;
public:
@@ -106,37 +109,37 @@ class NAZARA_API NzMaterial : public NzRefCounted, public NzResource
void Reset();
bool SetAlphaMap(const NzString& texturePath);
bool SetAlphaMap(const NzString& name);
void SetAlphaMap(NzTexture* map);
void SetAlphaThreshold(float alphaThreshold);
void SetAmbientColor(const NzColor& ambient);
void SetDepthFunc(nzRendererComparison depthFunc);
void SetDiffuseColor(const NzColor& diffuse);
bool SetDiffuseMap(const NzString& texturePath);
bool SetDiffuseMap(const NzString& name);
void SetDiffuseMap(NzTexture* map);
void SetDiffuseSampler(const NzTextureSampler& sampler);
void SetDstBlend(nzBlendFunc func);
bool SetEmissiveMap(const NzString& texturePath);
bool SetEmissiveMap(const NzString& name);
void SetEmissiveMap(NzTexture* map);
void SetFaceCulling(nzFaceSide faceSide);
void SetFaceFilling(nzFaceFilling filling);
bool SetHeightMap(const NzString& texturePath);
bool SetHeightMap(const NzString& name);
void SetHeightMap(NzTexture* map);
bool SetNormalMap(const NzString& texturePath);
bool SetNormalMap(const NzString& name);
void SetNormalMap(NzTexture* map);
void SetRenderStates(const NzRenderStates& states);
void SetShader(const NzUberShader* uberShader);
bool SetShader(const NzString& uberShaderName);
void SetShininess(float shininess);
void SetSpecularColor(const NzColor& specular);
bool SetSpecularMap(const NzString& texturePath);
bool SetSpecularMap(const NzString& name);
void SetSpecularMap(NzTexture* map);
void SetSpecularSampler(const NzTextureSampler& sampler);
void SetSrcBlend(nzBlendFunc func);
NzMaterial& operator=(const NzMaterial& material);
static NzMaterial* GetDefault();
static NzMaterialRef GetDefault();
template<typename... Args> static NzMaterialRef New(Args&&... args);
private:
@@ -177,6 +180,8 @@ class NAZARA_API NzMaterial : public NzRefCounted, public NzResource
static NzMaterialLibrary::LibraryMap s_library;
static NzMaterialLoader::LoaderList s_loaders;
static NzMaterialManager::ManagerMap s_managerMap;
static NzMaterialManager::ManagerParams s_managerParameters;
static NzMaterialRef s_defaultMaterial;
};

View File

@@ -14,6 +14,7 @@
#include <Nazara/Core/ObjectRef.hpp>
#include <Nazara/Core/RefCounted.hpp>
#include <Nazara/Core/Resource.hpp>
#include <Nazara/Core/ResourceManager.hpp>
#include <Nazara/Renderer/Enums.hpp>
#include <Nazara/Utility/AbstractImage.hpp>
#include <Nazara/Utility/CubemapParams.hpp>
@@ -25,6 +26,7 @@ using NzTextureConstListener = NzObjectListenerWrapper<const NzTexture>;
using NzTextureConstRef = NzObjectRef<const NzTexture>;
using NzTextureLibrary = NzObjectLibrary<NzTexture>;
using NzTextureListener = NzObjectListenerWrapper<NzTexture>;
using NzTextureManager = NzResourceManager<NzTexture, NzImageParams>;
using NzTextureRef = NzObjectRef<NzTexture>;
struct NzTextureImpl;
@@ -32,6 +34,7 @@ struct NzTextureImpl;
class NAZARA_API NzTexture : public NzAbstractImage, public NzRefCounted, public NzResource, NzNonCopyable
{
friend NzTextureLibrary;
friend NzTextureManager;
friend class NzRenderer;
public:
@@ -115,6 +118,8 @@ class NAZARA_API NzTexture : public NzAbstractImage, public NzRefCounted, public
NzTextureImpl* m_impl = nullptr;
static NzTextureLibrary::LibraryMap s_library;
static NzTextureManager::ManagerMap s_managerMap;
static NzTextureManager::ManagerParams s_managerParameters;
};
#include <Nazara/Renderer/Texture.inl>

View File

@@ -14,6 +14,7 @@
#include <Nazara/Core/RefCounted.hpp>
#include <Nazara/Core/Resource.hpp>
#include <Nazara/Core/ResourceLoader.hpp>
#include <Nazara/Core/ResourceManager.hpp>
#include <Nazara/Core/String.hpp>
#include <Nazara/Utility/Enums.hpp>
#include <Nazara/Utility/Sequence.hpp>
@@ -37,6 +38,7 @@ using NzAnimationConstRef = NzObjectRef<const NzAnimation>;
using NzAnimationLibrary = NzObjectLibrary<NzAnimation>;
using NzAnimationListener = NzObjectListenerWrapper<NzAnimation>;
using NzAnimationLoader = NzResourceLoader<NzAnimation, NzAnimationParams>;
using NzAnimationManager = NzResourceManager<NzAnimation, NzAnimationParams>;
using NzAnimationRef = NzObjectRef<NzAnimation>;
struct NzAnimationImpl;
@@ -45,6 +47,7 @@ class NAZARA_API NzAnimation : public NzRefCounted, public NzResource
{
friend NzAnimationLibrary;
friend NzAnimationLoader;
friend NzAnimationManager;
friend class NzUtility;
public:
@@ -94,6 +97,8 @@ class NAZARA_API NzAnimation : public NzRefCounted, public NzResource
static NzAnimationLibrary::LibraryMap s_library;
static NzAnimationLoader::LoaderList s_loaders;
static NzAnimationManager::ManagerMap s_managerMap;
static NzAnimationManager::ManagerParams s_managerParameters;
};
#include <Nazara/Utility/Animation.inl>

View File

@@ -2,6 +2,8 @@
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp
///TODO: FontManager ?
#pragma once
#ifndef NAZARA_FONT_HPP

View File

@@ -16,6 +16,7 @@
#include <Nazara/Core/RefCounted.hpp>
#include <Nazara/Core/Resource.hpp>
#include <Nazara/Core/ResourceLoader.hpp>
#include <Nazara/Core/ResourceManager.hpp>
#include <Nazara/Utility/AbstractImage.hpp>
#include <Nazara/Utility/CubemapParams.hpp>
#include <atomic>
@@ -40,12 +41,14 @@ using NzImageConstRef = NzObjectRef<const NzImage>;
using NzImageLibrary = NzObjectLibrary<NzImage>;
using NzImageListener = NzObjectListenerWrapper<NzImage>;
using NzImageLoader = NzResourceLoader<NzImage, NzImageParams>;
using NzImageManager = NzResourceManager<NzImage, NzImageParams>;
using NzImageRef = NzObjectRef<NzImage>;
class NAZARA_API NzImage : public NzAbstractImage, public NzRefCounted, public NzResource
{
friend NzImageLibrary;
friend NzImageLoader;
friend NzImageManager;
friend class NzUtility;
public:
@@ -156,6 +159,8 @@ class NAZARA_API NzImage : public NzAbstractImage, public NzRefCounted, public N
static NzImageLibrary::LibraryMap s_library;
static NzImageLoader::LoaderList s_loaders;
static NzImageManager::ManagerMap s_managerMap;
static NzImageManager::ManagerParams s_managerParameters;
};
#include <Nazara/Utility/Image.inl>

View File

@@ -16,6 +16,7 @@
#include <Nazara/Core/RefCounted.hpp>
#include <Nazara/Core/Resource.hpp>
#include <Nazara/Core/ResourceLoader.hpp>
#include <Nazara/Core/ResourceManager.hpp>
#include <Nazara/Core/String.hpp>
#include <Nazara/Math/Box.hpp>
#include <Nazara/Utility/Skeleton.hpp>
@@ -59,6 +60,7 @@ using NzMeshConstRef = NzObjectRef<const NzMesh>;
using NzMeshLibrary = NzObjectLibrary<NzMesh>;
using NzMeshListener = NzObjectListenerWrapper<NzMesh>;
using NzMeshLoader = NzResourceLoader<NzMesh, NzMeshParams>;
using NzMeshManager = NzResourceManager<NzMesh, NzMeshParams>;
using NzMeshRef = NzObjectRef<NzMesh>;
struct NzMeshImpl;
@@ -67,6 +69,7 @@ class NAZARA_API NzMesh : public NzRefCounted, public NzResource
{
friend NzMeshLibrary;
friend NzMeshLoader;
friend NzMeshManager;
friend class NzUtility;
public:
@@ -137,6 +140,8 @@ class NAZARA_API NzMesh : public NzRefCounted, public NzResource
static NzMeshLibrary::LibraryMap s_library;
static NzMeshLoader::LoaderList s_loaders;
static NzMeshManager::ManagerMap s_managerMap;
static NzMeshManager::ManagerParams s_managerParameters;
};
#include <Nazara/Utility/Mesh.inl>