changed width,height in window creation to Vec2i, added texture atlas debugging, fixed texture atlas bug resizing too early
This commit is contained in:
parent
2448143454
commit
d5ca42c83c
|
@ -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();
|
||||
|
|
|
@ -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);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -9,6 +9,7 @@ public interface IWindow {
|
|||
List<ITexture> loadTextureArray(String path, Vec2i tileSize);
|
||||
void renderFinish(IRenderContext context);
|
||||
IRenderContext renderStart();
|
||||
void setDebug(boolean on);
|
||||
boolean shouldClose();
|
||||
void tick();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue