-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpriteSheet.java
39 lines (33 loc) · 986 Bytes
/
SpriteSheet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package uet.oop.bomberman.graphics;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
/**
* Tất cả sprite (hình ảnh game) được lưu trữ vào một ảnh duy nhất
* Class này giúp lấy ra các sprite riêng từ 1 ảnh chung duy nhất đó
*/
public class SpriteSheet {
private String _path;
public final int SIZE;
public int[] _pixels;
public static SpriteSheet tiles = new SpriteSheet("/textures/classic.png", 256);
public SpriteSheet(String path, int size) {
_path = path;
SIZE = size;
_pixels = new int[SIZE * SIZE];
load();
}
private void load() {
try {
URL a = SpriteSheet.class.getResource(_path);
BufferedImage image = ImageIO.read(a);
int w = image.getWidth();
int h = image.getHeight();
image.getRGB(0, 0, w, h, _pixels, 0, w);
} catch (IOException e) {
e.printStackTrace();
System.exit(0);
}
}
}