Bug 20: Rework and refactor the Screen class

This commit is contained in:
Robert Vokac 2024-09-15 19:40:20 +02:00
parent 82c97670fd
commit 3bc112c17d
No known key found for this signature in database
GPG Key ID: C459E1E4B4A986BB
15 changed files with 117 additions and 57 deletions

View File

@ -4,9 +4,11 @@ import android.os.Bundle;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.openeggbert.core.main.OpenEggbertApplication;
import com.openeggbert.core.main.OpenEggbertGame;
import com.pixelgamelibrary.api.Pixel;
import com.pixelgamelibrary.backend.libgdx.PixelBackendLibGDX;
import com.pixelgamelibrary.backend.libgdx.game.LibGdxGame;
/** Launches the Android application. */
public class AndroidLauncher extends AndroidApplication {
@ -16,6 +18,6 @@ public class AndroidLauncher extends AndroidApplication {
AndroidApplicationConfiguration configuration = new AndroidApplicationConfiguration();
configuration.useImmersiveMode = true; // Recommended, but not required.
Pixel.initBackend(new PixelBackendLibGDX());
initialize(new OpenEggbertGame(), configuration);
initialize(new LibGdxGame(new OpenEggbertApplication().createGame()), configuration);
}
}

View File

@ -20,7 +20,6 @@
package com.openeggbert.core.configuration;
import com.openeggbert.core.main.OpenEggbertException;
import com.openeggbert.core.utils.OpenEggbertUtils;
import com.pixelgamelibrary.api.Pixel;
import java.util.HashMap;
import java.util.Map;

View File

@ -21,6 +21,7 @@ package com.openeggbert.core.configuration;
import com.pixelgamelibrary.api.Pixel;
import com.openeggbert.core.main.OpenEggbertException;
import com.pixelgamelibrary.api.WindowMode;
/**
*
@ -63,9 +64,9 @@ public enum OpenEggbertDisplayMode {
}
public static OpenEggbertDisplayMode setDisplayMode(OpenEggbertDisplayMode displayMode) {
String result = Pixel.graphics().setDisplayMode(displayMode == FULLSCREEN, displayMode == WINDOW);
WindowMode result = Pixel.graphics().setDisplayMode(displayMode == FULLSCREEN, displayMode == WINDOW);
return result == null ? null : OpenEggbertDisplayMode.valueOf(result);
return result == null ? null : OpenEggbertDisplayMode.valueOf(result.name());
}
public OpenEggbertDisplayMode flip() {

View File

@ -0,0 +1,57 @@
///////////////////////////////////////////////////////////////////////////////////////////////
// Open Eggbert: Free recreation of the computer game Speedy Eggbert.
// Copyright (C) 2024 the original author or authors.
//
// This program is free software: you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see
// <https://www.gnu.org/licenses/> or write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
///////////////////////////////////////////////////////////////////////////////////////////////
package com.openeggbert.core.main;
import com.openeggbert.core.gamespace.GameSpace;
import com.pixelgamelibrary.api.GameI;
import com.pixelgamelibrary.api.PixelApplication;
import java.util.Map;
/**
* {@link com.badlogic.gdx.ApplicationListener} implementation shared by all
* platforms.
*/
public class OpenEggbertApplication extends PixelApplication {
private static final String GAME_SPACE = "gameSpace";
private static final String ABSOLUTE_PATH_OF_ROOT_DIRECTORY_IN = "absolutePathOfRootDirectoryIn";
@Override
public GameI createGameViaMap(Map<String, Object> objects) {
String absolutePathOfRootDirectoryIn = null;
GameSpace gameSpace = null;
if (objects.containsKey(ABSOLUTE_PATH_OF_ROOT_DIRECTORY_IN)) {
absolutePathOfRootDirectoryIn = (String) objects.get(ABSOLUTE_PATH_OF_ROOT_DIRECTORY_IN);
}
if (objects.containsKey(GAME_SPACE)) {
gameSpace = (GameSpace) objects.get(GAME_SPACE);
}
if (absolutePathOfRootDirectoryIn != null) {
return gameSpace == null ? new OpenEggbertGame(absolutePathOfRootDirectoryIn) : new OpenEggbertGame(gameSpace, absolutePathOfRootDirectoryIn);
} else {
return new OpenEggbertGame();
}
}
}

