1 /* 2 Copyright (c) 2015 Timur Gafarov 3 4 Boost Software License - Version 1.0 - August 17th, 2003 5 6 Permission is hereby granted, free of charge, to any person or organization 7 obtaining a copy of the software and accompanying documentation covered by 8 this license (the "Software") to use, reproduce, display, distribute, 9 execute, and transmit the Software, and to prepare derivative works of the 10 Software, and to permit third-parties to whom the Software is furnished to 11 do so, all subject to the following: 12 13 The copyright notices in the Software and this entire statement, including 14 the above license grant, this restriction and the following disclaimer, 15 must be included in all copies of the Software, in whole or in part, and 16 all derivative works of the Software, unless such copies or derivative 17 works are solely in the form of machine-executable object code generated by 18 a source language processor. 19 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 DEALINGS IN THE SOFTWARE. 27 */ 28 29 module dgl.asset.resman; 30 31 import std.stdio; 32 import std.file; 33 import dlib.core.memory; 34 import dlib.container.array; 35 import dlib.container.dict; 36 import dlib.image.io.png; 37 import dlib.filesystem.filesystem; 38 import dlib.filesystem.stdfs; 39 import dlib.image.unmanaged; 40 import dgl.core.interfaces; 41 import dgl.vfs.vfs; 42 import dgl.graphics.texture; 43 import dgl.ui.font; 44 import dgl.graphics.scene; 45 import dgl.graphics.lightmanager; 46 import dgl.graphics.shadow; 47 import dgl.asset.dgl2; 48 49 class ResourceManager: Freeable, Drawable 50 { 51 VirtualFileSystem fs; 52 UnmanagedImageFactory imgFac; 53 Dict!(Font, string) fonts; 54 Dict!(Texture, string) textures; 55 56 DynamicArray!Scene _scenes; 57 Dict!(size_t, string) scenesByName; 58 59 LightManager lm; 60 ShadowMap shadow; 61 bool enableShadows = false; 62 63 this() 64 { 65 fs = New!VirtualFileSystem(); 66 imgFac = New!UnmanagedImageFactory(); 67 fonts = New!(Dict!(Font, string))(); 68 textures = New!(Dict!(Texture, string))(); 69 scenesByName = New!(Dict!(size_t, string))(); 70 lm = New!LightManager(); 71 lm.lightsVisible = true; 72 } 73 74 Font addFont(string name, Font f) 75 { 76 fonts[name] = f; 77 return f; 78 } 79 80 Font getFont(string name) 81 { 82 return fonts[name]; 83 } 84 85 Texture addTexture(string name, Texture t) 86 { 87 textures[name] = t; 88 return t; 89 } 90 91 Texture getTexture(string filename) 92 { 93 if (filename in textures) 94 return textures[filename]; 95 96 if (!fileExists(filename)) 97 { 98 writefln("Warning: cannot find image file (trying to load \'%s\')", filename); 99 return null; 100 } 101 102 auto fstrm = fs.openForInput(filename); 103 auto res = loadPNG(fstrm, imgFac); 104 Delete(fstrm); 105 106 if (res[0] is null) 107 { 108 return null; 109 } 110 else 111 { 112 auto tex = New!Texture(res[0]); 113 res[0].free(); 114 return addTexture(filename, tex); 115 } 116 } 117 118 Scene loadScene(string filename, bool visible = true) 119 { 120 Scene scene = New!Scene(this); 121 122 scene.clearArrays(); 123 auto fstrm = fs.openForInput(filename); 124 loadDGL2(fstrm, scene); 125 Delete(fstrm); 126 scene.resolveLinks(); 127 128 scene.visible = visible; 129 _scenes.append(scene); 130 scenesByName[filename] = _scenes.length; 131 return scene; 132 } 133 134 Scene addEmptyScene(string name, bool visible = true) 135 { 136 Scene scene = New!Scene(this); 137 scene.visible = visible; 138 _scenes.append(scene); 139 scenesByName[name] = _scenes.length; 140 return scene; 141 } 142 143 void freeFonts() 144 { 145 foreach(i, f; fonts) 146 f.free(); 147 Delete(fonts); 148 } 149 150 void freeTextures() 151 { 152 foreach(i, t; textures) 153 t.free(); 154 Delete(textures); 155 } 156 157 void freeScenes() 158 { 159 foreach(i, s; _scenes.data) 160 s.free(); 161 _scenes.free(); 162 Delete(scenesByName); 163 } 164 165 ~this() 166 { 167 Delete(imgFac); 168 fs.free(); 169 freeFonts(); 170 freeTextures(); 171 freeScenes(); 172 lm.free(); 173 if (shadow !is null) 174 shadow.free(); 175 } 176 177 void free() 178 { 179 Delete(this); 180 } 181 182 void draw(double dt) 183 { 184 if (enableShadows && shadow) 185 shadow.draw(dt); 186 187 foreach(i, s; _scenes.data) 188 if (s.visible) 189 s.draw(dt); 190 191 lm.draw(dt); 192 } 193 194 bool fileExists(string filename) 195 { 196 FileStat stat; 197 return fs.stat(filename, stat); 198 } 199 200 // Don't forget to delete the string! 201 string readText(string filename) 202 { 203 auto fstrm = fs.openForInput(filename); 204 string text = .readText(fstrm); 205 Delete(fstrm); 206 return text; 207 } 208 }