From 321c7ba1e911962ca1929637554260a9a8565bae Mon Sep 17 00:00:00 2001 From: halotroop2288 Date: Tue, 4 Feb 2020 16:33:01 -0800 Subject: [PATCH] Implemented splashes Including a file reader utility --- .../halotroop/litecraft/LiteCraftMain.java | 70 ++++++++-------- .../halotroop/litecraft/TextFileReader.java | 27 +++++++ src/main/resources/text/splashes.txt | 80 +++++++++++++++++++ 3 files changed, 144 insertions(+), 33 deletions(-) create mode 100644 src/main/java/com/github/halotroop/litecraft/TextFileReader.java create mode 100644 src/main/resources/text/splashes.txt diff --git a/src/main/java/com/github/halotroop/litecraft/LiteCraftMain.java b/src/main/java/com/github/halotroop/litecraft/LiteCraftMain.java index a071a13..6fd8084 100644 --- a/src/main/java/com/github/halotroop/litecraft/LiteCraftMain.java +++ b/src/main/java/com/github/halotroop/litecraft/LiteCraftMain.java @@ -1,7 +1,13 @@ package com.github.halotroop.litecraft; +import org.apache.log4j.Logger; + +import java.io.IOException; +import java.util.Random; + import org.aeonbits.owner.ConfigFactory; import org.apache.commons.cli.*; +import org.apache.log4j.Level; import org.lwjgl.*; import org.lwjgl.glfw.*; import org.lwjgl.opengl.*; @@ -14,19 +20,23 @@ import com.github.halotroop.litecraft.render.Renderer; public class LiteCraftMain implements Runnable { + public Logger logger = Logger.getLogger(Logger.class.getName()); private static SettingsConfig config; - private static boolean limitFPS = false; public static int width = 640, height = 480, maxFPS = 60; // Don't change these values. They just initialize it in case we forget to set them later. - public static boolean spamLog, debug; + public static boolean spamLog = false, debug = false, limitFPS = false; public String splashText = ""; - - protected Timer timer; private int fps, ups, tps; private long frameTimer; private Renderer renderer; - private Window window; - + protected Timer timer; + protected TickListener tickListener = new TickListener() + { + @Override + public void onTick(float deltaTime) + { tps++; } + }; + public static void main(String[] args) throws Exception { try @@ -43,7 +53,6 @@ public class LiteCraftMain implements Runnable System.err.println("Config failed to load."); e.printStackTrace(); } - try { Options options = SettingsHandler.createCommandLineOptions(); @@ -56,21 +65,15 @@ public class LiteCraftMain implements Runnable limitFPS = Boolean.parseBoolean(cmd.getOptionValue("limit_fps", "false")); } catch (ParseException e) - { e.printStackTrace(); } - + { + e.printStackTrace(); + } new LiteCraftMain().run(); } - - protected TickListener tickListener = new TickListener() - { - @Override - public void onTick(float deltaTime) - { tps++; } - }; private void init() { - if (debug) System.out.println("Initializing game..."); + logger.setLevel(debug ? Level.ALL : Level.INFO); // Setup an error callback. The default implementation will print the error message in System.err. GLFWErrorCallback.createPrint(System.err).set(); // Initialize GLFW. Most GLFW functions will not work before doing this. @@ -80,21 +83,23 @@ public class LiteCraftMain implements Runnable timer = new Timer(20); timer.addTickListener(tickListener); GL.createCapabilities(); // This line is critical for LWJGL's interoperation with GLFW. - renderer = new Renderer(); - if (splashText == "") + renderer = new Renderer(); + try { - splashText = "INSERT SPLASH TEXT HERE!"; + String[] splashes = TextFileReader.readFileToStringArray("text/splashes.txt"); + splashText = splashes[new Random().nextInt(splashes.length)]; } - window.setWindowTitle("LiteCraft - " + splashText); + catch (IOException e) + { e.printStackTrace(); } + window.setWindowTitle("LiteCraft - " + ((splashText == "" || splashText == null) ? "INSERT SPLASH TEXT HERE!" : splashText)); input(); - if (debug) System.out.println("Initialization complete."); } // Sets up the key inputs for the game (currently just esc for closing the game) public void input() { // A temporary key callback. It will tell GLFW to close the window whenever we press escape - GLFW.glfwSetKeyCallback(window.getWindowLong(), (window, key, scancode, action, mods) -> + GLFW.glfwSetKeyCallback(window.getWindowId(), (window, key, scancode, action, mods) -> { if (key == GLFW.GLFW_KEY_ESCAPE && action == GLFW.GLFW_RELEASE) GLFW.glfwSetWindowShouldClose(window, true); // We will detect this in the game loop @@ -111,7 +116,6 @@ public class LiteCraftMain implements Runnable if (fps < maxFPS || !limitFPS) render(); if (System.currentTimeMillis() > frameTimer + 1000) // wait for one second { - if (debug) window.setWindowTitle("LiteCraft | FPS: " + fps + " | TPS: " + tps + " | UPS: " + ups); fps = 0; ups = 0; tps = 0; @@ -121,6 +125,7 @@ public class LiteCraftMain implements Runnable public void render() { + if (debug) window.setWindowTitle("LiteCraft | FPS: " + fps + " | TPS: " + tps + " | UPS: " + ups); renderer.render(); window.render(); fps++; // After a successful frame render, increase the frame counter. @@ -128,26 +133,25 @@ public class LiteCraftMain implements Runnable public void run() { - System.out.println("Starting game..."); - System.out.println("LWJGL version: " + Version.getVersion()); - System.out.println("Resolution: " + width + 'x' + height); + System.out.println("Starting game..." + "\n" + "LWJGL version: " + Version.getVersion() + "\n" + "Resolution: " + width + 'x' + height); init(); frameTimer = System.currentTimeMillis(); // Run the rendering loop until the player has attempted to close the window - while (!GLFW.glfwWindowShouldClose(window.getWindowLong())) + while (!GLFW.glfwWindowShouldClose(window.getWindowId())) { loop(); } - destroy(); + shutDown(); } - + // Shuts down the game and destroys all the things that are using RAM (so the user doesn't have to restart their computer afterward...) - private void destroy() + private void shutDown() { - if (debug) System.out.println("Closing game..."); + logger.log(Level.DEBUG, "Closing game..."); renderer.cleanUp(); window.destroy(); // Terminate GLFW and free the error callback GLFW.glfwTerminate(); GLFW.glfwSetErrorCallback(null).free(); - if (debug) System.out.println("Game closed successfully."); + System.out.println("Game closed successfully."); + System.exit(0); } } diff --git a/src/main/java/com/github/halotroop/litecraft/TextFileReader.java b/src/main/java/com/github/halotroop/litecraft/TextFileReader.java new file mode 100644 index 0000000..0fc97b9 --- /dev/null +++ b/src/main/java/com/github/halotroop/litecraft/TextFileReader.java @@ -0,0 +1,27 @@ +package com.github.halotroop.litecraft; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +public class TextFileReader +{ + public static String[] readFileToStringArray(String filename) throws IOException + { + ClassLoader.getSystemClassLoader(); + InputStream inputStream = ClassLoader.getSystemResourceAsStream(filename); + InputStreamReader streamReader = new InputStreamReader(inputStream, "UTF-8"); + BufferedReader in = new BufferedReader(streamReader); + + String[] output = new String[] {}; + for (String line; (line = in.readLine()) != null;) + { + String[] bufferArray = new String[output.length + 1]; + System.arraycopy(output, 0, bufferArray, 0, output.length); + bufferArray[output.length] = line; + output = bufferArray; + } + return output; + } +} diff --git a/src/main/resources/text/splashes.txt b/src/main/resources/text/splashes.txt new file mode 100644 index 0000000..108c666 --- /dev/null +++ b/src/main/resources/text/splashes.txt @@ -0,0 +1,80 @@ +& Knuckles +0% Dairy +0% crackers +2+2=10 in base-4! +4bitsinabyte! +99 +Absolutely no mixins! +Also try 7 Days to Die! +Also try CastleMiner Z! +Also try Miner Dig Deep... oh wait +Also try Terraria! +Also try Total Miner Forge! +Alt-F4! +Are dimensions even real? +Are you feeling lucky punk? +Battle Royale! +Better without Kinect sensor! +CM != MC +California thinks it causes cancer! +Can't pirate what's free! +Conservatives not allowed! +Could not connect to PSN! +Ctrl-Alt-Delete! +Deluxe Edition +Don't forget to vote! +Don't punch trees! +Don't stay in school. +Drink! Drink! Drink! +Easy-to-use! +Episode 3 +Featuring Dante from the Devil May Cry Series! +Forget the coremods! +Fully 2D! +Go the fuck to sleep. +Happy birthday! +HeartGold +Horse Armour at no extra cost! +Howling at the moon! +I can't believe it's not Minecraft! +I mean for fucks sake, it's [current year]! +Innuendo abound! +Jam it up your pee hole! +Java Edition +Keep playing, or else! +LOL SO RANDOM! +Many of a kind! +Mega-dick: ACTIVATE! +Absolutely no mods! +Modular! +New Funky Mode! +Not available on Xbox! +Not for kids! +Not rated by PEGI +Not rated by the ESRB +Not safe for work! +Now or never! +On Linux AND PC! +Open-source! +Please try again! +Pull request! +Slap my nuts and call me grandma! +SoulSilver +Splashy fun! +Starfish really loves you! +Start-up complete! +The first of its kind! +They took my freaking kidney! +This is the end, friend. +Too many splashes? +Try turning it off and back on again! +U +Use at your own risk! +Vegan-friendly! +WTFPL +Watch out for dragons! +We don't need no education. +What was that? +Who's been drawing dicks? +With extra dip! +and a large soda. \ No newline at end of file