Skip to content
Open
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
18 changes: 11 additions & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ env:
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@v2
- uses: actions/setup-java@v2
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '16'
java-version: '24'
cache: gradle
- name: Validate Gradle wrapper
uses: gradle/wrapper-validation-action@e6e38bacfdf1a337459f332974bb2327a31aaf4b
Expand All @@ -22,12 +24,14 @@ jobs:
- run: gradle assemble --no-daemon
test:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- uses: actions/checkout@v2
- uses: actions/setup-java@v2
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '16'
java-version: '24'
cache: gradle
- name: Validate Gradle wrapper
uses: gradle/wrapper-validation-action@e6e38bacfdf1a337459f332974bb2327a31aaf4b
Expand All @@ -43,7 +47,7 @@ jobs:
- uses: actions/setup-java@master
with:
distribution: 'temurin'
java-version: '16'
java-version: '24'
cache: gradle
- name: Validate Gradle wrapper
uses: gradle/wrapper-validation-action@e6e38bacfdf1a337459f332974bb2327a31aaf4b
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ jobs:
contents: read
packages: write
steps:
- uses: actions/checkout@v2
- uses: actions/setup-java@v2
- uses: actions/checkout@v4
- uses: actions/setup-java@v4
with:
java-version: '16'
java-version: '24'
distribution: 'temurin'
- name: Validate Gradle wrapper
uses: gradle/wrapper-validation-action@e6e38bacfdf1a337459f332974bb2327a31aaf4b
Expand Down
14 changes: 9 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ plugins {
}

group 'io.github.math0898'
version '2.5.1'
version '3.0.0-zeta5'
var isSnapshot = true

repositories {
mavenCentral()
}

compileJava {
options.release = 19
options.release = 24
}

jacoco {
toolVersion = "0.8.8"
toolVersion = "0.8.13"
}

