下面是我的缓冲图像精灵表类。我有一个从某个位置加载图像,另一个有将图像切成一堆较小的图像供我的游戏引擎使用。最近,我决定将所有内容从Java.awt移至LWJGL和Slack,因为我想更多地使用图形卡并减少CPU正在努力承受的负载。
我当前遇到的问题是,除了对LWJGL和Slack非常陌生之外,似乎没有关于从Spritesheet加载的教程,只有几本关于分别加载每个图像的教程,这会花费我很多钱时间,并让以后的任何游戏成为负担。
我希望有人能看到这篇文章,并拥有LWGJL中的代码,就像我在Java.awt中所做的那样,可以对它进行裁剪和图像裁剪,或者我自己知道如何做到。从那以后,关于屏幕渲染的所有事情我都可以从在线教程中找到。我还发布了该代码,以防万一我只需要对其稍加修改。
图片加载
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import tilegame.debug.Debug;
/**
* 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
Debug.LogError(path + " was not loaded.");
}
return null;
}
}
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);
}
}
用于在屏幕上绘制图像的代码(以防更容易修改)
public void drawImage(Texture texture, int x, int y, int width, int height) {
texture.bind();
glTranslatef((float) x, (float) 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();
}