java中的慢地图 [英] Slow map in java

查看:25
本文介绍了java中的慢地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Java 制作一个游戏,是一个 rpg,但是,只有使用地图,游戏才会变慢.地图是在 TiledMap Editor 中制作的,因此,读取并加载到 ArrayList 中的 XML.我的电脑是双核 3.0、4GB 内存、1GB 视频.渲染是这样完成的:

I'm making a game in java, is a rpg, however, only with the map the game is slow. The map is made ​​in TiledMap Editor, therefore, an XML that is read and loaded into an ArrayList. My PC is a dual-core 3.0, 4GB RAM, 1GB Video. The do the rendering is done as follows:

//method of tileset class
public void loadTileset(){
    positions = new int[1 + tilesX * tilesY][2];
    int yy = 0;
    int xx = 0;
    int index = 0;
    // save the initial x and y point of each tile in an array named positions 
    // positions[tileNumber] [0] - X position 
    // positions[tileNumber] [1] - Y position
    for(int i = 1 ; i < positions.length; i++){ 
        if(index == tilesX ){
            yy += tileHeight;
            xx = 0;
            index = 0;
        }
        positions[i][0] = xx;
        positions[i][1] = yy;
        xx += tileWidth;    
        index++;
    }
}


//method of map class
public void draw(Graphics2D screen){
    //x and y position of each tile on the screen
    int x = 0; int y = 0;

    for(int j = 0; j < 20 ; j++){
        for(int i = initialTile ; i < initialTile + quantTiles  ; i++){
            int tile = map[j][i];
            if(tile != 0){
                screen.drawImage(tileSet.getTileImage().getSubimage(tileSet.getTileX(tile), tileSet.getTileY(tile),tileSet.getTileWidth(), tileSet.getTileHeight()),x,y,null);
            }
            x += tileSet.getTileWidth();
        }
        x = 0;
        y += tileSet.getTileHeight();
    }

}

我做错了吗?注意:我是论坛的新手,更糟糕的是我不太懂英语,所以请原谅任何错误.

Am I doing something wrong? Note: I'm new to the forum and to make matters worse I do not understand very much English, so excuse any mistake.

推荐答案

首先,您不应该在每次调用期间为图块创建子图像.严格来说,对于要绘制的图像,您根本不应该调用 getSubimage:这会使图像不受管理",这会按顺序降低渲染性能量级.您应该只为不想渲染的图像调用 getSubimage - 例如,当您最初为图块创建单个图像时.

First of all, you should not create the subimages for the tiles during each call. Strictly speaking, you should not call getSubimage at all for images that you want to paint: It will make the image "unmanaged", and this can degrade rendering performance by an order of magnitude. You should only call getSubimage for images that you do not want to render - for example, when you are initially creating individual images for the tiles.

您显然已经有了一个 TileSet 类.您可以向此类添加一些功能,以便您可以直接访问图块的图像.

You obviously already have a TileSet class. You could add a bit of functionality to this class so that you can directly access images for the tiles.

您当前的代码如下所示:

Your current code looks like this:

screen.drawImage(
    tileSet.getTileImage().getSubimage(
        tileSet.getTileX(tile), 
        tileSet.getTileY(tile),
        tileSet.getTileWidth(), 
        tileSet.getTileHeight()),
    x,y,null);

你可以把它改成这样:

screen.drawImage(tileSet.getTileImage(tile), x,y,null);

这里建议的 getTileImage(int tile) 方法可以获取内部存储的图块.

The getTileImage(int tile) method suggested here could then obtain tiles that have been stored internally.

我将从我的脑海中勾勒出几行代码,您可能可以将其转移到您的 TileSet 类中:

I'll sketch a few lines of code from the tip of my head, you'll probably be able to transfer this into your TileSet class:

class TileSet
{
    private Map<Integer, BufferedImage> tileImages;

    TileSet()
    {
        ....
        prepareTileImages();
    }

    private void prepareTileImages()
    {
        tileImages = new HashMap<Integer, BufferedImage>();

        for (int tile : allPossibleTileValuesThatMayBeInTheMap)
        {
            // These are the tiles that you originally rendered
            // in your "draw"-Method
            BufferedImage image = 
                getTileImage().getSubimage(
                    getTileX(tile), 
                    getTileY(tile),
                    getTileWidth(), 
                    getTileHeight());

            // Create a new, managed copy of the image,
            // and store it in the map
            BufferedImage managedImage = convertToARGB(image);
            tileImages.put(tile, managedImage);
        }
    }

    private static BufferedImage convertToARGB(BufferedImage image)
    {
        BufferedImage newImage = new BufferedImage(
            image.getWidth(), image.getHeight(),
            BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = newImage.createGraphics();
        g.drawImage(image, 0, 0, null);
        g.dispose();
        return newImage;
    }    

    // This is the new method: For a given "tile" value
    // that you found at map[x][y], this returns the
    // appropriate tile:
    public BufferedImage getTileImage(int tile) 
    {
        return tileImages.get(tile);
    }
}

这篇关于java中的慢地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