From 3a5aa301de5777a666e81c551a10f9243ef00680 Mon Sep 17 00:00:00 2001 From: austin Date: Thu, 15 May 2025 15:29:30 -0400 Subject: [PATCH] move window pointer into rendering engine --- docs/src/progress/renderertodo.md | 1 + .../controls/ControlHandler.java | 14 +++--- .../java/electrosphere/engine/Globals.java | 21 +++----- src/main/java/electrosphere/engine/Main.java | 2 +- .../renderer/RenderingEngine.java | 50 ++++++++++++------- .../renderer/pipelines/ImGuiPipeline.java | 2 +- .../StateCleanupCheckerExtension.java | 3 -- 7 files changed, 48 insertions(+), 45 deletions(-) diff --git a/docs/src/progress/renderertodo.md b/docs/src/progress/renderertodo.md index 12b92334..5361e108 100644 --- a/docs/src/progress/renderertodo.md +++ b/docs/src/progress/renderertodo.md @@ -1823,6 +1823,7 @@ Move timeKeeper into engineState Move signalSystem into engineState Move some global state into rendering engine Push settings into config variable +Move window pointer into rendering engine diff --git a/src/main/java/electrosphere/controls/ControlHandler.java b/src/main/java/electrosphere/controls/ControlHandler.java index 6a793a49..e7764f19 100644 --- a/src/main/java/electrosphere/controls/ControlHandler.java +++ b/src/main/java/electrosphere/controls/ControlHandler.java @@ -397,7 +397,7 @@ public class ControlHandler { private void getMousePositionInBuffer(){ //only if not headless, gather position if(!Globals.HEADLESS){ - glfwGetCursorPos(Globals.window, this.mouseState.getMouseBufferX(), this.mouseState.getMouseBufferY()); + glfwGetCursorPos(Globals.renderingEngine.getWindowPtr(), this.mouseState.getMouseBufferX(), this.mouseState.getMouseBufferY()); } } @@ -483,8 +483,8 @@ public class ControlHandler { * Hides the mouse */ public void hideMouse(){ - glfwSetInputMode(Globals.window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); - glfwSetInputMode(Globals.window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); + glfwSetInputMode(Globals.renderingEngine.getWindowPtr(), GLFW_CURSOR, GLFW_CURSOR_NORMAL); + glfwSetInputMode(Globals.renderingEngine.getWindowPtr(), GLFW_CURSOR, GLFW_CURSOR_DISABLED); mouseIsVisible = false; } @@ -492,7 +492,7 @@ public class ControlHandler { * Shows the mouse */ public void showMouse(){ - glfwSetInputMode(Globals.window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); + glfwSetInputMode(Globals.renderingEngine.getWindowPtr(), GLFW_CURSOR, GLFW_CURSOR_NORMAL); mouseIsVisible = true; } @@ -511,7 +511,7 @@ public class ControlHandler { public Vector2f getMousePosition(){ double posX[] = new double[1]; double posY[] = new double[1]; - GLFW.glfwGetCursorPos(Globals.window, posX, posY); + GLFW.glfwGetCursorPos(Globals.renderingEngine.getWindowPtr(), posX, posY); Vector2f rVal = new Vector2f((float)posX[0],(float)posY[0]); return rVal; } @@ -523,10 +523,10 @@ public class ControlHandler { public Vector2f getMousePositionNormalized(){ double posX[] = new double[1]; double posY[] = new double[1]; - GLFW.glfwGetCursorPos(Globals.window, posX, posY); + GLFW.glfwGetCursorPos(Globals.renderingEngine.getWindowPtr(), posX, posY); int sizeX[] = new int[1]; int sizeY[] = new int[1]; - GLFW.glfwGetWindowSize(Globals.window, sizeX, sizeY); + GLFW.glfwGetWindowSize(Globals.renderingEngine.getWindowPtr(), sizeX, sizeY); Vector2f rVal = new Vector2f((float)((2.0 * posX[0] / sizeX[0]) - 1.0),(float)(1.0 - (2.0 * posY[0] / sizeY[0]))); return rVal; } diff --git a/src/main/java/electrosphere/engine/Globals.java b/src/main/java/electrosphere/engine/Globals.java index 3337093a..e3506fca 100644 --- a/src/main/java/electrosphere/engine/Globals.java +++ b/src/main/java/electrosphere/engine/Globals.java @@ -57,6 +57,11 @@ public class Globals { */ public static EngineState engineState; + /** + * The engine and game configuration + */ + public static electrosphere.data.Config gameConfigCurrent; + /** * State for the client */ @@ -145,13 +150,6 @@ public class Globals { public static ScrollCallback scrollCallback = new ScrollCallback(); public static CursorState cursorState = new CursorState(); - - // - // Game config - // - public static electrosphere.data.Config gameConfigDefault; - public static electrosphere.data.Config gameConfigCurrent; - // // Database stuff // @@ -162,11 +160,6 @@ public class Globals { // public static CameraHandler cameraHandler = new CameraHandler(); - // - //Generic OpenGL Statements - // - public static long window = -1; - // @@ -291,8 +284,7 @@ public class Globals { Globals.engineState = new EngineState(); //game config - gameConfigDefault = electrosphere.data.Config.loadDefaultConfig(); - gameConfigCurrent = gameConfigDefault; + gameConfigCurrent = electrosphere.data.Config.loadDefaultConfig(); NetConfig.readNetConfig(); //render flags @@ -534,7 +526,6 @@ public class Globals { Globals.RENDER_FLAG_RENDER_UI = false; Globals.RENDER_FLAG_RENDER_BLACK_BACKGROUND = false; Globals.RENDER_FLAG_RENDER_WHITE_BACKGROUND = false; - Globals.window = -1; LoggerInterface.destroyLoggers(); } diff --git a/src/main/java/electrosphere/engine/Main.java b/src/main/java/electrosphere/engine/Main.java index 90700c41..2ea52110 100644 --- a/src/main/java/electrosphere/engine/Main.java +++ b/src/main/java/electrosphere/engine/Main.java @@ -368,7 +368,7 @@ public class Main { running = false; } } else { - if(Globals.ENGINE_SHUTDOWN_FLAG || (Globals.RUN_CLIENT && GLFW.glfwWindowShouldClose(Globals.window))){ + if(Globals.ENGINE_SHUTDOWN_FLAG || (Globals.RUN_CLIENT && GLFW.glfwWindowShouldClose(Globals.renderingEngine.getWindowPtr()))){ running = false; } } diff --git a/src/main/java/electrosphere/renderer/RenderingEngine.java b/src/main/java/electrosphere/renderer/RenderingEngine.java index a6fce858..ed25fa18 100644 --- a/src/main/java/electrosphere/renderer/RenderingEngine.java +++ b/src/main/java/electrosphere/renderer/RenderingEngine.java @@ -59,6 +59,12 @@ import electrosphere.renderer.texture.Texture; */ public class RenderingEngine { + + + /** + * Handle for the window created by glfw + */ + private long windowPtr = -1; public static final int GL_DEFAULT_FRAMEBUFFER = 0; @@ -231,12 +237,12 @@ public class RenderingEngine { //Creates the window reference object if(Globals.gameConfigCurrent.getSettings().displayFullscreen() || Globals.WINDOW_FULLSCREEN){ //below line is for fullscreen - Globals.window = GLFW.glfwCreateWindow(Globals.gameConfigCurrent.getSettings().getDisplayWidth(), Globals.gameConfigCurrent.getSettings().getDisplayHeight(), "ORPG", GLFW.glfwGetPrimaryMonitor(), NULL); + this.windowPtr = GLFW.glfwCreateWindow(Globals.gameConfigCurrent.getSettings().getDisplayWidth(), Globals.gameConfigCurrent.getSettings().getDisplayHeight(), "ORPG", GLFW.glfwGetPrimaryMonitor(), NULL); } else { - Globals.window = GLFW.glfwCreateWindow(Globals.gameConfigCurrent.getSettings().getDisplayWidth(), Globals.gameConfigCurrent.getSettings().getDisplayHeight(), "ORPG", NULL, NULL); + this.windowPtr = GLFW.glfwCreateWindow(Globals.gameConfigCurrent.getSettings().getDisplayWidth(), Globals.gameConfigCurrent.getSettings().getDisplayHeight(), "ORPG", NULL, NULL); } // Errors for failure to create window (IE: No GUI mode on linux ?) - if (Globals.window == NULL) { + if (this.windowPtr == NULL) { String message = "Failed to create window!\n" + "Error code: " + this.getGLFWErrorMessage(this.getGLFWError()); ; @@ -245,19 +251,19 @@ public class RenderingEngine { } //set resize callback - GLFW.glfwSetWindowSizeCallback(Globals.window, (long window, int width, int height) -> { + GLFW.glfwSetWindowSizeCallback(this.windowPtr, (long window, int width, int height) -> { Globals.WINDOW_HEIGHT = height; Globals.WINDOW_WIDTH = width; }); //Makes the window that was just created the current OS-level window context - GLFW.glfwMakeContextCurrent(Globals.window); + GLFW.glfwMakeContextCurrent(this.windowPtr); //Maximize it - GLFW.glfwMaximizeWindow(Globals.window); + GLFW.glfwMaximizeWindow(this.windowPtr); GLFW.glfwPollEvents(); //grab actual framebuffer IntBuffer xBuffer = BufferUtils.createIntBuffer(1); IntBuffer yBuffer = BufferUtils.createIntBuffer(1); - GLFW.glfwGetFramebufferSize(Globals.window, xBuffer, yBuffer); + GLFW.glfwGetFramebufferSize(this.windowPtr, xBuffer, yBuffer); int bufferWidth = xBuffer.get(); int bufferHeight = yBuffer.get(); @@ -275,9 +281,9 @@ public class RenderingEngine { // Attach controls callbacks // //set key callback - GLFW.glfwSetKeyCallback(Globals.window, Globals.controlCallback); - GLFW.glfwSetMouseButtonCallback(Globals.window, Globals.mouseCallback); - GLFW.glfwSetScrollCallback(Globals.window, Globals.scrollCallback); + GLFW.glfwSetKeyCallback(this.windowPtr, Globals.controlCallback); + GLFW.glfwSetMouseButtonCallback(this.windowPtr, Globals.mouseCallback); + GLFW.glfwSetScrollCallback(this.windowPtr, Globals.scrollCallback); //get title bar dimensions // setTitleBarDimensions(); @@ -299,7 +305,7 @@ public class RenderingEngine { openGLContext = new OpenGLContext(); //init imgui pipeline - imGuiPipeline = new ImGuiPipeline(Globals.window, glslVersion); + imGuiPipeline = new ImGuiPipeline(this.windowPtr, glslVersion); //This enables Z-buffering so that farther-back polygons are not drawn over nearer ones openGLState.glDepthTest(true); @@ -454,7 +460,7 @@ public class RenderingEngine { // //Set file drag-and-drop for app // - GLFW.glfwSetDropCallback(Globals.window, (long window, int count, long names) -> { + GLFW.glfwSetDropCallback(this.windowPtr, (long window, int count, long names) -> { PointerBuffer charPointers = MemoryUtil.memPointerBuffer(names, count); List paths = new LinkedList(); for(int i = 0; i < count; i++){ @@ -567,7 +573,7 @@ public class RenderingEngine { //check and call events and swap the buffers LoggerInterface.loggerRenderer.DEBUG_LOOP("GLFW Swap buffers"); - GLFW.glfwSwapBuffers(Globals.window); + GLFW.glfwSwapBuffers(this.windowPtr); LoggerInterface.loggerRenderer.DEBUG_LOOP("GLFW Poll Events"); GLFW.glfwPollEvents(); LoggerInterface.loggerRenderer.DEBUG_LOOP("Check OpenGL Errors"); @@ -592,7 +598,7 @@ public class RenderingEngine { IntBuffer tBottom = BufferUtils.createIntBuffer(1); // Get the title bar dims - GLFW.glfwGetWindowFrameSize(Globals.window, tLeft, tTop, tRight, tBottom); + GLFW.glfwGetWindowFrameSize(this.windowPtr, tLeft, tTop, tRight, tBottom); Globals.WINDOW_TITLE_BAR_HEIGHT = tTop.get(); // System.out.println(tLeft.get() + " " + tTop.get() + " " + tRight.get() + " " + tBottom.get()); } @@ -739,17 +745,25 @@ public class RenderingEngine { return this.nearClip; } + /** + * Gets the window pointer of the rendering engine + * @return The window pointer + */ + public long getWindowPtr(){ + return this.windowPtr; + } + /** * Tries to recapture the screen */ public static void recaptureIfNecessary(){ if(Globals.controlHandler.shouldRecapture()){ //Makes the window that was just created the current OS-level window context - GLFW.glfwMakeContextCurrent(Globals.window); + GLFW.glfwMakeContextCurrent(Globals.renderingEngine.windowPtr); // //Maximize it - GLFW.glfwMaximizeWindow(Globals.window); + GLFW.glfwMaximizeWindow(Globals.renderingEngine.windowPtr); //grab focus - GLFW.glfwFocusWindow(Globals.window); + GLFW.glfwFocusWindow(Globals.renderingEngine.windowPtr); //apply mouse controls state if(Globals.controlHandler.isMouseVisible()){ Globals.controlHandler.showMouse(); @@ -913,7 +927,7 @@ public class RenderingEngine { Globals.assetManager.handleDeleteQueue(); //end glfw - GLFW.glfwDestroyWindow(Globals.window); + GLFW.glfwDestroyWindow(this.windowPtr); GLFW.glfwTerminate(); } diff --git a/src/main/java/electrosphere/renderer/pipelines/ImGuiPipeline.java b/src/main/java/electrosphere/renderer/pipelines/ImGuiPipeline.java index bfbee33e..b7c36aee 100644 --- a/src/main/java/electrosphere/renderer/pipelines/ImGuiPipeline.java +++ b/src/main/java/electrosphere/renderer/pipelines/ImGuiPipeline.java @@ -49,7 +49,7 @@ public class ImGuiPipeline implements RenderPipeline { throw new IllegalStateException("Imgui failed to initialize."); } ImPlot.createContext(); - imGuiGlfw.init(Globals.window,true); + imGuiGlfw.init(Globals.renderingEngine.getWindowPtr(),true); imGuiGl13.init(glslVersion); } diff --git a/src/test/java/electrosphere/test/template/extensions/StateCleanupCheckerExtension.java b/src/test/java/electrosphere/test/template/extensions/StateCleanupCheckerExtension.java index eefe0ed6..8fd1bdd9 100644 --- a/src/test/java/electrosphere/test/template/extensions/StateCleanupCheckerExtension.java +++ b/src/test/java/electrosphere/test/template/extensions/StateCleanupCheckerExtension.java @@ -28,9 +28,6 @@ public class StateCleanupCheckerExtension implements AfterEachCallback { throw new Exception("Failed to cleanup state after test! " + object.toString()); } } - if(Globals.window != -1){ - throw new Exception("Failed to cleanup global window pointer!"); - } } }