From 24481434542c275e575ca5f951ef0110b8643ba7 Mon Sep 17 00:00:00 2001 From: DaniTheSkunk Date: Sun, 18 Sep 2022 17:15:37 +0200 Subject: [PATCH] defer texture atlas update to start of frame --- com/danitheskunk/skunkworks/GLTextureAtlas.java | 14 +++++++++++--- com/danitheskunk/skunkworks/GLWindow.java | 1 + 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/com/danitheskunk/skunkworks/GLTextureAtlas.java b/com/danitheskunk/skunkworks/GLTextureAtlas.java index 3151743..8ce6e12 100644 --- a/com/danitheskunk/skunkworks/GLTextureAtlas.java +++ b/com/danitheskunk/skunkworks/GLTextureAtlas.java @@ -13,6 +13,7 @@ import static org.lwjgl.opengl.GL11.*; class GLTextureAtlas { private Image img; + boolean shouldUpdate; private int textureID; private List textures; @@ -20,6 +21,7 @@ class GLTextureAtlas { img = new Image(new Vec2i(32, 32)); textureID = glGenTextures(); textures = new ArrayList<>(); + shouldUpdate = false; glBindTexture(GL_TEXTURE_2D, textureID); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); @@ -32,9 +34,7 @@ class GLTextureAtlas { //this.img = img; var texture = new GLTexture(new Recti(new Vec2i(0, 0), img.getSize()), img); textures.add(texture); - repack(); - updateImage(); - uploadToGpu(); + shouldUpdate = true; return texture; } @@ -43,6 +43,14 @@ class GLTextureAtlas { System.out.printf("Resized atlas to %dx%d\n", img.getSize().getX(), img.getSize().getY()); } + public void update() { + if(shouldUpdate) { + repack(); + updateImage(); + uploadToGpu(); + shouldUpdate = false; + } + } void repack() { int x = 0; int y = 0; diff --git a/com/danitheskunk/skunkworks/GLWindow.java b/com/danitheskunk/skunkworks/GLWindow.java index c38ed57..1d070b5 100644 --- a/com/danitheskunk/skunkworks/GLWindow.java +++ b/com/danitheskunk/skunkworks/GLWindow.java @@ -92,6 +92,7 @@ public class GLWindow implements IWindow { @Override public IRenderContext renderStart() { + textureAtlas.update(); glClearColor(0.1f, 0.2f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBegin(GL_TRIANGLES);