diff --git a/Test.java b/Test.java index 62f3af6..68f4ee2 100644 --- a/Test.java +++ b/Test.java @@ -1,20 +1,21 @@ -import com.danitheskunk.skunkworks.Engine; -import com.danitheskunk.skunkworks.GLWindow; -import com.danitheskunk.skunkworks.Vec2f; -import com.danitheskunk.skunkworks.Vec2i; +import com.danitheskunk.skunkworks.*; public class Test { public static void main(String[] args) { var engine = new Engine(); var window = engine.openWindow(1280, 720, "Skunkworks"); - var window2 = engine.openWindow(1280, 720, "Skunkworks"); var img = engine.loadImage("C:\\Users\\dani\\Videos\\Screenshot 2022-06-25 17-00-59.png"); //var img = engine.loadImage("C:\\art\\kyoko.png"); System.out.println(img.getPixel(new Vec2i(60, 60))); while(!window.shouldClose()) { window.tick(); - window2.tick(); + var renderContext = window.renderStart(); + renderContext.drawRectangle( + new Recti(100, 100, 100, 100), + new Color(150, 200, 250) + ); + window.renderFinish(renderContext); } } } diff --git a/com/danitheskunk/skunkworks/GLRenderContext.java b/com/danitheskunk/skunkworks/GLRenderContext.java new file mode 100644 index 0000000..15e1371 --- /dev/null +++ b/com/danitheskunk/skunkworks/GLRenderContext.java @@ -0,0 +1,29 @@ +package com.danitheskunk.skunkworks; + +import static org.lwjgl.opengl.GL11.*; + +public class GLRenderContext implements IRenderContext{ + @Override + public void drawRectangle(Recti rect, Color color) { + var tl = rect.getTopLeft(); + var tr = rect.getTopRight(); + var bl = rect.getBottomLeft(); + var br = rect.getBottomRight(); + + glColor4f( + color.getR() / 255.0f, + color.getG() / 255.0f, + color.getB() / 255.0f, + color.getA() / 255.0f + ); + + //counter clockwise triangles + glVertex2i(bl.getX(), bl.getY()); + glVertex2i(tr.getX(), tr.getY()); + glVertex2i(tl.getX(), tl.getY()); + + glVertex2i(tr.getX(), tr.getY()); + glVertex2i(bl.getX(), bl.getY()); + glVertex2i(br.getX(), bl.getY()); + } +} diff --git a/com/danitheskunk/skunkworks/GLWindow.java b/com/danitheskunk/skunkworks/GLWindow.java index 27eef33..80d4720 100644 --- a/com/danitheskunk/skunkworks/GLWindow.java +++ b/com/danitheskunk/skunkworks/GLWindow.java @@ -10,26 +10,30 @@ import static org.lwjgl.opengl.GL11.*; import static org.lwjgl.system.MemoryUtil.NULL; public class GLWindow implements IWindow { - private long window; + private GLRenderContext renderContext; private boolean shouldClose; + private long window; public GLWindow(int width, int height, String title) { GLFWErrorCallback.createPrint(System.err).set(); - if (!glfwInit()) - throw new IllegalStateException("Unable to initialize GLFW"); + if(!glfwInit()) throw new IllegalStateException("Unable to initialize GLFW"); glfwDefaultWindowHints(); window = glfwCreateWindow(width, height, 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"); - glfwMakeContextCurrent(window); - glfwSwapInterval(1); //vsync glfwShowWindow(window); - + glfwMakeContextCurrent(window); + glfwSwapInterval(1); //vsync GL.createCapabilities(); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(0.0f, width, height, 0.0f, 0.0f, 1.0f); + glEnable(GL_COLOR); + + renderContext = new GLRenderContext(); shouldClose = false; @@ -38,6 +42,20 @@ public class GLWindow implements IWindow { System.out.println(GL11.glGetInteger(GL_MAX_TEXTURE_SIZE)); } + @Override + public void renderFinish(IRenderContext context) { + glEnd(); + glfwSwapBuffers(window); + } + + @Override + public IRenderContext renderStart() { + glClearColor(0.1f, 0.2f, 0.3f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glBegin(GL_TRIANGLES); + return renderContext; + } + @Override public boolean shouldClose() { return shouldClose; @@ -45,14 +63,12 @@ public class GLWindow implements IWindow { @Override public void tick() { + glfwMakeContextCurrent(window); if(glfwWindowShouldClose(window)) { shouldClose = true; return; } - glClearColor(0.1f, 0.2f, 0.3f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glfwSwapBuffers(window); glfwPollEvents(); } } diff --git a/com/danitheskunk/skunkworks/IWindow.java b/com/danitheskunk/skunkworks/IWindow.java index f348b03..3cec2bb 100644 --- a/com/danitheskunk/skunkworks/IWindow.java +++ b/com/danitheskunk/skunkworks/IWindow.java @@ -1,6 +1,8 @@ package com.danitheskunk.skunkworks; public interface IWindow { + void renderFinish(IRenderContext context); + IRenderContext renderStart(); boolean shouldClose(); void tick(); }