Skip to content
This repository has been archived by the owner on May 31, 2024. It is now read-only.

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurLumertz committed Feb 8, 2024
1 parent eb7779d commit 95d22ea
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
31 changes: 20 additions & 11 deletions src/com/arthur/taplixic/Spritesheet.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,45 @@

import static org.lwjgl.opengl.GL11.*;

import java.awt.image.*;
import java.nio.*;

import javax.imageio.*;

import org.lwjgl.*;
import org.lwjgl.stb.*;

public class Spritesheet {
private int id, width, height, channels;

public Spritesheet(String filepath) {
BufferedImage bi;
try {
IntBuffer wx = BufferUtils.createIntBuffer(1);
IntBuffer hx = BufferUtils.createIntBuffer(1);
IntBuffer cx = BufferUtils.createIntBuffer(1);
bi = ImageIO.read(getClass().getResourceAsStream(filepath));
width = bi.getWidth();
height = bi.getWidth();

ByteBuffer buffer = STBImage.stbi_load("./res/spritesheet.png", wx, hx, cx, 4);
int[] pixels = new int[width * height * 4];
bi.getRGB(0, 0, width, height, pixels, 0, width);

this.width = wx.get(0);
this.height = hx.get(0);
this.channels = cx.get(0);
ByteBuffer buffer = BufferUtils.createByteBuffer(width * height * 4);
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
int pixel = pixels[x * width + y];
buffer.put((byte)((pixel >> 16) & 0xFF));
buffer.put((byte)((pixel >> 8) & 0xFF));
buffer.put((byte)(pixel & 0xFF));
buffer.put((byte)((pixel >> 24) & 0xFF));
}
}

// buffer.flip();
buffer.flip();

id = glGenTextures();
glBindTexture(GL_TEXTURE_2D, id);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

STBImage.stbi_image_free(buffer);

} catch (Exception e) {
e.printStackTrace();
}
Expand Down
2 changes: 1 addition & 1 deletion src/com/arthur/taplixic/Taplixic.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private void init() {

this.renderer = new Renderer();

this.spritesheet = new Spritesheet("./src/spritesheet.png");
this.spritesheet = new Spritesheet("/spritesheet.png");

this.items = new Items(this);

Expand Down

0 comments on commit 95d22ea

Please sign in to comment.