started adding Image8

This commit is contained in:
DaniTheSkunk 2022-12-19 00:53:33 +00:00
parent 4747abe678
commit f93f35dae3
2 changed files with 91 additions and 1 deletions

View File

@ -3,6 +3,7 @@ package com.danitheskunk.skunkworks;
import com.danitheskunk.skunkworks.backends.gl.Window; import com.danitheskunk.skunkworks.backends.gl.Window;
import com.danitheskunk.skunkworks.gfx.GraphicsBackend; import com.danitheskunk.skunkworks.gfx.GraphicsBackend;
import com.danitheskunk.skunkworks.gfx.Image32; import com.danitheskunk.skunkworks.gfx.Image32;
import com.danitheskunk.skunkworks.gfx.Image8;
import org.lwjgl.BufferUtils; import org.lwjgl.BufferUtils;
import java.io.FileInputStream; import java.io.FileInputStream;
@ -65,10 +66,14 @@ public class Engine {
return data; return data;
} }
public Image32 loadImage(String path) { public Image32 loadImage32(String path) {
return new Image32(loadBytes(path)); return new Image32(loadBytes(path));
} }
public Image8 loadImage8(String path) {
return new Image8(loadBytes(path));
}
public IWindow openWindow(Vec2i size, String title) { public IWindow openWindow(Vec2i size, String title) {
return switch(graphicsBackend) { return switch(graphicsBackend) {
case OPENGL -> new Window(size, title, this); case OPENGL -> new Window(size, title, this);

View File

@ -0,0 +1,85 @@
package com.danitheskunk.skunkworks.gfx;
import com.danitheskunk.skunkworks.Recti;
import com.danitheskunk.skunkworks.Vec2i;
import org.lwjgl.stb.STBImage;
import java.nio.ByteBuffer;
public class Image8 {
private final byte[] data;
private final Vec2i size;
//constructors
public Image8(ByteBuffer buffer) { //png or similar
//todo: resource system
int[] x = {0}, y = {0}, n = {0};
var img = STBImage.stbi_load_from_memory(buffer, x, y, n, 1);
size = new Vec2i(x[0], y[0]);
data = new byte[x[0] * y[0]];
assert img != null;
img.get(data);
}
public Image8(Vec2i size) {
this.size = size;
data = new byte[size.getX() * size.getY()];
}
public Image8(ByteBuffer buffer, Vec2i size) {
data = new byte[buffer.remaining()];
buffer.get(data);
this.size = size;
}
public void drawImage(Image8 srcImage, Vec2i destPos) {
drawImage(srcImage, destPos, new Recti(Vec2i.ZERO,
srcImage.getSize()));
}
public void drawImage(Image8 srcImage, Vec2i destPos, Recti srcRect) {
//todo: check bounds
//todo: write in faster way than pixel stuff
for(int y = 0; y < srcRect.getHeight(); ++y) {
for(int x = 0; x < srcRect.getWidth(); ++x) {
var iterPos = new Vec2i(x, y);
var to = Vec2i.add(destPos, iterPos);
var from = Vec2i.add(srcRect.getPos(), iterPos);
setPixel(to, srcImage.getPixel(from));
}
}
}
//getters
public byte[] getData() {
return data;
}
public int getHeight() {
return size.getY();
}
public int getPixel(Vec2i pos) {
int i = pos.getX() + pos.getY() * size.getX();
return (int)data[i] & 0xFF; //todo: check if that works > 127
}
public Vec2i getSize() {
return size;
}
public Image8 getSubImage(Recti rect) {
Image8 img = new Image8(rect.getSize());
img.drawImage(this, Vec2i.ZERO, rect);
return img;
}
public int getWidth() {
return size.getX();
}
public void setPixel(Vec2i pos, int col) {
int i = pos.getX() + pos.getY() * size.getX();
data[i] = (byte) (col & 0xFF);
}
}