moved things a lot just so that splash screen shows up WHILE things load

This commit is contained in:
Minjae Song
2018-12-25 01:38:23 +09:00
parent 3546213f34
commit 46284be77d
29 changed files with 434 additions and 400 deletions

View File

@@ -10,10 +10,17 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.FrameBuffer;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import net.torvald.dataclass.ArrayListMap;
import net.torvald.terrarum.utils.JsonFetcher;
import net.torvald.terrarum.utils.JsonWriter;
import net.torvald.terrarumsansbitmap.gdx.GameFontBase;
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Random;
@@ -30,10 +37,11 @@ public class AppLoader implements ApplicationListener {
* AA: Major version
* BB: Minor version
* XXXX: Revision (Repository commits, or something arbitrary)
*
* <p>
* e.g. 0x02010034 will be translated as 2.1.52
*/
public static final int VERSION_RAW = 0x00_02_04B1;
public static final String getVERSION_STRING() {
return String.format("%d.%d.%d", VERSION_RAW >>> 24, (VERSION_RAW & 0xff0000) >>> 16, VERSION_RAW & 0xFFFF);
}
@@ -56,7 +64,8 @@ public class AppLoader implements ApplicationListener {
/**
* Initialise the application with the alternative Screen you choose
* @param appConfig LWJGL(2) Application Configuration
*
* @param appConfig LWJGL(2) Application Configuration
* @param injectScreen GDX Screen you want to run
*/
public AppLoader(LwjglApplicationConfiguration appConfig, Screen injectScreen) {
@@ -66,6 +75,7 @@ public class AppLoader implements ApplicationListener {
/**
* Initialise the application with default game screen
*
* @param appConfig LWJGL(2) Application Configuration
*/
public AppLoader(LwjglApplicationConfiguration appConfig) {
@@ -80,6 +90,7 @@ public class AppLoader implements ApplicationListener {
/**
* Singleton pattern implementation in Java.
*
* @return
*/
public static AppLoader getINSTANCE() {
@@ -123,8 +134,10 @@ public class AppLoader implements ApplicationListener {
}
}
public static LwjglApplicationConfiguration appConfig;
private static boolean splashDisplayed = false;
private static boolean postInitFired = false;
public static LwjglApplicationConfiguration appConfig;
public static GameFontBase fontGame;
/**
@@ -133,6 +146,9 @@ public class AppLoader implements ApplicationListener {
public static int GLOBAL_RENDER_TIMER = new Random().nextInt(1020) + 1;
public static ArrayListMap debugTimers = new ArrayListMap<String, Long>();
public static void main(String[] args) {
ShaderProgram.pedantic = false;
@@ -165,6 +181,8 @@ public class AppLoader implements ApplicationListener {
private Color gradWhiteBottom = new Color(0xd8d8d8ff);
public Screen screen;
public static int screenW = 0;
public static int screenH = 0;
public static Texture textureWhiteSquare;
@@ -189,20 +207,20 @@ public class AppLoader implements ApplicationListener {
logoBatch = new SpriteBatch();
camera = new OrthographicCamera(((float) appConfig.width), ((float) appConfig.height));
initViewPort(appConfig.width, appConfig.height);
logo = new TextureRegion(new Texture(Gdx.files.internal("assets/graphics/logo_placeholder.tga")));
logo.flip(false, true);
textureWhiteSquare = new Texture(Gdx.files.internal("assets/graphics/ortho_line_tex_2px.tga"));
textureWhiteSquare.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest);
shaderBayerSkyboxFill = new ShaderProgram(Gdx.files.internal("assets/4096.vert"), Gdx.files.internal("assets/4096_bayer_skyboxfill.frag"));
shaderHicolour = new ShaderProgram(Gdx.files.internal("assets/4096.vert"), Gdx.files.internal("assets/hicolour.frag"));
shaderColLUT = new ShaderProgram(Gdx.files.internal("assets/4096.vert"), Gdx.files.internal("assets/passthru.frag"));
shaderBayerSkyboxFill = new ShaderProgram(Gdx.files.internal("assets/4096.vert"),
Gdx.files.internal("assets/4096_bayer_skyboxfill.frag")
);
shaderHicolour = new ShaderProgram(Gdx.files.internal("assets/4096.vert"),
Gdx.files.internal("assets/hicolour.frag")
);
shaderColLUT = new ShaderProgram(Gdx.files.internal("assets/4096.vert"),
Gdx.files.internal("assets/passthru.frag")
);
fullscreenQuad = new Mesh(
true, 4, 6,
@@ -212,31 +230,29 @@ public class AppLoader implements ApplicationListener {
);
fullscreenQuad.setVertices(new float[]{
0f, 0f, 0f, 1f, 1f, 1f, 1f, 0f, 1f,
((float) appConfig.width), 0f, 0f, 1f, 1f, 1f, 1f, 1f, 1f,
((float) appConfig.width), ((float) appConfig.height), 0f, 1f, 1f, 1f, 1f, 1f, 0f,
0f, ((float) appConfig.height), 0f, 1f, 1f, 1f, 1f, 0f, 0f
0f, 0f, 0f, 1f, 1f, 1f, 1f, 0f, 1f,
((float) appConfig.width), 0f, 0f, 1f, 1f, 1f, 1f, 1f, 1f,
((float) appConfig.width), ((float) appConfig.height), 0f, 1f, 1f, 1f, 1f, 1f, 0f,
0f, ((float) appConfig.height), 0f, 1f, 1f, 1f, 1f, 0f, 0f
});
fullscreenQuad.setIndices(new short[]{0, 1, 2, 2, 3, 0});
logo = new TextureRegion(new Texture(Gdx.files.internal("assets/graphics/logo_placeholder.tga")));
logo.flip(false, true);
TextureRegionPack.Companion.setGlobalFlipY(true);
fontGame = new GameFontBase("assets/graphics/fonts/terrarum-sans-bitmap", false, true, Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest, false, 128, false);
// if there is a predefined screen, open that screen after my init process
if (injectScreen != null) {
setScreen(injectScreen);
}
// load configs
getDefaultDirectory();
createDirs();
readConfigJson();
}
@Override
public void render() {
if (splashDisplayed && !postInitFired) {
postInit();
postInitFired = true;
}
FrameBufferManager.begin(renderFBO);
Gdx.gl.glClearColor(.094f, .094f, .094f, 0f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
@@ -250,7 +266,9 @@ public class AppLoader implements ApplicationListener {
FrameBufferManager.begin(renderFBO);
setCameraPosition(0, 0);
// if there's no predefined screen, default to the actual game's screen, and set the screen as the one
// draw splash screen when predefined screen is null
// because in normal operation, the only time screen == null is when the app is cold-launched
// you can't have a text drawn here :v
if (screen == null) {
shaderBayerSkyboxFill.begin();
shaderBayerSkyboxFill.setUniformMatrix("u_projTrans", camera.combined);
@@ -276,11 +294,10 @@ public class AppLoader implements ApplicationListener {
loadTimer += Gdx.graphics.getRawDeltaTime();
if (loadTimer >= showupTime) {
Terrarum.INSTANCE.setScreenW(appConfig.width);
Terrarum.INSTANCE.setScreenH(appConfig.height);
setScreen(Terrarum.INSTANCE);
}
}
// draw the screen
else {
screen.render(Gdx.graphics.getDeltaTime());
}
@@ -291,7 +308,7 @@ public class AppLoader implements ApplicationListener {
PostProcessor.INSTANCE.draw(camera.combined, renderFBO);
splashDisplayed = true;
GLOBAL_RENDER_TIMER += 1;
}
@@ -299,30 +316,35 @@ public class AppLoader implements ApplicationListener {
public void resize(int width, int height) {
//initViewPort(width, height);
Terrarum.INSTANCE.resize(width, height);
if (screen != null) screen.resize(Terrarum.INSTANCE.getWIDTH(), Terrarum.INSTANCE.getHEIGHT());
screenW = width;
screenH = height;
if (screenW % 2 == 1) screenW -= 1;
if (screenH % 2 == 1) screenH -= 1;
if (screen != null) screen.resize(screenW, screenH);
if (renderFBO == null ||
(renderFBO.getWidth() != Terrarum.INSTANCE.getWIDTH() ||
renderFBO.getHeight() != Terrarum.INSTANCE.getHEIGHT())
) {
(renderFBO.getWidth() != screenW ||
renderFBO.getHeight() != screenH)
) {
renderFBO = new FrameBuffer(
Pixmap.Format.RGBA8888,
Terrarum.INSTANCE.getWIDTH(),
Terrarum.INSTANCE.getHEIGHT(),
screenW,
screenH,
false
);
}
appConfig.width = Terrarum.INSTANCE.getWIDTH();
appConfig.height = Terrarum.INSTANCE.getHEIGHT();
appConfig.width = screenW;
appConfig.height = screenH;
printdbg(this, "Resize event");
}
@Override
public void dispose () {
public void dispose() {
if (screen != null) screen.hide();
System.out.println("Goodbye !");
@@ -332,12 +354,12 @@ public class AppLoader implements ApplicationListener {
}
@Override
public void pause () {
public void pause() {
if (screen != null) screen.pause();
}
@Override
public void resume () {
public void resume() {
if (screen != null) screen.resume();
}
@@ -354,6 +376,23 @@ public class AppLoader implements ApplicationListener {
printdbg(this, "Screen transisiton complete: " + this.screen.getClass().getCanonicalName());
}
private void postInit() {
textureWhiteSquare = new Texture(Gdx.files.internal("assets/graphics/ortho_line_tex_2px.tga"));
textureWhiteSquare.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest);
TextureRegionPack.Companion.setGlobalFlipY(true);
fontGame = new GameFontBase("assets/graphics/fonts/terrarum-sans-bitmap", false, true,
Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest, false, 128, false
);
// if there is a predefined screen, open that screen after my init process
if (injectScreen != null) {
setScreen(injectScreen);
}
}
private void setCameraPosition(float newX, float newY) {
camera.position.set((-newX + appConfig.width / 2), (-newY + appConfig.height / 2), 0f);
camera.update();
@@ -370,15 +409,226 @@ public class AppLoader implements ApplicationListener {
fullscreenQuad.setIndices(new short[]{0, 1, 2, 2, 3, 0});
}
// DEFAULT DIRECTORIES //
public static String OSName = System.getProperty("os.name");
public static String OSVersion = System.getProperty("os.version");
public static String operationSystem;
public static String defaultDir;
public static String defaultSaveDir;
public static String configDir;
public static RunningEnvironment environment;
private void getDefaultDirectory() {
String OS = OSName.toUpperCase();
if (OS.contains("WIN")) {
operationSystem = "WINDOWS";
defaultDir = System.getenv("APPDATA") + "/Terrarum";
}
else if (OS.contains("OS X")) {
operationSystem = "OSX";
defaultDir = System.getProperty("user.home") + "/Library/Application Support/Terrarum";
}
else if (OS.contains("NUX") || OS.contains("NIX") || OS.contains("BSD")) {
operationSystem = "LINUX";
defaultDir = System.getProperty("user.home") + "/.Terrarum";
}
else if (OS.contains("SUNOS")) {
operationSystem = "SOLARIS";
defaultDir = System.getProperty("user.home") + "/.Terrarum";
}
else if (System.getProperty("java.runtime.name").toUpperCase().contains("ANDROID")) {
operationSystem = "ANDROID";
defaultDir = System.getProperty("user.home") + "/.Terrarum";
environment = RunningEnvironment.MOBILE;
}
else {
operationSystem = "UNKNOWN";
defaultDir = System.getProperty("user.home") + "/.Terrarum";
}
defaultSaveDir = defaultDir + "/Saves";
configDir = defaultDir + "/config.json";
System.out.println("os.name = $OSName (with identifier $operationSystem)");
System.out.println("os.version = $OSVersion");
System.out.println("default directory: $defaultDir");
}
private void createDirs() {
File[] dirs = {new File(defaultSaveDir)};
for (File it : dirs) {
if (!it.exists())
it.mkdirs();
}
//dirs.forEach { if (!it.exists()) it.mkdirs() }
}
// CONFIG //
private static KVHashMap gameConfig = new KVHashMap();
private static void createConfigJson() throws IOException {
File configFile = new File(configDir);
if (!configFile.exists() || configFile.length() == 0L) {
JsonWriter.INSTANCE.writeToFile(DefaultConfig.INSTANCE.fetch(), configDir);
}
}
/**
*
* @return true on successful, false on failure.
*/
private static Boolean readConfigJson() {
try {
// read from disk and build config from it
JsonObject jsonObject = JsonFetcher.INSTANCE.invoke(configDir);
// make config
jsonObject.entrySet().forEach((entry) ->
gameConfig.set(entry.getKey(), entry.getValue())
);
return true;
}
catch (IOException e) {
// write default config to game dir. Call this method again to read config from it.
try {
createConfigJson();
}
catch (IOException e1) {
System.out.println("[AppLoader] Unable to write config.json file");
e.printStackTrace();
}
return false;
}
}
/**
* Return config from config set. If the config does not exist, default value will be returned.
* @param key
* *
* @return Config from config set or default config if it does not exist.
* *
* @throws NullPointerException if the specified config simply does not exist.
*/
public static int getConfigInt(String key) {
Object cfg = getConfigMaster(key);
if (cfg instanceof JsonPrimitive)
return ((JsonPrimitive) cfg).getAsInt();
else
return Integer.parseInt(((String) cfg));
}
/**
* Return config from config set. If the config does not exist, default value will be returned.
* @param key
* *
* @return Config from config set or default config if it does not exist.
* *
* @throws NullPointerException if the specified config simply does not exist.
*/
public static String getConfigString(String key) {
Object cfg = getConfigMaster(key);
if (cfg instanceof JsonPrimitive)
return ((JsonPrimitive) cfg).getAsString();
else
return ((String) cfg);
}
/**
* Return config from config set. If the config does not exist, default value will be returned.
* @param key
* *
* @return Config from config set or default config if it does not exist.
* *
* @throws NullPointerException if the specified config simply does not exist.
*/
public static boolean getConfigBoolean(String key) {
Object cfg = getConfigMaster(key);
if (cfg instanceof JsonPrimitive)
return ((JsonPrimitive) cfg).getAsBoolean();
else
return ((boolean) cfg);
}
public static int[] getConfigIntArray(String key) {
Object cfg = getConfigMaster(key);
if (cfg instanceof JsonArray) {
JsonArray jsonArray = ((JsonArray) cfg).getAsJsonArray();
//return IntArray(jsonArray.size(), { i -> jsonArray[i].asInt })
int[] intArray = new int[jsonArray.size()];
for (int i = 0; i < jsonArray.size(); i++) {
intArray[i] = jsonArray.get(i).getAsInt();
}
return intArray;
}
else
return ((int[]) cfg);
}
/**
* Get config from config file. If the entry does not exist, get from defaults; if the entry is not in the default, NullPointerException will be thrown
*/
private static JsonObject getDefaultConfig() {
return DefaultConfig.INSTANCE.fetch();
}
private static Object getConfigMaster(String key1) {
String key = key1.toLowerCase();
Object config;
try {
config = gameConfig.get(key);
}
catch (NullPointerException e) {
config = null;
}
Object defaults;
try {
defaults = getDefaultConfig().get(key);
}
catch (NullPointerException e) {
defaults = null;
}
if (config == null) {
if (defaults == null) {
throw new NullPointerException("key not found: '$key'");
}
else {
return defaults;
}
}
else {
return config;
}
}
public static void setConfig(String key, Object value) {
gameConfig.set(key, value);
}
// //
public static final void printdbg(Object obj, Object message) {
if (IS_DEVELOPMENT_BUILD) {
System.out.println("["+obj.getClass().getSimpleName()+"] "+message.toString());
System.out.println("[" + obj.getClass().getSimpleName() + "] " + message.toString());
}
}
public static final void printdbgerr(Object obj, Object message) {
if (IS_DEVELOPMENT_BUILD) {
System.err.println("["+obj.getClass().getSimpleName()+"] "+message.toString());
System.err.println("[" + obj.getClass().getSimpleName() + "] " + message.toString());
}
}
}
}