diff --git a/src/main/java/minicraft/core/Game.java b/src/main/java/minicraft/core/Game.java index b98beed2a..090087564 100644 --- a/src/main/java/minicraft/core/Game.java +++ b/src/main/java/minicraft/core/Game.java @@ -119,15 +119,14 @@ public static void main(String[] args) { player.eid = 0; new Load(true); // This loads any saved preferences. - // Load the selected language. - Localization.loadLanguage(); + Localization.loadLanguage(); Initializer.createAndDisplayFrame(); + + setDisplay(new TitleDisplay()); // Sets menu to the title screen. Renderer.initScreen(); - setDisplay(new TitleDisplay()); // Sets menu to the title screen. - // Loads the resorce pack locaded in save. new ResourcePackDisplay().initResourcePack(); diff --git a/src/main/java/minicraft/core/Renderer.java b/src/main/java/minicraft/core/Renderer.java index 5f71937f1..22fd7ef54 100644 --- a/src/main/java/minicraft/core/Renderer.java +++ b/src/main/java/minicraft/core/Renderer.java @@ -10,6 +10,7 @@ import java.util.Map; import java.util.Objects; +import minicraft.core.io.Settings; import minicraft.entity.furniture.Bed; import minicraft.entity.mob.Player; import minicraft.gfx.Color; @@ -34,8 +35,9 @@ public class Renderer extends Game { private Renderer() {} - public static final int HEIGHT = 192; - public static final int WIDTH = 288; + public static final int MAX_SIZE = 288; + public static int HEIGHT; + public static int WIDTH; static float SCALE = 3; public static Screen screen; // Creates the main screen @@ -76,7 +78,31 @@ public static SpriteSheet[] loadDefaultSpriteSheets() { return new SpriteSheet[] { itemSheet, tileSheet, entitySheet, guiSheet, skinsSheet }; } - static void initScreen() { + public static void setAspectRatio() { + int width; + int height; + + switch ((String) Settings.get("aspectratio")) { + case "4x3": + width = 4; + height = 3; + break; + case "16x9": + width = 16; + height = 9; + break; + default: + width = 16; + height = 9; + break; + } + + double s = Math.min((double) Renderer.MAX_SIZE/width, (double) Renderer.MAX_SIZE/height); + Renderer.HEIGHT = (int) (height * s); + Renderer.WIDTH = (int) (width * s); + } + + public static void initScreen() { SpriteSheet[] sheets = loadDefaultSpriteSheets(); screen = new Screen(sheets[0], sheets[1], sheets[2], sheets[3], sheets[4]); lightScreen = new Screen(sheets[0], sheets[1], sheets[2], sheets[3], sheets[4]); diff --git a/src/main/java/minicraft/core/io/Settings.java b/src/main/java/minicraft/core/io/Settings.java index 1837cb323..37b6ee4d2 100644 --- a/src/main/java/minicraft/core/io/Settings.java +++ b/src/main/java/minicraft/core/io/Settings.java @@ -52,6 +52,8 @@ public class Settings { options.put("textures", new ArrayEntry<>("textures", "Original", "Custom")); options.get("textures").setSelection(0); + + options.put("aspectratio", new ArrayEntry<>("Aspect Ratio", "4x3", "16x9")); } // Returns the value of the specified option diff --git a/src/main/java/minicraft/saveload/Load.java b/src/main/java/minicraft/saveload/Load.java index 2efe4165a..633230c74 100644 --- a/src/main/java/minicraft/saveload/Load.java +++ b/src/main/java/minicraft/saveload/Load.java @@ -19,6 +19,7 @@ import minicraft.core.Game; import minicraft.core.Network; +import minicraft.core.Renderer; import minicraft.core.Updater; import minicraft.core.World; import minicraft.core.io.Localization; @@ -141,7 +142,6 @@ public Load(boolean loadConfig) { Logger.warn("No preferences found, creating new file."); resave = true; } - // Load unlocks. (new version) File testFileOld = new File(location + "unlocks" + extension); File testFile = new File(location + "Unlocks" + extension); @@ -377,6 +377,8 @@ private void loadPrefs(String filename) { Settings.set("sound", json.getBoolean("sound")); Settings.set("autosave", json.getBoolean("autosave")); Settings.set("diff", json.has("diff") ? json.getString("diff") : "Normal"); + Settings.set("aspectratio", json.has("aspectratio") ? json.getString("aspectratio") : "4x3"); + Renderer.setAspectRatio(); // Sets the aspect ratio of the game window. Settings.set("fps", json.getInt("fps")); if (prefVer.compareTo(new Version("2.1.0-dev1")) > 0) { diff --git a/src/main/java/minicraft/saveload/Save.java b/src/main/java/minicraft/saveload/Save.java index ccb84ae3f..163b693ce 100644 --- a/src/main/java/minicraft/saveload/Save.java +++ b/src/main/java/minicraft/saveload/Save.java @@ -177,6 +177,7 @@ private void writePrefs() { json.put("sound", String.valueOf(Settings.get("sound"))); json.put("autosave", String.valueOf(Settings.get("autosave"))); json.put("fps", String.valueOf(Settings.get("fps"))); + json.put("aspectratio", String.valueOf(Settings.get("aspectratio"))); json.put("lang", Localization.getSelectedLocale().toLanguageTag()); json.put("skinIdx", String.valueOf(SkinDisplay.getSelectedSkinIndex())); json.put("savedIP", MultiplayerDisplay.savedIP); diff --git a/src/main/java/minicraft/screen/OptionsMainMenuDisplay.java b/src/main/java/minicraft/screen/OptionsMainMenuDisplay.java index 99e858d14..0be77da4f 100644 --- a/src/main/java/minicraft/screen/OptionsMainMenuDisplay.java +++ b/src/main/java/minicraft/screen/OptionsMainMenuDisplay.java @@ -1,23 +1,75 @@ package minicraft.screen; import minicraft.core.Game; +import minicraft.core.io.InputHandler; import minicraft.core.io.Localization; import minicraft.core.io.Settings; +import minicraft.gfx.Color; +import minicraft.gfx.Font; +import minicraft.gfx.Screen; import minicraft.saveload.Save; import minicraft.screen.entry.SelectEntry; +import minicraft.screen.entry.StringEntry; public class OptionsMainMenuDisplay extends Display { + String originalAspectRatio = (String) Settings.get("aspectratio"); + public OptionsMainMenuDisplay() { - super(true, new Menu.Builder(false, 6, RelPos.LEFT, + super(true); + + Menu optionsMenu = new Menu.Builder(false, 6, RelPos.LEFT, Settings.getEntry("fps"), Settings.getEntry("sound"), new SelectEntry("Change Key Bindings", () -> Game.setDisplay(new KeyInputDisplay())), Settings.getEntry("language"), + Settings.getEntry("aspectratio"), new SelectEntry("Resource packs", () -> Game.setDisplay(new ResourcePackDisplay()))) .setTitle("Main Menu Options") - .createMenu() - ); + .createMenu(); + + Menu popupMenu = new Menu.Builder(true, 4, RelPos.CENTER) + .setShouldRender(false) + .setSelectable(false) + .setEntries(StringEntry.useLines(Color.RED, "A restart is required", "enter to confirm", "escape to cancel")) + .setTitle("Confirm Action") + .createMenu(); + + menus = new Menu[]{ + optionsMenu, + popupMenu + }; + } + + @Override + public void render(Screen screen) { + super.render(screen); + + // Forcefully render the popup menu above everything else + if(menus[1].shouldRender) { + menus[1].render(screen); + } + } + + @Override + public void tick(InputHandler input) { + if (menus[1].shouldRender) { + if (input.getKey("enter").clicked) { + menus[1].shouldRender = false; + Game.exitDisplay(); + } else if (input.getKey("exit").clicked) { + menus[1].shouldRender = false; + } + return; + } + + // If exit key is pressed, then display the popup menu if changes requiring a restart have been made + if (input.getKey("exit").clicked && originalAspectRatio != (String) Settings.get("aspectratio")) { + menus[1].shouldRender = true; + return; + } + + super.tick(input); } @Override diff --git a/src/main/java/minicraft/screen/SkinDisplay.java b/src/main/java/minicraft/screen/SkinDisplay.java index 9b0c1eb5f..0bcf45c8f 100644 --- a/src/main/java/minicraft/screen/SkinDisplay.java +++ b/src/main/java/minicraft/screen/SkinDisplay.java @@ -199,7 +199,7 @@ public void render(Screen screen) { int h = 2; int w = 2; int xOffset = Screen.w / 2 - w * 4; // Put this in the center of the screen - int yOffset = 38; + int yOffset = 40; int spriteIndex = (step / 40) % 8; // 9 = 8 Frames for sprite @@ -244,4 +244,4 @@ public static MobSprite[][][] getSkinAsMobSprite() { return mobSprites; } -} +} \ No newline at end of file diff --git a/src/main/java/minicraft/screen/TitleDisplay.java b/src/main/java/minicraft/screen/TitleDisplay.java index 9df11f850..7bc1fdf66 100644 --- a/src/main/java/minicraft/screen/TitleDisplay.java +++ b/src/main/java/minicraft/screen/TitleDisplay.java @@ -34,7 +34,7 @@ public TitleDisplay() { super(true, false, new Menu.Builder(false, 2, RelPos.CENTER, new StringEntry("Checking for updates...", Color.BLUE), new BlankEntry(), - new BlankEntry(), + new SelectEntry("Play", () -> { if (WorldSelectDisplay.getWorldNames().size() > 0) Game.setDisplay(new Display(true, new Menu.Builder(false, 2, RelPos.CENTER, @@ -116,7 +116,7 @@ public void render(Screen screen) { int h = 2; // Height of squares (on the spritesheet) int w = 15; // Width of squares (on the spritesheet) int xo = (Screen.w - w * 8) / 2; // X location of the title - int yo = 28; // Y location of the title + int yo = 18; // Y location of the title for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { @@ -141,7 +141,7 @@ public void render(Screen screen) { int splashColor = isblue ? Color.BLUE : isRed ? Color.RED : isGreen ? Color.GREEN : Color.get(1, bcol*51, bcol*51, bcol*25); - Font.drawCentered(splashes[rand], screen, 52, splashColor); + Font.drawCentered(splashes[rand], screen, 40, splashColor); Font.draw("Version " + Game.VERSION, screen, 1, 1, Color.get(1, 51)); @@ -150,9 +150,9 @@ public void render(Screen screen) { String selectString = "(" + Game.input.getMapping("select") + Localization.getLocalized(" to accept") +")"; String exitString = "(" + Game.input.getMapping("exit") + Localization.getLocalized(" to return") +")"; - Font.drawCentered(upString, screen, Screen.h - 32, Color.DARK_GRAY); - Font.drawCentered(selectString, screen, Screen.h - 22, Color.DARK_GRAY); - Font.drawCentered(exitString, screen, Screen.h - 12, Color.DARK_GRAY); + Font.drawCentered(upString, screen, Screen.h - 30, Color.DARK_GRAY); + Font.drawCentered(selectString, screen, Screen.h - 20, Color.DARK_GRAY); + Font.drawCentered(exitString, screen, Screen.h - 10, Color.DARK_GRAY); } private static final String[] splashes = {