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

Commit 57d4f3e

Browse files
committed
Easy font
1 parent eaaa293 commit 57d4f3e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1830
-198
lines changed

src/main/java/module-info.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
exports org.overrun.swgl.core.cfg;
1717
exports org.overrun.swgl.core.gl;
1818
exports org.overrun.swgl.core.gl.ims;
19+
exports org.overrun.swgl.core.gui.font;
1920
exports org.overrun.swgl.core.io;
2021
exports org.overrun.swgl.core.level;
2122
exports org.overrun.swgl.core.model;
2223
exports org.overrun.swgl.core.model.obj;
2324
exports org.overrun.swgl.core.model.simple;
2425
exports org.overrun.swgl.core.util;
2526
exports org.overrun.swgl.core.util.math;
27+
exports org.overrun.swgl.core.util.timing;
2628
}

src/main/java/org/overrun/swgl/core/GlfwApplication.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,14 @@
3131
import org.overrun.swgl.core.io.Mouse;
3232
import org.overrun.swgl.core.io.ResManager;
3333
import org.overrun.swgl.core.io.Window;
34-
import org.overrun.swgl.core.util.Timer;
34+
import org.overrun.swgl.core.util.timing.Scheduler;
35+
import org.overrun.swgl.core.util.timing.Timer;
3536

37+
import java.util.ArrayList;
3638
import java.util.LinkedHashSet;
39+
import java.util.List;
3740
import java.util.Set;
41+
import java.util.function.BooleanSupplier;
3842

