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

Commit bf85a55

Browse files
committed
Lighting system (working in progress), human entity
1 parent 2925cad commit bf85a55

File tree

22 files changed

+588
-196
lines changed

22 files changed

+588
-196
lines changed

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626

2727
import org.overrun.swgl.core.io.IFileProvider;
2828

29-
import java.util.Optional;
30-
3129
/**
3230
* A swgl asset.
3331
*
@@ -43,16 +41,6 @@ public abstract class Asset {
4341
*/
4442
public abstract void reload(String name, IFileProvider provider);
4543

46-
/**
47-
* Converts the data to a byte array.
48-
*
49-
* @return The array. Can be empty.
50-
*/
51-
@Deprecated(forRemoval = true)
52-
public Optional<byte[]> toBytes() {
53-
return Optional.empty();
54-
}
55-
5644
/**
5745
* Closes this asset.
5846
*

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import java.util.HashMap;
3232
import java.util.Map;
33+
import java.util.Optional;
3334
import java.util.function.Consumer;
3435

3536
/**
@@ -183,8 +184,7 @@ public void reloadAssets(boolean force) {
183184
* @throws ClassCastException If value is not an instance of {@code T}
184185
*/
185186
@SuppressWarnings("unchecked")
186-
@Nullable
187-
public <T extends Asset> T getAsset(String name)
187+
public <T extends Asset> Optional<T> getAsset(String name)
188188
throws RuntimeException, ClassCastException {
189189
var wrapper = assets.get(name);
190190
// Check if asset present, else it is an alias
@@ -197,7 +197,7 @@ public <T extends Asset> T getAsset(String name)
197197
+ "' and original name '" + nm + "'!");
198198
}
199199
var t = wrapper.asset();
200-
return (T) t;
200+
return Optional.ofNullable((T) t);
201201
}
202202

203203
/**
@@ -232,7 +232,8 @@ public void close() throws Exception {
232232
for (var v : assets.values()) {
233233
try {
234234
v.asset().close();
235-
} catch (Exception ignored) {
235+
} catch (Exception e) {
236+
e.printStackTrace();
236237
}
237238
}
238239
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
* @author squid233
29+
* @since 0.1.0
30+
*/
31+
@FunctionalInterface
32+
public interface ITextureParam {
33+
/**
34+
* Set the texture parameter to GL.
35+
*
36+
* @param target The texture target.
37+
*/
38+
void set(int target);
39+
}

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

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,15 @@
2424

2525
package org.overrun.swgl.core.asset;
2626

27+
import org.jetbrains.annotations.Nullable;
2728
import org.lwjgl.opengl.GL;
2829
import org.lwjgl.system.MemoryStack;
2930
import org.overrun.swgl.core.cfg.GlobalConfig;
3031
import org.overrun.swgl.core.io.IFileProvider;
3132

3233
import java.nio.ByteBuffer;
33-
import java.util.ArrayList;
34-
import java.util.LinkedHashMap;
35-
import java.util.List;
36-
import java.util.Map;
34+
import java.util.Optional;
35+
import java.util.function.Consumer;
3736

3837
import static org.lwjgl.opengl.GL30C.*;
3938
import static org.lwjgl.stb.STBImage.*;
@@ -64,10 +63,8 @@ public class Texture2D extends Texture {
6463
private int id;
6564
private boolean failed;
6665
private int width, height;
67-
private final Map<Integer, Runnable> texParamRecorder = new LinkedHashMap<>();
68-
private final List<Integer> recorderIds = new ArrayList<>();
69-
private int nextRecorderId;
70-
private byte[] bytes;
66+
@Nullable
67+
private ITextureParam param;
7168

7269
/**
7370
* Create an empty 2D texture.
@@ -86,6 +83,31 @@ public Texture2D(String name, IFileProvider provider) {
8683
reload(name, provider);
8784
}
8885

86+
public static void createAsset(
87+
AssetManager mgr,
88+
String name,
89+
@Nullable Consumer<Texture2D> consumer,
90+
IFileProvider provider
91+
) {
92+
mgr.createAsset(name, AssetTypes.TEXTURE2D, consumer, provider);
93+
}
94+
95+
public static void createAssetParam(
96+
AssetManager mgr,
97+
String name,
98+
@Nullable ITextureParam param,
99+
IFileProvider provider
100+
) {
101+
mgr.createAsset(name, AssetTypes.TEXTURE2D, (Texture2D tex) -> tex.setParam(param), provider);
102+
}
103+
104+
public static Optional<Texture2D> getAsset(
105+
AssetManager mgr,
106+
String name
107+
) {
108+
return mgr.getAsset(name);
109+
}
110+
89111
/**
90112
* {@inheritDoc}
91113
* <p>
@@ -101,7 +123,7 @@ public Texture2D(String name, IFileProvider provider) {
101123
*/
102124
@Override
103125
public void reload(String name, IFileProvider provider) {
104-
bytes = provider.getAllBytes(name);
126+
byte[] bytes = provider.getAllBytes(name);
105127
var buffer = asBuffer(bytes, name);
106128
try {
107129
build(buffer);
@@ -110,26 +132,13 @@ public void reload(String name, IFileProvider provider) {
110132
}
111133
}
112134

113-
public int recordTexParam(Runnable recorder) {
114-
recorderIds.add(nextRecorderId++);
115-
texParamRecorder.put(nextRecorderId, recorder);
116-
return nextRecorderId;
135+
public void setParam(@Nullable ITextureParam param) {
136+
this.param = param;
117137
}
118138

119-
public void rerecordTexParam(int id, Runnable recorder) {
120-
if (!hasRecorder(id)) {
121-
throw new IllegalArgumentException("Please use recordTexParam first!");
122-
}
123-
texParamRecorder.put(id, recorder);
124-
}
125-
126-
public void destroyRecorder(int id) {
127-
recorderIds.remove((Object) id);
128-
texParamRecorder.remove(id);
129-
}
130-
131-
public boolean hasRecorder(int id) {
132-
return recorderIds.contains(id);
139+
@Nullable
140+
public ITextureParam getParam() {
141+
return param;
133142
}
134143

135144
private ByteBuffer fail() {
@@ -178,9 +187,8 @@ private void build(ByteBuffer buffer) {
178187
if (!glIsTexture(id))
179188
create();
180189
bindTexture2D(0, id);
181-
for (var recorder : texParamRecorder.values()) {
182-
recorder.run();
183-
}
190+
if (param != null)
191+
param.set(GL_TEXTURE_2D);
184192
glTexImage2D(GL_TEXTURE_2D,
185193
0,
186194
GL_RGBA,

0 commit comments

Comments
 (0)