From 5c9ef20e6b4137fd101d5d115168dab890202510 Mon Sep 17 00:00:00 2001 From: okseby Date: Fri, 26 May 2023 12:11:15 -0400 Subject: [PATCH] Add WindowManager class, get window drawing to screen and create all capabilities. this is the rest of the previous commit because im dumb --- .idea/inspectionProfiles/Project_Default.xml | 8 + src/main/java/com/okseby/core/Launcher.java | 10 +- .../java/com/okseby/core/WindowManager.java | 165 ++++++++++++++++++ 3 files changed, 182 insertions(+), 1 deletion(-) create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 src/main/java/com/okseby/core/WindowManager.java diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..a20f8bc --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,8 @@ + + + + \ No newline at end of file diff --git a/src/main/java/com/okseby/core/Launcher.java b/src/main/java/com/okseby/core/Launcher.java index 95d55b4..b4be82b 100644 --- a/src/main/java/com/okseby/core/Launcher.java +++ b/src/main/java/com/okseby/core/Launcher.java @@ -1,9 +1,17 @@ -package com.okseby.Main; +package com.okseby.core; import org.lwjgl.Version; public class Launcher { public static void main(String[] args) { System.out.println("LWJGL Version: " + Version.getVersion()); + + WindowManager window = new WindowManager("Warabi Engine", 1600, 900, false); + window.init(); + + while (!window.windowShouldClose()) + window.update(); + + window.cleanup(); } } diff --git a/src/main/java/com/okseby/core/WindowManager.java b/src/main/java/com/okseby/core/WindowManager.java new file mode 100644 index 0000000..e102fcb --- /dev/null +++ b/src/main/java/com/okseby/core/WindowManager.java @@ -0,0 +1,165 @@ +package com.okseby.core; + +import org.joml.Matrix4f; +import org.lwjgl.glfw.GLFW; +import org.lwjgl.glfw.GLFWErrorCallback; +import org.lwjgl.glfw.GLFWVidMode; +import org.lwjgl.opengl.GL; +import org.lwjgl.opengl.GL11; +import org.lwjgl.system.MemoryUtil; + +public class WindowManager { + public static final float FOV = (float) Math.toRadians(60); + public static final float Z_NEAR = 0.01f; + public static final float Z_FAR = 1000f; + + private String title; + + private int width, height; + private long window; + + private boolean resizeable, vSync; + + private final Matrix4f projectionMatrix; + + public WindowManager(String title, int width, int height, boolean vSync) { + this.title = title; + this.width = width; + this.height = height; + this.vSync = vSync; + + projectionMatrix = new Matrix4f(); + } + + public void init() { + GLFWErrorCallback.createPrint(System.err).set(); + + if (!GLFW.glfwInit()) + throw new IllegalStateException("Unable to initialize GLFW!"); + + GLFW.glfwDefaultWindowHints(); + GLFW.glfwWindowHint(GLFW.GLFW_VISIBLE, GL11.GL_FALSE); + GLFW.glfwWindowHint(GLFW.GLFW_RESIZABLE, GL11.GL_TRUE); + GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MAJOR, 3); + GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR, 2); + GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_PROFILE, GLFW.GLFW_OPENGL_CORE_PROFILE); + GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_FORWARD_COMPAT, GL11.GL_TRUE); + + boolean maximized = false; + if (width == 0 || height == 0) { + width = 100; + height = 100; + + GLFW.glfwWindowHint(GLFW.GLFW_MAXIMIZED, GLFW.GLFW_TRUE); + maximized = true; + } + + window = GLFW.glfwCreateWindow(width, height, title, MemoryUtil.NULL, MemoryUtil.NULL); + if (window == MemoryUtil.NULL) + throw new RuntimeException("Failed to create GLFW window!"); + + GLFW.glfwSetFramebufferSizeCallback(window, (window, width, height) -> { + this.width = width; + this.height = height; + this.setResizeable(true); + }); + + GLFW.glfwSetKeyCallback(window, (window, key, scancode, action, mods) -> { + if (key == GLFW.GLFW_KEY_ESCAPE || action == GLFW.GLFW_RELEASE) + GLFW.glfwSetWindowShouldClose(window, true); + }); + + if (maximized) + GLFW.glfwMaximizeWindow(window); + else { + GLFWVidMode vidMode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor()); + GLFW.glfwSetWindowPos(window, (vidMode.width() - width) / 2, (vidMode.height() - height) / 2); + } + + GLFW.glfwMakeContextCurrent(window); + + if (isvSync()) + GLFW.glfwSwapInterval(1); + + GLFW.glfwShowWindow(window); + + GL.createCapabilities(); + + GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + GL11.glEnable(GL11.GL_DEPTH_TEST); + GL11.glEnable(GL11.GL_STENCIL_TEST); + GL11.glEnable(GL11.GL_CULL_FACE); + GL11.glCullFace(GL11.GL_BACK); + } + + public void update() { + GLFW.glfwSwapBuffers(window); + GLFW.glfwPollEvents(); + } + + public void cleanup() { + GLFW.glfwDestroyWindow(window); + } + + public void setClearColor(float r, float g, float b, float a) { + GL11.glClearColor(r, g, b, a); + } + + public boolean isKeyPressed(int keycode) { + return GLFW.glfwGetKey(window, keycode) == GLFW.GLFW_PRESS; + } + + public boolean windowShouldClose() { + return GLFW.glfwWindowShouldClose(window); + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + GLFW.glfwSetWindowTitle(window, title); + } + + public boolean isResizeable() { + return resizeable; + } + + public boolean isvSync() { + return vSync; + } + + public void setvSync(boolean vSync) { + this.vSync = vSync; + } + + public void setResizeable(boolean resizeable) { + this.resizeable = resizeable; + } + + public int getWidth() { + return width; + } + + public int getHeight() { + return height; + } + + public long getWindow() { + return window; + } + + public Matrix4f getProjectionMatrix() { + return projectionMatrix; + } + + public Matrix4f updateProjectionMatrix() { + float aspectRatio = (float) width / height; + return projectionMatrix.setPerspective(FOV, aspectRatio, Z_NEAR, Z_FAR); + } + + public Matrix4f updateProjectionMatrix(Matrix4f matrix) { + float aspectRatio = (float) width / height; + return matrix.setPerspective(FOV, aspectRatio, Z_NEAR, Z_FAR); + } +}