1 /* 2 Copyright (c) 2017-2018 Timur Gafarov 3 4 Boost Software License - Version 1.0 - August 17th, 2003 5 Permission is hereby granted, free of charge, to any person or organization 6 obtaining a copy of the software and accompanying documentation covered by 7 this license (the "Software") to use, reproduce, display, distribute, 8 execute, and transmit the Software, and to prepare derivative works of the 9 Software, and to permit third-parties to whom the Software is furnished to 10 do so, all subject to the following: 11 12 The copyright notices in the Software and this entire statement, including 13 the above license grant, this restriction and the following disclaimer, 14 must be included in all copies of the Software, in whole or in part, and 15 all derivative works of the Software, unless such copies or derivative 16 works are solely in the form of machine-executable object code generated by 17 a source language processor. 18 19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 22 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 23 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 24 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 DEALINGS IN THE SOFTWARE. 26 */ 27 28 module dagon.resource.textureasset; 29 30 import std.stdio; 31 import std.path; 32 33 import dlib.core.memory; 34 import dlib.core.stream; 35 import dlib.core.compound; 36 import dlib.image.image; 37 import dlib.image.io.bmp; 38 import dlib.image.io.png; 39 import dlib.image.io.tga; 40 import dlib.image.io.jpeg; 41 import dlib.image.io.hdr; 42 import dlib.image.unmanaged; 43 import dlib.image.hdri; 44 import dlib.filesystem.filesystem; 45 46 import dagon.core.ownership; 47 import dagon.resource.asset; 48 import dagon.graphics.texture; 49 50 class TextureAsset: Asset 51 { 52 UnmanagedImageFactory imageFactory; 53 UnmanagedHDRImageFactory hdrImageFactory; 54 Texture texture; 55 56 this(UnmanagedImageFactory imgfac, UnmanagedHDRImageFactory hdrImgFac, Owner o) 57 { 58 super(o); 59 imageFactory = imgfac; 60 hdrImageFactory = hdrImgFac; 61 texture = New!Texture(this); 62 } 63 64 ~this() 65 { 66 release(); 67 } 68 69 override bool loadThreadSafePart(string filename, InputStream istrm, ReadOnlyFileSystem fs, AssetManager mngr) 70 { 71 string errMsg; 72 73 if (filename.extension == ".hdr" || 74 filename.extension == ".HDR") 75 { 76 Compound!(SuperHDRImage, string) res; 77 res = loadHDR(istrm, hdrImageFactory); 78 texture.image = res[0]; 79 errMsg = res[1]; 80 } 81 else 82 { 83 Compound!(SuperImage, string) res; 84 85 switch(filename.extension) 86 { 87 case ".bmp", ".BMP": 88 res = loadBMP(istrm, imageFactory); 89 break; 90 case ".jpg", ".JPG", ".jpeg", ".JPEG": 91 res = loadJPEG(istrm, imageFactory); 92 break; 93 case ".png", ".PNG": 94 res = loadPNG(istrm, imageFactory); 95 break; 96 case ".tga", ".TGA": 97 res = loadTGA(istrm, imageFactory); 98 break; 99 default: 100 return false; 101 } 102 103 texture.image = res[0]; 104 errMsg = res[1]; 105 } 106 107 if (texture.image !is null) 108 { 109 return true; 110 } 111 else 112 { 113 writeln(errMsg); 114 return false; 115 } 116 } 117 118 override bool loadThreadUnsafePart() 119 { 120 if (texture.image !is null) 121 { 122 texture.createFromImage(texture.image); 123 if (texture.valid) 124 { 125 return true; 126 } 127 else 128 return false; 129 } 130 else 131 { 132 return false; 133 } 134 } 135 136 override void release() 137 { 138 if (texture) 139 texture.release(); 140 } 141 } 142 143 TextureAsset textureAsset(AssetManager assetManager, string filename) 144 { 145 TextureAsset asset; 146 if (assetManager.assetExists(filename)) 147 { 148 asset = cast(TextureAsset)assetManager.getAsset(filename); 149 } 150 else 151 { 152 asset = New!TextureAsset(assetManager.imageFactory, assetManager.hdrImageFactory, assetManager); 153 assetManager.preloadAsset(asset, filename); 154 } 155 return asset; 156 }