1 /** 2 * Copyright: Copyright (C) 2018 Gabriel Gheorghe, All Rights Reserved 3 * Authors: $(Gabriel Gheorghe) 4 * License: $(LINK2 https://www.gnu.org/licenses/gpl-3.0.txt, GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007) 5 * Source: $(LINK2 https://github.com/GabyForceQ/LibertyEngine/blob/master/source/liberty/graphics/texture/cache.d) 6 * Documentation: 7 * Coverage: 8 **/ 9 module liberty.graphics.texture.cache; 10 11 import liberty.image.io; 12 import liberty.graphics.texture.impl; 13 import liberty.graphics.texture.io; 14 import liberty.logger.impl; 15 16 /** 17 * 18 **/ 19 class TextureCache { 20 private { 21 Texture[string] _textureMap; 22 } 23 24 /** 25 * 26 **/ 27 Texture getTexture(string path) { 28 // Check if texture is in the map 29 // If it's not then load a new one and return it 30 if (path !in _textureMap) { 31 import std.array : split; 32 33 Texture tex; 34 35 // Check extension 36 string[] splitArray = path.split("."); 37 immutable extension = splitArray[$ - 1]; 38 switch (extension) { 39 case "bmp": 40 tex = TextureIO.loadBMP(path); 41 break; 42 default: 43 Logger.error( 44 "File format not supported for texture data: " ~ extension, 45 typeof(this).stringof 46 ); 47 } 48 49 _textureMap[path] = tex; 50 tex.setRealtivePath(path); 51 return tex; 52 } 53 54 // Otherwise return the loaded texture 55 return _textureMap[path]; 56 } 57 58 /** 59 * 60 **/ 61 Texture getCubeMapTexture(string[6] paths) { 62 Texture tex = TextureIO.loadCubeMap(paths); 63 _textureMap[paths[0]] = tex; 64 tex.setRealtivePath(paths[0]); 65 return tex; 66 } 67 }