Intro :
Pour gérer nos fichiers textures au sein d’un moteur 3D, nous allons utiliser un « TextureManager ».
Prérequis :
– Savoir utiliser la classe Singleton. Voir cet article
Explications :
Voici le fichier TextureManager.h :
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#ifndef TEXTURE_MANAGER_H #define TEXTURE_MANAGER_H class TextureManager : public Singleton<TextureManager> { public: TextureManager(); virtual ~TextureManager(); ID3D10ShaderResourceView* AddTexture(const std::string& sTextureFileName, const std::string& sTextureAliasName); bool HasTexture(const std::string& sTextureAliasName); ID3D10ShaderResourceView* GetTexture(const std::string& sTextureAliasName); // Charge la texture dans la mémoire et si elle est déjà chargée, elle renvoit le pointeur de la ressource ID3D10ShaderResourceView* LoadTexture(const std::string& sTextureFilename); void RemoveTexture(const std::string& sTextureAliasName); void RemoveTexture(ID3D10ShaderResourceView* pTextureRV); private: std::map<std::string, ID3D10ShaderResourceView*> m_texturesAlias; }; #endif |
Voici le fichier TextureManager.cpp :
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
#include "TextureManager.h" template<> TextureManager* Singleton<TextureManager>::ms_instance = nullptr; TextureManager::TextureManager() { } TextureManager::~TextureManager() { std::map<std::string, ID3D10ShaderResourceView*>::iterator it; for (it = m_texturesAlias.begin(); it != m_texturesAlias.end(); it++) { ID3D10ShaderResourceView* pTextureRV = (it->second); SAFE_RELEASE(pTextureRV); } } ID3D10ShaderResourceView* TextureManager::AddTexture(const std::string& sTextureFileName, const std::string& sTextureAliasName) { HRESULT hr; // On charge la texture ID3D10ShaderResourceView* pTextureRV = nullptr; hr = D3DX10CreateShaderResourceViewFromFileA(D3D10_RENDERER->GetDevice(), sTextureFileName.c_str(), nullptr, nullptr, &pTextureRV, nullptr); if (FAILED(hr)) { ShowMessageBoxDXError(hr); return nullptr; } if (m_texturesAlias.count(sTextureAliasName) == 0) { m_texturesAlias[sTextureAliasName] = pTextureRV; } else { std::cout << Formater(L"Texture déjà enregistrée !") << std::endl; } return pTextureRV; } bool TextureManager::HasTexture(const std::string& sTextureAliasName) { return m_texturesAlias.count(sTextureAliasName) != 0; } ID3D10ShaderResourceView* TextureManager::GetTexture(const std::string& sTextureAliasName) { if (m_texturesAlias.count(sTextureAliasName) != 0) { return m_texturesAlias[sTextureAliasName]; } else { return nullptr; } } void TextureManager::RemoveTexture(const std::string& sTextureAliasName) { Assert(m_texturesAlias.count(sTextureAliasName) > 0); ID3D10ShaderResourceView* pRV = m_texturesAlias[sTextureAliasName]; SAFE_RELEASE(pRV); m_texturesAlias.erase(sTextureAliasName); } void TextureManager::RemoveTexture(ID3D10ShaderResourceView* pTextureRV) { std::string sTextureAliasName = ""; for (auto it = m_texturesAlias.begin(); it != m_texturesAlias.end(); it++) { sTextureAliasName = it->first; ID3D10ShaderResourceView* pRV = it->second; if (pRV == pTextureRV) { SAFE_RELEASE(pRV); } } m_texturesAlias.erase(sTextureAliasName); } ID3D10ShaderResourceView* TextureManager::LoadTexture(const std::string& sTextureFilename) { if (HasTexture(sTextureFilename)) { return GetTexture(sTextureFilename); } else { return AddTexture(sTextureFilename, sTextureFilename); } } |
Résumé :
Nous avons présenté une classe gestionnaire de chargement de textures.


