Guest User

Untitled

a guest
Jun 24th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. import java.awt.image.BufferedImage;
  2. import java.io.IOException;
  3.  
  4. import javax.imageio.ImageIO;
  5.  
  6. import tilegame.debug.Debug;
  7. /**
  8. * This class is responsible for loading in images
  9. * @author Kenneth
  10. *
  11. */
  12. public class ImageLoader {
  13. /**
  14. * This method tries to load in the selected image from the path given.
  15. * @param path
  16. * @return
  17. */
  18. public static BufferedImage loadImage(String path){
  19. try {
  20. return ImageIO.read(ImageLoader.class.getResource(path)); //Loads in image
  21. } catch (IOException e) {
  22. e.printStackTrace();
  23. System.exit(1); //If the image cannot be loaded, the window closes
  24. Debug.LogError(path + " was not loaded.");
  25. }
  26. return null;
  27. }
  28.  
  29. }
  30.  
  31. import java.awt.image.BufferedImage;
  32. /**
  33. * This class is responsible for splitting up sprite sheets into multiple images.
  34. * @author Kenneth
  35. *
  36. */
  37. public class SpriteSheet {
  38.  
  39. private BufferedImage sheet;
  40. /**
  41. * This constructor receives the image that needs to be modified.
  42. * @param sheet
  43. */
  44. public SpriteSheet(BufferedImage sheet){
  45. this.sheet = sheet;
  46. }
  47. /**
  48. * This crops a sprite sheet to get the subimage within the picture.
  49. * @param x
  50. * @param y
  51. * @param width
  52. * @param height
  53. * @return
  54. */
  55. public BufferedImage crop(int x, int y, int width, int height){
  56. return sheet.getSubimage(x*width, y*height, width, height);
  57. }
  58. }
  59.  
  60. public void drawImage(Texture texture, int x, int y, int width, int height) {
  61. texture.bind();
  62. glTranslatef((float) x, (float) y, 0);
  63. glBegin(GL_QUADS);
  64. glTexCoord2f(0, 0);
  65. glVertex2f(0, 0);
  66. glTexCoord2f(1, 0);
  67. glVertex2f(width, 0);
  68. glTexCoord2f(1, 1);
  69. glVertex2f(width, height);
  70. glTexCoord2f(0, 1);
  71. glVertex2f(0, height);
  72. glEnd();
  73. glLoadIdentity();
  74. }
Add Comment
Please, Sign In to add comment