Game Window: add the possibility resize the window

This commit is contained in:
Lukas 2020-12-08 17:27:12 +01:00
parent 8c47dedf3b
commit 386dc882a0

View File

@ -14,9 +14,8 @@
package de.bixilon.minosoft.render; package de.bixilon.minosoft.render;
import de.bixilon.minosoft.protocol.network.Connection; import de.bixilon.minosoft.protocol.network.Connection;
import org.lwjgl.glfw.GLFWCursorPosCallback; import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWErrorCallback; import org.lwjgl.glfw.*;
import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.system.MemoryStack; import org.lwjgl.system.MemoryStack;
import java.nio.IntBuffer; import java.nio.IntBuffer;
@ -28,7 +27,7 @@ import static org.lwjgl.system.MemoryStack.stackPush;
import static org.lwjgl.system.MemoryUtil.NULL; import static org.lwjgl.system.MemoryUtil.NULL;
public class OpenGLWindow { public class OpenGLWindow {
private static final float fovY = 45f; private static final float fovY = 45f; // degrees
private static final boolean fullscreenEnabled = false; private static final boolean fullscreenEnabled = false;
private static int width = 800; private static int width = 800;
private static int height = 800; private static int height = 800;
@ -64,6 +63,7 @@ public class OpenGLWindow {
if (fullscreenEnabled) { if (fullscreenEnabled) {
long monitor = glfwGetPrimaryMonitor(); long monitor = glfwGetPrimaryMonitor();
GLFWVidMode mode = glfwGetVideoMode(monitor); GLFWVidMode mode = glfwGetVideoMode(monitor);
assert mode != null;
width = mode.width(); width = mode.width();
height = mode.height(); height = mode.height();
} }
@ -74,17 +74,19 @@ public class OpenGLWindow {
} }
try (MemoryStack stack = stackPush()) { try (MemoryStack stack = stackPush()) {
IntBuffer pWidth = stack.mallocInt(1); // int* IntBuffer pWidth = stack.mallocInt(1);
IntBuffer pHeight = stack.mallocInt(1); // int* IntBuffer pHeight = stack.mallocInt(1);
glfwGetWindowSize(windowId, pWidth, pHeight); glfwGetWindowSize(windowId, pWidth, pHeight);
GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor()); GLFWVidMode videoMode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(windowId, (vidmode.width() - pWidth.get(0)) / 2, (vidmode.height() - pHeight.get(0)) / 2); assert videoMode != null;
glfwSetWindowPos(windowId, (videoMode.width() - pWidth.get(0)) / 2, (videoMode.height() - pHeight.get(0)) / 2);
} }
// the context belongs to this ond only this thread. It enables displaying anything
glfwMakeContextCurrent(windowId); glfwMakeContextCurrent(windowId);
// cursor Callback
glfwSetCursorPosCallback(windowId, new GLFWCursorPosCallback() { glfwSetCursorPosCallback(windowId, new GLFWCursorPosCallback() {
@Override @Override
public void invoke(long windowId, double xPos, double yPos) { public void invoke(long windowId, double xPos, double yPos) {
@ -93,11 +95,21 @@ public class OpenGLWindow {
} }
}); });
// resizeable window
GLFW.glfwSetFramebufferSizeCallback(windowId, new GLFWFramebufferSizeCallback() {
@Override
public void invoke(long window, int width, int height) {
OpenGLWindow.width = width;
OpenGLWindow.height = height;
glViewport(0,0,width, height);
}
});
// Enable v-sync // Enable v-sync
glfwSwapInterval(1); glfwSwapInterval(1);
createCapabilities(); createCapabilities();
// background color
glClearColor(.2f, .2f, .6f, 1f); glClearColor(.2f, .2f, .6f, 1f);
glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_VERTEX_ARRAY);
@ -106,15 +118,21 @@ public class OpenGLWindow {
glEnable(GL_BLEND); glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
// no overlapping textures
glEnable(GL_CULL_FACE); glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT); glCullFace(GL_FRONT);
glEnable(GL_ALPHA_TEST); glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.0f); glAlphaFunc(GL_GREATER, 0.0f);
// if one face is behind another, don't show it
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL); glDepthFunc(GL_LEQUAL);
// mouse is captured
mouseEnable(false); mouseEnable(false);
// enable usage of textures
glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);
} }
@ -147,10 +165,13 @@ public class OpenGLWindow {
} else { } else {
escDown = false; escDown = false;
} }
// clear the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
float currentFrame = (float) glfwGetTime(); float currentFrame = (float) glfwGetTime();
float deltaTime = currentFrame - lastFrame; float deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame; lastFrame = currentFrame;
setupPerspective(); setupPerspective();
return deltaTime; return deltaTime;
} }
@ -160,6 +181,7 @@ public class OpenGLWindow {
} }
public void setCurrentConnection(Connection connection) { public void setCurrentConnection(Connection connection) {
// use the mouse for the new connection and not for the old one
glfwSetCursorPosCallback(windowId, connection.getRenderProperties().getController().getCameraMovement()::mouseCallback); glfwSetCursorPosCallback(windowId, connection.getRenderProperties().getController().getCameraMovement()::mouseCallback);
} }