1 /* 2 Copyright (c) 2017-2022 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.texture; 29 30 import std.stdio; 31 import std.path; 32 33 import dlib.core.memory; 34 import dlib.core.ownership; 35 import dlib.core.stream; 36 import dlib.core.compound; 37 import dlib.image.image; 38 import dlib.image.io; 39 import dlib.image.unmanaged; 40 import dlib.filesystem.filesystem; 41 42 import dagon.graphics.texture; 43 import dagon.resource.asset; 44 import dagon.resource.dds; 45 46 class TextureAsset: Asset 47 { 48 Texture texture; 49 SuperImage image; 50 protected TextureBuffer buffer; 51 52 this(Owner o) 53 { 54 super(o); 55 texture = New!Texture(this); 56 } 57 58 ~this() 59 { 60 release(); 61 } 62 63 override bool loadThreadSafePart(string filename, InputStream istrm, ReadOnlyFileSystem fs, AssetManager assetManager) 64 { 65 string format = filename.extension; 66 if (format == ".dds" || format == ".DDS") 67 { 68 // Load to TextureBuffer 69 loadDDS(istrm, &buffer); 70 return true; 71 } 72 // TODO: KTX support 73 else 74 { 75 // Load to SuperImage 76 auto res = assetManager.loadImage(filename.extension, istrm); 77 image = res[0]; 78 string errMsg = res[1]; 79 if (image !is null) 80 { 81 return true; 82 } 83 else 84 { 85 writeln(errMsg); 86 return false; 87 } 88 } 89 } 90 91 override bool loadThreadUnsafePart() 92 { 93 if (texture.valid) 94 return true; 95 96 if (image !is null) 97 { 98 texture.createFromImage(image, true); 99 if (texture.valid) 100 return true; 101 else 102 return false; 103 } 104 else if (buffer.data.length) 105 { 106 texture.createFromBuffer(buffer, true); 107 Delete(buffer.data); 108 return true; 109 } 110 else 111 return false; 112 } 113 114 override void release() 115 { 116 if (texture) 117 texture.release(); 118 if (image) 119 Delete(image); 120 if (buffer.data.length) 121 Delete(buffer.data); 122 } 123 } 124 125 TextureAsset textureAsset(AssetManager assetManager, string filename) 126 { 127 TextureAsset asset; 128 if (assetManager.assetExists(filename)) 129 { 130 asset = cast(TextureAsset)assetManager.getAsset(filename); 131 } 132 else 133 { 134 asset = New!TextureAsset(assetManager); 135 assetManager.preloadAsset(asset, filename); 136 } 137 return asset; 138 }