defer texture atlas update to start of frame

This commit is contained in:
DaniTheSkunk 2022-09-18 17:15:37 +02:00
parent 1316ee560b
commit 2448143454
2 changed files with 12 additions and 3 deletions

View File

@ -13,6 +13,7 @@ import static org.lwjgl.opengl.GL11.*;
class GLTextureAtlas {
private Image img;
boolean shouldUpdate;
private int textureID;
private List<GLTexture> 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;

View File

@ -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);