diff options
author | Tyler Murphy <tylerm@tylerm.dev> | 2023-04-17 12:12:01 -0400 |
---|---|---|
committer | Tyler Murphy <tylerm@tylerm.dev> | 2023-04-17 12:12:01 -0400 |
commit | 180aad05decc7eefa87e4e45d6747c48f40e5361 (patch) | |
tree | 51545197f7c94b4022acab880772c9f4fc65db0e /src/main/java/net/tylermurphy/Minecraft/Render/Data/Display.java | |
download | minecraftjava-180aad05decc7eefa87e4e45d6747c48f40e5361.tar.gz minecraftjava-180aad05decc7eefa87e4e45d6747c48f40e5361.tar.bz2 minecraftjava-180aad05decc7eefa87e4e45d6747c48f40e5361.zip |
Diffstat (limited to 'src/main/java/net/tylermurphy/Minecraft/Render/Data/Display.java')
-rwxr-xr-x | src/main/java/net/tylermurphy/Minecraft/Render/Data/Display.java | 175 |
1 files changed, 175 insertions, 0 deletions
diff --git a/src/main/java/net/tylermurphy/Minecraft/Render/Data/Display.java b/src/main/java/net/tylermurphy/Minecraft/Render/Data/Display.java new file mode 100755 index 0000000..f866e97 --- /dev/null +++ b/src/main/java/net/tylermurphy/Minecraft/Render/Data/Display.java @@ -0,0 +1,175 @@ +package net.tylermurphy.Minecraft.Render.Data;
+
+import java.nio.DoubleBuffer;
+import java.nio.IntBuffer;
+
+import org.joml.Vector2f;
+import org.lwjgl.BufferUtils;
+import org.lwjgl.glfw.GLFW;
+import org.lwjgl.glfw.GLFWFramebufferSizeCallback;
+import org.lwjgl.glfw.GLFWImage;
+import org.lwjgl.glfw.GLFWVidMode;
+import org.lwjgl.opengl.GL;
+import org.lwjgl.opengl.GL11;
+
+import net.tylermurphy.Minecraft.Input.Input;
+
+public class Display {
+
+ private static long monitor;
+ private static long window;
+
+ private static int width,height;
+
+ private static double previousTime = GLFW.glfwGetTime();
+ private static double passedTime = 0;
+
+ private static boolean contextReady = false;
+
+ public static boolean wasResized = false;
+
+ public static boolean fullscreen = false;
+ private static int backup_xpos,backup_ypos,backup_xsize,backup_ysize;
+
+ public static void create(int width, int height, String title, Texture icon) {
+
+ Display.width = width;
+ Display.height = height;
+
+ if(!GLFW.glfwInit()) {
+ System.err.println("Error: Coudnt init GLFW");
+ System.exit(-1);
+ }
+
+ Input.init();
+
+ GLFW.glfwWindowHint(GLFW.GLFW_VISIBLE, GLFW.GLFW_FALSE);
+
+ monitor = GLFW.glfwGetPrimaryMonitor();
+ window = GLFW.glfwCreateWindow(width, height, title, 0, 0);
+
+ GLFW.glfwSetKeyCallback(window, Input.getKeyboardCallback());
+ GLFW.glfwSetCursorPosCallback(window, Input.getMouseMoveCallback());
+ GLFW.glfwSetMouseButtonCallback(window, Input.getMouseButtonCallback());
+ GLFW.glfwSetCharCallback(window, Input.getCharCallback());
+
+ GLFWFramebufferSizeCallback framebufferCallback = new GLFWFramebufferSizeCallback() {
+ public void invoke(long window, int width, int height) {
+ if(contextReady) {
+ Display.width = width;
+ Display.height = height;
+ Display.wasResized = true;
+ GL11.glViewport(0, 0, width, height);
+ }
+ }
+ };
+
+ GLFW.glfwSetFramebufferSizeCallback(window, framebufferCallback);
+
+ if(window==0) {
+ System.err.println("Error: Coudnt create window");
+ System.exit(-1);
+ }
+
+ if(icon != null) {
+ GLFWImage image = GLFWImage.malloc(); GLFWImage.Buffer imagebf = GLFWImage.malloc(1);
+ image.set(icon.getWidth(), icon.getHeight(), icon.getImage());
+ imagebf.put(0, image);
+ GLFW.glfwSetWindowIcon(window, imagebf);
+ }
+
+ GLFWVidMode videoMode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor());
+ assert videoMode != null;
+ GLFW.glfwSetWindowPos(window, (videoMode.width() - width) / 2, (videoMode.height() - height) / 2);
+ GLFW.glfwMakeContextCurrent(window);
+ GLFW.glfwSwapInterval(0);
+ GLFW.glfwShowWindow(window);
+
+ GL.createCapabilities();
+ grabCursor();
+
+ contextReady = true;
+
+ }
+
+ public static boolean closed() {
+ return !GLFW.glfwWindowShouldClose(window);
+ }
+
+ public static void update() {
+ double currentTime = GLFW.glfwGetTime();
+ passedTime = currentTime - previousTime;
+ previousTime = currentTime;
+ GLFW.glfwPollEvents();
+ }
+
+ public static void swapBuffers() {
+ GLFW.glfwSwapBuffers(window);
+ }
+
+ public static void close() {
+ Input.destroy();
+ GLFW.glfwDestroyWindow(window);
+ GLFW.glfwTerminate();
+ GL.destroy();
+ }
+
+ public static int getWidth() {
+ return width;
+ }
+
+ public static int getHeight() {
+ return height;
+ }
+
+ public static float getFrameTimeSeconds() {
+ return (float) passedTime;
+ }
+
+ public static void grabCursor() {
+ GLFW.glfwSetInputMode(window, GLFW.GLFW_CURSOR, GLFW.GLFW_CURSOR_DISABLED);
+ }
+
+ public static void releaseCursor() {
+ GLFW.glfwSetInputMode(window, GLFW.GLFW_CURSOR, GLFW.GLFW_CURSOR_NORMAL);
+ }
+
+ public static Vector2f getCursorPos() {
+ DoubleBuffer xpos = BufferUtils.createDoubleBuffer(1);
+ DoubleBuffer ypos = BufferUtils.createDoubleBuffer(1);
+ GLFW.glfwGetCursorPos(window, xpos, ypos);
+ return new Vector2f((float)xpos.get(),(float)ypos.get());
+ }
+
+ public static void setFullScreen(boolean fullscreen) {
+
+ if(Display.fullscreen == fullscreen)
+ return;
+
+ Display.fullscreen = fullscreen;
+
+ if(fullscreen) {
+
+ IntBuffer xpos = BufferUtils.createIntBuffer(1);
+ IntBuffer ypos = BufferUtils.createIntBuffer(1);
+ GLFW.glfwGetWindowPos(window, xpos, ypos);
+
+ IntBuffer xsize = BufferUtils.createIntBuffer(1);
+ IntBuffer ysize = BufferUtils.createIntBuffer(1);
+ GLFW.glfwGetWindowSize(window, xsize, ysize);
+
+ backup_xpos = xpos.get();
+ backup_ypos = ypos.get();
+ backup_xsize = xsize.get();
+ backup_ysize = ysize.get();
+
+ GLFWVidMode mode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor());
+
+ assert mode != null;
+ GLFW.glfwSetWindowMonitor(window, monitor, backup_xpos, backup_ypos, mode.width(), mode.height(), 0);
+ } else {
+ GLFW.glfwSetWindowMonitor(window, 0, backup_xpos, backup_ypos, backup_xsize, backup_ysize, 0);
+ }
+ }
+
+}
|