added image functions

This commit is contained in:
DaniTheSkunk 2022-09-14 06:16:20 +02:00 committed by Dani The Skunk
parent 4583d24c86
commit 46a94c2b92
3 changed files with 36 additions and 10 deletions

View File

@ -1,10 +1,14 @@
import com.danitheskunk.skunkworks.Engine;
import com.danitheskunk.skunkworks.GLWindow;
import com.danitheskunk.skunkworks.Vec2f;
import com.danitheskunk.skunkworks.Vec2i;
public class Test {
public static void main(String[] args) {
var engine = new Engine();
var window = engine.openWindow(1280, 720, "Skunkworks");
var img = engine.loadImage("C:\\Users\\dani\\Videos\\Screenshot 2022-06-25 17-00-59.png");
System.out.println(img.getPixel(new Vec2i(0, 0)));
while(!window.shouldClose()) {
window.tick();

View File

@ -1,35 +1,35 @@
package com.danitheskunk.skunkworks;
public final class Color {
final int r, g, b, a;
final byte r, g, b, a;
public Color(int r, int g, int b) {
public Color(byte r, byte g, byte b) {
this.r = r;
this.g = g;
this.b = b;
this.a = 0xff;
this.a = (byte)0xff;
}
public Color(int r, int g, int b, int a) {
public Color(byte r, byte g, byte b, byte a) {
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}
public int getR() {
public byte getR() {
return r;
}
public int getG() {
public byte getG() {
return g;
}
public int getB() {
public byte getB() {
return b;
}
public int getA() {
public byte getA() {
return a;
}
}

View File

@ -5,13 +5,35 @@ import org.lwjgl.stb.STBImage;
import java.nio.ByteBuffer;
public class Image {
ByteBuffer data;
byte[] data;
int width, height;
Image(ByteBuffer buffer) {
//todo: resorce system
int[] x = {0}, y = {0}, n = {0};
data = STBImage.stbi_load(buffer, x, y, n, 4);
data = STBImage.stbi_load(buffer, x, y, n, 4).array();
width = x[0];
height = y[0];
}
public byte[] getData() {
return data;
}
public Color getPixel(Vec2i pos) {
int i = pos.getX() * 4 + pos.getY() * 4 * width;
return new Color(
data[i + 0],
data[i + 1],
data[i + 2],
data[i + 3]
);
}
public void setPixel(Vec2i pos, Color col) {
int i = pos.getX() * 4 + pos.getY() * 4 * width;
data[i + 0] = col.r;
data[i + 1] = col.g;
data[i + 2] = col.b;
data[i + 3] = col.a;
}
}