changed width,height in window creation to Vec2i, added texture atlas debugging, fixed texture atlas bug resizing too early

This commit is contained in:
DaniTheSkunk 2022-09-18 18:36:15 +02:00
parent 2448143454
commit d5ca42c83c
5 changed files with 30 additions and 9 deletions

View File

@ -4,13 +4,14 @@ import org.w3c.dom.css.Rect;
public class Test { public class Test {
public static void main(String[] args) { public static void main(String[] args) {
var engine = new Engine(); 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 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 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 tex2 = window.loadTexture("C:\\art\\pixel stuff.png");
var tileset = window.loadTextureArray("C:\\stream\\coding\\rlc\\tilemap.png", new Vec2i(16, 16)); var tileset = window.loadTextureArray("C:\\stream\\coding\\rlc\\tilemap.png", new Vec2i(16, 16));
//img.drawImage(img2, Vec2i.ZERO); //img.drawImage(img2, Vec2i.ZERO);
var tex = window.loadTexture(img); var tex = window.loadTexture(img);
window.setDebug(true);
while(!window.shouldClose()) { while(!window.shouldClose()) {
window.tick(); window.tick();

View File

@ -38,9 +38,9 @@ public class Engine {
return new Image(buf); return new Image(buf);
} }
public IWindow openWindow(int width, int height, String title) { public IWindow openWindow(Vec2i size, String title) {
return switch(graphicsBackend) { return switch(graphicsBackend) {
case OPENGL -> new GLWindow(width, height, title, this); case OPENGL -> new GLWindow(size, title, this);
}; };
} }
} }

View File

@ -12,6 +12,7 @@ import java.util.List;
import static org.lwjgl.opengl.GL11.*; import static org.lwjgl.opengl.GL11.*;
class GLTextureAtlas { class GLTextureAtlas {
private GLTexture atlasTexture; //for debugging
private Image img; private Image img;
boolean shouldUpdate; boolean shouldUpdate;
private int textureID; private int textureID;
@ -21,7 +22,7 @@ class GLTextureAtlas {
img = new Image(new Vec2i(32, 32)); img = new Image(new Vec2i(32, 32));
textureID = glGenTextures(); textureID = glGenTextures();
textures = new ArrayList<>(); textures = new ArrayList<>();
shouldUpdate = false; shouldUpdate = true;
glBindTexture(GL_TEXTURE_2D, textureID); glBindTexture(GL_TEXTURE_2D, textureID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
@ -41,6 +42,7 @@ class GLTextureAtlas {
void doubleAtlasSize() { void doubleAtlasSize() {
img = new Image(Vec2i.mul(img.getSize(), 2)); img = new Image(Vec2i.mul(img.getSize(), 2));
System.out.printf("Resized atlas to %dx%d\n", img.getSize().getX(), img.getSize().getY()); 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() { public void update() {
@ -62,6 +64,7 @@ class GLTextureAtlas {
for(var tex : textures) { for(var tex : textures) {
//texture larger than atlas? resize atlas and try again //texture larger than atlas? resize atlas and try again
if(tex.img.getHeight() > img.getHeight() || tex.img.getWidth() > img.getWidth()) { if(tex.img.getHeight() > img.getHeight() || tex.img.getWidth() > img.getWidth()) {
System.out.println("Texture too large");
doubleAtlasSize(); doubleAtlasSize();
repack(); repack();
return; return;
@ -74,7 +77,8 @@ class GLTextureAtlas {
height = tex.img.getHeight(); height = tex.img.getHeight();
//not enough space for new row? resize atlas and try again //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(); doubleAtlasSize();
repack(); repack();
return; return;
@ -111,6 +115,10 @@ class GLTextureAtlas {
); );
} }
public GLTexture getAtlasTexture() {
return atlasTexture;
}
public Vec2i getSize() { public Vec2i getSize() {
return img.getSize(); return img.getSize();
} }

View File

@ -13,21 +13,24 @@ import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryUtil.NULL; import static org.lwjgl.system.MemoryUtil.NULL;
public class GLWindow implements IWindow { public class GLWindow implements IWindow {
private boolean debug;
private Engine engine; private Engine engine;
private GLRenderContext renderContext; private GLRenderContext renderContext;
private boolean shouldClose; private boolean shouldClose;
private Vec2i size;
private GLTextureAtlas textureAtlas; private GLTextureAtlas textureAtlas;
private long window; 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(); GLFWErrorCallback.createPrint(System.err).set();
if(!glfwInit()) throw new IllegalStateException("Unable to initialize GLFW"); if(!glfwInit()) throw new IllegalStateException("Unable to initialize GLFW");
glfwDefaultWindowHints(); glfwDefaultWindowHints();
this.debug = false;
this.engine = engine; 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"); if(window == NULL) throw new RuntimeException("Failed to create GLFW window");
@ -37,7 +40,7 @@ public class GLWindow implements IWindow {
GL.createCapabilities(); GL.createCapabilities();
glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);
glLoadIdentity(); 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_COLOR);
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
@ -86,6 +89,9 @@ public class GLWindow implements IWindow {
@Override @Override
public void renderFinish(IRenderContext context) { public void renderFinish(IRenderContext context) {
if(debug) {
context.drawTextureRectangle(new Recti(Vec2i.ZERO, size), textureAtlas.getAtlasTexture());
}
glEnd(); glEnd();
glfwSwapBuffers(window); glfwSwapBuffers(window);
} }
@ -99,6 +105,11 @@ public class GLWindow implements IWindow {
return renderContext; return renderContext;
} }
@Override
public void setDebug(boolean on) {
this.debug = on;
}
@Override @Override
public boolean shouldClose() { public boolean shouldClose() {
return shouldClose; return shouldClose;

View File

@ -9,6 +9,7 @@ public interface IWindow {
List<ITexture> loadTextureArray(String path, Vec2i tileSize); List<ITexture> loadTextureArray(String path, Vec2i tileSize);
void renderFinish(IRenderContext context); void renderFinish(IRenderContext context);
IRenderContext renderStart(); IRenderContext renderStart();
void setDebug(boolean on);
boolean shouldClose(); boolean shouldClose();
void tick(); void tick();
} }