From d5ca42c83c1131c4c0a0983aa0555b9eaddab17c Mon Sep 17 00:00:00 2001 From: DaniTheSkunk Date: Sun, 18 Sep 2022 18:36:15 +0200 Subject: [PATCH] changed width,height in window creation to Vec2i, added texture atlas debugging, fixed texture atlas bug resizing too early --- Test.java | 3 ++- com/danitheskunk/skunkworks/Engine.java | 4 ++-- .../skunkworks/GLTextureAtlas.java | 12 ++++++++++-- com/danitheskunk/skunkworks/GLWindow.java | 19 +++++++++++++++---- com/danitheskunk/skunkworks/IWindow.java | 1 + 5 files changed, 30 insertions(+), 9 deletions(-) diff --git a/Test.java b/Test.java index aa3bb97..4376a3b 100644 --- a/Test.java +++ b/Test.java @@ -4,13 +4,14 @@ import org.w3c.dom.css.Rect; public class Test { public static void main(String[] args) { var engine = new Engine(); - var window = engine.openWindow(1280, 720, "Skunkworks"); + var window = engine.openWindow(new Vec2i(1280, 720), "Skunkworks"); var img2 = engine.loadImage("C:\\art\\pixel stuff.png"); var img = engine.loadImage("C:\\Users\\dani\\Videos\\Screenshot 2022-06-25 17-00-59.png"); var tex2 = window.loadTexture("C:\\art\\pixel stuff.png"); var tileset = window.loadTextureArray("C:\\stream\\coding\\rlc\\tilemap.png", new Vec2i(16, 16)); //img.drawImage(img2, Vec2i.ZERO); var tex = window.loadTexture(img); + window.setDebug(true); while(!window.shouldClose()) { window.tick(); diff --git a/com/danitheskunk/skunkworks/Engine.java b/com/danitheskunk/skunkworks/Engine.java index 4be01b5..4bf4fc1 100644 --- a/com/danitheskunk/skunkworks/Engine.java +++ b/com/danitheskunk/skunkworks/Engine.java @@ -38,9 +38,9 @@ public class Engine { return new Image(buf); } - public IWindow openWindow(int width, int height, String title) { + public IWindow openWindow(Vec2i size, String title) { return switch(graphicsBackend) { - case OPENGL -> new GLWindow(width, height, title, this); + case OPENGL -> new GLWindow(size, title, this); }; } } diff --git a/com/danitheskunk/skunkworks/GLTextureAtlas.java b/com/danitheskunk/skunkworks/GLTextureAtlas.java index 8ce6e12..b0cd16a 100644 --- a/com/danitheskunk/skunkworks/GLTextureAtlas.java +++ b/com/danitheskunk/skunkworks/GLTextureAtlas.java @@ -12,6 +12,7 @@ import java.util.List; import static org.lwjgl.opengl.GL11.*; class GLTextureAtlas { + private GLTexture atlasTexture; //for debugging private Image img; boolean shouldUpdate; private int textureID; @@ -21,7 +22,7 @@ class GLTextureAtlas { img = new Image(new Vec2i(32, 32)); textureID = glGenTextures(); textures = new ArrayList<>(); - shouldUpdate = false; + shouldUpdate = true; glBindTexture(GL_TEXTURE_2D, textureID); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); @@ -41,6 +42,7 @@ class GLTextureAtlas { void doubleAtlasSize() { img = new Image(Vec2i.mul(img.getSize(), 2)); System.out.printf("Resized atlas to %dx%d\n", img.getSize().getX(), img.getSize().getY()); + atlasTexture = new GLTexture(new Recti(Vec2i.ZERO, img.getSize()), img); } public void update() { @@ -62,6 +64,7 @@ class GLTextureAtlas { for(var tex : textures) { //texture larger than atlas? resize atlas and try again if(tex.img.getHeight() > img.getHeight() || tex.img.getWidth() > img.getWidth()) { + System.out.println("Texture too large"); doubleAtlasSize(); repack(); return; @@ -74,7 +77,8 @@ class GLTextureAtlas { height = tex.img.getHeight(); //not enough space for new row? resize atlas and try again - if(y + height > tex.img.getHeight()) { + if(y + height > img.getHeight()) { + System.out.println("Texture not enough space for new row"); doubleAtlasSize(); repack(); return; @@ -111,6 +115,10 @@ class GLTextureAtlas { ); } + public GLTexture getAtlasTexture() { + return atlasTexture; + } + public Vec2i getSize() { return img.getSize(); } diff --git a/com/danitheskunk/skunkworks/GLWindow.java b/com/danitheskunk/skunkworks/GLWindow.java index 1d070b5..62d5479 100644 --- a/com/danitheskunk/skunkworks/GLWindow.java +++ b/com/danitheskunk/skunkworks/GLWindow.java @@ -13,21 +13,24 @@ import static org.lwjgl.opengl.GL11.*; import static org.lwjgl.system.MemoryUtil.NULL; public class GLWindow implements IWindow { + private boolean debug; private Engine engine; private GLRenderContext renderContext; private boolean shouldClose; + private Vec2i size; private GLTextureAtlas textureAtlas; private long window; - public GLWindow(int width, int height, String title, Engine engine) { + public GLWindow(Vec2i size, String title, Engine engine) { GLFWErrorCallback.createPrint(System.err).set(); if(!glfwInit()) throw new IllegalStateException("Unable to initialize GLFW"); glfwDefaultWindowHints(); - + this.debug = false; this.engine = engine; + this.size = size; - window = glfwCreateWindow(width, height, title, NULL, NULL); + window = glfwCreateWindow(size.getX(), size.getY(), title, NULL, NULL); if(window == NULL) throw new RuntimeException("Failed to create GLFW window"); @@ -37,7 +40,7 @@ public class GLWindow implements IWindow { GL.createCapabilities(); glMatrixMode(GL_PROJECTION); glLoadIdentity(); - glOrtho(0.0f, width, height, 0.0f, 0.0f, 1.0f); + glOrtho(0.0f, size.getX(), size.getY(), 0.0f, 0.0f, 1.0f); glEnable(GL_COLOR); glEnable(GL_TEXTURE_2D); @@ -86,6 +89,9 @@ public class GLWindow implements IWindow { @Override public void renderFinish(IRenderContext context) { + if(debug) { + context.drawTextureRectangle(new Recti(Vec2i.ZERO, size), textureAtlas.getAtlasTexture()); + } glEnd(); glfwSwapBuffers(window); } @@ -99,6 +105,11 @@ public class GLWindow implements IWindow { return renderContext; } + @Override + public void setDebug(boolean on) { + this.debug = on; + } + @Override public boolean shouldClose() { return shouldClose; diff --git a/com/danitheskunk/skunkworks/IWindow.java b/com/danitheskunk/skunkworks/IWindow.java index 2d473ae..8da9faf 100644 --- a/com/danitheskunk/skunkworks/IWindow.java +++ b/com/danitheskunk/skunkworks/IWindow.java @@ -9,6 +9,7 @@ public interface IWindow { List loadTextureArray(String path, Vec2i tileSize); void renderFinish(IRenderContext context); IRenderContext renderStart(); + void setDebug(boolean on); boolean shouldClose(); void tick(); }