【问题标题】:Crop a texture down to a smaller texture using slick and lwjgl (JAVA)使用 slick 和 lwjgl (JAVA) 将纹理裁剪为更小的纹理
【发布时间】:2018-10-21 16:36:15
【问题描述】:

下面我写了一个方法来绘制它的参数中给出的纹理。

    public void drawImage(Texture texture, int x, int y, int width, int height) {
    texture.bind();
    glTranslatef(x, y, 0);
    glBegin(GL_QUADS);
    glTexCoord2f(0, 0);
    glVertex2f(0, 0);
    glTexCoord2f(1, 0);
    glVertex2f(width, 0);
    glTexCoord2f(1, 1);
    glVertex2f(width, height);
    glTexCoord2f(0, 1);
    glVertex2f(0, height);
    glEnd();
    glLoadIdentity();
}

我不想改变这个方法。相反,我想做的是创建一种方法,该方法可以裁剪任意大小的精灵表,并且我可能需要将任意数量的裁剪保存为单独的纹理。

以前,我使用的是 Java.AWT,它允许我做到这一点。请参阅下面的代码。注意:sheet 是 BufferedImage 类型的。

public BufferedImage crop(int x, int y, int width, int height){
    return sheet.getSubimage(x*width, y*height, width, height);
}    

我正在尝试为上述方法找到等效代码,该代码可以采用任意大小的纹理,裁剪任意区域并将该区域作为自己的纹理返回。

我并不想更改 drawImage 方法,因为我需要该方法的原样。

谢谢

【问题讨论】:

    标签: java lwjgl crop slick


    【解决方案1】:

    事实证明,slick 中有一个名为 BufferedImageUtil 的内置函数,可将缓冲图像转换为纹理。这是一个简单的答案,而不是像在 BufferedImage 中那样尝试制作获得 subImage 的东西,更聪明的方法是将 BufferedImage 转换为我已经尝试了大约 3 个月的纹理。

    对于任何为这种转换而苦苦挣扎的人,只需使用下面的代码即可。

        SpriteSheet Tiles = new SpriteSheet(ImageLoader.loadImage("/res/Tiles.png"));
        BufferedImage test = Tiles.crop(0, 0, 64, 64);
        Texture test2 = null;
        try {
            test2 = BufferedImageUtil.getTexture("", test);                             
        } catch (IOException e) {
            e.printStackTrace();
        }
        g.drawImage(test2, 0, 0, 64, 64);
    

    SpriteSheet 类:

    import java.awt.image.BufferedImage;
    /**
     * This Class is responsible for splitting up sprite sheets into multiple images.
     * @author Kenneth
     *
     */
    public class SpriteSheet {
    
    private BufferedImage sheet;
    /**
     * This constructor receives the image that needs to be modified.
     * @param sheet
     */
    public SpriteSheet(BufferedImage sheet){
        this.sheet = sheet;
    }
    /**
     * This crops a sprite sheet to get the subimage within the picture.
     * @param x
     * @param y
     * @param width
     * @param height
     * @return
     */
    public BufferedImage crop(int x, int y, int width, int height){
        return sheet.getSubimage(x*width, y*height, width, height);
    }
    }
    

    图像加载器:

    import java.awt.image.BufferedImage;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    /**
     * This class is responsible for loading in images
     * @author Kenneth
     *
     */
    public class ImageLoader {
    /**
     * This method tries to load in the selected image from the path given.
     * @param path
     * @return
     */
    public static BufferedImage loadImage(String path){
        try {
            return ImageIO.read(ImageLoader.class.getResource(path)); //Loads in image
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1); //If the image cannot be loaded, the window closes
            System.err.println(path + " was not loaded.");
        }
        return null;
    }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多