Texture.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package me.hammerle.snuviengine.api;
  2. import java.awt.image.BufferedImage;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.nio.IntBuffer;
  6. import javax.imageio.ImageIO;
  7. import org.lwjgl.BufferUtils;
  8. import static org.lwjgl.opengl.GL11.*;
  9. import static org.lwjgl.opengl.GL12.*;
  10. import static org.lwjgl.opengl.GL30.*;
  11. public class Texture
  12. {
  13. public class Animation
  14. {
  15. private IntBuffer[] data;
  16. private int[] wi;
  17. private int[] he;
  18. private final int offsetX;
  19. private final int offsetY;
  20. private int index = 0;
  21. private Animation(int offsetX, int offsetY, String... paths)
  22. {
  23. this.offsetX = offsetX;
  24. this.offsetY = offsetY;
  25. int l = paths.length;
  26. data = new IntBuffer[l];
  27. wi = new int[l];
  28. he = new int[l];
  29. for(int i = 0; i < l; i++)
  30. {
  31. BufferedImage image;
  32. try
  33. {
  34. image = ImageIO.read(new File(paths[i]));
  35. }
  36. catch(IOException | IllegalArgumentException ex)
  37. {
  38. throw new TextureException("cannot read texture file '" + paths[i] + "'");
  39. }
  40. if(offsetX + image.getWidth() > w || offsetY + image.getHeight() > h)
  41. {
  42. System.out.println(offsetX + " " + image.getWidth() + " " + w + " " + offsetY + " " + image.getHeight() + " " + h);
  43. throw new TextureException("animation image '" + paths[i] + "'is out of range");
  44. }
  45. wi[i] = image.getWidth();
  46. he[i] = image.getHeight();
  47. IntBuffer buffer = BufferUtils.createIntBuffer(wi[i] * he[i]);
  48. for(int y = 0; y < he[i]; y++)
  49. {
  50. for(int x = 0; x < wi[i]; x++)
  51. {
  52. int c = image.getRGB(x, y);
  53. buffer.put((c << 8) | ((c >> 24) & 0xFF));
  54. }
  55. }
  56. buffer.flip();
  57. data[i] = buffer;
  58. }
  59. }
  60. public void nextFrame()
  61. {
  62. GLHelper.glBindTexture(texture);
  63. glTexSubImage2D(GL_TEXTURE_2D, 0, offsetX, offsetY, wi[index], he[index], GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, data[index]);
  64. index = (index + 1) % data.length;
  65. }
  66. }
  67. private int texture = -1;
  68. private final String path;
  69. private final int w;
  70. private final int h;
  71. private IntBuffer buffer;
  72. public Texture(String path)
  73. {
  74. this.path = path;
  75. BufferedImage image;
  76. try
  77. {
  78. image = ImageIO.read(new File(path));
  79. }
  80. catch(IOException | IllegalArgumentException ex)
  81. {
  82. throw new TextureException("cannot read texture file '" + path + "'");
  83. }
  84. w = image.getWidth();
  85. h = image.getHeight();
  86. buffer = BufferUtils.createIntBuffer(w * h);
  87. for(int y = 0; y < h; y++)
  88. {
  89. for(int x = 0; x < w; x++)
  90. {
  91. int c = image.getRGB(x, y);
  92. buffer.put((c << 8) | ((c >> 24) & 0xFF));
  93. }
  94. }
  95. buffer.flip();
  96. Shader.addTask(() -> loadTexture());
  97. }
  98. public Animation addAnimation(int offsetX, int offsetY, String... paths)
  99. {
  100. return new Animation(offsetX, offsetY, paths);
  101. }
  102. private void loadTexture()
  103. {
  104. if(texture != -1)
  105. {
  106. throw new TextureException("a texture is already loaded");
  107. }
  108. texture = glGenTextures();
  109. GLHelper.glBindTexture(texture);
  110. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  111. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  112. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  113. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  114. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, buffer);
  115. buffer = null;
  116. glGenerateMipmap(GL_TEXTURE_2D);
  117. }
  118. public void bindTexture()
  119. {
  120. if(texture == -1)
  121. {
  122. throw new TextureException("cannot load non loaded texture");
  123. }
  124. GLHelper.glBindTexture(texture);
  125. }
  126. }