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 	public T Load(T)(string name) if (is(T : Shader)) {
84 		// Shaders can't be loaded locally
85 		if (name[0] == '!') throw new Exception("Shaders cannot be loaded rawly, please use ppcc to convert to PSGL");
86 
87 		// Otherwise load PPC file
88 		Logger.Debug("Loading {0}...", name);
89 		PPC ppc = PPC(this.ContentRoot~name~".ppc");
90 		auto shd = ppct.Shader(ppc.data);
91 		ShaderCode sc = new ShaderCode();
92 		Logger.VerboseDebug("Shader Count: {0}", shd.shaders.length);
93 		foreach(k, v; shd.shaders) {
94 			if (k == ppct.ShaderType.Vertex) {
95 				sc.Vertex = v.toString;
96 				Logger.VerboseDebug("Vertex Shader:\n{0}", sc.Vertex);
97 			}
98 			if (k == ppct.ShaderType.Fragment) {
99 				sc.Fragment = v.toString;
100 				Logger.VerboseDebug("Fragment Shader:\n{0}", sc.Fragment);
101 			}
102 			if (k == ppct.ShaderType.Geometry) {
103 				sc.Geometry = v.toString;
104 			}
105 		}
106 		return new GLShader(sc);
107 	}
108 }