diff --git a/src/de/teamteamteam/spacescooter/Main.java b/src/de/teamteamteam/spacescooter/Main.java index 0a48ed3..00be404 100644 --- a/src/de/teamteamteam/spacescooter/Main.java +++ b/src/de/teamteamteam/spacescooter/Main.java @@ -25,7 +25,7 @@ public class Main { * @param args Command line arguments. */ public static void main(String[] args) { - final GraphicsSettings gs = new GraphicsSettings(); // Get settings + final GraphicsSettings gs = GraphicsSettings.instance; // Get settings // Initialize SuperScreen and add to GameFrame, so we can call doPaint() // on it. diff --git a/src/de/teamteamteam/spacescooter/utility/GraphicsSettings.java b/src/de/teamteamteam/spacescooter/utility/GraphicsSettings.java index e604843..143377e 100644 --- a/src/de/teamteamteam/spacescooter/utility/GraphicsSettings.java +++ b/src/de/teamteamteam/spacescooter/utility/GraphicsSettings.java @@ -1,8 +1,11 @@ package de.teamteamteam.spacescooter.utility; import java.awt.DisplayMode; +import java.awt.Graphics2D; import java.awt.GraphicsDevice; import java.awt.GraphicsEnvironment; +import java.awt.image.BufferedImage; +import java.awt.image.ColorModel; import de.teamteamteam.spacescooter.brain.GameConfig; @@ -32,11 +35,30 @@ public class GraphicsSettings { */ private int bitDepth; + /** + * ScreenDevice this game is supposed to be run on. + * Probably just the primary screen device. + */ + private GraphicsDevice screenDevice; /** - * Constructor. Just retrieve the settings once. + * Instance holder for GraphicsSettings. */ - public GraphicsSettings() { + public static GraphicsSettings instance; + + + /** + * Create the instance and store it in the instance holder. + */ + static { + GraphicsSettings.instance = new GraphicsSettings(); + } + + /** + * Private Constructor. Just retrieve the settings once and + * storing the instance in GraphicsSettings.instance. + */ + private GraphicsSettings() { this.retrieveSettings(); } @@ -73,6 +95,7 @@ public class GraphicsSettings { this.bitDepth = dm.getBitDepth(); this.height = dm.getHeight(); this.width = dm.getWidth(); + this.screenDevice = gs[i]; } if(GameConfig.DEBUG) { System.out.println("Display Mode " + i + ": " + this.width + "x" + this.height+ "@" + this.refreshRate + "Hz, " + this.bitDepth + " bit"); @@ -80,4 +103,25 @@ public class GraphicsSettings { } } + /** + * Get the ColorModel of the screen device the game is supposed to be run on. + */ + public ColorModel getColorModel() { + return this.screenDevice.getDefaultConfiguration().getColorModel(); + } + + /** + * Creates a compatible BufferedImage from a given BufferedImage and returns it. + */ + public BufferedImage createCompatibleBufferedImage(BufferedImage originalImage) { + BufferedImage compatibleImage = this.screenDevice.getDefaultConfiguration() + .createCompatibleImage(originalImage.getWidth(), originalImage.getHeight(), originalImage.getTransparency()); + //Transfer the actual image content by simply drawing. + Graphics2D g = (Graphics2D) compatibleImage.getGraphics(); + g.drawImage(originalImage, 0, 0, null); + g.dispose(); + //We're done. + return compatibleImage; + } + } diff --git a/src/de/teamteamteam/spacescooter/utility/Loader.java b/src/de/teamteamteam/spacescooter/utility/Loader.java index 7c07651..52e6bbd 100644 --- a/src/de/teamteamteam/spacescooter/utility/Loader.java +++ b/src/de/teamteamteam/spacescooter/utility/Loader.java @@ -164,11 +164,13 @@ public class Loader { } /** - * Load a BufferedImage by relative filename. + * Load a BufferedImage by relative filename, creating a compatible + * BufferedImage using the GraphicsSettings ColorModel. */ private static void addBufferedImageByFilename(String filename) { try { - BufferedImage image = ImageIO.read(Loader.class.getClassLoader().getResourceAsStream(filename)); + BufferedImage storedImage = ImageIO.read(Loader.class.getClassLoader().getResourceAsStream(filename)); + BufferedImage image = Loader.createCompatibleImage(storedImage); Loader.images.put(filename, image); } catch (Exception e) { System.err.println("Unable to load BufferedImage: " + filename); @@ -177,6 +179,20 @@ public class Loader { } + /** + * Helper method creating a compatible BufferedImage from a + * given BufferedImage taking into account the current ColorModel of the + * current ScreenDevice. + */ + private static BufferedImage createCompatibleImage(BufferedImage storedImage) { + if(GraphicsSettings.instance.getColorModel().equals(storedImage.getColorModel())) { + return storedImage; + } else { + return GraphicsSettings.instance.createCompatibleBufferedImage(storedImage); + } + } + + /** * Load an AudioInputStream by relative filename. */