Advertisement
Guest User

Ep 86 spritesheet class

a guest
Oct 24th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.91 KB | None | 0 0
  1. package com.solargames.silvenica.graphics;
  2.  
  3. import java.awt.image.BufferedImage;
  4. import java.io.IOException;
  5.  
  6. import javax.imageio.ImageIO;
  7.  
  8. public class SpriteSheet {
  9.    
  10.     private String path;
  11.     public final int SIZE;
  12.     public final int WIDTH, HEIGHT;
  13.     public int[] pixels;
  14.    
  15.     public static SpriteSheet tiles = new SpriteSheet("/textures/spritesheet.png", 256);
  16.     public static SpriteSheet mage_projectile_fire = new SpriteSheet("/textures/projectiles/mageprojectiles.png", 48);
  17. //  public static SpriteSheet mage_fire = new SpriteSheet("/textures/mage.png", 48);
  18.    
  19.     public static SpriteSheet player = new SpriteSheet("/textures/mage.png", 48, 48);
  20.     public static SpriteSheet player_down = new SpriteSheet(player, 0, 0, 1, 3, 16); //problem occurs here
  21.    
  22.     public SpriteSheet(SpriteSheet sheet, int x, int y, int width, int height, int spriteSize) {
  23.         int xx = x * spriteSize;
  24.         int yy = y * spriteSize;
  25.         int w = width * spriteSize;
  26.         int h = height * spriteSize;
  27.         if (width == height) SIZE = width;
  28.         else SIZE = -1;
  29.         WIDTH = w;
  30.         HEIGHT = h;
  31.         pixels = new int[w * h];
  32.         for (int y0 = 0; y0 < h; y++) {
  33.             int yp = yy + y0;
  34.             for (int x0 = 0; x0 < w; x++) {
  35.                 int xp = xx + x0;
  36.                 pixels[x0 + y0 * w] = sheet.pixels[xp + yp * sheet.WIDTH];
  37.             }
  38.         }
  39.         load();
  40.     }
  41.    
  42.     public SpriteSheet(String path, int size) {
  43.         this.path = path;
  44.         SIZE = size;
  45.         WIDTH = size;
  46.         HEIGHT = size;
  47.         pixels = new int[SIZE * SIZE];
  48.         load();
  49.     }
  50.    
  51.    
  52.     public SpriteSheet(String path, int width, int height) {
  53.         this.path = path;
  54.         SIZE = -1;
  55.         WIDTH = width;
  56.         HEIGHT = height;
  57.         pixels = new int[WIDTH * HEIGHT];
  58.         load();
  59.     }
  60.    
  61.     private void load() {
  62.         try {
  63.             BufferedImage image = ImageIO.read(SpriteSheet.class.getResource(path));
  64.             int w = image.getWidth();
  65.             int h = image.getHeight();
  66.             image.getRGB(0, 0, w, h, pixels, 0, w);
  67.         }catch (IOException e) {
  68.             e.printStackTrace();
  69.         }
  70.     }
  71.    
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement