NLS Engine  v0.1
The Next Logical Step in game engine design.
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Defines
BasicTexture.cpp
Go to the documentation of this file.
00001 
00010 #pragma once
00011 
00012 #include "BasicTexture.h"
00013 
00014 // Standard Includes
00015 #include <string>
00016 
00017 // Library Includes
00018 #include <EngineConfig.h>
00019 
00020 // Local Includes
00021 #include "../../sharedbase/EventLogger.h"
00022 
00023 // Forward Declarations
00024 
00025 // Typedefs
00026 
00027 // Local Constants
00028 
00029 namespace GraphicsCore {
00030   TextureCacheMap BasicTexture::TextureCache;
00031 }
00032 
00033 namespace GraphicsCore {
00034   BasicTexture::BasicTexture() :
00035     texture(nullptr)
00036     {
00037   }
00038   
00039   BasicTexture::BasicTexture(RenderModule* renderModule, std::string file) {
00040     this->LoadTextureFromFile(renderModule, file);
00041   }
00042   
00043   BasicTexture::~BasicTexture() {
00044   }
00045   
00046   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00047   void BasicTexture::Apply(LPDIRECT3DDEVICE9 pd3dDevice) {
00048     // NOTE: null textures are allowed: they are the only way to clear out a previous texture!
00049     pd3dDevice->SetTexture(0, this->texture);
00050   }
00051   
00052   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00053   void BasicTexture::LoadTextureFromFile(RenderModule* renderModule, std::string file) {
00054     if (file.length()) {
00055       TextureCacheMap::const_iterator tex_cache_it = BasicTexture::TextureCache.find(file);
00056       if (tex_cache_it != BasicTexture::TextureCache.end()) {
00057         LOG(LOG_PRIORITY::FLOW, "Texture cache hit on file '" + file + "'!");
00058         
00059         this->texture = tex_cache_it->second;
00060       }
00061       else {
00062         LOG(LOG_PRIORITY::FLOW, "Texture cache miss on file '" + file + "', loading from disk and storing in cache.");
00063         
00064         HRESULT result = renderModule->TextureFactory(NLS_ENGINE_DATA_PATH + file, &this->texture);
00065         
00066         BasicTexture::TextureCache.insert(TextureCacheMapValue(file, this->texture));
00067         
00068         if (FAILED(result)) {
00069           std::string error;
00070           switch (result) {
00071             case D3DERR_NOTAVAILABLE:
00072               error = "Direct3D device requested is not availiable.";
00073             break;
00074             case D3DERR_OUTOFVIDEOMEMORY:
00075               error = "There isn't enough video memory to create Direct3D device.";
00076             break;
00077             case D3DERR_INVALIDCALL:
00078               error = "Invalid Direct3D device creation call.";
00079             break;
00080             case D3DXERR_INVALIDDATA:
00081               error = "Invalid Direct3D data call.";
00082             break;
00083             case E_OUTOFMEMORY:
00084               error = "Out of System memory.";
00085             break;
00086             default:
00087               error = "Unexpected error; do some debugging.";
00088             break;
00089           }
00090           
00091           LOG(LOG_PRIORITY::INFO, error + "  Could not create texture from file: " + file);
00092         }
00093       }
00094     }
00095     else {
00096       this->texture = nullptr;
00097     }
00098   }
00099 }