Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added multiple aspect ratios #301

Merged
merged 8 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/minicraft/core/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ public static void main(String[] args) {
player.eid = 0;
new Load(true); // This loads any saved preferences.

Renderer.setAspectRatio(); // Sets the screen resolution.

setMenu(new TitleDisplay()); // Sets menu to the title screen.


Initializer.createAndDisplayFrame();

Renderer.initScreen();
Expand Down
37 changes: 34 additions & 3 deletions src/main/java/minicraft/core/Renderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,8 +36,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
Expand Down Expand Up @@ -78,7 +80,36 @@ public static SpriteSheet[] loadDefaultSpriteSheets() {
return new SpriteSheet[] { itemSheet, tileSheet, entitySheet, guiSheet, skinsSheet };
}

static void initScreen() {
public static void setAspectRatio() {
int width = 0;
lino-levan marked this conversation as resolved.
Show resolved Hide resolved
int height = 0;

switch ((String) Settings.get("aspectratio")) {
case "Native":
java.awt.Dimension size = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
lino-levan marked this conversation as resolved.
Show resolved Hide resolved
width = (int) size.getWidth();
height = (int) size.getHeight();
break;

case "4x3":
width = 4;
height = 3;
break;

case "16x9":
width = 16;
height = 9;
break;
default:
break;
lino-levan marked this conversation as resolved.
Show resolved Hide resolved
}

double s = Math.min((double) Renderer.MAX_SIZE/width, (double) Renderer.MAX_SIZE/height);
Renderer.HEIGHT = (int) (height * s);
lino-levan marked this conversation as resolved.
Show resolved Hide resolved
Renderer.WIDTH = (int) (width * s);
}

public static void initScreen() {
image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();

Expand Down
1 change: 1 addition & 0 deletions src/main/java/minicraft/core/io/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class Settings {
private static final HashMap<String, ArrayEntry> options = new HashMap<>();

static {
options.put("aspectratio", new ArrayEntry<>("Aspect Ratio", "4x3", "16x9", "Native"));
options.put("fps", new RangeEntry("Max FPS", 10, 300, getRefreshRate())); // Has to check if the game is running in a headless mode. If it doesn't set the fps to 60
lino-levan marked this conversation as resolved.
Show resolved Hide resolved
options.put("diff", new ArrayEntry<>("Difficulty", "Easy", "Normal", "Hard"));
options.get("diff").setSelection(1);
Expand Down
1 change: 1 addition & 0 deletions src/main/java/minicraft/saveload/Load.java
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ 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");
Settings.set("fps", json.getInt("fps"));

if (prefVer.compareTo(new Version("2.1.0-dev1")) > 0) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/minicraft/saveload/Save.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/minicraft/screen/OptionsMainMenuDisplay.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import minicraft.core.Game;
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;

Expand All @@ -11,6 +14,7 @@ public class OptionsMainMenuDisplay extends Display {
public OptionsMainMenuDisplay() {
super(true, new Menu.Builder(false, 6, RelPos.LEFT,
Settings.getEntry("fps"),
Settings.getEntry("aspectratio"),
Settings.getEntry("sound"),
new SelectEntry("Change Key Bindings", () -> Game.setMenu(new KeyInputDisplay())),
Settings.getEntry("language"),
Expand All @@ -20,6 +24,14 @@ public OptionsMainMenuDisplay() {
);
}

@Override
public void render(Screen screen) {
super.render(screen);

// Warning users about screen resoluton requiring a restart
lino-levan marked this conversation as resolved.
Show resolved Hide resolved
Font.drawCentered("Some settings may require a restart", screen, Screen.h-20, Color.RED);
}
lino-levan marked this conversation as resolved.
Show resolved Hide resolved

@Override
public void onExit() {
Localization.changeLanguage((String)Settings.get("language"));
Expand Down