Skip to content

Commit

Permalink
Utilisation des images "compatibles" et correction d'un bug
Browse files Browse the repository at this point in the history
Voilà, les images utilisées sont maintenant des "compatibleImage".
Je n'ai pas vu une grande différence mais on dirait que c'est quand même un peu
plus fluide (sous Linux et Windows).

J'ai aussi corrigé une erreur bête dans CameraFantome.
  • Loading branch information
Cédric Connes authored and Cédric Connes committed Dec 22, 2011
1 parent 75e2576 commit 1c725fc
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 10 deletions.
9 changes: 5 additions & 4 deletions src/affichage/CameraFantome.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void keyPressed(KeyEvent e) {
if (yBase < yBaseMax) {
yBase += increment;
yBase = yBase > yBaseMax ? yBaseMax : yBase;
yLim = yBase + largeur - 1;
yLim = yBase + hauteur - 1;
if (ecran == null || g == null) return;
synchronized (g) {
g.copyArea(0, increment, largeur, hauteur - increment, 0, -increment);
Expand All @@ -110,7 +110,7 @@ public void keyPressed(KeyEvent e) {
if (xBase > 0) {
xBase -= increment;
xBase = xBase < 0 ? 0 : xBase;
xLim = xBase + hauteur - 1;
xLim = xBase + largeur - 1;
if (ecran == null || g == null) return;
synchronized (g) {
g.copyArea(0, 0, largeur - increment, hauteur, increment, 0);
Expand All @@ -122,7 +122,7 @@ public void keyPressed(KeyEvent e) {
if (xBase < xBaseMax) {
xBase += increment;
xBase = xBase > xBaseMax ? xBaseMax : xBase;
xLim = xBase + hauteur - 1;
xLim = xBase + largeur - 1;
if (ecran == null || g == null) return;
synchronized (g) {
g.copyArea(increment, 0, largeur - increment, hauteur, -increment, 0);
Expand All @@ -132,8 +132,9 @@ public void keyPressed(KeyEvent e) {
break;
case KeyEvent.VK_UP:
if (yBase > 0) {
yBase -= increment;
yBase = yBase < 0 ? 0 : yBase;
yLim = yBase + largeur - 1;
yLim = yBase + hauteur - 1;
if (ecran == null || g == null) return;
synchronized (g) {
g.copyArea(0, 0, largeur, hauteur - increment, 0, increment);
Expand Down
2 changes: 2 additions & 0 deletions src/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class Client extends JFrame {
private Ecran ecran;

public Client() throws IOException {
Ressources.setConfig(getGraphicsConfiguration());

long debut = System.currentTimeMillis();
/*
ressources.Theme theme = Ressources.getTheme("tileset.png");
Expand Down
6 changes: 4 additions & 2 deletions src/modele/Carte.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package modele;
import java.awt.Graphics;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
Expand All @@ -15,6 +16,7 @@
import affichage.Camera;

import ressources.Element;
import ressources.Ressources;

public class Carte implements Serializable {
private static final long serialVersionUID = 42L;
Expand Down Expand Up @@ -141,8 +143,8 @@ public static Carte lire(File fichier) {
}

public void exporterImage(File fichier, String type) throws IOException {
BufferedImage image = new BufferedImage(32 * largeur, 32 * hauteur,
BufferedImage.TYPE_INT_ARGB);
BufferedImage image = Ressources.getConfig().createCompatibleImage(
32 * largeur, 32 * hauteur, Transparency.TRANSLUCENT);
dessiner(image.getGraphics(), 0, 0, 0, 0, 32 * largeur, 32 * hauteur);
ImageIO.write(image, type, fichier);
}
Expand Down
7 changes: 5 additions & 2 deletions src/ressources/Apparence.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ressources;

import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -46,8 +47,10 @@ public Apparence(File fichier) throws IOException {
if (largeur != 128 || hauteur != 192)
throw new InputMismatchException("Les dimensions de l'image " +
fichier + " sont incorrectes");
for (int j = 0; j < 4; j++) for (int i = 0; i < 4; i++)
images[i][j] = ressource.getSubimage(32 * i, 48 * j, 32, 48);
for (int j = 0; j < 4; j++) for (int i = 0; i < 4; i++) {
images[i][j] = Ressources.getConfig().createCompatibleImage(32, 48, Transparency.TRANSLUCENT);
images[i][j].createGraphics().drawImage(ressource.getSubimage(32 * i, 48 * j, 32, 48), 0, 0, null);
}
}

/**
Expand Down
25 changes: 25 additions & 0 deletions src/ressources/Ressources.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package ressources;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
Expand All @@ -10,6 +12,11 @@
* @author Sébastien LENTINI
*/
public class Ressources {
/**
* Configuration graphique à utiliser pour charger les images.
*/
private static GraphicsConfiguration config = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();

/**
* Emplacement du répertoire contenant les ressources.
*/
Expand Down Expand Up @@ -40,6 +47,24 @@ public class Ressources {
*/
private Ressources() {}


/**
* Retourne la configuration graphique actuellement utilisée pour charger les images.
* @return la configuration graphique actuellement utilisée
*/
public static GraphicsConfiguration getConfig() {
return config;
}

/**
* Modifie la configuration graphique à utiliser pour charger les images.
* <br>Remarque : cette méthode n'a aucun effet sur les images déjà chargées.
* @param config nouvelle configuration graphique
*/
public static void setConfig(GraphicsConfiguration config) {
Ressources.config = config;
}

/**
* Retourne l'emplacement du répertoire contenant les ressources.
* @return l'emplacement du répertoire contenant les ressources
Expand Down
5 changes: 3 additions & 2 deletions src/ressources/Theme.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package ressources;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -144,8 +145,8 @@ public Element getElement(Element.Cle cle) throws IOException {
if (res == null) {
// Chargement de l'élément
if (theme == null) theme = ImageIO.read(fichier);
BufferedImage image = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);
image.getGraphics().drawImage(theme.getSubimage(32 * cle.i, 32 * cle.j, 32, 32), 0, 0, null);
BufferedImage image = Ressources.getConfig().createCompatibleImage(32, 32, Transparency.TRANSLUCENT);
image.createGraphics().drawImage(theme.getSubimage(32 * cle.i, 32 * cle.j, 32, 32), 0, 0, null);
res = new Element(nom, cle, image);
elements.put(cle, new WeakReference<Element>(res));
}
Expand Down

0 comments on commit 1c725fc

Please sign in to comment.