1 module resourcemanager; 2 3 import collider; 4 import dsfml.graphics : Texture; 5 import dsfml.audio : SoundBuffer; 6 7 private class ResourceManager { 8 Texture[string] textures; 9 Collider[string] colliders; 10 SoundBuffer[string] buffers; 11 12 bool register(T)(const string path, const string name, bool generate_collider = no_collider) if(is(T == Texture)) { 13 auto newtex = new Texture; 14 if(!newtex.loadFromFile(path)) { 15 return false; 16 } 17 18 textures[name] = newtex; 19 if(generate_collider) { 20 colliders[name] = new Collider(newtex); 21 } 22 23 return true; 24 } 25 bool register(T)(const string path, const string name) if(is(T == SoundBuffer)) { 26 auto newbuf = new SoundBuffer; 27 if(!newbuf.loadFromFile(path)) { 28 return false; 29 } 30 31 buffers[name] = newbuf; 32 return true; 33 } 34 35 const(Texture) get(T)(const string name) const if(is(T == Texture)) { 36 return textures[name]; 37 } 38 const(Collider) get(T)(const string name) const if(is(T == Collider)) { 39 return colliders[name]; 40 } 41 const(SoundBuffer) get(T)(const string name) const if(is(T == SoundBuffer)) { 42 return buffers[name]; 43 } 44 } 45 46 47 /// Named bool to indicate need of a collider 48 static bool make_collider = true; 49 50 /// Named bool to indicate that no collider is needed 51 static bool no_collider = false; 52 53 /// Manages sound buffers and textures 54 static ResourceManager resource_manager; 55 56 static this() { 57 resource_manager = new ResourceManager; 58 }