View File

@ -1,25 +1,5 @@
///////////////////////////////////////////////////////////////////////////////////////////////
// Open Eggbert: Free recreation of the computer game Speedy Eggbert.
// Copyright (C) 2024 the original author or authors.
//
// This program is free software: you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 3
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see
// <https://www.gnu.org/licenses/> or write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
///////////////////////////////////////////////////////////////////////////////////////////////
package com.openeggbert.core.main;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
@ -28,14 +8,14 @@ import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.utils.ObjectMap;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.openeggbert.core.configuration.ConfigDef;
import com.openeggbert.core.configuration.OpenEggbertDisplayMode;
import com.openeggbert.core.gamespace.GameSpace;
import com.openeggbert.core.mod.Mod;
import com.openeggbert.core.mod.ModIdentification;
import com.openeggbert.core.screen.GameSpaceListScreen;
import com.openeggbert.core.screen.InitScreen;
import com.openeggbert.core.configuration.OpenEggbertDisplayMode;
import com.pixelgamelibrary.api.Game;
import com.openeggbert.core.utils.OpenEggbertUtils;
import com.pixelgamelibrary.api.GameAdapter;
import com.pixelgamelibrary.api.Pixel;
import com.pixelgamelibrary.api.storage.FileHandle;
import java.util.ArrayList;
@ -44,11 +24,11 @@ import java.util.Optional;
import lombok.Data;
/**
* {@link com.badlogic.gdx.ApplicationListener} implementation shared by all
* platforms.
*
* @author robertvokac
*/
@Data
public class OpenEggbertGame extends Game {
public class OpenEggbertGame extends GameAdapter {
private Texture image;
private GameSpace gameSpace = null;
@ -77,6 +57,7 @@ public class OpenEggbertGame extends Game {
public OpenEggbertGame(GameSpace gameSpace, String absolutePathOfRootDirectoryIn) {
this.gameSpace = gameSpace;
this.absolutePathOfRootDirectory = absolutePathOfRootDirectoryIn;
Pixel.app().setGame(this);
}
@ -85,41 +66,40 @@ public class OpenEggbertGame extends Game {
// viewport = new FitViewport(640,480);
// viewport.apply();
//camera = new OrthographicCamera();
//.setToOrtho(false,640,480);
//.setToOrtho(false,640,480);
System.out.println("Searching mods");
// for(FileHandle f:Gdx.files.internal(".").list()) {
// System.out.println("assets contains also: " + f.name());
// }
com.pixelgamelibrary.api.storage.FileHandle embeddedModsDirectory = Pixel.asset().getAssets().file("/embedded_mods");
System.out.println("embeddedModsDirectory.exists=" + embeddedModsDirectory.exists());
System.out.println("embeddedModsDirectory.list().size()=" + embeddedModsDirectory.list().size());
embeddedModsDirectory.list().forEach(e->System.out.println(e.path()));
Pixel.asset().getAssets().list().forEach(e->System.out.println(e));
embeddedModsDirectory.list().forEach(e -> System.out.println(e.path()));
Pixel.asset().getAssets().list().forEach(e -> System.out.println(e));
for (FileHandle embeddedModGroup : embeddedModsDirectory.list()) {
if(embeddedModGroup.name().equals("README.md"))continue;
if (embeddedModGroup.name().equals("README.md")) {
continue;
}
System.out.println("Found group " + embeddedModGroup.name());
for (FileHandle embeddedMod : embeddedModGroup.list()) {
System.out.println("Found mod " + embeddedMod.name());
FileHandle modXml = null;
for(FileHandle file: embeddedMod.list()) {
if(file.name().equals("mod.xml")) {
for (FileHandle file : embeddedMod.list()) {
if (file.name().equals("mod.xml")) {
modXml = file;
}
}
if (modXml == null) {
continue;
}
System.out.println("Found mod: " + embeddedMod.name());
Mod mod = new Mod(modXml.readString());
embeddedMods.add(mod);
System.out.println("embeddedMods.size(): " + embeddedMods.size());
@ -133,6 +113,7 @@ public class OpenEggbertGame extends Game {
image = new Texture("libgdx.png");
shapeRenderer = new ShapeRenderer();
font = new BitmapFont();
System.out.println("Going to set screen");
setScreen(gameSpace == null ? new GameSpaceListScreen(this) : new InitScreen(this));
}
@ -178,8 +159,9 @@ public class OpenEggbertGame extends Game {
return Optional.empty();
}
}
public Mod loadMod(ModIdentification modIdentification) {
return embeddedMods.stream().filter(m->m.getIdentification().asString().equals(modIdentification.asString())).findFirst().get();
return embeddedMods.stream().filter(m -> m.getIdentification().asString().equals(modIdentification.asString())).findFirst().get();
}
}

View File

@ -30,7 +30,7 @@ import com.openeggbert.core.main.OpenEggbertGame;
*
* @author robertvokac
*/
public class AbstractGameScreen extends AbstractOpenEggbertScreen {
public class AbstractGameScreen extends OpenEggbertScreen {
public AbstractGameScreen(OpenEggbertGame openEggbertGame) {
super(openEggbertGame);

View File

@ -31,7 +31,7 @@ import java.util.Optional;
*
* @author robertvokac
*/
public class DemoScreen extends AbstractOpenEggbertScreen {
public class DemoScreen extends OpenEggbertScreen {
public DemoScreen(OpenEggbertGame openEggbertGame) {
super(openEggbertGame);

View File

@ -29,6 +29,7 @@ import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.utils.ScreenUtils;
import com.openeggbert.core.gamespace.GameSpace;
import com.openeggbert.core.main.OpenEggbertApplication;
import com.openeggbert.core.main.OpenEggbertGame;
import com.openeggbert.core.mod.Mod;
import com.openeggbert.core.mod.ModType;
@ -44,7 +45,7 @@ import lombok.ToString;
*
* @author robertvokac
*/
public class GameSpaceListScreen extends AbstractOpenEggbertScreen {
public class GameSpaceListScreen extends OpenEggbertScreen {
private int pageNumber = 1;
private final int pageSize = 5;
@ -85,6 +86,10 @@ public class GameSpaceListScreen extends AbstractOpenEggbertScreen {
@Override
public void show() {
System.out.println("Calling : GameSpaceListScreen : show");
try{
throw new RuntimeException();
} catch (Exception e) {e.printStackTrace();}
Gdx.input.setInputProcessor(new InputAdapter() {
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
@ -184,6 +189,7 @@ public class GameSpaceListScreen extends AbstractOpenEggbertScreen {
@Override
public void renderOpenEggbertScreen(float delta) {
System.out.println("Calling : GameSpaceListScreen : renderOpenEggbertScreen");
//Gdx.app.log(getClass().getName(), game.getStorage().debug());
timeSeconds += Gdx.graphics.getRawDeltaTime();

View File

@ -34,7 +34,7 @@ import java.util.Optional;
*
* @author robertvokac
*/
public class InitScreen extends AbstractOpenEggbertScreen {
public class InitScreen extends OpenEggbertScreen {
private float timeSeconds = 0f;

View File

@ -25,10 +25,10 @@ import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.pixelgamelibrary.api.Screen;
import com.openeggbert.core.gamespace.GameFileType;
import com.openeggbert.core.main.OpenEggbertGame;
import com.openeggbert.core.utils.OpenEggbertUtils;
import com.pixelgamelibrary.api.ScreenAdapter;
import java.util.List;
import java.util.Optional;
@ -36,12 +36,12 @@ import java.util.Optional;
*
* @author robertvokac
*/
public abstract class AbstractOpenEggbertScreen extends Screen {
public abstract class OpenEggbertScreen extends ScreenAdapter {
protected OpenEggbertGame game;
protected SpriteBatch batch;
public AbstractOpenEggbertScreen(OpenEggbertGame openEggbertGame) {
public OpenEggbertScreen(OpenEggbertGame openEggbertGame) {
this.game = openEggbertGame;
this.batch = openEggbertGame.getBatch();
loadBackgroundTextureIfNeeded();

View File

@ -32,7 +32,7 @@ import java.util.function.Function;
*
* @author robertvokac
*/
public class TestScreen extends AbstractOpenEggbertScreen {
public class TestScreen extends OpenEggbertScreen {
public TestScreen(OpenEggbertGame openEggbertGame) {
super(openEggbertGame);

View File

@ -22,7 +22,7 @@
<inherits name="com.github.czyzby.websocket.GdxWebSocket" />
<inherits name="com.github.czyzby.websocket.GdxWebSocketGwt" />
<inherits name="com.github.czyzby.websocket.GdxWebSocketSerialization" />
<inherits name="com.openeggbert.core.main.OpenEggbertGame" />
<inherits name="com.openeggbert.core.main.OpenEggbertApplication" />
<inherits name="com.pixelgamelibrary.pixelapi" />
<inherits name="com.pixelgamelibrary.backend.pixelbackendlibgdx" />
<inherits name="formic" />

View File

@ -6,8 +6,10 @@ import com.badlogic.gdx.backends.gwt.GwtApplicationConfiguration;
import com.openeggbert.core.configuration.ScreenResolution;
import com.openeggbert.core.main.OpenEggbertGame;
import com.badlogic.gdx.ai.GdxLogger;
import com.openeggbert.core.main.OpenEggbertApplication;
import com.pixelgamelibrary.api.Pixel;
import com.pixelgamelibrary.backend.libgdx.PixelBackendLibGDX;
import com.pixelgamelibrary.backend.libgdx.game.LibGdxGame;
/** Launches the GWT application. */
public class GwtLauncher extends GwtApplication {
@ -26,6 +28,6 @@ public class GwtLauncher extends GwtApplication {
@Override
public ApplicationListener createApplicationListener () {
return new OpenEggbertGame();
return new LibGdxGame(new OpenEggbertApplication().createGame());
}
}

View File

@ -23,11 +23,13 @@ package com.openeggbert.lwjgl3;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3Application;
import com.badlogic.gdx.backends.lwjgl3.Lwjgl3ApplicationConfiguration;
import com.openeggbert.core.configuration.ScreenResolution;
import com.openeggbert.core.main.OpenEggbertGame;
import com.openeggbert.core.main.OpenEggbertApplication;
import com.openeggbert.core.gamespace.GameSpace;
import com.pixelgamelibrary.api.Pixel;
import com.pixelgamelibrary.backend.libgdx.PixelBackendLibGDX;
import java.util.Optional;
import com.pixelgamelibrary.api.GameI;
import com.pixelgamelibrary.backend.libgdx.game.LibGdxGame;
/** Launches the desktop (LWJGL3) application. */
public class Lwjgl3Launcher {
@ -40,8 +42,17 @@ public class Lwjgl3Launcher {
private static Lwjgl3Application createApplication() {
Optional<GameSpace> gameSpace = DesktopUtils.tryToLoadGameSpace();
String currentDirectory = DesktopUtils.getPathOfDirectoryWhereJarIsRunning();
final OpenEggbertGame openEggbertGame = gameSpace.isPresent() ? new OpenEggbertGame(gameSpace.get(), currentDirectory) : new OpenEggbertGame(currentDirectory);
return new Lwjgl3Application(openEggbertGame, getDefaultConfiguration());
OpenEggbertApplication openEggbertApplication = new OpenEggbertApplication();
GameI game;
if (gameSpace.isPresent()) {
game = openEggbertApplication.createGame("gameSpace", gameSpace.get(), "currentDirectory", currentDirectory);
} else {
game = openEggbertApplication.createGame("currentDirectory", currentDirectory);
}
LibGdxGame libGdxGame = new LibGdxGame(game);
return new Lwjgl3Application(libGdxGame, getDefaultConfiguration());
}
private static Lwjgl3ApplicationConfiguration getDefaultConfiguration() {