Skip to content

Commit 7f381ca

Browse files
author
Benjamin Yap
committed
Add framework for new Resource manager system
Create a new ResourceManager class that uses a JSON file to keep track of resource locations. Revamp resource loading methods so that it is more centralised and better managed by the ResourceManager.
1 parent 4b75af8 commit 7f381ca

File tree

6 files changed

+356
-2
lines changed

6 files changed

+356
-2
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package com.bwyap.engine.resource;
2+
3+
import java.util.Map;
4+
5+
import org.json.simple.JSONObject;
6+
7+
import com.bwyap.utility.resource.JSONWrapper;
8+
9+
10+
/**
11+
* A JSON wrapper that provides access to the resource file
12+
* paths specified by a Resource library JSON file.
13+
* @author bwyap
14+
*
15+
*/
16+
@SuppressWarnings("unchecked")
17+
public class JSONResourceLibrary extends JSONWrapper {
18+
19+
20+
/**
21+
* Create a new JSON Resource library with the specified JSON object
22+
* @param object
23+
*/
24+
public JSONResourceLibrary(JSONObject object) {
25+
super(object);
26+
}
27+
28+
29+
/**
30+
* Auxillary method for getting internal resource paths
31+
* @param property
32+
* @return
33+
*/
34+
private Map<String, String> getInternal(String property) {
35+
return (Map<String, String>) ((JSONObject)object.get("internal")).get(property);
36+
}
37+
38+
39+
/**
40+
* Auxillary method for getting external resource paths
41+
* @param property
42+
* @return
43+
*/
44+
private Map<String, String> getExternal(String property) {
45+
return (Map<String, String>) ((JSONObject)object.get("external")).get(property);
46+
}
47+
48+
49+
50+
// INTERNAL
51+
52+
/**
53+
* Get the Map of shader file paths specified by the resource library
54+
* @return
55+
*/
56+
public Map<String, String> getShaders() {
57+
return getInternal("shaders");
58+
}
59+
60+
61+
/**
62+
* Get the Map of mesh file paths specified by the resource library
63+
* @return
64+
*/
65+
public Map<String, String> getMeshes() {
66+
return getInternal("meshes");
67+
}
68+
69+
70+
/**
71+
* Get the Map of texture file paths specified by the resource library
72+
* @return
73+
*/
74+
public Map<String, String> getTextures() {
75+
return getInternal("textures");
76+
}
77+
78+
79+
/**
80+
* Get the Map of font file paths specified by the resource library
81+
* @return
82+
*/
83+
public Map<String, String> getFonts() {
84+
return getInternal("fonts");
85+
}
86+
87+
88+
89+
// EXTERNAL
90+
91+
/**
92+
* Get the external folder paths specified by the resource library
93+
* @return
94+
*/
95+
public Map<String, String> getFolders() {
96+
return getExternal("folders");
97+
}
98+
99+
100+
101+
/**
102+
* Validate the loaded JSON file.
103+
* @return
104+
*/
105+
@Override
106+
public boolean isValid() {
107+
try {
108+
//TODO
109+
//
110+
//
111+
}
112+
catch (Exception e) {
113+
e.printStackTrace();
114+
return false;
115+
}
116+
return true;
117+
}
118+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.bwyap.engine.resource;
2+
3+
import com.bwyap.utility.resource.ResourceLoader;
4+
5+
/**
6+
* A base class for a Resource manager. Each implementation
7+
* of this engine should have its own subclass of this class.
8+
* The ResourceManager should be implemented as a Singleton.
9+
* @author bwyap
10+
*
11+
*/
12+
public abstract class ResourceManagerBase implements ResourceManagerInterface {
13+
14+
15+
protected final JSONResourceLibrary lib;
16+
17+
18+
/**
19+
* Create a new resource manager with the
20+
* resource library specified at the path
21+
* @param JSONPath
22+
*/
23+
protected ResourceManagerBase(String JSONPath) {
24+
lib = new JSONResourceLibrary(ResourceLoader.loadInternalJSON(JSONPath));
25+
}
26+
27+
28+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.bwyap.engine.resource;
2+
3+
/**
4+
* Methods required by a concrete Resource Manager implementation
5+
* @author bwyap
6+
*
7+
*/
8+
public interface ResourceManagerInterface {
9+
10+
11+
/**
12+
* Load all non-engine dependent resources
13+
*/
14+
public void loadIndepedentResources() throws Exception;
15+
16+
17+
/**
18+
* Load all engine dependent resources
19+
*/
20+
public void loadDependentResources() throws Exception;
21+
22+
23+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"internal": {
3+
4+
"shaders": {
5+
"vertex" : "/com/bwyap/lwjgl/engine/render3d/shader/shaders/vertexShader.vs",
6+
"fragment" : "/com/bwyap/lwjgl/engine/render3d/shader/shaders/fragmentShader.vs",
7+
"unlit_vertex" : "/com/bwyap/lwjgl/engine/render3d/shader/shaders/vertexShaderUnlit.vs",
8+
"unlit_fragment" : "/com/bwyap/lwjgl/engine/render3d/shader/shaders/fragmentShaderUnlit.vs",
9+
"particle_vertex" : "/com/bwyap/lwjgl/engine/render3d/shader/shaders/particleVertexShader.vs",
10+
"particle_fragment" : "/com/bwyap/lwjgl/engine/render3d/shader/shaders/particleFragmentShader.vs"
11+
},
12+
13+
"meshes": {
14+
"cube" : "/com/bwyap/enginedriver/resource/obj/cube.obj",
15+
"bunny" : "/com/bwyap/enginedriver/resource/obj/bunny.obj",
16+
"particle" : "/com/bwyap/enginedriver/resource/obj/particle.obj"
17+
},
18+
19+
"textures": {
20+
"test" : "/com/bwyap/enginedriver/resource/textures/texture.png",
21+
"test_particle" : "/com/bwyap/enginedriver/resource/textures/particle_smooth.png",
22+
"test_animated_particle" : "/com/bwyap/enginedriver/resource/textures/particle_anim.png",
23+
"test_button" : "/com/bwyap/enginedriver/resource/textures/button_normal.png",
24+
"test_button_mouseover" : "/com/bwyap/enginedriver/resource/textures/button_mouseover.png",
25+
"test_button_pressed" : "/com/bwyap/enginedriver/resource/textures/button_pressed.png",
26+
},
27+
28+
"fonts": {
29+
"aeromatics_reg" : "/com/bwyap/enginedriver/resource/font/aeromatics_reg.ttf"
30+
}
31+
},
32+
33+
"external": {
34+
35+
"folders": {
36+
"data_folder" : "data/",
37+
"config_folder" : "data/config"
38+
},
39+
40+
"config": {
41+
"config" : "data/config/config.json"
42+
}
43+
}
44+
}

src/com/bwyap/enginedriver/resource/Resource.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
*/
2323
public class Resource {
2424

25+
public static final String RESOURCE_JSON = "/com/bwyap/engine/resource/resources.json";
26+
27+
2528
public static final String IN_ROOT = "/com/bwyap/enginedriver/";
2629

2730
/* ==============
@@ -37,7 +40,7 @@ public class Resource {
3740
public static final String IN_SHADER_UNLIT_VERTEX = "vertexShaderUnlit.vs";
3841
public static final String IN_SHADER_UNLIT_FRAGMENT = "fragmentShaderUnlit.fs";
3942
public static final String IN_SHADER_PARTICLE_VERTEX = "particleVertexShader.vs";
40-
public static final String IN_SHADER_PARTICLE_GEOMETRY = "particleGeometryShader.gs";
43+
//public static final String IN_SHADER_PARTICLE_GEOMETRY = "particleGeometryShader.gs";
4144
public static final String IN_SHADER_PARTICLE_FRAGMENT = "particleFragmentShader.fs";
4245

4346
// object meshes
@@ -126,7 +129,7 @@ public static void load() {
126129
}
127130

128131
//TODO
129-
//
132+
//
130133
}
131134

132135

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package com.bwyap.lwjgl.engine.resource;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
6+
import com.bwyap.engine.resource.ResourceManagerBase;
7+
import com.bwyap.lwjgl.engine.gui.nanovg.NVGFont;
8+
import com.bwyap.lwjgl.engine.gui.nanovg.NVGTexture;
9+
import com.bwyap.lwjgl.engine.render3d.LWJGLTexture;
10+
import com.bwyap.lwjgl.engine.render3d.shader.Shader;
11+
import com.bwyap.lwjgl.mesh.OBJLoader;
12+
import com.bwyap.utility.resource.ResourceLoader;
13+
14+
15+
/**
16+
* A resource manager for the LWJGL engine.
17+
* This class is a Singleton and its instance should be called using the method {@code instance()}.
18+
* The instance must first be initialised through the method {@code initInstance()}.
19+
* @author bwyap
20+
*
21+
*/
22+
public class LWJGLResourceManager extends ResourceManagerBase {
23+
24+
25+
private static LWJGLResourceManager instance;
26+
27+
28+
/**
29+
* Get the resource manager instance
30+
* @return
31+
*/
32+
public static LWJGLResourceManager instance() {
33+
return instance;
34+
}
35+
36+
37+
/**
38+
* Initialise the resource manager instance with
39+
* the resource library at the given path
40+
* @param JSONPath
41+
*/
42+
public static void initInstance(String JSONPath) {
43+
instance = new LWJGLResourceManager(JSONPath);
44+
}
45+
46+
47+
protected LWJGLResourceManager(String JSONPath) {
48+
super(JSONPath);
49+
}
50+
51+
52+
@Override
53+
public void loadIndepedentResources() throws IOException {
54+
loadFolders();
55+
loadConfig();
56+
loadShaders();
57+
}
58+
59+
60+
public void loadNVGResources(long ctx) throws IOException {
61+
loadNVGFonts();
62+
loadNVGTextures(ctx);
63+
}
64+
65+
66+
/**
67+
* {@inheritDoc}
68+
* <p>
69+
* This method requires that LWJGL's {@code GL.createCapabilities()} method be called.
70+
* </p>
71+
*/
72+
@Override
73+
public void loadDependentResources() throws Exception {
74+
loadMeshes();
75+
loadLWJGLTextures();
76+
}
77+
78+
79+
/*
80+
* ===============
81+
* PRIVATE METHODS
82+
* ===============
83+
*/
84+
85+
86+
// EXTERNAL
87+
88+
private void loadFolders() {
89+
ResourceLoader.loadFolder(new File(lib.getFolders().get("data_folder")));
90+
ResourceLoader.loadFolder(new File(lib.getFolders().get("config_folder")));
91+
}
92+
93+
94+
private void loadConfig() {
95+
//TODO
96+
//
97+
}
98+
99+
100+
101+
// INTERNAL
102+
103+
private void loadShaders() {
104+
Shader.addSource("vertex", ResourceLoader.loadShaderFile(lib.getShaders().get("vertex")));
105+
Shader.addSource("fragment", ResourceLoader.loadShaderFile(lib.getShaders().get("fragment")));
106+
Shader.addSource("particle_vertex", ResourceLoader.loadShaderFile(lib.getShaders().get("particle_vertex")));
107+
Shader.addSource("particle_fragment", ResourceLoader.loadShaderFile(lib.getShaders().get("particle_fragment")));
108+
}
109+
110+
111+
private void loadMeshes() throws Exception {
112+
OBJLoader.loadInstancedMesh("particle", lib.getMeshes().get("particle"), 2000);
113+
OBJLoader.loadInstancedMesh("icube", lib.getMeshes().get("cube"), 64);
114+
OBJLoader.loadMesh("bunny", lib.getMeshes().get("bunny"));
115+
OBJLoader.loadMesh("cube", lib.getMeshes().get("cube"));
116+
}
117+
118+
119+
private void loadLWJGLTextures() throws IOException {
120+
LWJGLTexture.loadTexture("testTexture", lib.getTextures().get("test"));
121+
LWJGLTexture.loadTexture("testParticle", lib.getTextures().get("test_particle"));
122+
LWJGLTexture.loadTexture("testAnimatedParticle", lib.getTextures().get("test_animated_particle"), 4, 4);
123+
}
124+
125+
126+
private void loadNVGFonts() throws IOException {
127+
NVGFont.createFont("aeromatics_reg", lib.getFonts().get("aeromatics_reg"));
128+
}
129+
130+
131+
private void loadNVGTextures(long ctx) throws IOException{
132+
NVGTexture.loadTexture("test_button", lib.getTextures().get("test_button"), ctx);
133+
NVGTexture.loadTexture("test_button_mouseover", lib.getTextures().get("test_button_mouseover"), ctx);
134+
NVGTexture.loadTexture("test_button_pressed", lib.getTextures().get("test_button_pressed"), ctx);
135+
}
136+
137+
138+
}

0 commit comments

Comments
 (0)