基于Java Tile的游戏性能

时间:2012-04-06 16:38:38

标签: java 2d

我目前正在尝试使用Java中基于2D平铺的侧滚动游戏,主要基于David Brackeen的“开发Java游戏”中的代码和示例

目前地图文件的大小为100x100平铺(每个平铺为64x64像素)。我已经将系统配置为仅显示播放器可见的图块。 Graphics系统由ScreenManager类管理,该类返回当前BufferStrategy的图形对象,如下所示:

ScreenManager.java

private GraphicsDevice device;

...

/**
 * Gets the graphics context for the display. The
 * ScreenManager uses double buffering, so applications must 
 * call update() to show any graphics drawn.
 * <p>
 * The application must dispose of the graphics object.
 */
public Graphics2D getGraphics(){
    Window window = device.getFullScreenWindow();
    if(window != null){
        BufferStrategy strategy = window.getBufferStrategy();
        return (Graphics2D)strategy.getDrawGraphics();
    }
    else{
        return null;
    }
}

将此ScreenManager中的图形在游戏循环中传递给TreeRenderer的draw方法。

TreeMapRenderer.java

/**
    Draws the specified TileMap.
*/
public void draw(Graphics2D g, TileMap map,
    int screenWidth, int screenHeight, float fr)
{
    Sprite player = map.getPlayer();
    int mapWidth = tilesToPixels(map.getWidth());
    int mapHeight = tilesToPixels(map.getHeight());

    // get the scrolling position of the map
    // based on player's position
    int offsetX = screenWidth / 2 -
        Math.round(player.getX()) - TILE_SIZE;
    offsetX = Math.min(offsetX, 0);
    offsetX = Math.max(offsetX, screenWidth - mapWidth);

    // get the y offset to draw all sprites and tiles
    int offsetY = screenHeight /2 -
            Math.round(player.getY()) - TILE_SIZE;
    offsetY = Math.min(offsetY,0);
    offsetY = Math.max(offsetY, screenHeight - mapHeight);

    // draw the visible tiles
    int firstTileY = pixelsToTiles(-offsetY);
    int lastTileY = firstTileY + pixelsToTiles(screenHeight) +1;

    int firstTileX = pixelsToTiles(-offsetX);
    int lastTileX = firstTileX +
        pixelsToTiles(screenWidth) + 1;

    //HERE IS WHERE THE SYSTEM BOGS dOWN (checking ~280 tiles per iteration)
    for (int y=firstTileY; y<lastTileY; y++) {
        for (int x=firstTileX; x <= lastTileX; x++) {
            if(map.getTile(x, y) != null){
                Image image = map.getTile(x, y).getImage();
                if (image != null) {
                    g.drawImage(image,
                        tilesToPixels(x) + offsetX,
                        tilesToPixels(y) + offsetY,
                        null);
                }
            }
        }
    }

    // draw player
    g.drawImage(player.getImage(),
       Math.round(player.getX()) + offsetX,
       Math.round(player.getY()) + offsetY,
       null);

算法正确地为X轴和Y轴选择正确的FROM和TO值,从而剔除10000到~285之间所需的切片。

我的问题是,即使这样,游戏也只能以大约8-10 FPS的速度运行。如果我关闭平铺渲染而不是系统以80 FPS运行(当没有任何事情可以快速运行时)

您对如何加快此过程有任何想法吗?我希望看到至少在30 FPS标记左右的东西才能使这个可玩。

最后虽然我愿意使用第三方库来做到这一点,但我想在承认失败之前尝试自己实现这个逻辑。

修改
这里要求提供有关Image image = map.getTile(x, y).getImage();呼叫如何工作的额外信息。

地图来自以下TileMap类

TileMap.java

public class TileMap {

private Tile[][] tiles;
private LinkedList sprites;
private Sprite player;
private GraphicsConfiguration gc;

/**
    Creates a new TileMap with the specified width and
    height (in number of tiles) of the map.
*/
public TileMap(GraphicsConfiguration gc, int width, int height) {
    this.gc = gc;
    tiles = new Tile[width][height];
    overlayer = new Tile[width][height];
    sprites = new LinkedList();
}


/**
    Gets the width of this TileMap (number of tiles across).
*/
public int getWidth() {
    return tiles.length;
}


/**
    Gets the height of this TileMap (number of tiles down).
*/
public int getHeight() {
    return tiles[0].length;
}


/**
    Gets the tile at the specified location. Returns null if
    no tile is at the location or if the location is out of
    bounds.
*/
public Tile getTile(int x, int y) {
    if (x < 0 || x >= getWidth() ||
        y < 0 || y >= getHeight())
    {
        return null;
    }
    else {
        return tiles[x][y];
    }
}


/**
 * Helper method to set a tile. If blocking is not defined than it is set to false.
 * 
 * @param x
 * @param y
 * @param tile
 */
public void setTile(int x, int y,Image tile){
    this.setTile(x,y,tile,false);
}

/**
    Sets the tile at the specified location.
*/
public void setTile(int x, int y, Image tile, boolean blocking) {
    if(tiles[x][y] == null){
        Tile t = new Tile(gc, tile, blocking);
        tiles[x][y] = t;
    }
    else{
        tiles[x][y].addImage(tile);
        tiles[x][y].setBlocking(blocking);
    }
}

...

这里的Tile是以下代码的实例。本质上这个类只保存Image,可以通过使用gc.createCompatibleImage(w,h,Transparency.TRANSLUCENT)添加覆盖层来更新Image;和一个布尔值来判断它是否会阻止玩家。传入的图像也以这种方式创建。

Tile.java

public class Tile {
private Image image;
private boolean blocking = false;
private GraphicsConfiguration gc;

/**
 * Creates a new Tile to be used with a TileMap
 * @param image The base image for this Tile
 * @param blocking Will this tile allow the user to walk over/through
 */
public Tile(GraphicsConfiguration gc, Image image, boolean blocking){
    this.gc = gc;
    this.image = image;
    this.blocking = blocking;
}

public Tile(GraphicsConfiguration gc, Image image){
    this.gc = gc;
    this.image = image;
    this.blocking = false;
}

/**
Creates a duplicate of this animation. The list of frames
are shared between the two Animations, but each Animation
can be animated independently.
*/
public Object clone() {

    return new Tile(gc, image, blocking);
}

/**
 * Used to add an overlay to the existing tile
 * @param image2 The image to overlay
 */
public void addImage(Image image2){
    BufferedImage base = (BufferedImage)image;
    BufferedImage overlay = (BufferedImage)image2;

    // create the new image, canvas size is the max. of both image sizes
    int w = Math.max(base.getWidth(), overlay.getWidth());
    int h = Math.max(base.getHeight(), overlay.getHeight());
    //BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    BufferedImage combined = gc.createCompatibleImage(w, h, Transparency.TRANSLUCENT);
    // paint both images, preserving the alpha channels
    Graphics g = combined.getGraphics();
    g.drawImage(image, 0, 0, null);
    g.drawImage(overlay, 0, 0, null);

    this.image = (Image)combined;
}

public boolean isBlocking(){
    return this.blocking;
}

public void setBlocking(boolean blocking){
    this.blocking = blocking;
}

public Image getImage(){
    return this.image;
}

}

2 个答案:

答案 0 :(得分:1)

我会使用像素渲染引擎(google it; D)

基本上你做了什么,它有一个巨大的整数阵列对应于你正在绘制的图像。

基本上你有每个瓷砖都有一个表示其像素的整数数组。 当你渲染那个瓷砖时,你将瓷砖阵列“复制”(比它稍微复杂一点)到大数组:)

然后,一旦完成将所有内容渲染到主阵列,就可以在屏幕上绘制它。

这样,每次画画时,你只处理整数而不是整张图片。这使得它更快。

我使用MrDeathJockey(youtube)教程学习了这一点,并将它们与DesignsbyZephyr(也是youtube)相结合。虽然我不建议使用他的技术(他只使用4种颜色和8位图形,与deathJockey的教程一样,你可以自定义图像的大小,甚至有多个不同分辨率的精灵表(对字体有用)

然而我确实使用了一些偏移的东西(使屏幕移动而不是播放器)和Zephyr的InputHandler:)

希望这有帮助! -Camodude009

答案 1 :(得分:0)

创建透明图像(不是半透明),因为半透明图像需要更高的ram并存储在系统内存而不是标准的Java堆中。创建半透明图像需要多次本机调用才能访问本机系统内存。使用BufferedImages而不是Image。您可以随时将BufferedImage强制转换为Image。