drawing-area: hold in-memory tile images in an object, rather than static variables
This will allow changing tile sizes on a per-window basis. git-svn-id: https://micropolis.googlecode.com/svn/trunk/micropolis-java@752 d9718cc8-9f43-0410-858b-315f434eb58c
This commit is contained in:
parent
8a9af13de9
commit
d452c45d21
3 changed files with 131 additions and 65 deletions
|
@ -991,8 +991,8 @@ public class MainWindow extends JFrame
|
||||||
private void onToolDown(MouseEvent ev)
|
private void onToolDown(MouseEvent ev)
|
||||||
{
|
{
|
||||||
if (ev.getButton() == MouseEvent.BUTTON3) {
|
if (ev.getButton() == MouseEvent.BUTTON3) {
|
||||||
doQueryTool(ev.getX() / MicropolisDrawingArea.TILE_WIDTH,
|
CityLocation loc = drawingArea.getCityLocation(ev.getX(), ev.getY());
|
||||||
ev.getY() / MicropolisDrawingArea.TILE_HEIGHT);
|
doQueryTool(loc.x, loc.y);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1002,8 +1002,9 @@ public class MainWindow extends JFrame
|
||||||
if (currentTool == null)
|
if (currentTool == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
int x = ev.getX() / MicropolisDrawingArea.TILE_WIDTH;
|
CityLocation loc = drawingArea.getCityLocation(ev.getX(), ev.getY());
|
||||||
int y = ev.getY() / MicropolisDrawingArea.TILE_HEIGHT;
|
int x = loc.x;
|
||||||
|
int y = loc.y;
|
||||||
|
|
||||||
if (currentTool == MicropolisTool.QUERY) {
|
if (currentTool == MicropolisTool.QUERY) {
|
||||||
doQueryTool(x, y);
|
doQueryTool(x, y);
|
||||||
|
@ -1058,8 +1059,9 @@ public class MainWindow extends JFrame
|
||||||
if ((ev.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) == 0)
|
if ((ev.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
int x = ev.getX() / MicropolisDrawingArea.TILE_WIDTH;
|
CityLocation loc = drawingArea.getCityLocation(ev.getX(), ev.getY());
|
||||||
int y = ev.getY() / MicropolisDrawingArea.TILE_HEIGHT;
|
int x = loc.x;
|
||||||
|
int y = loc.y;
|
||||||
if (x == lastX && y == lastY)
|
if (x == lastX && y == lastY)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -1083,8 +1085,9 @@ public class MainWindow extends JFrame
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int x = ev.getX() / MicropolisDrawingArea.TILE_WIDTH;
|
CityLocation loc = drawingArea.getCityLocation(ev.getX(), ev.getY());
|
||||||
int y = ev.getY() / MicropolisDrawingArea.TILE_HEIGHT;
|
int x = loc.x;
|
||||||
|
int y = loc.y;
|
||||||
int w = currentTool.getWidth();
|
int w = currentTool.getWidth();
|
||||||
int h = currentTool.getHeight();
|
int h = currentTool.getHeight();
|
||||||
|
|
||||||
|
|
|
@ -36,9 +36,15 @@ public class MicropolisDrawingArea extends JComponent
|
||||||
static final Dimension PREFERRED_VIEWPORT_SIZE = new Dimension(640,640);
|
static final Dimension PREFERRED_VIEWPORT_SIZE = new Dimension(640,640);
|
||||||
static final ResourceBundle strings = MainWindow.strings;
|
static final ResourceBundle strings = MainWindow.strings;
|
||||||
|
|
||||||
|
static final int DEFAULT_TILE_SIZE = 16;
|
||||||
|
TileImages tileImages;
|
||||||
|
int TILE_WIDTH;
|
||||||
|
int TILE_HEIGHT;
|
||||||
|
|
||||||
public MicropolisDrawingArea(Micropolis engine)
|
public MicropolisDrawingArea(Micropolis engine)
|
||||||
{
|
{
|
||||||
this.m = engine;
|
this.m = engine;
|
||||||
|
selectTileSize(DEFAULT_TILE_SIZE);
|
||||||
m.addMapListener(this);
|
m.addMapListener(this);
|
||||||
|
|
||||||
addAncestorListener(new AncestorListener() {
|
addAncestorListener(new AncestorListener() {
|
||||||
|
@ -52,6 +58,18 @@ public class MicropolisDrawingArea extends JComponent
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void selectTileSize(int newTileSize)
|
||||||
|
{
|
||||||
|
tileImages = TileImages.getInstance(newTileSize);
|
||||||
|
TILE_WIDTH = tileImages.TILE_WIDTH;
|
||||||
|
TILE_HEIGHT = tileImages.TILE_HEIGHT;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CityLocation getCityLocation(int x, int y)
|
||||||
|
{
|
||||||
|
return new CityLocation(x / TILE_WIDTH, y / TILE_HEIGHT);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Dimension getPreferredSize()
|
public Dimension getPreferredSize()
|
||||||
{
|
{
|
||||||
|
@ -77,65 +95,11 @@ public class MicropolisDrawingArea extends JComponent
|
||||||
repaint();
|
repaint();
|
||||||
}
|
}
|
||||||
|
|
||||||
static Image [] tileImages = loadTileImages("/tiles.png");
|
|
||||||
public static final int TILE_WIDTH = 16;
|
|
||||||
public static final int TILE_HEIGHT = 16;
|
|
||||||
|
|
||||||
static Image [] loadTileImages(String resourceName)
|
|
||||||
{
|
|
||||||
URL iconUrl = MicropolisDrawingArea.class.getResource(resourceName);
|
|
||||||
Image refImage = new ImageIcon(iconUrl).getImage();
|
|
||||||
|
|
||||||
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
|
||||||
GraphicsDevice dev = env.getDefaultScreenDevice();
|
|
||||||
GraphicsConfiguration conf = dev.getDefaultConfiguration();
|
|
||||||
|
|
||||||
Image [] images = new Image[refImage.getHeight(null) / TILE_HEIGHT];
|
|
||||||
for (int i = 0; i < images.length; i++)
|
|
||||||
{
|
|
||||||
BufferedImage bi = conf.createCompatibleImage(TILE_WIDTH, TILE_HEIGHT, Transparency.OPAQUE);
|
|
||||||
Graphics2D gr = bi.createGraphics();
|
|
||||||
gr.drawImage(refImage, 0, 0, TILE_WIDTH, TILE_HEIGHT,
|
|
||||||
0, i * TILE_HEIGHT,
|
|
||||||
TILE_WIDTH, (i+1)*TILE_HEIGHT,
|
|
||||||
null);
|
|
||||||
|
|
||||||
images[i] = bi;
|
|
||||||
}
|
|
||||||
return images;
|
|
||||||
}
|
|
||||||
|
|
||||||
static Map<SpriteKind, Map<Integer, Image> > spriteImages;
|
|
||||||
static {
|
|
||||||
spriteImages = new EnumMap<SpriteKind, Map<Integer,Image> >(SpriteKind.class);
|
|
||||||
for (SpriteKind kind : SpriteKind.values())
|
|
||||||
{
|
|
||||||
HashMap<Integer,Image> imgs = new HashMap<Integer,Image>();
|
|
||||||
for (int i = 0; i < kind.numFrames; i++) {
|
|
||||||
Image img = loadSpriteImage(kind, i);
|
|
||||||
if (img != null) {
|
|
||||||
imgs.put(i, img);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
spriteImages.put(kind, imgs);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static Image loadSpriteImage(SpriteKind kind, int frameNo)
|
|
||||||
{
|
|
||||||
String resourceName = "/obj"+kind.objectId+"-"+frameNo+".png";
|
|
||||||
URL iconUrl = MicropolisDrawingArea.class.getResource(resourceName);
|
|
||||||
if (iconUrl == null)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
return new ImageIcon(iconUrl).getImage();
|
|
||||||
}
|
|
||||||
|
|
||||||
void drawSprite(Graphics gr, Sprite sprite)
|
void drawSprite(Graphics gr, Sprite sprite)
|
||||||
{
|
{
|
||||||
assert sprite.isVisible();
|
assert sprite.isVisible();
|
||||||
|
|
||||||
Image img = spriteImages.get(sprite.kind).get(sprite.frame-1);
|
Image img = tileImages.getSpriteImage(sprite.kind, sprite.frame-1);
|
||||||
if (img != null) {
|
if (img != null) {
|
||||||
gr.drawImage(img, sprite.x + sprite.offx, sprite.y + sprite.offy, null);
|
gr.drawImage(img, sprite.x + sprite.offx, sprite.y + sprite.offy, null);
|
||||||
}
|
}
|
||||||
|
@ -179,8 +143,7 @@ public class MicropolisDrawingArea extends JComponent
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int tile = (cell & LOMASK) % tileImages.length;
|
gr.drawImage(tileImages.getTileImage(cell),
|
||||||
gr.drawImage(tileImages[tile],
|
|
||||||
x*TILE_WIDTH + (shakeStep != 0 ? getShakeModifier(y) : 0),
|
x*TILE_WIDTH + (shakeStep != 0 ? getShakeModifier(y) : 0),
|
||||||
y*TILE_HEIGHT,
|
y*TILE_HEIGHT,
|
||||||
null);
|
null);
|
||||||
|
|
100
src/micropolisj/gui/TileImages.java
Normal file
100
src/micropolisj/gui/TileImages.java
Normal file
|
@ -0,0 +1,100 @@
|
||||||
|
// This file is part of MicropolisJ.
|
||||||
|
// Copyright (C) 2013 Jason Long
|
||||||
|
// Portions Copyright (C) 1989-2007 Electronic Arts Inc.
|
||||||
|
//
|
||||||
|
// MicropolisJ is free software; you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU GPLv3, with additional terms.
|
||||||
|
// See the README file, included in this distribution, for details.
|
||||||
|
|
||||||
|
package micropolisj.gui;
|
||||||
|
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.image.*;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.util.*;
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
|
import micropolisj.engine.*;
|
||||||
|
import static micropolisj.engine.TileConstants.*;
|
||||||
|
|
||||||
|
public class TileImages
|
||||||
|
{
|
||||||
|
final int TILE_WIDTH;
|
||||||
|
final int TILE_HEIGHT;
|
||||||
|
Image [] images;
|
||||||
|
Map<SpriteKind, Map<Integer, Image> > spriteImages;
|
||||||
|
|
||||||
|
private TileImages(int size)
|
||||||
|
{
|
||||||
|
this.TILE_WIDTH = size;
|
||||||
|
this.TILE_HEIGHT = size;
|
||||||
|
this.images = loadTileImages("/tiles.png");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static TileImages getInstance(int size)
|
||||||
|
{
|
||||||
|
return new TileImages(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Image getTileImage(int cell)
|
||||||
|
{
|
||||||
|
int tile = (cell & LOMASK) % images.length;
|
||||||
|
return images[tile];
|
||||||
|
}
|
||||||
|
|
||||||
|
private Image [] loadTileImages(String resourceName)
|
||||||
|
{
|
||||||
|
URL iconUrl = TileImages.class.getResource(resourceName);
|
||||||
|
Image refImage = new ImageIcon(iconUrl).getImage();
|
||||||
|
|
||||||
|
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
|
||||||
|
GraphicsDevice dev = env.getDefaultScreenDevice();
|
||||||
|
GraphicsConfiguration conf = dev.getDefaultConfiguration();
|
||||||
|
|
||||||
|
Image [] images = new Image[refImage.getHeight(null) / TILE_HEIGHT];
|
||||||
|
for (int i = 0; i < images.length; i++)
|
||||||
|
{
|
||||||
|
BufferedImage bi = conf.createCompatibleImage(TILE_WIDTH, TILE_HEIGHT, Transparency.OPAQUE);
|
||||||
|
Graphics2D gr = bi.createGraphics();
|
||||||
|
gr.drawImage(refImage, 0, 0, TILE_WIDTH, TILE_HEIGHT,
|
||||||
|
0, i * TILE_HEIGHT,
|
||||||
|
TILE_WIDTH, (i+1)*TILE_HEIGHT,
|
||||||
|
null);
|
||||||
|
|
||||||
|
images[i] = bi;
|
||||||
|
}
|
||||||
|
return images;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Image getSpriteImage(SpriteKind kind, int frameNumber)
|
||||||
|
{
|
||||||
|
return spriteImages.get(kind).get(frameNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void loadSpriteImages()
|
||||||
|
{
|
||||||
|
spriteImages = new EnumMap<SpriteKind, Map<Integer,Image> >(SpriteKind.class);
|
||||||
|
for (SpriteKind kind : SpriteKind.values())
|
||||||
|
{
|
||||||
|
HashMap<Integer,Image> imgs = new HashMap<Integer,Image>();
|
||||||
|
for (int i = 0; i < kind.numFrames; i++) {
|
||||||
|
Image img = loadSpriteImage(kind, i);
|
||||||
|
if (img != null) {
|
||||||
|
imgs.put(i, img);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
spriteImages.put(kind, imgs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static Image loadSpriteImage(SpriteKind kind, int frameNo)
|
||||||
|
{
|
||||||
|
String resourceName = "/obj"+kind.objectId+"-"+frameNo+".png";
|
||||||
|
URL iconUrl = TileImages.class.getResource(resourceName);
|
||||||
|
if (iconUrl == null)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
return new ImageIcon(iconUrl).getImage();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in a new issue