1 module polyplex.core.content.contentmanager; 2 import polyplex.core.content.textures; 3 import polyplex.core.content.gl; 4 import polyplex.core.content.vk; 5 import polyplex.core.content.font; 6 import polyplex.core.content.data; 7 import polyplex.utils.logging; 8 import polyplex.utils.strutils; 9 import polyplex.core.audio; 10 11 import polyplex.core.render : Shader, ShaderCode; 12 import polyplex.core.render.gl.shader; 13 14 static import ppct = ppc.types; 15 import ppc.backend.loaders.ppc; 16 import ppc.backend.cfile; 17 import bindbc.sdl; 18 19 public enum SupportedAudio { 20 OGG 21 } 22 23 public class ContentManager { 24 private static bool content_init = false; 25 protected SupportedAudio supported_audio; 26 27 public string ContentRoot = "content/"; 28 29 this() { 30 if (!content_init) { 31 Logger.Debug("ContentManagerWarmup: Starting warmup..."); 32 33 // TODO: Configure OpenAL 34 35 content_init = true; 36 Logger.Debug("ContentManager initialized..."); 37 } 38 } 39 40 T loadLocal(T)(string name) if (is(T : SoundEffect)) { 41 return new SoundEffect(ppct.Audio(loadFile(name))); 42 } 43 44 public T Load(T)(string name) if (is(T : SoundEffect)) { 45 // Load raw file if instructed to. 46 if (name[0] == '!') return loadLocal!T(name[1..$]); 47 48 // Otherwise load PPC file 49 PPC ppc = PPC(loadFile(this.ContentRoot~name~".ppc")); 50 return new SoundEffect(ppct.Audio(ppc.data)); 51 } 52 53 T loadLocal(T)(string name) if (is(T : Music)) { 54 return new Music(ppct.Audio(loadFile(name))); 55 } 56 57 public T Load(T)(string name) if (is(T : Music)) { 58 // Load raw file if instructed to. 59 if (name[0] == '!') return loadLocal!T(name[1..$]); 60 61 // Otherwise load PPC file 62 PPC ppc = PPC(loadFile(this.ContentRoot~name~".ppc")); 63 return new Music(ppct.Audio(ppc.data)); 64 } 65 66 T loadLocal(T)(string name) if (is(T : Texture2D)) { 67 auto imgd = ppct.Image(loadFile(name)); 68 TextureImg img = new TextureImg(cast(int)imgd.width, cast(int)imgd.height, imgd.pixelData, name); 69 return new GlTexture2D(img); 70 } 71 72 public T Load(T)(string name) if (is(T : Texture2D)) { 73 // Load raw file if instructed to. 74 if (name[0] == '!') return loadLocal!T(name[1..$]); 75 76 // Otherwise load PPC file 77 PPC ppc = PPC(this.ContentRoot~name~".ppc"); 78 auto imgd = ppct.Image(ppc.data); 79 TextureImg img = new TextureImg(cast(int)imgd.width, cast(int)imgd.height, imgd.pixelData, name); 80 return new GlTexture2D(img); 81 } 82 83 T loadLocal(T)(string name) if (is(T : SpriteFont)) { 84 throw new Exception("SpriteFont does not support localLoad at current time!"); 85 86 // auto tface = ppct.TypeFace(loadFile(name)); 87 // return new SpriteFont(tface); 88 } 89 90 public T Load(T)(string name) if (is(T : SpriteFont)) { 91 // Load raw file if instructed to. 92 if (name[0] == '!') return loadLocal!T(name[1..$]); 93 94 // Otherwise load PPC file 95 PPC ppc = PPC(this.ContentRoot~name~".ppc"); 96 auto tface = ppct.TypeFace(ppc.data); 97 return new SpriteFont(tface); 98 } 99 100 public T Load(T)(string name) if (is(T : Shader)) { 101 // Shaders can't be loaded locally 102 if (name[0] == '!') throw new Exception("Shaders cannot be loaded rawly, please use ppcc to convert to PSGL"); 103 104 // Otherwise load PPC file 105 Logger.Debug("Loading {0}...", name); 106 PPC ppc = PPC(this.ContentRoot~name~".ppc"); 107 auto shd = ppct.Shader(ppc.data); 108 ShaderCode sc = new ShaderCode(); 109 Logger.VerboseDebug("Shader Count: {0}", shd.shaders.length); 110 foreach(k, v; shd.shaders) { 111 if (k == ppct.ShaderType.Vertex) { 112 sc.Vertex = v.toString; 113 Logger.VerboseDebug("Vertex Shader:\n{0}", sc.Vertex); 114 } 115 if (k == ppct.ShaderType.Fragment) { 116 sc.Fragment = v.toString; 117 Logger.VerboseDebug("Fragment Shader:\n{0}", sc.Fragment); 118 } 119 if (k == ppct.ShaderType.Geometry) { 120 sc.Geometry = v.toString; 121 } 122 } 123 return new Shader(sc); 124 } 125 126 /** 127 Load Raw file 128 */ 129 public T Load(T)(string name) if (is(T : string)) { 130 if (name[0] == '!') return loadLocal!T(name[1..$]); 131 PPC ppc = PPC(this.ContentRoot~name~".ppc"); 132 return cast(T)ppc.data.toArray(); 133 } 134 135 public T loadLocal(T)(string name) if (is(T : string)) { 136 import fio = std.file; 137 return cast(T)fio.read(name); 138 } 139 }