Clarify naming in the Loader, add comments.

This commit is contained in:
Jan Philipp Timme 2014-11-06 22:46:40 +01:00
parent b60abdc577
commit 3d80faddef
7 changed files with 95 additions and 15 deletions

View File

@ -128,7 +128,7 @@ public class SoundSystem {
public static void playSound(String filename) { public static void playSound(String filename) {
SoundSystem.playFromAudioInputStream(Loader.getAudioInputStreamByFilename(filename)); SoundSystem.playFromAudioInputStream(Loader.getSoundURLByFilename(filename));
} }

View File

@ -1,5 +1,10 @@
package de.teamteamteam.spacescooter.thread; package de.teamteamteam.spacescooter.thread;
/**
* Since things like drawing the next image or triggering the next game tick
* need to happen in time, this TimedThread allows more precise timing
* combined with a nap for the cpu in case there is enough time for it.
*/
public abstract class TimedThread extends Thread { public abstract class TimedThread extends Thread {
private long workInterval; private long workInterval;

View File

@ -2,18 +2,34 @@ package de.teamteamteam.spacescooter.thread;
import de.teamteamteam.spacescooter.screen.Screen; import de.teamteamteam.spacescooter.screen.Screen;
/**
* This UpdateThread triggers the hierarchical Screen structure,
* resulting in constant game ticks.
*/
public class UpdateThread extends TimedThread { public class UpdateThread extends TimedThread {
/**
* The SuperScreen instance to trigger.
*/
private Screen superScreen; private Screen superScreen;
/**
* Constructor. Sets the ThreadName.
*/
public UpdateThread() { public UpdateThread() {
this.setName("UpdateThread"); this.setName("UpdateThread");
} }
/**
* Setter for the SuperScreen.
*/
public void setSuperScreen(Screen superScreen) { public void setSuperScreen(Screen superScreen) {
this.superScreen = superScreen; this.superScreen = superScreen;
} }
/**
* On every tick, hit the SuperScreens doUpdate() method.
*/
public void work() { public void work() {
this.superScreen.doUpdate(); this.superScreen.doUpdate();
} }

View File

@ -9,6 +9,12 @@ import java.util.List;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream; import java.util.zip.ZipInputStream;
/**
* This helper is used by the Loader to get a list of files of the whole Code
* and determine specific runtime configurations.
* Since there are some differences between OS and jar/non-jar environments,
* this helper makes sure, everything still works.
*/
public class CodeEnvironment { public class CodeEnvironment {
/** /**

View File

@ -1,10 +1,23 @@
package de.teamteamteam.spacescooter.utility; package de.teamteamteam.spacescooter.utility;
/**
* This static class contains important game configuration.
*/
public class GameConfig { public class GameConfig {
/**
* Whether debug output (and more) is enabled or disabled.
*/
public static boolean DEBUG = false; public static boolean DEBUG = false;
/**
* Width of GameWindow.
*/
public static int windowWidth; public static int windowWidth;
/**
* Height of GameWindow.
*/
public static int windowHeight; public static int windowHeight;

View File

@ -4,39 +4,76 @@ import java.awt.DisplayMode;
import java.awt.GraphicsDevice; import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment; import java.awt.GraphicsEnvironment;
/**
* GraphicsSettings allows to fetch the current graphical settings in order
* to determine a sane refresh rate and the current screen resolution.
*/
public class GraphicsSettings { public class GraphicsSettings {
/**
* Height of the primary screen.
*/
private int height; private int height;
/**
* Width of the primary screen.
*/
private int width; private int width;
/**
* The set refreshRate in Hz.
*/
private int refreshRate; private int refreshRate;
/**
* Available bitDepth. In case you get -1, do not worry. :-)
*/
private int bitDepth; private int bitDepth;
/**
* Constructor. Just retrieve the settings once.
*/
public GraphicsSettings() { public GraphicsSettings() {
this.retrieveSettings(); this.retrieveSettings();
} }
/**
* Returns the refresh rate to use.
*/
public int getRefreshRate() { public int getRefreshRate() {
return this.refreshRate; return this.refreshRate;
} }
/**
* This method fetches the current settings and refreshes the GraphicsSettings attributes.
* In case the current refresh rate could not be detected, it uses 60Hz as a default value.
*/
public void retrieveSettings() { public void retrieveSettings() {
if(GameConfig.DEBUG) {
System.out.println("OS: " + System.getProperty("os.name"));
}
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] gs = ge.getScreenDevices(); GraphicsDevice[] gs = ge.getScreenDevices();
int refreshRate;
//iterate over all available screen devices
for(int i=0; i < gs.length; i++) { for(int i=0; i < gs.length; i++) {
DisplayMode dm = gs[i].getDisplayMode(); DisplayMode dm = gs[i].getDisplayMode();
this.refreshRate = dm.getRefreshRate(); refreshRate = dm.getRefreshRate();
if(this.refreshRate == DisplayMode.REFRESH_RATE_UNKNOWN) { //handle unknown refresh rate
if(refreshRate == DisplayMode.REFRESH_RATE_UNKNOWN) {
System.err.println("[!]Display Mode " + i + ": Unknown refresh rate!"); System.err.println("[!]Display Mode " + i + ": Unknown refresh rate!");
this.refreshRate = 60; //sane default value. :/ refreshRate = 60; //sane default value. :/
} }
//only store the values of the primary screen.
if(i == 0) {
this.refreshRate = refreshRate;
this.bitDepth = dm.getBitDepth(); this.bitDepth = dm.getBitDepth();
this.height = dm.getHeight(); this.height = dm.getHeight();
this.width = dm.getWidth(); this.width = dm.getWidth();
}
if(GameConfig.DEBUG) { if(GameConfig.DEBUG) {
System.out.println("Display Mode " + i + ": " + this.width + "x" + this.height+ "@" + this.refreshRate + "Hz, " + this.bitDepth + " bit"); System.out.println("Display Mode " + i + ": " + this.width + "x" + this.height+ "@" + this.refreshRate + "Hz, " + this.bitDepth + " bit");
System.out.println("OS: " + System.getProperty("os.name"));
} }
} }
} }

View File

@ -29,6 +29,9 @@ public class Loader {
*/ */
private static Hashtable<String, URL> sounds; private static Hashtable<String, URL> sounds;
/**
* Initialize the HashTables on load.
*/
static { static {
Loader.images = new Hashtable<String, BufferedImage>(); Loader.images = new Hashtable<String, BufferedImage>();
Loader.sounds = new Hashtable<String, URL>(); Loader.sounds = new Hashtable<String, URL>();
@ -59,9 +62,9 @@ public class Loader {
} }
/** /**
* Return the loaded AudioInputStream by its relative filename. * Return the loaded sound URL by its relative filename.
*/ */
public static URL getAudioInputStreamByFilename(String filename) { public static URL getSoundURLByFilename(String filename) {
if(CodeEnvironment.isJar()) { if(CodeEnvironment.isJar()) {
return Loader.sounds.get(filename); return Loader.sounds.get(filename);
} else { } else {
@ -88,7 +91,7 @@ public class Loader {
if(e.endsWith(".wav")) { if(e.endsWith(".wav")) {
if(GameConfig.DEBUG) if(GameConfig.DEBUG)
System.out.println("Creating AudioInputStream for: " + e); System.out.println("Creating AudioInputStream for: " + e);
Loader.addAudioInputStreamByFilename(e); Loader.addSoundURLByFilename(e);
} }
loadingScreen.increaseCurrentProcessed(); loadingScreen.increaseCurrentProcessed();
} }
@ -111,7 +114,7 @@ public class Loader {
/** /**
* Load an AudioInputStream by relative filename. * Load an AudioInputStream by relative filename.
*/ */
private static void addAudioInputStreamByFilename(String filename) { private static void addSoundURLByFilename(String filename) {
try { try {
URL soundURL = Loader.class.getClassLoader().getResource(filename); URL soundURL = Loader.class.getClassLoader().getResource(filename);
//make sure the sound is in a valid AudioFormat //make sure the sound is in a valid AudioFormat