Skip to content
This repository was archived by the owner on Aug 27, 2024. It is now read-only.

Commit 2925cad

Browse files
committed
Asset type
1 parent 0c43925 commit 2925cad

File tree

8 files changed

+149
-62
lines changed

8 files changed

+149
-62
lines changed

src/main/java/org/overrun/swgl/core/asset/AssetManager.java

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import org.overrun.swgl.core.cfg.GlobalConfig;
2929
import org.overrun.swgl.core.io.IFileProvider;
3030

31-
import java.lang.reflect.InvocationTargetException;
3231
import java.util.HashMap;
3332
import java.util.Map;
3433
import java.util.function.Consumer;
@@ -40,21 +39,21 @@
4039
* @since 0.1.0
4140
*/
4241
public class AssetManager implements AutoCloseable {
43-
private final Map<String, Map<Class<?>, AssetWrapper>> assets = new HashMap<>();
42+
private final Map<String, AssetWrapper> assets = new HashMap<>();
4443
private final Map<String, String[]> aliases = new HashMap<>();
4544
private final Map<String, String> biAliases = new HashMap<>();
4645
private boolean frozen;
4746

4847
private static final class AssetWrapper {
4948
private final String name;
50-
private final Class<?> type;
49+
private final IAssetTypeProvider type;
5150
private Asset asset;
5251
private final Consumer<Asset> consumer;
5352
private final IFileProvider provider;
5453
private boolean reloaded;
5554

5655
private AssetWrapper(String name,
57-
Class<?> type,
56+
IAssetTypeProvider type,
5857
Consumer<Asset> consumer,
5958
IFileProvider provider) {
6059
this.name = name;
@@ -72,11 +71,8 @@ public boolean createAsset(String name,
7271
if (!reloaded)
7372
// Construct from constructor without any parameters
7473
try {
75-
asset = (Asset) type.getDeclaredConstructor().newInstance();
76-
} catch (InstantiationException
77-
| IllegalAccessException
78-
| InvocationTargetException
79-
| NoSuchMethodException e) {
74+
asset = type.createInstance();
75+
} catch (Exception e) {
8076
e.printStackTrace();
8177
return false;
8278
}
@@ -95,35 +91,33 @@ public boolean createAsset(String name,
9591
* Create an asset. Not loaded.
9692
*
9793
* @param name The asset original name.
98-
* @param type The asset type class.
94+
* @param type The asset type provider.
9995
* @param consumer The consumer to be accepted before loading asset.
10096
* @param provider The file provider.
10197
* @param <T> The asset type.
10298
*/
10399
@SuppressWarnings("unchecked")
104100
public <T extends Asset>
105101
void createAsset(String name,
106-
Class<T> type,
102+
IAssetTypeProvider type,
107103
@Nullable Consumer<T> consumer,
108104
IFileProvider provider) {
109105
if (isFrozen())
110106
throw new IllegalStateException("Couldn't create asset in frozen state!");
111-
var map = new HashMap<Class<?>, AssetWrapper>();
112-
map.put(type, new AssetWrapper(name, type, (Consumer<Asset>) consumer, provider));
113-
assets.put(name, map);
107+
assets.put(name, new AssetWrapper(name, type, (Consumer<Asset>) consumer, provider));
114108
}
115109

116110
/**
117111
* Create an asset. Not loaded.
118112
*
119113
* @param name The asset original name.
120-
* @param type The asset type class.
114+
* @param type The asset type provider.
121115
* @param provider The file provider.
122116
* @param <T> The asset type.
123117
*/
124118
public <T extends Asset>
125119
void createAsset(String name,
126-
Class<T> type,
120+
IAssetTypeProvider type,
127121
IFileProvider provider) {
128122
createAsset(name, type, null, provider);
129123
}
@@ -151,22 +145,20 @@ public void addAliases(String name, String... aliases) {
151145
public void reloadAssets(boolean force, boolean forcePerAsset) {
152146
if (isFrozen() && !force)
153147
throw new IllegalStateException("Couldn't reload asset in frozen state!");
154-
for (var map : assets.entrySet()) {
155-
for (var e : map.getValue().entrySet()) {
156-
var wrapper = e.getValue();
157-
var nm = map.getKey();
158-
var key = nm;
159-
var aliasArr = aliases.get(nm);
160-
int i = 0;
161-
boolean ok;
162-
// If load failed, load from aliases.
163-
while (!(ok = wrapper.createAsset(key, forcePerAsset)) && i < aliasArr.length) {
164-
key = aliasArr[i];
165-
++i;
166-
}
167-
if (!ok) {
168-
GlobalConfig.getDebugStream().println("Failed to load asset '" + nm + "' from any aliases.");
169-
}
148+
for (var e : assets.entrySet()) {
149+
var nm = e.getKey();
150+
var wrapper = e.getValue();
151+
var key = nm;
152+
var aliasArr = aliases.get(nm);
153+
int i = 0;
154+
boolean ok;
155+
// If load failed, load from aliases.
156+
while (!(ok = wrapper.createAsset(key, forcePerAsset)) && i < aliasArr.length) {
157+
key = aliasArr[i];
158+
++i;
159+
}
160+
if (!ok) {
161+
GlobalConfig.getDebugStream().println("Failed to load asset '" + nm + "' from any aliases.");
170162
}
171163
}
172164
}
@@ -185,27 +177,27 @@ public void reloadAssets(boolean force) {
185177
* Get the asset from created assets.
186178
*
187179
* @param name The asset name or alias.
188-
* @param type The asset type class.
189180
* @param <T> The asset type.
190181
* @return The asset.
191-
* @throws RuntimeException If asset not found.
182+
* @throws RuntimeException If asset not found.
183+
* @throws ClassCastException If value is not an instance of {@code T}
192184
*/
193185
@SuppressWarnings("unchecked")
194186
@Nullable
195-
public <T extends Asset> T getAsset(String name,
196-
Class<T> type)
197-
throws RuntimeException {
198-
var map = assets.get(name);
187+
public <T extends Asset> T getAsset(String name)
188+
throws RuntimeException, ClassCastException {
189+
var wrapper = assets.get(name);
199190
// Check if asset present, else it is an alias
200-
if (map == null) {
191+
if (wrapper == null) {
201192
var nm = biAliases.get(name);
202193
if (nm == null)
203194
throw new RuntimeException("Couldn't get original name from '" + name + "'!");
204-
if ((map = assets.get(name)) == null)
205-
throw new RuntimeException("Couldn't get asset map from alias name '" + name
195+
if ((wrapper = assets.get(name)) == null)
196+
throw new RuntimeException("Couldn't get asset wrapper from alias name '" + name
206197
+ "' and original name '" + nm + "'!");
207198
}
208-
return (T) map.get(type).asset();
199+
var t = wrapper.asset();
200+
return (T) t;
209201
}
210202

211203
/**
@@ -238,8 +230,9 @@ public boolean isFrozen() {
238230
@Override
239231
public void close() throws Exception {
240232
for (var v : assets.values()) {
241-
for (var a : v.values()) {
242-
a.asset().close();
233+
try {
234+
v.asset().close();
235+
} catch (Exception ignored) {
243236
}
244237
}
245238
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2022 Overrun Organization
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package org.overrun.swgl.core.asset;
26+
27+
/**
28+
* The builtin asset types.
29+
*
30+
* @author squid233
31+
* @since 0.1.0
32+
*/
33+
public enum AssetTypes implements IAssetTypeProvider {
34+
PLAIN_TEXT(PlainTextAsset.class),
35+
TEXTURE2D(Texture2D.class);
36+
37+
private final Class<?> type;
38+
39+
AssetTypes(Class<?> type) {
40+
this.type = type;
41+
}
42+
43+
@SuppressWarnings("unchecked")
44+
@Override
45+
public <T> T createInstance() throws Exception {
46+
return (T) type.getDeclaredConstructor().newInstance();
47+
}
48+
49+
@Override
50+
public <T> boolean isInstanceOf(T t) {
51+
return type.isInstance(t);
52+
}
53+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2022 Overrun Organization
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
package org.overrun.swgl.core.asset;
26+
27+
/**
28+
* The asset type provider.
29+
*
30+
* @author squid233
31+
* @since 0.1.0
32+
*/
33+
public interface IAssetTypeProvider {
34+
<T> T createInstance() throws Exception;
35+
36+
<T> boolean isInstanceOf(T t);
37+
}

src/main/java/org/overrun/swgl/core/gl/GLUniform.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public final class GLUniform implements AutoCloseable {
4242
private final int location;
4343
private final GLUniformType type;
4444
private final ByteBuffer buffer;
45-
private boolean isDirty;
45+
private boolean isDirty = true;
4646

4747
/**
4848
* Construct the uniform.

src/main/java/org/overrun/swgl/core/gl/ims/GLImmeMode.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ void main() {
181181
fragSrc.append(" gl_FragColor = fragColor;\n");
182182
fragSrc.append("}");
183183
// Fragment shader END
184-
//todo System.out.println(fragSrc);
185184
Shaders.linkSimple(pipeline,
186185
vertSrc,
187186
fragSrc.toString());
@@ -204,6 +203,7 @@ void main() {
204203
}
205204
pipeline.getUniformSafe("projectionMat", M4F).set(projectionMat);
206205
pipeline.getUniformSafe("modelviewMat", M4F).set(modelviewMat);
206+
pipeline.getUniformSafe("lightModelAmbient", F4).set(lightModelAmbient);
207207
pipeline.updateUniforms();
208208
pipeline.unbind();
209209
}
@@ -613,6 +613,10 @@ public static void lglTranslate(float x,
613613
currentMat.translate(x, y, z);
614614
}
615615

616+
public static void lglTranslate(Vector3fc offset) {
617+
currentMat.translate(offset);
618+
}
619+
616620
///////////////////////////////////////////////////////////////////////////
617621
// End
618622
///////////////////////////////////////////////////////////////////////////

src/test/java/org/overrun/swgl/game/SwglGame.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ private void setupCamera(double delta) {
205205
lglMatrixMode(MatrixMode.MODELVIEW);
206206
lglLoadIdentity();
207207
moveCameraToPlayer(delta);
208+
Frustum.getFrustum(lglGetMatrix(MatrixMode.PROJECTION), lglGetMatrix(MatrixMode.MODELVIEW));
208209
}
209210

210211
@Override
@@ -232,7 +233,6 @@ public void run() {
232233

233234
clear(COLOR_BUFFER_BIT | DEPTH_BUFFER_BIT);
234235

235-
Frustum.getFrustum(lglGetMatrix(MatrixMode.PROJECTION), lglGetMatrix(MatrixMode.MODELVIEW));
236236
enableCullFace();
237237

238238
worldRenderer.updateDirtyChunks(player);
@@ -244,6 +244,8 @@ public void run() {
244244
worldRenderer.render(1);
245245
blocksTexture.unbind();
246246

247+
lglDisableLighting();
248+
247249
if (hitResult != null) {
248250
worldRenderer.renderHit(hitResult);
249251

@@ -253,17 +255,13 @@ public void run() {
253255
int ty = hitResult.y() + face.getOffsetY();
254256
int tz = hitResult.z() + face.getOffsetZ();
255257
if (world.isReplaceable(tx, ty, tz)) {
256-
lglSetLightModelAmbient(1.0f,
257-
1.0f,
258-
1.0f,
259-
((float) Math.sin(Timer.getTime() * 10) + 1.0f) / 4.0f + 0.3f);
260258
blocksTexture.bind();
261259
enableTexture2D();
262260
enableBlend();
263261
blendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
264262
lglSetTexCoordArrayState(true);
265263
lglBegin(GLDrawMode.TRIANGLES);
266-
lglColor(1.0f, 1.0f, 1.0f);
264+
lglColor(1.0f, 1.0f, 1.0f, ((float) Math.sin(Timer.getTime() * 10) + 1.0f) / 4.0f + 0.3f);
267265
handBlock.renderAll(tx, ty, tz);
268266
lglEnd();
269267
lglSetTexCoordArrayState(false);

src/test/java/org/overrun/swgl/test/CameraApp.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.lwjgl.opengl.GLUtil;
2929
import org.overrun.swgl.core.GlfwApplication;
3030
import org.overrun.swgl.core.asset.AssetManager;
31+
import org.overrun.swgl.core.asset.AssetTypes;
3132
import org.overrun.swgl.core.asset.PlainTextAsset;
3233
import org.overrun.swgl.core.asset.Texture2D;
3334
import org.overrun.swgl.core.cfg.GlobalConfig;
@@ -86,7 +87,7 @@ public static void main(String[] args) {
8687

8788
private void createTextureAsset(String name,
8889
Consumer<Texture2D> consumer) {
89-
assetManager.createAsset(name, Texture2D.class, consumer, FILE_PROVIDER);
90+
assetManager.createAsset(name, AssetTypes.TEXTURE2D, consumer, FILE_PROVIDER);
9091
}
9192

9293
@Override
@@ -195,8 +196,8 @@ public void start() {
195196
createTextureAsset(AWESOME_FACE_TEXTURE, recorder);
196197
assetManager.reloadAssets(true);
197198
assetManager.freeze();
198-
container = assetManager.getAsset(CONTAINER_TEXTURE, Texture2D.class);
199-
awesomeFace = assetManager.getAsset(AWESOME_FACE_TEXTURE, Texture2D.class);
199+
container = assetManager.getAsset(CONTAINER_TEXTURE);
200+
awesomeFace = assetManager.getAsset(AWESOME_FACE_TEXTURE);
200201

201202
camera.restrictPitch = true;
202203

0 commit comments

Comments
 (0)