Skip to content
This repository has been archived by the owner on Sep 3, 2023. It is now read-only.

Commit

Permalink
make the game starting way easier
Browse files Browse the repository at this point in the history
  • Loading branch information
sanj0 committed Dec 17, 2018
1 parent c54ee7a commit 0b29557
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 26 deletions.
1 change: 1 addition & 0 deletions Salty Engine.iml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Maven: de.edgelord.stdf:stdf:1.3.1" level="project" />
<orderEntry type="library" name="Maven: de.edgelord.sysDepFiles:systemDependentFiles:1.2-SNAPSHOT" level="project" />
<orderEntry type="library" name="Maven: de.edgelord.stdf:stdf:1.3.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.11" level="project" />
Expand Down
76 changes: 67 additions & 9 deletions src/main/java/de/edgelord/saltyengine/core/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@

import java.io.IOException;

/**
* The central core class for a Salty Engine game. To start a game, call whether
* {@link #init(GameConfig)} or {@link #init(Host, String, long)} <br>
*
* and then
*
* {@link #start()}, {@link #start(long)}, {@link #start(long, SplashWindow.Splash)} or {@link #start(SplashWindow.Splash)}
*/
public class Game {

private static GFXController defaultGFXController = new GFXController();
Expand All @@ -49,30 +57,54 @@ public class Game {
private static Host host;
private static Engine engine;

public Game(float resolutionWidth, float resolutionHeight, String gameName, long fixedTickMillis) {
/**
* Initialized the game with the given {@link GameConfig} and a {@link DisplayManager} as {@link Host}.
*
* @param config the configuration of the game
*/
public static void init(GameConfig config) {
internalPreInitDisplayManager(config);
}

System.setProperty("sun.java2d.opengl", "True");
/**
* Initializes the game with the given {@link Host}, the given name and the given milliseconds for the fixed tick.
*
* @param host the {@link Host} for the game
* @param gameName the name of the game
* @param fixedTickMillis the milliseconds for the periodical fixed update for e.g. physics
*/
public static void init(Host host, String gameName, long fixedTickMillis) {
internalPreInitForForeignHost(host, gameName, fixedTickMillis);
}

private static void internalPreInitForForeignHost(Host host, String gameName, long fixedTickMillis) {

enableOpenGl();

engine = new Engine(fixedTickMillis);
host = new DisplayManager(new DisplayRatio(new Dimensions(resolutionWidth, resolutionHeight)), gameName, engine);
gameDimensions = new Dimensions(resolutionWidth, resolutionHeight);
gameDimensions = host.getCurrentDimensions();

Game.host = host;
SaltySystem.defaultHiddenOuterResource = new OuterResource(true);
SaltySystem.defaultOuterResource = new OuterResource(false);
}

public Game(Host host, String gameName, long fixedTickMillis) {
private static void internalPreInitDisplayManager(GameConfig config) {

System.setProperty("sun.java2d.opengl", "True");
enableOpenGl();

engine = new Engine(fixedTickMillis);
gameDimensions = host.getCurrentDimensions();
engine = new Engine(config.getFixedTickMillis());
host = new DisplayManager(new DisplayRatio(new Dimensions(config.getResWidth(), config.getResHeight())), config.getGameName(), engine);
gameDimensions = new Dimensions(config.getResWidth(), config.getResHeight());

Game.host = host;
SaltySystem.defaultHiddenOuterResource = new OuterResource(true);
SaltySystem.defaultOuterResource = new OuterResource(false);
}

private static void enableOpenGl() {
System.setProperty("sun.java2d.opengl", "True");
}

public static Dimensions getGameDimensions() {
return gameDimensions;
}
Expand All @@ -85,21 +117,47 @@ public static float getGameHeight() {
return gameDimensions.getHeight();
}

/**
* Starts the game with the given {@link de.edgelord.saltyengine.display.SplashWindow.Splash} before and running
* with the maximum fps.
* You should call {@link #init(GameConfig)} or {@link #init(Host, String, long)} first.
*
* @param splash the {@link de.edgelord.saltyengine.display.SplashWindow.Splash} to be displayed before the game
*/
public static void start(SplashWindow.Splash splash) {

GameStarter.startGame(-1, splash);
}

/**
* Starts the game with the given fps and the given {@link de.edgelord.saltyengine.display.SplashWindow.Splash}.
* You should call {@link #init(GameConfig)} or {@link #init(Host, String, long)} first.
*
* @param fixedFPS the fps with which the game should run
* @param splash the {@link de.edgelord.saltyengine.display.SplashWindow.Splash} to be displayed when the game starts
*/
public static void start(long fixedFPS, SplashWindow.Splash splash) {

GameStarter.startGame(fixedFPS, splash);
}

/**
* Starts the game with the default {@link de.edgelord.saltyengine.display.SplashWindow.Splash} and as much fps as
* possible.
* You should call {@link #init(GameConfig)} or {@link #init(Host, String, long)} first.
*/
public static void start() {

GameStarter.startGame(-1, SplashWindow.Splash.DEFAULT_SPLASH);
}

/**
* Start the game with the default {@link de.edgelord.saltyengine.display.SplashWindow.Splash} and the given
* fps.
* You should call {@link #init(GameConfig)} or {@link #init(Host, String, long)} first.
*
* @param fixedFPS the fps with which the game should run.
*/
public static void start(long fixedFPS) {

GameStarter.startGame(fixedFPS, SplashWindow.Splash.DEFAULT_SPLASH);
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/de/edgelord/saltyengine/core/GameConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2018 Malte Dostal
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package de.edgelord.saltyengine.core;

public class GameConfig {

private final float resWidth;
private final float resHeight;
private final long fixedTickMillis;
private final String gameName;

public GameConfig(float resWidth, float resHeight, String gameName, long fixedTickMillis) {
this.resWidth = resWidth;
this.resHeight = resHeight;
this.fixedTickMillis = fixedTickMillis;
this.gameName = gameName;
}

public static GameConfig config(float resWidth, float resHeight, String gameName, long fixedTickMillis) {
return new GameConfig(resWidth, resHeight, gameName, fixedTickMillis);
}

public float getResWidth() {
return resWidth;
}

public float getResHeight() {
return resHeight;
}

public long getFixedTickMillis() {
return fixedTickMillis;
}

public String getGameName() {
return gameName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
public class PaneledGame extends Game {

public PaneledGame(Container container, int x, int y, int width, int height, long fixedTicksMillis, String gameName) {
super(new PaneledGameHost(container, x, y, width, height, fixedTicksMillis), gameName, fixedTicksMillis);
init(new PaneledGameHost(container, x, y, width, height, fixedTicksMillis), gameName, fixedTicksMillis);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,15 @@
package de.edgelord.saltyengine.example;

import de.edgelord.saltyengine.core.Game;
import de.edgelord.saltyengine.core.GameConfig;

/**
* This is an example for the main class of a game.
*/
public class ExampleMain extends Game {

public ExampleMain(int windowWidth, int windowHeight, String gameName, long fixedTickMillis) {
super(windowWidth, windowHeight, gameName, fixedTickMillis);
}

public static void main(String[] args) {
new ExampleMain(1920, 1080, "Salty Engine Example Game", 1);

init(GameConfig.config(1920, 1080, "Salty Engine Example Game", 1));
Game.start();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,16 @@
package de.edgelord.saltyengine.example.screen;

import de.edgelord.saltyengine.core.Game;
import de.edgelord.saltyengine.core.GameConfig;
import de.edgelord.saltyengine.scene.SceneManager;
import de.edgelord.saltyengine.ui.elements.BorderedLabel;

public class ScreenExampleMain extends Game {

public ScreenExampleMain(int windowWidth, int windowHeight, String gameName, long fixedTickMillis) {
super(windowWidth, windowHeight, gameName, fixedTickMillis);
}

public static void main(String[] args) {
new ScreenExampleMain(920, 720, "Example of a simple Screen", 10);

Game.getHostAsDisplayManager().getStage().setHighQuality(true);

Game.init(GameConfig.config(920, 720, "Example of a simple Screen", 10));
Game.start();

SceneManager.getCurrentScene().addGameObject(new Screen());
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/testing/Tester.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import de.edgelord.saltyengine.audio.AudioSystem;
import de.edgelord.saltyengine.core.Game;
import de.edgelord.saltyengine.core.GameConfig;
import de.edgelord.saltyengine.display.SplashWindow;
import de.edgelord.saltyengine.factory.AudioFactory;
import de.edgelord.saltyengine.resource.InnerResource;
Expand All @@ -31,8 +32,9 @@ public class Tester extends Game {

private static AudioSystem audioSystem;

public Tester(final int windowWidth, final int windowHeight, final String gameName, final long fixedTickMillis) {
super(windowWidth, windowHeight, gameName, fixedTickMillis);
private static void initGame() {

Game.init(GameConfig.config(1200f, 900f, "testing", 1L));

System.out.println("Welcome to Salty Engine v" + SaltySystem.versionTag);

Expand All @@ -49,8 +51,7 @@ public Tester(final int windowWidth, final int windowHeight, final String gameNa
}

public static void main(final String[] args) throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {

final Tester tester = new Tester(1200, 900, "testing", 1);
initGame();

SceneManager.addScene("testingScene", TestingScene.class);

Expand Down

0 comments on commit 0b29557

Please sign in to comment.