From 69a7919d149f77ce50a80746cc7961d0f41f4c2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Delorme?= Date: Wed, 15 Nov 2017 23:10:06 +0100 Subject: [PATCH] Start fixing #2 - Add GameOptions about command line arguments. --- .../java/com/snapgames/gdj/core/Game.java | 58 +++- .../snapgames/gdj/core/utils/GameOptions.java | 326 +++++++++--------- .../snapgames/gdj/gdj107/OptionsState.java | 1 - 3 files changed, 212 insertions(+), 173 deletions(-) diff --git a/src/main/java/com/snapgames/gdj/core/Game.java b/src/main/java/com/snapgames/gdj/core/Game.java index 6509b86..7af6dc5 100644 --- a/src/main/java/com/snapgames/gdj/core/Game.java +++ b/src/main/java/com/snapgames/gdj/core/Game.java @@ -33,6 +33,8 @@ import com.snapgames.gdj.core.io.InputHandler; import com.snapgames.gdj.core.state.GameStateManager; import com.snapgames.gdj.core.ui.Window; +import com.snapgames.gdj.core.utils.GameOptions; +import com.snapgames.gdj.core.utils.OptionDoesNotExistsException; /** * the basic Game container is a JPanel child. @@ -138,6 +140,8 @@ public class Game extends JPanel { private GameStateManager gsm; + private GameOptions options = GameOptions.loadOptions(); + /** * the default constructor for the {@link Game} panel with a game * title. @@ -383,6 +387,13 @@ public int getDebug() { return debug; } + /** + * @return the options loaded from file `options.properties` file. + */ + public GameOptions getOptions() { + return options; + } + /** * request for a screen shot. */ @@ -439,20 +450,43 @@ private void parse(String[] argv) { try { CommandLineParser clp = new DefaultParser(); CommandLine line = clp.parse(options, argv); - int debug = Integer.parseInt(line.getOptionValue("debug", "0")); - int width = Integer.parseInt(line.getOptionValue("width", "320")); - int height = Integer.parseInt(line.getOptionValue("height", "240")); - int scale = Integer.parseInt(line.getOptionValue("scale", "2")); - // boolean fullScreen = Boolean.parseBoolean(line.getOptionValue("fullscreen", - // "false")); - - WIDTH = width; - HEIGHT = height; - SCALE = scale; - this.debug = debug; + int debug = 0; + int width = 0; + int height = 0; + int scale = 0; + boolean fullScreen = false; + // retrieve values from command line and set value to GameOptions. + if (line.hasOption("debug")) { + debug = Integer.parseInt(line.getOptionValue("debug", "0")); + GameOptions.set(GameOptions.OPTION_SCREEN_DEBUG, debug); + } + if (line.hasOption("width")) { + width = Integer.parseInt(line.getOptionValue("width", "320")); + GameOptions.set(GameOptions.OPTION_SCREEN_WIDTH, width); + } + if (line.hasOption("height")) { + height = Integer.parseInt(line.getOptionValue("height", "240")); + GameOptions.set(GameOptions.OPTION_SCREEN_HEIGHT, height); + } + if (line.hasOption("scale")) { + scale = Integer.parseInt(line.getOptionValue("scale", "2")); + GameOptions.set(GameOptions.OPTION_SCREEN_SCALE, scale); + } + if (line.hasOption("fullscreen")) { + fullScreen = Boolean.parseBoolean(line.getOptionValue("fullscreen", "false")); + GameOptions.set(GameOptions.OPTION_SCREEN_FULLSCREEN_MODE, fullScreen); + } + + // Load options from the file. + WIDTH = width = GameOptions.getInteger("width").intValue(); + HEIGHT = height = GameOptions.getInteger("height").intValue(); + SCALE = scale = GameOptions.getInteger("scale").intValue(); + this.debug = debug = GameOptions.getInteger("debug").intValue(); // TODO implement full screen management. + // this.fullscreen = + fullScreen = GameOptions.getBoolean("fullscreen").booleanValue(); - } catch (ParseException e) { + } catch (ParseException | OptionDoesNotExistsException e) { logger.error("unable to parse command line. Try executing command this the -help option."); } diff --git a/src/main/java/com/snapgames/gdj/core/utils/GameOptions.java b/src/main/java/com/snapgames/gdj/core/utils/GameOptions.java index db292d0..7f56970 100644 --- a/src/main/java/com/snapgames/gdj/core/utils/GameOptions.java +++ b/src/main/java/com/snapgames/gdj/core/utils/GameOptions.java @@ -1,160 +1,166 @@ -/** - * SnapGames - * - * Game Development Java - * - * gdj106 - * - * @year 2017 - */ -package com.snapgames.gdj.core.utils; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.Properties; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * - * @author Frédéric Delorme - * - */ -public class GameOptions { - - private static GameOptions instance; - - private static final Logger logger = LoggerFactory.getLogger(GameOptions.class); - - protected static Properties props = new Properties(); - - /** - * the name of the Option for Music. - */ - public static final String OPTION_MUSIC_FLAG = "Options.MusicFlag"; - - /** - * the name of the Option for Sound. - */ - public static final String OPTION_SOUND_FLAG = "Options.SoundFlag"; - - /** - * file to store configuration of options. - */ - public static final String OPTIONS_PROPERTIES_FILE = "/options.properties"; - - private GameOptions() { - - } - - public static void set(String name, Object value) { - props.put(name, value.toString()); - logger.debug("Add option {} with value {}", name, value.toString()); - } - - public static Boolean getBoolean(String name) throws OptionDoesNotExistsException { - if (props.containsKey(name)) { - return Boolean.parseBoolean((String) props.get(name)); - } else { - throw new OptionDoesNotExistsException(name); - } - } - - public static Integer getInteger(String name) throws OptionDoesNotExistsException { - if (props.containsKey(name)) { - return Integer.parseInt((String) props.get(name)); - } else { - throw new OptionDoesNotExistsException(name); - } - } - - public static String getString(String name) throws OptionDoesNotExistsException { - if (props.containsKey(name)) { - return (String) props.get(name); - } else { - throw new OptionDoesNotExistsException(name); - } - } - - public static Float getFloat(String name) throws OptionDoesNotExistsException { - if (props.containsKey(name)) { - return Float.parseFloat((String) props.get(name)); - } else { - throw new OptionDoesNotExistsException(name); - } - } - - public static void load(InputStream inStream) { - try { - props.load(inStream); - logger.debug("Options loaded"); - } catch (IOException e) { - e.printStackTrace(); - } - } - - public static void write(OutputStream outStream) { - try { - props.store(outStream, null); - logger.debug("Options stored"); - } catch (IOException e) { - e.printStackTrace(); - } - } - - /** - * Load options from file options.properties. - */ - public void load() { - try { - if (Files.isRegularFile(Paths - .get(this.getClass().getResource(GameOptions.OPTIONS_PROPERTIES_FILE).getFile().substring(1)))) { - GameOptions.load(this.getClass().getResourceAsStream(GameOptions.OPTIONS_PROPERTIES_FILE)); - logger.info("Options loaded from {}", GameOptions.OPTIONS_PROPERTIES_FILE); - } - } catch (NullPointerException e) { - logger.info("Unable to read Options from {}", GameOptions.OPTIONS_PROPERTIES_FILE); - save(); - } - } - - /** - * Save game options to file options.properties. - */ - public void save() { - - File fout = new File(this.getClass().getResource("/").getPath() + "options.properties"); - FileOutputStream fos; - try { - fos = new FileOutputStream(fout); - GameOptions.write(fos); - logger.info("Options saved to {}", GameOptions.OPTIONS_PROPERTIES_FILE); - } catch (FileNotFoundException e) { - logger.error("Unable to save Options to {}", GameOptions.OPTIONS_PROPERTIES_FILE); - } - } - - public static GameOptions getInstance() { - if (instance == null) { - instance = new GameOptions(); - instance.load(); - } - return instance; - } - - public static void loadOptions() { - getInstance(); - } - - public static void saveOptions() { - getInstance().save(); - } - -} +/** + * SnapGames + * + * Game Development Java + * + * gdj106 + * + * @year 2017 + */ +package com.snapgames.gdj.core.utils; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Properties; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * + * @author Frédéric Delorme + * + */ +public class GameOptions { + + private static GameOptions instance; + + private static final Logger logger = LoggerFactory.getLogger(GameOptions.class); + + protected static Properties props = new Properties(); + + /** + * the name of the Option for Music. + */ + public static final String OPTION_MUSIC_FLAG = "Options.MusicFlag"; + + /** + * the name of the Option for Sound. + */ + public static final String OPTION_SOUND_FLAG = "Options.SoundFlag"; + + public static final String OPTION_SCREEN_WIDTH = "Options.screen.width"; + public static final String OPTION_SCREEN_HEIGHT = "Options.screen.height"; + public static final String OPTION_SCREEN_SCALE = "Options.screen.scale"; + public static final String OPTION_SCREEN_DEBUG = "Options.screen.debug"; + public static final String OPTION_SCREEN_FULLSCREEN_MODE = "Options.screen.debug"; + + /** + * file to store configuration of options. + */ + public static final String OPTIONS_PROPERTIES_FILE = "/options.properties"; + + private GameOptions() { + + } + + public static void set(String name, Object value) { + props.put(name, value.toString()); + logger.debug("Add option {} with value {}", name, value.toString()); + } + + public static Boolean getBoolean(String name) throws OptionDoesNotExistsException { + if (props.containsKey(name)) { + return Boolean.parseBoolean((String) props.get(name)); + } else { + throw new OptionDoesNotExistsException(name); + } + } + + public static Integer getInteger(String name) throws OptionDoesNotExistsException { + if (props.containsKey(name)) { + return Integer.parseInt((String) props.get(name)); + } else { + throw new OptionDoesNotExistsException(name); + } + } + + public static String getString(String name) throws OptionDoesNotExistsException { + if (props.containsKey(name)) { + return (String) props.get(name); + } else { + throw new OptionDoesNotExistsException(name); + } + } + + public static Float getFloat(String name) throws OptionDoesNotExistsException { + if (props.containsKey(name)) { + return Float.parseFloat((String) props.get(name)); + } else { + throw new OptionDoesNotExistsException(name); + } + } + + public static void load(InputStream inStream) { + try { + props.load(inStream); + logger.debug("Options loaded"); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static void write(OutputStream outStream) { + try { + props.store(outStream, null); + logger.debug("Options stored"); + } catch (IOException e) { + e.printStackTrace(); + } + } + + /** + * Load options from file options.properties. + */ + public void load() { + try { + if (Files.isRegularFile(Paths + .get(this.getClass().getResource(GameOptions.OPTIONS_PROPERTIES_FILE).getFile().substring(1)))) { + GameOptions.load(this.getClass().getResourceAsStream(GameOptions.OPTIONS_PROPERTIES_FILE)); + logger.info("Options loaded from {}", GameOptions.OPTIONS_PROPERTIES_FILE); + } + } catch (NullPointerException e) { + logger.info("Unable to read Options from {}", GameOptions.OPTIONS_PROPERTIES_FILE); + save(); + } + } + + /** + * Save game options to file options.properties. + */ + public void save() { + + File fout = new File(this.getClass().getResource("/").getPath() + "options.properties"); + FileOutputStream fos; + try { + fos = new FileOutputStream(fout); + GameOptions.write(fos); + logger.info("Options saved to {}", GameOptions.OPTIONS_PROPERTIES_FILE); + } catch (FileNotFoundException e) { + logger.error("Unable to save Options to {}", GameOptions.OPTIONS_PROPERTIES_FILE); + } + } + + public static GameOptions getInstance() { + if (instance == null) { + instance = new GameOptions(); + instance.load(); + } + return instance; + } + + public static GameOptions loadOptions() { + return getInstance(); + } + + public static void saveOptions() { + getInstance().save(); + } + +} diff --git a/src/main/java/com/snapgames/gdj/gdj107/OptionsState.java b/src/main/java/com/snapgames/gdj/gdj107/OptionsState.java index 6db7aca..9d640aa 100644 --- a/src/main/java/com/snapgames/gdj/gdj107/OptionsState.java +++ b/src/main/java/com/snapgames/gdj/gdj107/OptionsState.java @@ -85,7 +85,6 @@ public void initialize(Game game) { super.initialize(game); try { - GameOptions.loadOptions(); soundFlag = GameOptions.getBoolean(GameOptions.OPTION_SOUND_FLAG); musicFlag = GameOptions.getBoolean(GameOptions.OPTION_MUSIC_FLAG); } catch (OptionDoesNotExistsException e) {