{"id":3129,"date":"2015-07-12T16:14:11","date_gmt":"2015-07-12T16:14:11","guid":{"rendered":"http:\/\/anthroponaute.fr\/blog-informatique\/?p=3129"},"modified":"2015-07-28T11:11:55","modified_gmt":"2015-07-28T11:11:55","slug":"une-classe-gestionnaire-de-textures-pour-directx","status":"publish","type":"post","link":"https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/?p=3129","title":{"rendered":"Une classe gestionnaire de textures pour DirectX 10"},"content":{"rendered":"<p><a href=\"https:\/\/anthropoya.cluster014.ovh.net\/blog-informatique\/wp-content\/uploads\/2015\/07\/texture.jpg\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-3130\" src=\"https:\/\/anthropoya.cluster014.ovh.net\/blog-informatique\/wp-content\/uploads\/2015\/07\/texture.jpg\" alt=\"texture\" width=\"296\" height=\"296\" srcset=\"https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/wp-content\/uploads\/2015\/07\/texture.jpg 1024w, https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/wp-content\/uploads\/2015\/07\/texture-150x150.jpg 150w, https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/wp-content\/uploads\/2015\/07\/texture-300x300.jpg 300w, https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/wp-content\/uploads\/2015\/07\/texture-624x624.jpg 624w\" sizes=\"(max-width: 296px) 100vw, 296px\" \/><\/a><\/p>\n<p><strong>Intro :<\/strong><\/p>\n<p>Pour g\u00e9rer nos fichiers textures au sein d&rsquo;un moteur 3D, nous allons utiliser un \u00ab\u00a0<strong>TextureManager\u00a0\u00bb<\/strong>.<\/p>\n<p><strong>Pr\u00e9requis :<\/strong><\/p>\n<p>&#8211; Savoir utiliser la classe Singleton. Voir cet article<\/p>\n<p><strong>Explications :<\/strong><\/p>\n<p>Voici le fichier TextureManager.h :<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#ifndef TEXTURE_MANAGER_H\r\n#define TEXTURE_MANAGER_H\r\n\r\nclass TextureManager : public Singleton&lt;TextureManager&gt;\r\n{\r\npublic:\r\n\u00a0\u00a0 \u00a0TextureManager();\r\n\u00a0\u00a0 \u00a0virtual ~TextureManager();\r\n\r\n\u00a0\u00a0 \u00a0ID3D10ShaderResourceView* AddTexture(const std::string&amp; sTextureFileName, const std::string&amp; sTextureAliasName);\r\n\r\n\u00a0\u00a0 \u00a0bool HasTexture(const std::string&amp; sTextureAliasName);\r\n\r\n\u00a0\u00a0 \u00a0ID3D10ShaderResourceView* GetTexture(const std::string&amp; sTextureAliasName);\r\n\r\n\u00a0\u00a0\u00a0 \/\/ Charge la texture dans la m\u00e9moire et si elle est d\u00e9j\u00e0 charg\u00e9e, elle renvoit le pointeur de la ressource\r\n\u00a0\u00a0 \u00a0ID3D10ShaderResourceView* LoadTexture(const std::string&amp; sTextureFilename);\r\n\r\n\u00a0\u00a0\u00a0 void RemoveTexture(const std::string&amp; sTextureAliasName);\r\n\u00a0\u00a0\u00a0 void RemoveTexture(ID3D10ShaderResourceView* pTextureRV);\r\n\r\nprivate:\r\n\u00a0\u00a0 \u00a0std::map&lt;std::string, ID3D10ShaderResourceView*&gt; m_texturesAlias;\r\n};\r\n\r\n#endif\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Voici le fichier TextureManager.cpp :<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &quot;TextureManager.h&quot;\r\n\r\ntemplate&lt;&gt; TextureManager* Singleton&lt;TextureManager&gt;::ms_instance = nullptr;\r\n\r\nTextureManager::TextureManager()\r\n{\r\n}\r\n\r\nTextureManager::~TextureManager()\r\n{\r\n\u00a0\u00a0 \u00a0std::map&lt;std::string, ID3D10ShaderResourceView*&gt;::iterator it;\r\n\r\n\u00a0\u00a0 \u00a0for (it = m_texturesAlias.begin(); it != m_texturesAlias.end(); it++)\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0ID3D10ShaderResourceView* pTextureRV = (it-&gt;second);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0SAFE_RELEASE(pTextureRV);\r\n\u00a0\u00a0 \u00a0}\r\n}\r\n\r\nID3D10ShaderResourceView* TextureManager::AddTexture(const std::string&amp; sTextureFileName, const std::string&amp; sTextureAliasName)\r\n{\r\n\u00a0\u00a0 \u00a0HRESULT hr;\r\n\r\n\u00a0\u00a0\u00a0 \/\/ On charge la texture\r\n\u00a0\u00a0 \u00a0ID3D10ShaderResourceView* pTextureRV = nullptr;\r\n\r\n\u00a0\u00a0 \u00a0hr = D3DX10CreateShaderResourceViewFromFileA(D3D10_RENDERER-&gt;GetDevice(), sTextureFileName.c_str(), nullptr, nullptr, &amp;pTextureRV, nullptr);\r\n\r\n\u00a0\u00a0\u00a0 if (FAILED(hr))\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0ShowMessageBoxDXError(hr);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return nullptr;\r\n\u00a0\u00a0 \u00a0}\r\n\r\n\u00a0\u00a0 \u00a0if (m_texturesAlias.count(sTextureAliasName) == 0)\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0m_texturesAlias[sTextureAliasName] = pTextureRV;\r\n\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0else\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0std::cout &lt;&lt; Formater(L&quot;Texture d\u00e9j\u00e0 enregistr\u00e9e !&quot;) &lt;&lt; std::endl;\r\n\u00a0\u00a0 \u00a0}\r\n\r\n\u00a0\u00a0 \u00a0return pTextureRV;\r\n}\r\n\r\nbool TextureManager::HasTexture(const std::string&amp; sTextureAliasName)\r\n{\r\n\u00a0\u00a0 \u00a0return m_texturesAlias.count(sTextureAliasName) != 0;\r\n}\r\n\r\nID3D10ShaderResourceView* TextureManager::GetTexture(const std::string&amp; sTextureAliasName)\r\n{\r\n\u00a0\u00a0 \u00a0if (m_texturesAlias.count(sTextureAliasName) != 0)\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return m_texturesAlias[sTextureAliasName];\r\n\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0else\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return nullptr;\r\n\u00a0\u00a0 \u00a0}\r\n}\r\n\r\nvoid TextureManager::RemoveTexture(const std::string&amp; sTextureAliasName)\r\n{\r\n\u00a0\u00a0 \u00a0Assert(m_texturesAlias.count(sTextureAliasName) &gt; 0);\r\n\r\n\u00a0\u00a0 \u00a0ID3D10ShaderResourceView* pRV = m_texturesAlias[sTextureAliasName];\r\n\r\n\u00a0\u00a0 \u00a0SAFE_RELEASE(pRV);\r\n\r\n\u00a0\u00a0 \u00a0m_texturesAlias.erase(sTextureAliasName);\r\n}\r\n\r\nvoid TextureManager::RemoveTexture(ID3D10ShaderResourceView* pTextureRV)\r\n{\r\n\u00a0\u00a0 \u00a0std::string sTextureAliasName = &quot;&quot;;\r\n\r\n\u00a0\u00a0 \u00a0for (auto it = m_texturesAlias.begin(); it != m_texturesAlias.end(); it++)\r\n\u00a0\u00a0 \u00a0{\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0sTextureAliasName = it-&gt;first;\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0ID3D10ShaderResourceView* pRV = it-&gt;second;\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0if (pRV == pTextureRV)\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0SAFE_RELEASE(pRV);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0m_texturesAlias.erase(sTextureAliasName);\r\n}\r\n\r\nID3D10ShaderResourceView* TextureManager::LoadTexture(const std::string&amp; sTextureFilename)\r\n{\r\n\u00a0\u00a0 \u00a0if (HasTexture(sTextureFilename))\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return GetTexture(sTextureFilename);\r\n\u00a0\u00a0 \u00a0}\r\n\u00a0\u00a0 \u00a0else\r\n\u00a0\u00a0 \u00a0{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0return AddTexture(sTextureFilename, sTextureFilename);\r\n\u00a0\u00a0 \u00a0}\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>R\u00e9sum\u00e9 :<\/strong><\/p>\n<p>Nous avons pr\u00e9sent\u00e9 une classe gestionnaire de chargement de textures.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Intro : Pour g\u00e9rer nos fichiers textures au sein d&rsquo;un moteur 3D, nous allons utiliser un \u00ab\u00a0TextureManager\u00a0\u00bb. Pr\u00e9requis : &#8211; Savoir utiliser la classe Singleton. Voir cet article Explications : Voici le fichier TextureManager.h : &nbsp; Voici le fichier TextureManager.cpp : &nbsp; R\u00e9sum\u00e9 : Nous avons pr\u00e9sent\u00e9 une classe gestionnaire de chargement de textures.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"_links":{"self":[{"href":"https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/index.php?rest_route=\/wp\/v2\/posts\/3129"}],"collection":[{"href":"https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3129"}],"version-history":[{"count":15,"href":"https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/index.php?rest_route=\/wp\/v2\/posts\/3129\/revisions"}],"predecessor-version":[{"id":3167,"href":"https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/index.php?rest_route=\/wp\/v2\/posts\/3129\/revisions\/3167"}],"wp:attachment":[{"href":"https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3129"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3129"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.la-porte-des-nebuleuses.net\/blog-informatique\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3129"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}