jacocoTestReport {
Expand All @@ -27,7 +28,7 @@ jacocoTestReport {

java {
toolchain {
languageVersion = JavaLanguageVersion.of(19)
languageVersion = JavaLanguageVersion.of(24)
}
withSourcesJar()
withJavadocJar()
Expand All @@ -48,7 +49,10 @@ publishing {
publications {
maven(MavenPublication) {
groupId(group)
artifactId('suga-engine')
if (isSnapshot)
artifactId('suga-engine-snapshot')
else
artifactId('suga-engine')
version(version) // Pulls version from Gradle root
from components.java

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
92 changes: 72 additions & 20 deletions src/main/java/suga/engine/GameEngine.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package suga.engine;

import suga.engine.game.Game;
import suga.engine.graphics.GraphicsPanel;
import suga.engine.input.keyboard.GameKeyListener;
import suga.engine.input.mouse.GameMouseListener;
import suga.engine.logger.GeneralLogger;
import suga.engine.logger.Logger;
import suga.engine.threads.GameLogicThread;
import suga.engine.threads.GraphicsThread;
import suga.engine.threads.SugaThread;
import suga.engine.input.keyboard.GameKeyListener;
import suga.engine.game.Game;

import javax.swing.*;
import java.awt.*;
Expand All @@ -21,25 +21,30 @@
*/
public class GameEngine {

/**
* Reference to the single GameEngine that exists at runtime.
*/
private static GameEngine globalEngine;

/**
* The currently opened frame.
*/
protected static JFrame frame;
private JFrame frame;

/**
* The current graphics thread if one is running.
*/
protected static SugaThread graphics;
private SugaThread graphics;

/**
* The current game logic thread if one is running.
*/
protected static SugaThread logic;
private SugaThread logic;

/**
* The logger currently being used by the GameEngine.
*/
protected static Logger logger = new GeneralLogger();
private Logger logger = new GeneralLogger();

/**
* An enum of pre-defined resolutions to open a game window at.
Expand All @@ -51,15 +56,64 @@ public enum Window {
/**
* Creates the window at the largest possible resolution in full screen mode.
*/
FULL_SCREEN
FULL_SCREEN,

/**
* Creates the window at half the maximum size in each direction. Leaves top bar.
*/
WINDOWED;

/**
* Modifies the given frame's size depending on the given Window parameter.
*
* @param frame The frame that should be modified to match the window state.
*/
public void modifyFrame (JFrame frame) {
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
switch (this) {
case FULL_SCREEN -> {
frame.setSize(dim);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(true);
}
case WINDOWED -> {
frame.setSize(dim.width / 2, dim.height / 2);
frame.setUndecorated(false);
}
}
}
}

/**
* The GameEngine constructor.
*/
protected GameEngine () {

}

/**
* Creates a new GameEngine object and assigns it to the static reference.
*/
private static void createGameEngine () {
globalEngine = new GameEngine();
}

/**
* Accessor method to the global instance of the GameEngine.
*
* @return A reference to the GameEngine.
*/
public static GameEngine getInstance () {
if (globalEngine == null) createGameEngine();
return globalEngine;
}

/**
* Accessor method for the logger being used by the GameEngine.
*
* @return The logger currently in use by the GameEngine.
*/
public static Logger getLogger () {
public Logger getLogger () {
return logger;
}

Expand All @@ -68,16 +122,16 @@ public static Logger getLogger () {
*
* @param logger The new logger for the GameEngine to use.
*/
public static void setLogger (Logger logger) {
GameEngine.logger.log("GameEngine: Switching to a new logger.");
GameEngine.logger = logger;
GameEngine.logger.log("GameEngine: Switched logger.");
public void setLogger (Logger logger) {
this.logger.log("GameEngine: Switching to a new logger.");
this.logger = logger;
this.logger.log("GameEngine: Switched logger.");
}

/**
* Closes both the logic and graphics thread.
*/
public static void stop () {
public void stop () {
logger.log("GameEngine: Stopping the game.");
graphics.setStopped(true);
logic.setStopped(true);
Expand All @@ -89,10 +143,8 @@ public static void stop () {
/**
* Creates a new game window with all the possible configuration options being specified.
*
* @param width The width to create the game window at.
* @param height The height to create the game window at.
* @param window An enum parameter which is used to create the desired window size.
* @param name The name for the resulting window.
* @param border Whether to hide the border or not when creating the window.
* @param panel The graphics panel to be used for this game.
* @param background The background color for the panel.
* @param logicRate How many times per second the game logic should be called.
Expand All @@ -101,17 +153,17 @@ public static void stop () {
* @param mouseListener The mouse listener to use for this window. Will override active frame.
* @param game The game to attach to this window. Will override currently active panel or input listeners.
*/
public static void launchGameWindow (int width, int height, String name, boolean border, GraphicsPanel panel,
public void launchGameWindow (Window window, String name, GraphicsPanel panel,
Color background, int logicRate, int frameRate, GameKeyListener keyListener,
GameMouseListener mouseListener, Game game) {
logger.log("GameEngine: Starting the game window.");
panel.setBackground(background);
frame = new JFrame(name);
frame.setSize(width, height);
window.modifyFrame(frame);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(dim.width / 4, dim.height / 4);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(panel, BorderLayout.CENTER);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setUndecorated(!border);
frame.setVisible(true);
graphics = new GraphicsThread(panel, frameRate);
graphics.start();
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/suga/engine/game/BasicGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
import suga.engine.game.objects.GameObject;
import suga.engine.graphics.DrawListener;
import suga.engine.graphics.GraphicsPanel;
import suga.engine.input.keyboard.GameKeyListener;
import suga.engine.input.keyboard.KeyValue;
import suga.engine.input.mouse.BasicMouseListener;
import suga.engine.input.mouse.GameMouseListener;
import suga.engine.logger.Level;
import suga.engine.physics.BasicPhysicsEngine;
import suga.engine.physics.PhysicsEngine;
import suga.engine.physics.collidables.Collidable;
import suga.engine.threads.SugaThread;
import suga.engine.input.keyboard.GameKeyListener;
import suga.engine.input.keyboard.KeyValue;

import java.awt.event.MouseEvent;
import java.util.*;
Expand Down Expand Up @@ -159,7 +159,7 @@ public void loop () {
@Override
public void processInput () {
if (loadedScene == null) {
GameEngine.getLogger().log("BasicGame: No loaded scene. Cannot process inputs.", Level.WARNING);
GameEngine.getInstance().getLogger().log("BasicGame: No loaded scene. Cannot process inputs.", Level.WARNING);
return;
}
Stack<MouseEvent> mice = mouseListener.getEvents();
Expand Down Expand Up @@ -231,8 +231,8 @@ public void clear () {
agents = new ArrayList<>();
objects = new HashMap<>();
if (panel != null) panel.clearListeners();
else GameEngine.getLogger().log("A clear of game objects was requested but this game does not have an active panel.", Level.WARNING);
GameEngine.getLogger().log("Cleared game objects.", Level.INFO);
else GameEngine.getInstance().getLogger().log("A clear of game objects was requested but this game does not have an active panel.", Level.WARNING);
GameEngine.getInstance().getLogger().log("Cleared game objects.", Level.INFO);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/main/java/suga/engine/game/BasicScene.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package suga.engine.game;

import suga.engine.input.keyboard.KeyValue;

import java.awt.*;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/suga/engine/game/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import suga.engine.game.objects.GameObject;
import suga.engine.graphics.DrawListener;
import suga.engine.graphics.GraphicsPanel;
import suga.engine.input.keyboard.GameKeyListener;
import suga.engine.input.mouse.GameMouseListener;
import suga.engine.threads.SugaThread;
import suga.engine.input.keyboard.GameKeyListener;

/**
* Games require a main game loop to run along with game components that need to be run every game cycle.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package suga.engine.game.objects;

import suga.engine.graphics.GraphicsPanel;
import suga.engine.graphics.DrawListener;
import suga.engine.graphics.GraphicsPanel;
import suga.engine.physics.BasicPhysical;
import suga.engine.physics.Vector;
import suga.engine.physics.collidables.Collidable;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/suga/engine/graphics/GraphicsPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void drawing (int width, int height) {
l.applyChanges(width, height, this);
}
} catch (ConcurrentModificationException e) {
GameEngine.getLogger().log(e, Level.WARNING); // This sometimes occurs when loading while drawing a frame.
GameEngine.getInstance().getLogger().log(e, Level.WARNING); // This sometimes occurs when loading while drawing a frame.
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,10 @@ public interface GraphicsPanelInterface {
* @param image The image to draw to the screen.
*/
void addImage (int x, int y, int width, int height, BufferedImage image);

/**
* Called each frame by the GraphicsThread. This method should be inherited from JComponent but is defined in
* Component.
*/
void repaint ();
}
Loading