java - 如何使用 Slick-Utils 从 Java 中的 spritesheet 加载纹理?

标签 java lwjgl slick2d

我正在使用 LWJGL 和 Slick Utils 创建游戏。我正在尝试将动画纹理加载为单个 PNG 图像中包含的一组帧。

我试图弄清楚如何使用 Slick 获取子图像,但到目前为止,我在该主题上所能找到的只是使用 BufferedImages 在 Slick 之外完成此操作的方法。我想知道是否有办法使用 Slick Utils 库来执行此操作,因为到目前为止我的项目中的所有图像加载代码都在使用 Slick。

最佳答案

当然,Slick 提供了一种方法来做到这一点。如果您查看 org.newdawn.slick.SpriteSheet.initImpl() 和后来的 org.newdawn.slick.SpriteSheet.getSprite(),您会注意到 org.newdawn.slick.Image.getSubImage() 如何用于快速提取现有图像的特定部分。

SpriteSheet.java

protected void initImpl() {
    if (subImages != null) {
        return;
    }

    int tilesAcross = ((getWidth()-(margin*2) - tw) / (tw + spacing)) + 1;
    int tilesDown = ((getHeight()-(margin*2) - th) / (th + spacing)) + 1; 
    if ((getHeight() - th) % (th+spacing) != 0) {
        tilesDown++;
    }

    subImages = new Image[tilesAcross][tilesDown];
    for (int x=0;x<tilesAcross;x++) {
        for (int y=0;y<tilesDown;y++) {
            //extract parts of the main image and store them in an array as sprites
            subImages[x][y] = getSprite(x,y);
        }
    }
}

/**
 * Get a sprite at a particular cell on the sprite sheet
 * 
 * @param x The x position of the cell on the sprite sheet
 * @param y The y position of the cell on the sprite sheet
 * @return The single image from the sprite sheet
 */
public Image getSprite(int x, int y) {
    target.init();
    initImpl();

    if ((x < 0) || (x >= subImages.length)) {
        throw new RuntimeException("SubImage out of sheet bounds: "+x+","+y);
    }
    if ((y < 0) || (y >= subImages[0].length)) {
        throw new RuntimeException("SubImage out of sheet bounds: "+x+","+y);
    }
    //Call Image.getSubImage() to get a portion of the image
    return target.getSubImage(x*(tw+spacing) + margin, y*(th+spacing) + margin,tw,th); 
}

您应该可以将其用作引用。我记得曾经将与 Slick 捆绑在一起的 Tiled 渲染器完全移植到 Java2D,使用旧的 PixelGrabber 来提取 Sprite 。

如果您决定使用 SpriteSheet,您可以在 org.newdawn.slick.tiled.Layer.render() 中找到使用示例:

Layer.java

public void render(int x, int y, int sx, int sy, int width, int ty,
        boolean lineByLine, int mapTileWidth, int mapTileHeight) {
    for (int tileset = 0; tileset < map.getTileSetCount(); tileset++) {
        TileSet set = null;

        for (int tx = 0; tx < width; tx++) {
            if ((sx + tx < 0) || (sy + ty < 0)) {
                continue;
            }
            if ((sx + tx >= this.width) || (sy + ty >= this.height)) {
                continue;
            }

            if (data[sx + tx][sy + ty][0] == tileset) {
                if (set == null) {
                    set = map.getTileSet(tileset);
                    set.tiles.startUse();
                }

                int sheetX = set.getTileX(data[sx + tx][sy + ty][1]);
                int sheetY = set.getTileY(data[sx + tx][sy + ty][1]);

                int tileOffsetY = set.tileHeight - mapTileHeight;

                //Call SpriteSheet.renderInUse() to render the sprite cached at slot [sheetX, sheetY]
                set.tiles.renderInUse(x + (tx * mapTileWidth), y
                        + (ty * mapTileHeight) - tileOffsetY, sheetX,
                        sheetY);
            }
        }

        if (lineByLine) {
            if (set != null) {
                set.tiles.endUse();
                set = null;
            }
            map.renderedLine(ty, ty + sy, index);
        }

        if (set != null) {
            set.tiles.endUse();
        }
    }
}

SpriteSheet.java

public void renderInUse(int x,int y,int sx,int sy) {
    //Draw the selected sprite at (x,y), using the width/height defined for this SpriteSheet
    subImages[sx][sy].drawEmbedded(x, y, tw, th);
}

希望这对您有帮助。

关于java - 如何使用 Slick-Utils 从 Java 中的 spritesheet 加载纹理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43713486/

相关文章:

java - 如何将对象的类型传递给类?

java - EnterState() 转换如何工作? (光滑2D)

java - SLICK && LWJGL 闪烁问题

java - LWJGL Circle 程序创建椭圆形形状

java - OpenGL 有纹理的四边形和无纹理的四边形

java - 比较Java中的扑克手牌

java - 是否有用于 IntelliJ IDEA 的 GWT UI 设计器?

opengl - 如何在 Matrix4f 中获取当前的 OpenGL 变换矩阵?

Java/SpotBugs,如果在接口(interface)中声明 "named static inner class",那么它是什么?

java.security.cert.CertificateException : Certificates does not conform to algorithm constraints