Skip to content
Merged
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
4 changes: 2 additions & 2 deletions assets/another_test.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class AnotherTestClass extends Script {
}

@Override
void onRender(float delta) {
super.onRender(delta)
void onUpdate(float delta) {
super.onUpdate(delta)

if (Gdx.input.isKeyJustPressed(Input.Keys.SPACE)) {
var sprite = new FlixelSprite().loadGraphic(FlixelPaths.sharedImageAsset("pauseAlt/bfLol"))
Expand Down
4 changes: 2 additions & 2 deletions assets/test.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class TestScript extends SystemScript {
}

@Override
void onRender(float delta) {
super.onRender(delta)
void onUpdate(float delta) {
super.onUpdate(delta)

if (Gdx.input.isKeyJustPressed(Input.Keys.Q)) {
Flixel.switchState(new TestState())
Expand Down
69 changes: 52 additions & 17 deletions flixelgdx/src/main/java/me/stringdotjar/flixelgdx/Flixel.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public final class Flixel {
/** Has the global manager been initialized yet? */
private static boolean initialized = false;

/** The active logging mode used by the global logger. */
private static FlixelLogMode logMode = FlixelLogMode.SIMPLE;

/**
* Initializes the global manager.
*
Expand Down Expand Up @@ -364,6 +367,10 @@ public static void error(String tag, Object message, Throwable throwable) {
outputLog(tag, msg, FlixelLogLevel.ERROR);
}

public static FlixelLogMode getLogMode() {
return logMode;
}

public static FlixelGame getGame() {
return game;
}
Expand Down Expand Up @@ -412,6 +419,10 @@ public static boolean isFullscreen() {
return Gdx.graphics.isFullscreen();
}

public static void setLogMode(@NotNull FlixelLogMode mode) {
logMode = (mode != null) ? mode : FlixelLogMode.SIMPLE;
}

/**
* Contains all the global events that get dispatched when something happens in the game.
*
Expand Down Expand Up @@ -443,41 +454,60 @@ public static final class Signals {
private Signals() {}
}

// ======================================
// UTILITY FUNCTIONS, IGNORE BELOW
// ======================================

private static void outputLog(String tag, Object message, FlixelLogLevel level) {
StackWalker.StackFrame caller = StackWalker.getInstance()
.walk(s -> s.skip(3).findFirst())
.orElse(null);

String file = "UnknownFile.java:0";
String simpleFile = "UnknownFile.java:0";
String method = "unknownMethod()";
if (caller != null) {
file = caller.getFileName() + ":" + caller.getLineNumber();
String className = caller.getClassName();
int lastDot = className.lastIndexOf('.');
String packagePath = (lastDot > 0) ? className.substring(0, lastDot).replace('.', '/') : "";
simpleFile = packagePath.isEmpty()
? caller.getFileName() + ":" + caller.getLineNumber()
: packagePath + "/" + caller.getFileName() + ":" + caller.getLineNumber();
method = caller.getMethodName() + "()";
}

String rawMessage = (message != null) ? message.toString() : "null";
String color = switch (level) {
case INFO -> FlixelConstants.AsciiCodes.WHITE;
case WARN -> FlixelConstants.AsciiCodes.YELLOW;
case ERROR -> FlixelConstants.AsciiCodes.RED;
};
boolean underlineFile = (level == FlixelLogLevel.ERROR);
String plainLog;
String coloredLog;

if (logMode == FlixelLogMode.SIMPLE) {
plainLog = simpleFile + ": " + rawMessage;
coloredLog = colorText(simpleFile, color, true, false, underlineFile)
+ ": "
+ colorText(rawMessage, color, false, true, false);
} else {
String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
String levelTag = "[" + level + "]";
String tagPart = "[" + tag + "]";
String filePart = "[" + file + "]";
String methodPart = "[" + method + "]";

// Plain text for the log file, no ANSI codes.
plainLog = timestamp + " " + levelTag + " " + tagPart + " " + filePart + " " + methodPart + " " + rawMessage;

// ANSI-colored version for the console.
coloredLog = colorText(timestamp + " ", color, false, false, false)
+ colorText(levelTag + " ", color, true, false, false)
+ colorText(tagPart + " ", color, true, false, false)
+ colorText(filePart + " ", color, true, false, underlineFile)
+ colorText(methodPart + " ", color, false, false, false)
+ colorText(rawMessage, color, false, true, false);
}

boolean underline = (level == FlixelLogLevel.ERROR);
String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
String rawTag = "[" + level + "] [" + tag + "] [" + file + "] [" + method + "] ";
String rawMessage = (message != null) ? message.toString() : "null";

// Plain text for the log file, no ANSI codes.
String plainLog = timestamp + " " + rawTag + rawMessage;

// ANSI-colored version for the console.
String coloredLog = colorText(timestamp + " ", color, true, false, underline)
+ colorText(rawTag, color, true, false, underline)
+ colorText(rawMessage, color, false, true, underline);

// Output and store the log message.
System.out.println(coloredLog);
if (game.canStoreLogs()) {
game.enqueueLog(plainLog);
Expand Down Expand Up @@ -509,3 +539,8 @@ enum FlixelLogLevel {
WARN,
ERROR
}

enum FlixelLogMode {
SIMPLE,
DETAILED
}
59 changes: 49 additions & 10 deletions flixelgdx/src/main/java/me/stringdotjar/flixelgdx/FlixelGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,49 @@
import java.util.concurrent.ConcurrentLinkedQueue;

/**
* Flixel's enhanced game object used for containing the main loop and core elements of Flixel.
* The game object used for containing the main loop and core elements of the Flixel game.
*
* <p>If you want to change what happens to the pre and window configurations, you might want to see
* {@code Lwjgl3Launcher} in the {@code lwjgl3} folder.
* <p>Use this for changing the core logic of the game's behavior. To actually use this properly, you
* need to create a subclass of this and override the methods you want to change.
*
* <p>It is recommended for using this in the following way:
*
* <pre>{@code
* // Create a new subclass of FlixelGame.
* // Remember that you can override any methods to add extra functionality
* // to the game's behavior.
* public class MyGame extends FlixelGame {
* public MyGame(String title, int width, int height, FlixelState initialScreen) {
* super(title, width, height, initialScreen);
* }
* }
* }</pre>
*
* Then, in a platform-specific launcher, you can create a new instance of your game and run it:
*
* <pre>{@code
* // Example of how to create a new game instance and run it using the LWJGL3 launcher.
* public class Lwjgl3Launcher {
* public static void main(String[] args) {
* if (StartupHelper.startNewJvmIfRequired()) { // This handles macOS support and helps on Windows.
* return;
* }
* createApplication();
* }
*
* private static void createApplication() {
* MyGame game = new MyGame(
* "My Game",
* 800,
* 600,
* new InitialScreen() // The initial screen the game enters when it starts!
* );
* Flixel.initialize(game); // This is VERY important to do before creating the application!
* Vector2 size = game.getWindowSize();
* new Lwjgl3Application(game, createWindowConfiguration("My Game", (int) size.x, (int) size.y));
* }
* }
* }
*/
public abstract class FlixelGame implements ApplicationListener {

Expand Down Expand Up @@ -225,11 +264,12 @@ public void draw() {
members.end();
state.draw(batch);
stage.draw();

Flixel.Signals.postDraw.dispatch();
}

@Override
public void render() {
public final void render() {
float delta = Gdx.graphics.getDeltaTime();

windowSize.x = Gdx.graphics.getWidth();
Expand Down Expand Up @@ -302,16 +342,11 @@ public void toggleFullscreen() {
* Gets called when the game is closing to perform custom cleanup
* after core resources are disposed and before the log thread shuts down, so any
* logs written here (e.g. via {@link Flixel#info}) are persisted to the log file.
*
* <p>Override this method instead of {@link #dispose()} when you need to add
* cleanup logic. Do not override {@code dispose()} and add logging after
* {@code super.dispose()}, as the log thread will have already exited and logs
* will not be persisted to the file.
*/
protected void close() {}

@Override
public void dispose() {
public final void dispose() {
if (isClosing) {
Flixel.warn("Game is already closing. Skipping dispose...");
return;
Expand Down Expand Up @@ -339,6 +374,10 @@ public void dispose() {
Flixel.info("Disposing all registered fonts...");
FlixelFontRegistry.dispose();

if (AnsiConsole.isInstalled()) {
AnsiConsole.systemUninstall();
}

close();

Flixel.Signals.postGameClose.dispatch();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import java.util.Map;
import java.util.function.Consumer;

/** Core manager class for managing the Polyverse scripting and modding environment. */
/** Core manager class for managing the Polyverse modding environment. */
public final class Polyverse {

/** A map that stores lists of scripts based on their parent class type. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ public void create() {
}

@Override
public void render() {
super.render();
public void update(float elapsed) {
super.update(elapsed);

if (Flixel.keyJustPressed(Input.Keys.F11)) {
toggleFullscreen();
}

Polyverse.forAllScripts(script -> script.onRender(Flixel.getElapsed()));
Polyverse.forAllScripts(script -> script.onUpdate(elapsed));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public String getId() {
/** Called when {@code this} script is created. */
public void onCreate() {}

/** Called every frame to update {@code this} script. */
public void onRender(float delta) {}
/** Called every frame to update the logic of {@code this} script. */
public void onUpdate(float elapsed) {}

/** Called when {@code this} script is disposed of (usually when the game refreshes or closes). */
public void onDispose() {}
Expand Down