drawing coloured rectangle

This commit is contained in:
Dani The Skunk 2022-09-14 19:08:16 +02:00
parent a5c48c2ef2
commit b74608e8d3
4 changed files with 65 additions and 17 deletions

View File

@ -1,20 +1,21 @@
import com.danitheskunk.skunkworks.Engine; import com.danitheskunk.skunkworks.*;
import com.danitheskunk.skunkworks.GLWindow;
import com.danitheskunk.skunkworks.Vec2f;
import com.danitheskunk.skunkworks.Vec2i;
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(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:\\Users\\dani\\Videos\\Screenshot 2022-06-25 17-00-59.png");
//var img = engine.loadImage("C:\\art\\kyoko.png"); //var img = engine.loadImage("C:\\art\\kyoko.png");
System.out.println(img.getPixel(new Vec2i(60, 60))); System.out.println(img.getPixel(new Vec2i(60, 60)));
while(!window.shouldClose()) { while(!window.shouldClose()) {
window.tick(); window.tick();
window2.tick(); var renderContext = window.renderStart();
renderContext.drawRectangle(
new Recti(100, 100, 100, 100),
new Color(150, 200, 250)
);
window.renderFinish(renderContext);
} }
} }
} }

View File

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

View File

@ -10,26 +10,30 @@ 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 long window; private GLRenderContext renderContext;
private boolean shouldClose; private boolean shouldClose;
private long window;
public GLWindow(int width, int height, String title) { public GLWindow(int width, int height, String title) {
GLFWErrorCallback.createPrint(System.err).set(); GLFWErrorCallback.createPrint(System.err).set();
if (!glfwInit()) if(!glfwInit()) throw new IllegalStateException("Unable to initialize GLFW");
throw new IllegalStateException("Unable to initialize GLFW");
glfwDefaultWindowHints(); glfwDefaultWindowHints();
window = glfwCreateWindow(width, height, title, NULL, NULL); window = glfwCreateWindow(width, height, title, NULL, NULL);
if (window == NULL) if(window == NULL) throw new RuntimeException("Failed to create GLFW window");
throw new RuntimeException("Failed to create GLFW window");
glfwMakeContextCurrent(window);
glfwSwapInterval(1); //vsync
glfwShowWindow(window); glfwShowWindow(window);
glfwMakeContextCurrent(window);
glfwSwapInterval(1); //vsync
GL.createCapabilities(); 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; shouldClose = false;
@ -38,6 +42,20 @@ public class GLWindow implements IWindow {
System.out.println(GL11.glGetInteger(GL_MAX_TEXTURE_SIZE)); 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 @Override
public boolean shouldClose() { public boolean shouldClose() {
return shouldClose; return shouldClose;
@ -45,14 +63,12 @@ public class GLWindow implements IWindow {
@Override @Override
public void tick() { public void tick() {
glfwMakeContextCurrent(window);
if(glfwWindowShouldClose(window)) { if(glfwWindowShouldClose(window)) {
shouldClose = true; shouldClose = true;
return; return;
} }
glClearColor(0.1f, 0.2f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents(); glfwPollEvents();
} }
} }

View File

@ -1,6 +1,8 @@
package com.danitheskunk.skunkworks; package com.danitheskunk.skunkworks;
public interface IWindow { public interface IWindow {
void renderFinish(IRenderContext context);
IRenderContext renderStart();
boolean shouldClose(); boolean shouldClose();
void tick(); void tick();
} }