From f0514953370cadbc993d45c2a556b397654cecde Mon Sep 17 00:00:00 2001 From: leonwang2 Date: Wed, 8 Oct 2025 19:19:22 -0400 Subject: [PATCH] Spain --- src/jbsae/JBSAE.java | 43 +++++++------- src/jbsae/Log.java | 43 +++++++++++--- src/jbsae/core/Assets.java | 35 +++++++----- src/jbsae/core/GameLoop.java | 80 +++++++++++++-------------- src/jbsae/files/assets/FontFi.java | 1 + src/jbsae/files/assets/ShaderFi.java | 1 + src/jbsae/files/assets/SoundFi.java | 1 + src/jbsae/files/assets/TextureFi.java | 1 + src/jbsae/graphics/gl/Window.java | 10 +++- 9 files changed, 131 insertions(+), 84 deletions(-) diff --git a/src/jbsae/JBSAE.java b/src/jbsae/JBSAE.java index 5b1e61c..11c16dc 100644 --- a/src/jbsae/JBSAE.java +++ b/src/jbsae/JBSAE.java @@ -26,21 +26,24 @@ public class JBSAE{ public static Draw draw = new Draw(); public static void init(){ - glfwInit(); - assets.init(); - time.init(); - window.init(); - renderer.init(); - input.init(); - loop.init(); - sounds.init(); + Log.span("init", () -> { + glfwInit(); + assets.init(); + time.init(); + window.init(); + renderer.init(); + input.init(); + loop.init(); + sounds.init(); + }); } public static void load(){ - assets.load(); - - Texture icon = assets.textures.get(programName + ".png"); - window.icon(icon); + Log.span("load", () -> { + assets.load(); + Texture icon = assets.textures.get(programName + ".png"); + window.icon(icon); + }); } public static void start(){ @@ -52,13 +55,15 @@ public static void exit(){ } public static void dispose(){ - input.dispose(); - window.dispose(); - renderer.dispose(); - sounds.dispose(); - assets.dispose(); + Log.span("dispose", () -> { + input.dispose(); + window.dispose(); + renderer.dispose(); + sounds.dispose(); + assets.dispose(); - glfwTerminate(); + glfwTerminate(); + }); } public static void screen(Screen s){ @@ -77,7 +82,7 @@ public static void jbsae(Prog prog){ prog.run(); start(); dispose(); - }catch(Exception e){ + }catch(Throwable e){ Log.error(getStackTrace(e)); Log.writeToFile("log" + System.currentTimeMillis() + ".log"); // TODO: Dynamic log dump also don't always dump logs } diff --git a/src/jbsae/Log.java b/src/jbsae/Log.java index c377312..faf6596 100644 --- a/src/jbsae/Log.java +++ b/src/jbsae/Log.java @@ -1,5 +1,6 @@ package jbsae; +import jbsae.func.*; import jbsae.struct.*; import jbsae.struct.prim.*; @@ -8,11 +9,12 @@ import static jbsae.util.Stringf.*; public class Log{ - public static String envar = "JBSAE_LOG_LEVEL"; + public static String ENVAR = "JBSAE_LOG_LEVEL"; public static int maxLogs = 10000; // -1 for unlimited public static Queue logs; public static Queue spans = new Queue<>(); + public static String current = ""; public static LogLevel level = LogLevel.INFO; @@ -22,7 +24,7 @@ public static void init(){ logs = new Queue<>(maxLogs == -1 ? 1000 : maxLogs + 1); startTime = System.currentTimeMillis(); - String env = System.getenv(envar); + String env = System.getenv(ENVAR); if(env != null){ String setting = env.toUpperCase(); LogLevel[] levels = LogLevel.values(); @@ -57,11 +59,7 @@ public static void log(Object msg){ public static void log(LogLevel level, Object msg){ if(logs == null) return; - CharSeq result = new CharSeq(spans.size() * 10); - for(String s : spans) result.add(s).add(":"); - if(spans.size > 0) result.remove(result.size - 1).add(' '); - - LogInfo info = new LogInfo(level, result.toString(), msg.toString()); + LogInfo info = new LogInfo(level, current, msg.toString()); if(level.ordinal() >= Log.level.ordinal()){ logs.addLast(info); System.out.println(info.toString()); @@ -77,12 +75,34 @@ public static void writeToFile(String path){ } } + public static void update(){ + CharSeq result = new CharSeq(spans.size() * 10); + for(String s : spans) result.add(s).add(":"); + if(spans.size > 0) result.remove(result.size - 1).add(' '); + + current = result.toString(); + } + public static void span(String name){ spans.addLast(name); + update(); + } + + public static void span(String name, Prog run){ + try{ + span(name); + run.run(); + }catch(Throwable e){ + Log.error(getStackTrace(e)); + end(); + throw new LogException(e); + } + end(); } public static void end(){ spans.popLast(); + update(); } @@ -119,4 +139,13 @@ public String toString(){ return result.toString(); } } + + public static class LogException extends RuntimeException{ + public Throwable throwable; + + public LogException(Throwable throwable){ + super(); + this.throwable = throwable; + } + } } diff --git a/src/jbsae/core/Assets.java b/src/jbsae/core/Assets.java index 8f59d19..5156ec9 100644 --- a/src/jbsae/core/Assets.java +++ b/src/jbsae/core/Assets.java @@ -32,26 +32,30 @@ public Assets(){ } public void init(){ - root = new AssetDir(assetsFolder); - - if(root.file.exists() && root.file.isDirectory()) jar = false; - - if(!jar) gen(); - else{ - String assetListFile = assetsFolder + "/" + assetList; - AssetFi file = new AssetFi(assetListFile); - try(BufferedReader reader = file.reader()){ - String line; - while((line = reader.readLine()) != null && line.length() > 0) create(line); - }catch(IOException e){ - Log.error("Failed read asset list file: " + assetListFile); - Log.error(getStackTrace(e)); + Log.span("assets", () -> { + root = new AssetDir(assetsFolder); + + if(root.file.exists() && root.file.isDirectory()) jar = false; + + if(!jar) gen(); + else{ + Log.info("Reading asset list"); + String assetListFile = assetsFolder + "/" + assetList; + AssetFi file = new AssetFi(assetListFile); + try(BufferedReader reader = file.reader()){ + String line; + while((line = reader.readLine()) != null && line.length() > 0) create(line); + }catch(IOException e){ + Log.error("Failed read asset list file: " + assetListFile); + Log.error(getStackTrace(e)); + } } - } + }); } /** Generate and load assets/list file from assets folder. */ private void gen(){ + Log.info("Generating asset list..."); String assetListFile = assetsFolder + "/" + assetList; Fi file = new Fi(assetListFile); file.create(); @@ -78,6 +82,7 @@ public void dispose(){ } public AssetFi create(String path){ + Log.info("Creating asset: " + path); if(files.contains(path)) return files.get(path); if(path.endsWith(".fnt")) return new FontFi(path); if(path.endsWith(".frag")) return new ShaderFi(path); diff --git a/src/jbsae/core/GameLoop.java b/src/jbsae/core/GameLoop.java index 0c280f6..c196d31 100644 --- a/src/jbsae/core/GameLoop.java +++ b/src/jbsae/core/GameLoop.java @@ -17,46 +17,46 @@ public void init(){ } public void start(){ - long lastLoop = time.millis(); - int frameTimer = 0, updateTimer = 0, loopTimer = 0; - int lastFrames = 0, lastUpdates = 0; - - screen.init(); - while(window.keep()){ - Log.span("loop"); - time.update(); - frameTimer += time.time - lastLoop; - updateTimer += time.time - lastLoop; - loopTimer += time.time - lastLoop; - lastLoop = time.time; - - if(updateTimer >= (1000 / ups)){ - updateTimer %= (1000 / ups); - time.updates++; - window.update(); - screen.update(); - rotation++; + Log.span("loop", () -> { + long lastLoop = time.millis(); + int frameTimer = 0, updateTimer = 0, loopTimer = 0; + int lastFrames = 0, lastUpdates = 0; + + screen.init(); + while(window.keep()){ + time.update(); + frameTimer += time.time - lastLoop; + updateTimer += time.time - lastLoop; + loopTimer += time.time - lastLoop; + lastLoop = time.time; + + if(updateTimer >= (1000 / ups)){ + updateTimer %= (1000 / ups); + time.updates++; + window.update(); + screen.update(); + rotation++; + } + if(frameTimer >= (1000 / fps)){ + renderer.binded = null; + frameTimer %= (1000 / fps); + time.frames++; + glClear(GL_COLOR_BUFFER_BIT); + screen.draw(); + draw.render(); + renderer.flush(); + window.swap(); + } + if(loopTimer >= 1000){ + loopTimer %= 1000; + time.cfps = time.frames - lastFrames; + time.cups = time.updates - lastUpdates; + lastFrames = time.frames; + lastUpdates = time.updates; + Log.info("FPS: " + time.cfps + " UPS: " + time.cups); + } + window.poll(); } - if(frameTimer >= (1000 / fps)){ - renderer.binded = null; - frameTimer %= (1000 / fps); - time.frames++; - glClear(GL_COLOR_BUFFER_BIT); - screen.draw(); - draw.render(); - renderer.flush(); - window.swap(); - } - if(loopTimer >= 1000){ - loopTimer %= 1000; - time.cfps = time.frames - lastFrames; - time.cups = time.updates - lastUpdates; - lastFrames = time.frames; - lastUpdates = time.updates; - Log.info("FPS: " + time.cfps + " UPS: " + time.cups); - } - window.poll(); - Log.end(); - } + }); } } diff --git a/src/jbsae/files/assets/FontFi.java b/src/jbsae/files/assets/FontFi.java index d87ce75..8fe28be 100644 --- a/src/jbsae/files/assets/FontFi.java +++ b/src/jbsae/files/assets/FontFi.java @@ -19,6 +19,7 @@ public FontFi(String path){ @Override public FontFi gen(){ + Log.info("Loading font: " + path()); try(BufferedReader reader = reader()){ font = new Font(); StringTokenizer info = new StringTokenizer(reader.readLine()); diff --git a/src/jbsae/files/assets/ShaderFi.java b/src/jbsae/files/assets/ShaderFi.java index 14941fd..031fa1a 100644 --- a/src/jbsae/files/assets/ShaderFi.java +++ b/src/jbsae/files/assets/ShaderFi.java @@ -18,6 +18,7 @@ public ShaderFi(String path){ @Override public ShaderFi gen(){ + Log.info("Loading shader: " + path()); try(BufferedReader reader = reader()){ StringBuilder builder = new StringBuilder(); String line; diff --git a/src/jbsae/files/assets/SoundFi.java b/src/jbsae/files/assets/SoundFi.java index 4321739..d988b6b 100644 --- a/src/jbsae/files/assets/SoundFi.java +++ b/src/jbsae/files/assets/SoundFi.java @@ -39,6 +39,7 @@ public AudioInputStream input(){ @Override public SoundFi gen(){ + Log.info("Loading sound: " + path()); try(AudioInputStream stream = input()){ final int MONO = 1, STEREO = 2; diff --git a/src/jbsae/files/assets/TextureFi.java b/src/jbsae/files/assets/TextureFi.java index fb6f78e..1d24621 100644 --- a/src/jbsae/files/assets/TextureFi.java +++ b/src/jbsae/files/assets/TextureFi.java @@ -20,6 +20,7 @@ public TextureFi(String path){ @Override public TextureFi gen(){ + Log.info("Loading image: " + path()); try(InputStream stream = input()){ PngReader reader = new PngReader(); ByteBuffer buffer = reader.read(stream); diff --git a/src/jbsae/graphics/gl/Window.java b/src/jbsae/graphics/gl/Window.java index 42a9acc..7f528c2 100644 --- a/src/jbsae/graphics/gl/Window.java +++ b/src/jbsae/graphics/gl/Window.java @@ -1,5 +1,6 @@ package jbsae.graphics.gl; +import jbsae.*; import jbsae.util.*; import org.lwjgl.glfw.*; import org.lwjgl.opengl.*; @@ -47,9 +48,12 @@ public void init(){ } public void icon(Texture icon){ - GLFWImage image = GLFWImage.malloc(); - image.set(icon.width, icon.height, icon.image); - glfwSetWindowIcon(id, GLFWImage.malloc(1).put(0, image)); + if(Platform.get() != Platform.MACOSX){ + Log.info("Setting window icon"); + GLFWImage image = GLFWImage.malloc(); + image.set(icon.width, icon.height, icon.image); + glfwSetWindowIcon(id, GLFWImage.malloc(1).put(0, image)); + }else Log.error("MacOS icons should be managed outside of the program"); } public void update(){