Working on the Pixel Game Library III

This commit is contained in:
Robert Vokac 2024-09-03 18:50:32 +02:00
parent e6051bbc01
commit 5f16f8b6f7
No known key found for this signature in database
GPG Key ID: C459E1E4B4A986BB
10 changed files with 91 additions and 30 deletions

View File

@ -63,7 +63,7 @@ public enum OpenEggbertDisplayMode {
}
public static OpenEggbertDisplayMode setDisplayMode(OpenEggbertDisplayMode displayMode) {
String result = Pixel.get().graphics().setDisplayMode(displayMode == FULLSCREEN, displayMode == WINDOW);
String result = Pixel.graphics().setDisplayMode(displayMode == FULLSCREEN, displayMode == WINDOW);
return result == null ? null : OpenEggbertDisplayMode.valueOf(result);
}

View File

@ -34,7 +34,7 @@ import lombok.Data;
public class Mod {
public Mod(String xml) {
XmlElement root = Pixel.get().utils().parseXml(xml);
XmlElement root = Pixel.utils().parseXml(xml);
XmlElement parentElement = root.getChildByName("parent");
if (parentElement != null) {
parent = new ModIdentification(parentElement);

View File

@ -22,6 +22,7 @@ package com.openeggbert.core.utils;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.pixelgamelibrary.Pixel;
import com.pixelgamelibrary.Platform;
/**
*
@ -35,7 +36,7 @@ public class EmbeddedFileHandleFactory {
public static FileHandle create(String name) {
if (Pixel.get().getPlatform().isAndroid() || Pixel.get().getPlatform().isWeb()) {
if (Pixel.app().isOneOfPlatforms(Platform.ANDROID, Platform.WEB)) {
return Gdx.files.internal(name);
} else {
return Gdx.files.classpath(name);

View File

@ -19,7 +19,15 @@
///////////////////////////////////////////////////////////////////////////////////////////////
package com.pixelgamelibrary;
import com.pixelgamelibrary.api.AppI;
import com.pixelgamelibrary.api.AssetI;
import com.pixelgamelibrary.api.AudioI;
import com.pixelgamelibrary.api.GraphicsI;
import com.pixelgamelibrary.api.InputI;
import com.pixelgamelibrary.api.NetI;
import com.pixelgamelibrary.api.PixelBackend;
import com.pixelgamelibrary.api.StorageI;
import com.pixelgamelibrary.api.UtilsI;
/**
*
@ -33,10 +41,44 @@ public class Pixel {
//Not meant to be instantiated.
}
public static PixelBackend get() {
private static PixelBackend get() {
return getBackend();
}
////
public static AppI app() {
return get().app();
}
public static GraphicsI graphics() {
return get().graphics();
}
public static AudioI audio() {
return get().audio();
}
public static InputI input() {
return get().input();
}
public static NetI net() {
return get().net();
}
public static AssetI asset() {
return get().asset();
}
public static StorageI storage() {
return get().storage();
}
public static UtilsI utils() {
return get().utils();
}
////
public static void setBackend(PixelBackend pixelBackend) {
if (isBackendSet()) {
throw new PixelException("Pixel Backend was already set");
@ -44,7 +86,7 @@ public class Pixel {
INSTANCE = pixelBackend;
}
public static PixelBackend getBackend() {
private static PixelBackend getBackend() {
if (!isBackendSet()) {
throw new PixelException("Pixel Backend was not set");
}

View File

@ -19,11 +19,24 @@
///////////////////////////////////////////////////////////////////////////////////////////////
package com.pixelgamelibrary.api;
import com.pixelgamelibrary.Platform;
/**
*
* @author robertvokac
*/
public interface AppI {
Platform getPlatform();
default boolean isOneOfPlatforms(Platform ... platforms) {
for(Platform p: platforms) {
if(getPlatform() == p) {
return true;
}
}
return false;
}
void exit();
void log(String msg);
void error(String msg);
void debug(String msg);

View File

@ -28,7 +28,7 @@ import com.pixelgamelibrary.Platform;
public interface PixelBackend {
Platform getPlatform();
AppI app();
GraphicsI graphics();
AudioI audio();
@ -37,6 +37,5 @@ public interface PixelBackend {
AssetI asset();
StorageI storage();
UtilsI utils();
void exit();
}

View File

@ -20,7 +20,12 @@
package com.pixelgamelibrary.backends.libgdx;
import com.badlogic.gdx.Application;
import static com.badlogic.gdx.Application.ApplicationType.Android;
import static com.badlogic.gdx.Application.ApplicationType.Desktop;
import static com.badlogic.gdx.Application.ApplicationType.WebGL;
import com.badlogic.gdx.Gdx;
import com.pixelgamelibrary.PixelException;
import com.pixelgamelibrary.Platform;
import com.pixelgamelibrary.api.AppI;
/**
@ -29,6 +34,26 @@ import com.pixelgamelibrary.api.AppI;
*/
public class AppLibGDXImpl implements AppI {
@Override
public void exit() {
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
@Override
public Platform getPlatform() {
Application.ApplicationType applicationType = Gdx.app.getType();
switch (applicationType) {
case Desktop:
return Platform.DESKTOP;
case Android:
return Platform.ANDROID;
case WebGL:
return Platform.WEB;
default:
throw new PixelException("Unsupported platform: " + applicationType);
}
}
@Override
public void log(String msg) {
Application app = Gdx.app;

View File

@ -49,25 +49,6 @@ public class PixelLibGDXBackend implements PixelBackend {
private StorageI pixelStorageLibGdxImpl = null;
private UtilsI pixelUtilsLibGdxImpl = null;
@Override
public void exit() {
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
@Override
public Platform getPlatform() {
Application.ApplicationType applicationType = Gdx.app.getType();
switch (applicationType) {
case Desktop:
return Platform.DESKTOP;
case Android:
return Platform.ANDROID;
case WebGL:
return Platform.WEB;
default:
throw new PixelException("Unsupported platform: " + applicationType);
}
}
@Override
public GraphicsI graphics() {

View File

@ -37,7 +37,7 @@ public class StorageFactory {
private static Storage storage = null;
public static Storage getStorage() {
final Platform platform = Pixel.get().getPlatform();
final Platform platform = Pixel.app().getPlatform();
if (storage == null) {
storage = new MemoryStorage();
}

View File

@ -281,7 +281,7 @@ public class MapStorage implements Storage {
return null;
}
text = text.substring(BINARYFILE.length());
return Pixel.get().utils().decodeBase64AsByteArray(text);
return Pixel.utils().decodeBase64AsByteArray(text);
}
private static final String BINARYFILE = "BINARYFILE";
@ -292,7 +292,7 @@ public class MapStorage implements Storage {
@Override
public String savebin(String name, byte[] data) {
return savetext(name, BINARYFILE + Pixel.get().utils().encodeToBase64(data));
return savetext(name, BINARYFILE + Pixel.utils().encodeToBase64(data));
}
@Override
@ -338,7 +338,7 @@ public class MapStorage implements Storage {
}
private void logError(String msg) {
Pixel.get().app().error(msg);
Pixel.app().error(msg);
}
}