3943
import static org.lwjgl.glfw.GLFW.*;
4044
import static org.overrun.swgl.core.cfg.GlobalConfig.*;
@@ -70,6 +74,14 @@ public abstract class GlfwApplication extends Application {
7074
* The resource managers.
7175
*/
7276
protected final Set<ResManager> resManagers = new LinkedHashSet<>();
77+
/**
78+
* The scheduled tasks executed per loop.
79+
*/
80+
protected final List<Scheduler> scheduledPerLoopTasks = new ArrayList<>();
81+
/**
82+
* The passed application ticks.
83+
*/
84+
protected int passedAppTicks = 0;
7385

7486
/**
7587
* Update the time and ticking.
@@ -78,7 +90,11 @@ public void updateTime() {
7890
timer.update();
7991

8092
for (int i = 0; i < timer.ticks; i++) {
93+
for (var task : scheduledPerLoopTasks) {
94+
task.tick(passedAppTicks);
95+
}
8196
tick();
97+
++passedAppTicks;
8298
}
8399
}
84100

@@ -193,6 +209,16 @@ public void addResManager(ResManager manager) {
193209
resManagers.add(manager);
194210
}
195211

212+
/**
213+
* Scheduled tasks executed per loop.
214+
*
215+
* @param frequency The frequency in ticks.
216+
* @param command The command to be executed.
217+
*/
218+
public void schedulePerLoop(int frequency, BooleanSupplier command) {
219+
scheduledPerLoopTasks.add(new Scheduler(passedAppTicks, frequency, command));
220+
}
221+
196222
/**
197223
* Will be called when a key is pressed.
198224
*

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
* @since 0.1.0
3434
*/
3535
public abstract class Asset {
36+
public static final String BUILTIN_RES_BASE_DIR = "__$$swgl_core$$__unique_res$$__";
37+
3638
/**
3739
* Reloads or loads the asset by the specified name and {@link IFileProvider FileProvider}.
3840
*

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,8 @@ public void reloadAssets(boolean force) {
226226
* @throws ClassCastException If value is not an instance of {@code T}
227227
*/
228228
@SuppressWarnings("unchecked")
229-
public <T extends Asset> Optional<T> getAsset(String name)
229+
public <T extends Asset>
230+
Optional<T> getAsset(String name)
230231
throws RuntimeException, ClassCastException {
231232
var wrapper = assets.get(name);
232233
// Check if asset present, else it is an alias
@@ -242,6 +243,21 @@ public <T extends Asset> Optional<T> getAsset(String name)
242243
return Optional.ofNullable((T) t);
243244
}
244245

246+
/**
247+
* Get the asset from assets lazily.
248+
*
249+
* @param name The asset name or alias.
250+
* @param <T> The asset type.
251+
* @return The asset.
252+
* @throws RuntimeException If asset not found.
253+
* @throws ClassCastException If value is not an instance of {@code T}
254+
*/
255+
public <T extends Asset>
256+
Supplier<Optional<T>> getAssetLazy(String name)
257+
throws RuntimeException, ClassCastException {
258+
return () -> getAsset(name);
259+
}
260+
245261
/**
246262
* Freeze this manager.
247263
*

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

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@
2828
import org.lwjgl.opengl.GL;
2929
import org.lwjgl.system.MemoryStack;
3030
import org.overrun.swgl.core.cfg.GlobalConfig;
31+
import org.overrun.swgl.core.io.HeapManager;
3132
import org.overrun.swgl.core.io.IFileProvider;
3233

3334
import java.nio.ByteBuffer;
3435
import java.util.Optional;
3536
import java.util.function.Consumer;
37+
import java.util.function.Supplier;
3638

3739
import static org.lwjgl.opengl.GL30C.*;
3840
import static org.lwjgl.stb.STBImage.*;
@@ -46,25 +48,12 @@
4648
* @since 0.1.0
4749
*/
4850
public class Texture2D extends Texture {
49-
/**
50-
* The missing texture storage in RGB.
51-
* <h3>Preview</h3>
52-
* <div style="display:grid;grid-template-columns:8px 8px;grid-template-rows:8px 8px">
53-
* <div style="background-color:#f800f8"></div>
54-
* <div style="background-color:#000000"></div>
55-
* <div style="background-color:#000000"></div>
56-
* <div style="background-color:#f800f8"></div>
57-
* </div>
58-
*/
59-
private static final int[] MISSING_NO = {
60-
0xf800f8, 0x000000,
61-
0x000000, 0xf800f8
62-
};
6351
private int id;
6452
private boolean failed;
6553
private int width, height;
6654
@Nullable
6755
private ITextureParam param;
56+
public int defaultWidth = 16, defaultHeight = 16;
6857

6958
/**
7059
* Create an empty 2D texture.
@@ -79,7 +68,6 @@ public Texture2D() {
7968
* @param provider The file provider.
8069
*/
8170
public Texture2D(String name, IFileProvider provider) {
82-
create();
8371
reload(name, provider);
8472
}
8573

@@ -108,6 +96,13 @@ public static Optional<Texture2D> getAsset(
10896
return mgr.getAsset(name);
10997
}
11098

99+
public static Supplier<Optional<Texture2D>> getAssetLazy(
100+
AssetManager mgr,
101+
String name
102+
) {
103+
return mgr.getAssetLazy(name);
104+
}
105+
111106
/**
112107
* {@inheritDoc}
113108
* <p>
@@ -143,24 +138,36 @@ public ITextureParam getParam() {
143138

144139
private ByteBuffer fail() {
145140
failed = true;
146-
width = 2;
147-
height = 2;
148-
var buffer = memAlloc(MISSING_NO.length * 4);
149-
buffer.asIntBuffer().put(MISSING_NO).flip();
141+
width = (defaultWidth == 0 ? 16 : defaultWidth);
142+
height = (defaultHeight == 0 ? 16 : defaultHeight);
143+
int[] missingNo = new int[width * height];
144+
final int hx = width >> 1;
145+
final int hy = height >> 1;
146+
for (int x = 0; x < width; x++) {
147+
for (int y = 0; y < height; y++) {
148+
int index = x + y * width;
149+
if (y < hy)
150+
missingNo[index] = x < hx ? 0xf800f8 : 0x000000;
151+
else
152+
missingNo[index] = x < hx ? 0x000000 : 0xf800f8;
153+
}
154+
}
155+
var buffer = memAlloc(missingNo.length * 4);
156+
buffer.asIntBuffer().put(missingNo).flip();
150157
return buffer;
151158
}
152159

153160
private ByteBuffer asBuffer(byte[] bytes,
154161
String name) {
155162
if (bytes == null)
156163
return fail();
157-
var bb = memAlloc(bytes.length);
158-
try (var stack = MemoryStack.stackPush()) {
159-
bb.put(bytes).flip();
164+
try (var heap = new HeapManager();
165+
var stack = MemoryStack.stackPush()) {
160166
var xp = stack.mallocInt(1);
161167
var yp = stack.mallocInt(1);
162168
var cp = stack.mallocInt(1);
163-
var buffer = stbi_load_from_memory(bb,
169+
var buffer = stbi_load_from_memory(
170+
heap.utilMemAlloc(bytes.length).put(bytes).flip(),
164171
xp,
165172
yp,
166173
cp,
@@ -176,8 +183,6 @@ private ByteBuffer asBuffer(byte[] bytes,
176183
height = yp.get(0);
177184
}
178185
return buffer;
179-
} finally {
180-
memFree(bb);
181186
}
182187
}
183188

@@ -225,7 +230,7 @@ public int getId() {
225230
}
226231

227232
/**
228-
* If true, the texture used {@link #MISSING_NO missing texture}.
233+
* If true, the texture uses the missing texture.
229234
*
230235
* @return If the texture loading failed.
231236
*/

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,10 @@ public class GLClear {
3939
/**
4040
* AttribMask
4141
*/
42-
public static final int COLOR_BUFFER_BIT = GL_COLOR_BUFFER_BIT;
43-
/**
44-
* AttribMask
45-
*/
46-
public static final int DEPTH_BUFFER_BIT = GL_DEPTH_BUFFER_BIT;
47-
/**
48-
* AttribMask
49-
*/
50-
public static final int STENCIL_BUFFER_BIT = GL_STENCIL_BUFFER_BIT;
42+
public static final int
43+
COLOR_BUFFER_BIT = GL_COLOR_BUFFER_BIT,
44+
DEPTH_BUFFER_BIT = GL_DEPTH_BUFFER_BIT,
45+
STENCIL_BUFFER_BIT = GL_STENCIL_BUFFER_BIT;
5146
private static final float[] clearColor = {0.0f, 0.0f, 0.0f, 0.0f};
5247
private static double clearDepth = 1.0;
5348
private static int clearStencil = 1;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
import org.jetbrains.annotations.Contract;
2828

29-
import static org.lwjgl.opengl.GL40.*;
29+
import static org.lwjgl.opengl.GL40C.*;
3030

3131
/**
3232
* The OpenGL drawing modes.
@@ -42,7 +42,7 @@ public enum GLDrawMode {
4242
TRIANGLES(GL_TRIANGLES, false),
4343
TRIANGLE_STRIP(GL_TRIANGLE_STRIP, false),
4444
TRIANGLE_FAN(GL_TRIANGLE_FAN, false),
45-
// QUADS(GL_QUADS, true),
45+
QUADS(GL_TRIANGLES, true),
4646
// QUAD_STRIP(GL_QUAD_STRIP, true),
4747
// POLYGON(GL_POLYGON, true),
4848
LINES_ADJACENCY(GL_LINES_ADJACENCY, false),

0 commit comments

Comments
 (0)