Add WindowManager class, get window drawing to screen and create all capabilities. this is the rest of the previous commit because im dumb

This commit is contained in:
Sebastian Cabrera 2023-05-26 12:11:15 -04:00
parent 402e4e4bb7
commit 5c9ef20e6b
3 changed files with 182 additions and 1 deletions

View file

@ -0,0 +1,8 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="AutoCloseableResource" enabled="true" level="WARNING" enabled_by_default="true">
<option name="METHOD_MATCHER_CONFIG" value="java.util.Formatter,format,java.io.Writer,append,com.google.common.base.Preconditions,checkNotNull,org.hibernate.Session,close,java.io.PrintWriter,printf,java.io.PrintStream,printf,org.lwjgl.glfw.GLFW,glfwSetFramebufferSizeCallback" />
</inspection_tool>
</profile>
</component>

View file

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

View file

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