Skip to content

Commit

Permalink
Gestion du mode headless
Browse files Browse the repository at this point in the history
  • Loading branch information
marcbal committed Feb 24, 2016
1 parent 07a7615 commit 59f8848
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 19 deletions.
31 changes: 29 additions & 2 deletions src/fr/univ_artois/iut_lens/spaceinvader/MegaSpaceInvader.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.univ_artois.iut_lens.spaceinvader;

import java.awt.GraphicsEnvironment;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
Expand Down Expand Up @@ -65,10 +66,13 @@ public static void main(String[] args) {
shutdownHook();
Thread.currentThread().setName("Main");

boolean headless = GraphicsEnvironment.isHeadless();


try {
// donne à l'interface graphique le thème associé au système d'exploitation
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
if (!headless)
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
Expand All @@ -84,6 +88,12 @@ public static void main(String[] args) {

// si la ligne de commande ne donne rien de concluant, affichage du launcher.
if (launchConfig == null) {
if (headless) {
showCommandLineHelp();
System.exit(1);
}


LauncherDialog diag = new LauncherDialog();

ConfigurationSaver saver = new ConfigurationSaver();
Expand Down Expand Up @@ -116,7 +126,8 @@ public static void main(String[] args) {



if (launchConfig.clientEnabled) {
if (launchConfig.clientEnabled && !headless) {


String addr = launchConfig.clientConnectionAddress;
int port;
Expand Down Expand Up @@ -151,6 +162,22 @@ public static void main(String[] args) {



private static void showCommandLineHelp() {
System.out.println("paramètres : [-s [PORT_NUMBER [score]]] [-c NICKNAME [SERVER_ADDR:PORT]]");
System.out.println();
System.out.println("-s : activer le serveur local");
System.out.println(" PORT_NUMBER : numéro de port attribué au serveur (par défaut 34567)");
System.out.println(" 'score' pour activer le système de score (désactivé par défaut)");
System.out.println("-c : activer le client local (ignoré si le mode headless est activé)");
System.out.println(" NICKNAME le pseudo du joueur");
System.out.println(" SERVER_ADDR:PORT : adresse de connexion au serveur. Ignoré si le serveur local est démarré, obligatoire sinon.");
}








private static void shutdownHook() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public Client(InetSocketAddress serverAddress, String playerName) throws IOExcep

// create a frame to contain our game
container = new JFrame("Méga Space Invader");
container.setIconImage(SpriteStore.get().getSprite("sprites/ComplexShot.png").image);
container.setIconImage(SpriteStore.get().getSprite("sprites/ComplexShot.png").getAWTImage());

// get hold the content of the frame and set up the resolution of the game
JPanel panel = (JPanel) container.getContentPane();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class LauncherDialog extends JFrame {
* Create the dialog.
*/
public LauncherDialog() {
setIconImage(SpriteStore.get().getSprite("sprites/ComplexShot.png").image);
setIconImage(SpriteStore.get().getSprite("sprites/ComplexShot.png").getAWTImage());
setResizable(false);

setDefaultCloseOperation(JDialog.EXIT_ON_CLOSE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.image.BufferedImage;

/**
* A sprite to be displayed on the screen. Note that a sprite
Expand All @@ -14,17 +16,27 @@
*/
public class Sprite {
private static int identifierIncrement = 0;

private final BufferedImage bImage;
/** The image to be drawn for this sprite */
public final Image image;
private Image image = null;
public final int id;

/**
* Create a new sprite based on an image
*
* @param image The image that is this sprite
*/
public Sprite(Image image) {
this.image = image;
public Sprite(BufferedImage bI) {
bImage = bI;

if (SpriteStore.graphicsConfig != null) {
// create an accelerated image of the right size to store our sprite in
image = SpriteStore.graphicsConfig.createCompatibleImage(bI.getWidth(),bI.getHeight(),Transparency.TRANSLUCENT);
// draw our source image into the accelerated image
image.getGraphics().drawImage(bI,0,0,null);
}

id = identifierIncrement++;
}

Expand All @@ -34,7 +46,7 @@ public Sprite(Image image) {
* @return The width in pixels of this sprite
*/
public int getWidth() {
return image.getWidth(null);
return bImage.getWidth();
}

/**
Expand All @@ -43,7 +55,27 @@ public int getWidth() {
* @return The height in pixels of this sprite
*/
public int getHeight() {
return image.getHeight(null);
return bImage.getHeight();
}



/**
*
* @return l'image à afficher, ou null si on est en mode headless
*/
public Image getAWTImage() {
return image;
}



/**
*
* @return le buffer contenant l'image
*/
public BufferedImage getBufferedImage() {
return bImage;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.HeadlessException;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
Expand All @@ -30,6 +29,14 @@ public class SpriteStore {
/** The single instance of this class */
private static SpriteStore single = new SpriteStore();

static GraphicsConfiguration graphicsConfig = null;

static {
try {
graphicsConfig = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
} catch (HeadlessException e) { }
}

/**
* Get the single instance of this class
*
Expand Down Expand Up @@ -80,15 +87,8 @@ public synchronized Sprite getSprite(String ref) {
fail("Failed to load: "+ref);
}

// create an accelerated image of the right size to store our sprite in
GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
Image image = gc.createCompatibleImage(sourceImage.getWidth(),sourceImage.getHeight(),Transparency.TRANSLUCENT);

// draw our source image into the accelerated image
image.getGraphics().drawImage(sourceImage,0,0,null);

// create a sprite, add it the cache then return it
Sprite sprite = new Sprite(image);
Sprite sprite = new Sprite(sourceImage);

sprites.put(ref,sprite);
newSprites.put(ref, sprite);
Expand Down

0 comments on commit 59f8848

Please sign in to comment.