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
10 changes: 5 additions & 5 deletions assets/test.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Input
import com.badlogic.gdx.graphics.Color
import me.stringdotjar.flixelgdx.Flixel
import me.stringdotjar.flixelgdx.graphics.screen.FlixelScreen
import me.stringdotjar.flixelgdx.graphics.FlixelScreen
import me.stringdotjar.flixelgdx.backend.FlixelPaths
import me.stringdotjar.flixelgdx.graphics.sprite.FlixelSprite
import me.stringdotjar.polyverse.script.type.SystemScript
Expand Down Expand Up @@ -46,8 +46,8 @@ class TestScreen extends FlixelScreen {
private FlixelSprite test

@Override
void show() {
super.show()
void create() {
super.create()

test = new FlixelSprite().loadGraphic(FlixelPaths.sharedImageAsset('NOTE_hold_assets'))
add(test)
Expand All @@ -58,7 +58,7 @@ class TestScreen extends FlixelScreen {
}

@Override
void render(float delta) {
super.render(delta)
void update(float delta) {
super.update(delta)
}
}
27 changes: 21 additions & 6 deletions flixelgdx/src/main/java/me/stringdotjar/flixelgdx/Flixel.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Stage;
import games.rednblack.miniaudio.MAGroup;
import games.rednblack.miniaudio.MASound;
import games.rednblack.miniaudio.MiniAudio;
import games.rednblack.miniaudio.loader.MASoundLoader;
import me.stringdotjar.flixelgdx.graphics.screen.FlixelScreen;
import me.stringdotjar.flixelgdx.graphics.FlixelScreen;
import me.stringdotjar.flixelgdx.graphics.FlixelViewport;
import me.stringdotjar.flixelgdx.util.FlixelConstants;
import me.stringdotjar.flixelgdx.signal.FlixelSignal;
import me.stringdotjar.flixelgdx.signal.FlixelSignalData.MusicPlayedSignalData;
Expand Down Expand Up @@ -88,12 +90,13 @@ public static void setScreen(FlixelScreen newScreen) {
if (newScreen == null) {
throw new IllegalArgumentException("Screen cannot be null!");
}
if (Flixel.screen != null) {
Flixel.screen.hide();
Flixel.screen.dispose();
if (screen != null) {
screen.hide();
screen.dispose();
}
Flixel.screen = newScreen;
Flixel.screen.show();
game.resetViewports();
screen = newScreen;
screen.create();
Signals.postScreenSwitch.dispatch(new ScreenSwitchSignalData(newScreen));
}

Expand Down Expand Up @@ -377,6 +380,10 @@ public static MASound getMusic() {
return music;
}

public static Vector2 getWindowSize() {
return game.windowSize;
}

public static MiniAudio getAudioEngine() {
return engine;
}
Expand All @@ -397,6 +404,14 @@ public static float getDelta() {
return Gdx.graphics.getDeltaTime();
}

public static FlixelViewport getViewport() {
return game.getViewport();
}

public static boolean isFullscreen() {
return Gdx.graphics.isFullscreen();
}

/**
* Contains all the global events that get dispatched when something happens in the game.
*
Expand Down
168 changes: 110 additions & 58 deletions flixelgdx/src/main/java/me/stringdotjar/flixelgdx/FlixelGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,21 @@
import com.badlogic.gdx.Application;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.ScreenUtils;
import com.badlogic.gdx.utils.viewport.FitViewport;
import me.stringdotjar.flixelgdx.graphics.screen.FlixelScreen;
import me.stringdotjar.flixelgdx.graphics.sprite.FlixelObject;
import com.badlogic.gdx.utils.SnapshotArray;
import me.stringdotjar.flixelgdx.graphics.FlixelObject;
import me.stringdotjar.flixelgdx.graphics.FlixelScreen;
import me.stringdotjar.flixelgdx.graphics.FlixelViewport;
import me.stringdotjar.flixelgdx.graphics.sprite.FlixelSprite;
import me.stringdotjar.flixelgdx.tween.FlixelTween;
import me.stringdotjar.flixelgdx.util.FlixelConstants;
import me.stringdotjar.flixelgdx.util.FlixelRuntimeUtil;

import java.awt.EventQueue;

import static me.stringdotjar.flixelgdx.signal.FlixelSignalData.RenderSignalData;

/**
Expand All @@ -41,26 +38,23 @@ public abstract class FlixelGame implements ApplicationListener {
protected FlixelScreen initialScreen;

/** Is the game's window currently focused? */
protected boolean isFocused = true;
private boolean isFocused = true;

/** Is the game's window currently minimized? */
protected boolean isMinimized = false;
private boolean isMinimized = false;

/** The main stage used for rendering all screens and sprites on screen. */
protected Stage stage;

/** The main viewport used to fit the world no matter the screen size. */
protected FitViewport viewport;

/** The main camera used to see the world. */
protected OrthographicCamera camera;

/** The main sprite batch used for rendering all sprites on screen. */
protected SpriteBatch batch;

/** The 1x1 texture used to draw the background color of the current screen. */
protected Texture bgTexture;

/** Where all the global viewports are stored. */
protected SnapshotArray<FlixelViewport> viewports;

/**
* Creates a new game instance with the specified title, window width/height, and initial screen. This configures
* the game's core parts, such as the viewport, stage, etc.
Expand All @@ -81,13 +75,10 @@ public void create() {
configureCrashHandler(); // This should ALWAYS be called first no matter what!

batch = new SpriteBatch();
viewport = new FitViewport(windowSize.x, windowSize.y);
viewport.apply();

camera = new OrthographicCamera();
camera.setToOrtho(false, windowSize.x, windowSize.y);
viewports = new SnapshotArray<>(FlixelViewport.class);
viewports.add(new FlixelViewport((int) windowSize.x, (int) windowSize.y));

stage = new Stage(viewport, batch);
stage = new Stage(getViewport(), batch);

Pixmap pixmap = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
pixmap.setColor(Color.WHITE);
Expand All @@ -101,8 +92,14 @@ public void create() {

@Override
public void resize(int width, int height) {
windowSize.set(width, height);
viewport.update(width, height, true);
FlixelViewport[] viewportsArray = viewports.begin();
for (int i = 0; i < viewports.size; i++) {
FlixelViewport viewport = viewportsArray[i];
if (viewport != null) {
viewport.update(width, height, true);
}
}
viewports.end();
}

@Override
Expand All @@ -112,34 +109,60 @@ public void render() {

Flixel.Signals.preRender.dispatch(new RenderSignalData(delta));

stage.act(delta);
FlixelTween.getGlobalManager().update(delta);

// Update and render the current screen that's active.
ScreenUtils.clear(Color.BLACK);
viewport.apply();
batch.setProjectionMatrix(camera.combined);
batch.begin();

if (screen != null) {
batch.setColor(screen.getBgColor());
batch.draw(bgTexture, 0, 0, viewport.getWorldWidth(), viewport.getWorldHeight());
batch.setColor(Color.WHITE); // Set color back to white so display objects aren't affected.
screen.render(delta);
var members = screen.members.begin();
for (FlixelObject object : members) {
if (object == null) {
screen.update(delta);

// Loop through all viewports and draw the current screen's members onto their set viewports.
FlixelViewport[] vps = viewports.begin();
SnapshotArray<FlixelObject> members = screen.getMembers();
for (int i = 0; i < viewports.size; i++) {
FlixelViewport viewport = vps[i];
if (viewport == null) {
continue;
}
object.update(delta);
object.draw(batch);

viewport.apply();
var camera = viewport.getCamera();
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();

// Draw the background color first.
batch.setColor(screen.getBgColor());
batch.draw(bgTexture, 0, 0, getViewport().getWorldWidth(), getViewport().getWorldHeight());
batch.setColor(Color.WHITE); // Set the batch color back to white so display objects aren't affected.
// Draw all the current screens members.
FlixelObject[] mbrs = members.begin();
for (int j = 0; j < members.size; j++) {
FlixelObject member = mbrs[j];
if (member == null) {
continue;
}
member.update(delta);
if (member instanceof FlixelSprite s) {
// Check if the current sprite is in the visible part of the viewport.
// If it cannot be seen, then we don't draw the sprite to save performance.
if (camera.frustum.boundsInFrustum(s.getX(), s.getY(), 0, s.getWidth(), s.getHeight(), 0)) {
s.draw(batch);
}
} else {
member.draw(batch);
}
}
batch.end();
members.end();
}
screen.members.end();
viewports.end();
screen.draw(batch);
}

batch.end();
stage.act(delta);
stage.draw();

FlixelTween.getGlobalManager().update(delta);

Flixel.Signals.postRender.dispatch(new RenderSignalData(delta));
}

Expand Down Expand Up @@ -170,7 +193,7 @@ public void onWindowUnfocused() {
* Called when the user minimizes the game's window.
*
* @param iconified Whether the window is iconified (minimized) or not. This parameter is provided
* for compatibility with the window listener in the LWJGL3 (desktop) launcher.
* for compatibility with the window listener in the LWJGL3 (desktop) launcher.
*/
public void onWindowMinimized(boolean iconified) {
isMinimized = iconified;
Expand All @@ -182,18 +205,29 @@ public void onWindowMinimized(boolean iconified) {
Flixel.info("Game window has been minimized.");
}

/** Toggles fullscreen mode on or off, depending on the current state. */
public void toggleFullscreen() {
boolean isFullscreen = Gdx.graphics.isFullscreen();
if (isFullscreen) {
Gdx.graphics.setWindowedMode((int) windowSize.x, (int) windowSize.y);
Flixel.info("Exiting fullscreen mode.");
} else {
/**
* Sets fullscreen mode for the game's window.
*
* @param enabled If the game's window should be in fullscreen mode.
*/
public void setFullscreen(boolean enabled) {
if (enabled) {
if (!Flixel.isFullscreen()) {
windowSize.set(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}
Gdx.graphics.setFullscreenMode(Gdx.graphics.getDisplayMode());
Flixel.info("Entering fullscreen mode.");
Flixel.info("Entered fullscreen mode.");
} else {
Gdx.graphics.setWindowedMode((int) windowSize.x, (int) windowSize.y);
Flixel.info("Exited fullscreen mode.");
}
}

/** Toggles fullscreen mode on or off, depending on the current state. */
public void toggleFullscreen() {
setFullscreen(!Flixel.isFullscreen());
}

@Override
public void dispose() {
Flixel.warn("SHUTTING DOWN GAME AND DISPOSING ALL RESOURCES.");
Expand All @@ -214,8 +248,6 @@ public void dispose() {
Flixel.getSoundsGroup().dispose();
Flixel.getAudioEngine().dispose();

Flixel.info("Disposing and shutting down scripts...");

Flixel.Signals.postGameClose.dispatch();
}

Expand All @@ -226,7 +258,7 @@ protected void configureCrashHandler() {
Thread.setDefaultUncaughtExceptionHandler((thread, throwable) -> {
String logs = FlixelRuntimeUtil.getFullExceptionMessage(throwable);
String msg = "There was an uncaught exception on thread \"" + thread.getName() + "\"!\n" + logs;
Flixel.error(FlixelConstants.System.LOG_TAG, msg);
Flixel.error(msg);
FlixelRuntimeUtil.showErrorAlert("Uncaught Exception", msg);
dispose();
// Only use Gdx.app.exit() on non-iOS platforms to avoid App Store guideline violations!
Expand All @@ -252,12 +284,26 @@ public Stage getStage() {
return stage;
}

public FitViewport getViewport() {
return viewport;
public FlixelViewport getViewport() {
Vector2 windowSize = Flixel.getWindowSize();
if (viewports == null) {
Flixel.warn("Viewport list is null. Resigning with fresh array...");
viewports = new SnapshotArray<>();
}
if (viewports.isEmpty()) {
Flixel.warn("Viewport list is empty. Adding new fresh default viewport...");
viewports.add(new FlixelViewport((int) windowSize.x, (int) windowSize.y));
}
return viewports.first();
}

public OrthographicCamera getCamera() {
return camera;
public void resetViewports() {
viewports.clear();
viewports.add(new FlixelViewport((int) windowSize.x, (int) windowSize.y));
}

public SnapshotArray<FlixelViewport> getViewports() {
return viewports;
}

public SpriteBatch getBatch() {
Expand All @@ -271,4 +317,10 @@ public Texture getBgTexture() {
public boolean isMinimized() {
return isMinimized;
}

public void setWindowSize(Vector2 windowSize) {
this.windowSize = windowSize;
Gdx.graphics.setWindowedMode((int) windowSize.x, (int) windowSize.y);
Flixel.info("Set window size. (WIDTH=" + windowSize.x + ", HEIGHT=" + windowSize.y + ")");
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package me.stringdotjar.flixelgdx.graphics.sprite;
package me.stringdotjar.flixelgdx.graphics;

import com.badlogic.gdx.graphics.g2d.Batch;
import me.stringdotjar.flixelgdx.graphics.screen.FlixelScreen;

/** An interface which allows any class that implements it to be added to a {@link FlixelScreen}. */
public interface FlixelObject {
void update(float delta);
void draw(Batch batch);
void destroy();
}
Loading