简体   繁体   English

Java Slick2d图像/纹理.equals()

[英]Java Slick2d Image/Texture .equals()

I am creating a 2d tile game. 我正在创建一个2D图块游戏。 In this game, borders between different types of tiles are drawn in the program (corner tiles, side tiles, etc.) There is a finite number of possible tiles like this. 在此游戏中,程序中会绘制不同类型的图块之间的边界(角落图块,侧面图块等)。像这样的图块数量有限。 As soon as I implemented this however, the frame rate crashed pretty badly. 但是,一旦实现此功能,帧率就会严重崩溃。 I figured out that the act of redrawing the 80-90 visible tiles every frame was the problem. 我发现,每帧重新绘制80-90可见瓷砖的行为就是问题所在。 I tried to implement a cache system to cache the unique tiles. 我试图实现一个缓存系统来缓存唯一的图块。 This system works except for the fact that I cannot compare a generated image to a cache of images. 除了无法将生成的图像与图像缓存进行比较之外,该系统均有效。

Here is what I would like to be able to do: 这是我想做的:

//tile object is an Image
if (cache.contains(tile)){
    return cache;
} else {
    cache.add(tile);
    return tile;
}

The problem lies with the comparison of two Slick2D Image objects. 问题在于两个Slick2D Image对象的比较。 It seems like the image.equals() method will only be true if both the image pointers represent the same object. 看起来image.equals()方法只有在两个图像指针都表示同一对象时才为true。 Other than pixel by pixel comparison, is there a way to quickly see if two different Image objects have exactly the same data? 除了逐像素比较之外,是否可以快速查看两个不同的Image对象是否具有完全相同的数据?

In the past how I have used tile systems is by using the TiledMap classes and then redrawing what the world tiles would look like in the Tiled Editor ( http://www.mapeditor.org ). 过去,我如何使用图块系统是通过使用TiledMap类,然后重绘图块编辑器( http://www.mapeditor.org )中的世界图块。 This creates a .tmx file which can be used for collisions and rendering. 这将创建一个.tmx文件,该文件可用于碰撞和渲染。

However, if you are looking for procedural generation my best recommendation would be to create a 'Tile' class. 但是,如果您正在寻找程序生成,那么我的最佳建议是创建一个“ Tile”类。 For example: 例如:

public class Tile {
    public static int CORNER_TILE = 0;
    public static int SIDE_TILE = 1;
    public static int CENTRE_TILE = 2;

    public int x, y, type;

    public Tile(int type, int x, int y) {
        this.type = type;
        this.x = x;
        this.y = y;
    } 
    public void render(Graphics g) {
        //Insert rendering code for each different tile type
        //For example:

        if (this.type == Tile.CENTRE_TILE) {
            g.drawImage(Main.someTileImage, x * 32, y * 32);
            //Multiply x and y by dimensions of tile
        }
   }
}

All you would then need is a 2d array of tiles which you loop through to update each frame. 然后,您需要的是一个2D的瓷砖阵列,您可以循环遍历这些瓷砖以更新每个帧。 I know this isn't exactly what you asked but these are my ways of working with tiles. 我知道这不完全是您的要求,但这是我处理瓷砖的方式。 Hope this helps :) 希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM