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

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

问题描述

我用java制作游戏,是一个RPG,然而,只有在地图上游戏速度缓慢。
该地图由TiledMap Editor编辑,因此是一个读取并加载到ArrayList中的XML。我的电脑是一款双核3.0,4GB RAM,1GB视频。
渲染完成如下:

  // tileset类的方法
public void loadTileset (){
positions = new int [1 + tilesX * tilesY] [2];
int yy = 0;
int xx = 0;
int index = 0;
//保存位于数组中的每个图块的初始x和y点位置
//位置[tileNumber] [0] - X位置
//位置[tileNumber] [1] - 对于(int i = 1; i< positions.length; i ++){
if(index == tilesX){
yy + = tileHeight;
xx = 0;
index = 0;
}
职位[i] [0] = xx;
职位[i] [1] = yy;
xx + = tileWidth;
index ++;
}
}


//地图类的方法
public void draw(Graphics2D屏幕){
// x和y位置屏幕上的每个图块
int x = 0; int y = 0; (int j = 0; j <20; j ++){
for(int i = initialTile; i int tile(

) =地图[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();
}

}

我做错了什么?
注意:我是新来的论坛,并且使情况变得更糟,我不太懂英文,所以请原谅任何错误。 解决方案 / div>

首先,在每次调用期间,您都不应该为瓷砖创建子图像。严格地说,您应 c> getSubimage 来为您想要绘制的图片:它将使图片不受管理,这可以使渲染性能降低一个数量级。对于不想渲染的图像,您应该只为 getSubimage 调用 - 例如,当您最初为图块创建单个图像时。



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



您当前的代码如下所示:

  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)方法然后在这里可以获得内部存储的图块。



我会从头脑里画出几行代码,您可能会转移这到你的 TileSet 类中:

  class TileSet 
{
私人地图< Integer,BufferedImage> tileImages;

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


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

(int tile:allPossibleTileValuesThatMayBeInTheMap)
{
//这些是您最初在绘制中渲染
的图块 - 方法
BufferedImage image =
getTileImage()。getSubimage(
getTileX(tile),
getTileY(tile),
getTileWidth(),
getTileHeight());

//创建图像的新管理副本
//并将其保存在地图中
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();
返回newImage;
}

//这是新方法:对于在map [x] [y]处找到的给定tile值
//,这将返回
//合适的tile:
public BufferedImage getTileImage(int tile)
{
return tileImages.get(tile);
}
}


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.

解决方案

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.

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);

You could change it to look like this:

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

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

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天全站免登陆