Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 24 additions & 19 deletions src/jbsae/JBSAE.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand All @@ -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){
Expand All @@ -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
}
Expand Down
43 changes: 36 additions & 7 deletions src/jbsae/Log.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package jbsae;

import jbsae.func.*;
import jbsae.struct.*;
import jbsae.struct.prim.*;

Expand All @@ -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<LogInfo> logs;
public static Queue<String> spans = new Queue<>();
public static String current = "";

public static LogLevel level = LogLevel.INFO;

Expand All @@ -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();
Expand Down Expand Up @@ -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());
Expand All @@ -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();
}


Expand Down Expand Up @@ -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;
}
}
}
35 changes: 20 additions & 15 deletions src/jbsae/core/Assets.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand Down
80 changes: 40 additions & 40 deletions src/jbsae/core/GameLoop.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
});
}
}
1 change: 1 addition & 0 deletions src/jbsae/files/assets/FontFi.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
1 change: 1 addition & 0 deletions src/jbsae/files/assets/ShaderFi.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/jbsae/files/assets/SoundFi.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions src/jbsae/files/assets/TextureFi.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 7 additions & 3 deletions src/jbsae/graphics/gl/Window.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package jbsae.graphics.gl;

import jbsae.*;
import jbsae.util.*;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;
Expand Down Expand Up @@ -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(){
Expand Down