made image loading work

This commit is contained in:
Dani The Skunk 2022-09-14 06:49:58 +02:00
parent 46a94c2b92
commit 0116082508
4 changed files with 16 additions and 3 deletions

View File

@ -8,7 +8,8 @@ public class Test {
var engine = new Engine(); var engine = new Engine();
var window = engine.openWindow(1280, 720, "Skunkworks"); var window = engine.openWindow(1280, 720, "Skunkworks");
var img = engine.loadImage("C:\\Users\\dani\\Videos\\Screenshot 2022-06-25 17-00-59.png"); 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))); //var img = engine.loadImage("C:\\art\\kyoko.png");
System.out.println(img.getPixel(new Vec2i(60, 60)));
while(!window.shouldClose()) { while(!window.shouldClose()) {
window.tick(); window.tick();

View File

@ -32,4 +32,8 @@ public final class Color {
public byte getA() { public byte getA() {
return a; return a;
} }
@Override public String toString() {
return String.format("#%02X%02X%02X%02X", r, g, b, a);
}
} }

View File

@ -1,5 +1,7 @@
package com.danitheskunk.skunkworks; package com.danitheskunk.skunkworks;
import org.lwjgl.BufferUtils;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
@ -10,13 +12,17 @@ import static com.danitheskunk.skunkworks.GraphicsBackend.*;
public class Engine { public class Engine {
public Image loadImage(String path) { public Image loadImage(String path) {
ByteBuffer buf; ByteBuffer buf;
byte[] bytes;
try(FileInputStream fis = new FileInputStream(path)) { try(FileInputStream fis = new FileInputStream(path)) {
buf = ByteBuffer.wrap(fis.readAllBytes()); bytes = fis.readAllBytes();
} catch(FileNotFoundException e) { } catch(FileNotFoundException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} catch(IOException e) { } catch(IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
buf = BufferUtils.createByteBuffer(bytes.length);
buf.put(bytes);
buf.flip();
return new Image(buf); return new Image(buf);
} }

View File

@ -10,9 +10,11 @@ public class Image {
Image(ByteBuffer buffer) { Image(ByteBuffer buffer) {
//todo: resorce system //todo: resorce system
int[] x = {0}, y = {0}, n = {0}; int[] x = {0}, y = {0}, n = {0};
data = STBImage.stbi_load(buffer, x, y, n, 4).array(); var img = STBImage.stbi_load_from_memory(buffer, x, y, n, 4);
width = x[0]; width = x[0];
height = y[0]; height = y[0];
data = new byte[width * height * 4];
img.get(data);
} }
public byte[] getData() { public byte[] getData() {