SHOW:
|
|
- or go back to the newest paste.
1 | import java.util.LinkedList; | |
2 | import java.awt.image.BufferedImage; | |
3 | import javax.imageio.ImageIO; | |
4 | import java.net.URL; | |
5 | import java.awt.image.RasterFormatException; | |
6 | ||
7 | public class SpriteSheet | |
8 | { | |
9 | // What we're going to store the images in | |
10 | private BufferedImage[] images; | |
11 | ||
12 | // Holds the location of our sprites | |
13 | private URL loc; | |
14 | ||
15 | // The image we'll grab our sprites from | |
16 | private BufferedImage sprites; | |
17 | ||
18 | // Tile size of each image | |
19 | private int tileSizeX; | |
20 | private int tileSizeY; | |
21 | ||
22 | // Tiles in the image | |
23 | private int xTiles; | |
24 | private int yTiles; | |
25 | ||
26 | SpriteSheet(String sourceLoc, int tX, int tY) | |
27 | { | |
28 | try | |
29 | { | |
30 | // Find where our resources are stored | |
31 | loc = this.getClass().getResource(sourceLoc); | |
32 | ||
33 | }catch(Exception e) | |
34 | { | |
35 | // Some error handling | |
36 | System.out.println(sourceLoc + " is missing."); | |
37 | e.printStackTrace(); | |
38 | } | |
39 | try | |
40 | { | |
41 | // Get our resources from the URL | |
42 | sprites = ImageIO.read(loc); | |
43 | ||
44 | }catch(Exception e) | |
45 | { | |
46 | // Some error handling | |
47 | System.out.println("Image missing or corrupted."); | |
48 | e.printStackTrace(); | |
49 | } | |
50 | ||
51 | // Amount of tiles in the x and y directions | |
52 | xTiles = tX; | |
53 | yTiles = tY; | |
54 | ||
55 | // Array stores the images after they're split up | |
56 | // The size is the amount of x tiles and y tiles (area) | |
57 | images = new BufferedImage[xTiles * yTiles]; | |
58 | ||
59 | // Figure out the size of the tiles | |
60 | tileSizeX = sprites.getWidth()/xTiles; | |
61 | tileSizeY = sprites.getHeight()/yTiles; | |
62 | ||
63 | // Do the actual splitting of the image into the array | |
64 | loadSprites(); | |
65 | } | |
66 | ||
67 | // Gets the instance of the spritesheet | |
68 | public BufferedImage[] getSpritesheet() { return images;} | |
69 | ||
70 | private void loadSprites() | |
71 | { | |
72 | BufferedImage temp; | |
73 | ||
74 | - | for(int x = 0, y = 0, i = 0; x < xTiles; x++) |
74 | + | for(int x = 0, y = 0, i = 0; ; x += tileSizeX) |
75 | { | |
76 | temp = null; | |
77 | ||
78 | - | System.out.println((sprites.getWidth() - (x * tileSizeX)) + ", " + (sprites.getHeight() - (y * tileSizeY))); |
78 | + | System.out.println((sprites.getWidth() - x) + ", " + (sprites.getHeight() - y )); |
79 | ||
80 | - | /* |
80 | + | |
81 | - | * |
81 | + | |
82 | - | * WHY DON'T YOU WORK GOD DAMN IT |
82 | + | temp = sprites.getSubimage(x, y, x + tileSizeX, y + tileSizeY); |
83 | - | * |
83 | + | |
84 | - | */ |
84 | + | |
85 | { | |
86 | System.out.println("Tile size is wrong, out of bounds."); | |
87 | } | |
88 | - | temp = sprites.getSubimage(x * tileSizeX, y * tileSizeY, (x * tileSizeX) + tileSizeX, (y * tileSizeY) + tileSizeY); |
88 | + | |
89 | // Add the image to the array | |
90 | images[i] = temp; | |
91 | ||
92 | // Should stop the loop from getting outside the raster | |
93 | if( i + 1 == images.length) | |
94 | { | |
95 | break; | |
96 | } | |
97 | ||
98 | // Makes the loop jump to the next row to load new images | |
99 | if(i == xTiles) | |
100 | { | |
101 | y += tileSizeY; | |
102 | x = 0; | |
103 | } | |
104 | ||
105 | i++; | |
106 | } | |
107 | - | y ++; |
107 | + | |
108 | } |