Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/
package com.project.physics.engine.core;

import static org.lwjgl.opengl.GL11.glViewport;

/**
* This class contains the implementation for a fixed timestep game loop.
*
Expand All @@ -43,6 +45,11 @@ public void gameLoop() {
running = false;
}

if (window.hasResized()) {
renderer.setProjection(window.getWidth(), window.getHeight());
glViewport(0, 0, window.getWidth(), window.getHeight());
}

/* Get delta time and update the accumulator */
delta = timer.getDelta();
accumulator += delta;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/
package com.project.physics.engine.core;

import static org.lwjgl.opengl.GL11.glViewport;

/**
* This class contains the implementation for a variable timestep game loop.
*
Expand All @@ -40,6 +42,11 @@ public void gameLoop() {
running = false;
}

if (window.hasResized()) {
renderer.setProjection(window.getWidth(), window.getHeight());
glViewport(0, 0, window.getWidth(), window.getHeight());
}

/* Get delta time */
delta = timer.getDelta();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -470,10 +470,14 @@ private void setupShaderProgram() {
int uniView = program.getUniformLocation("view");
program.setUniform(uniView, view);

setProjection(width, height);
}

public void setProjection(int width, int height) {
/* Set projection matrix to an orthographic projection */
Matrix4f projection = Matrix4f.orthographic(0f, width, 0f, height, -1f, 1f);
int uniProjection = program.getUniformLocation("projection");
program.setUniform(uniProjection, projection);
int uniformProjection = program.getUniformLocation("projection");
program.setUniform(uniformProjection, projection);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import static org.lwjgl.glfw.GLFW.GLFW_OPENGL_FORWARD_COMPAT;
import static org.lwjgl.glfw.GLFW.GLFW_OPENGL_PROFILE;
import static org.lwjgl.glfw.GLFW.GLFW_PRESS;
import static org.lwjgl.glfw.GLFW.GLFW_RESIZABLE;
import static org.lwjgl.glfw.GLFW.GLFW_TRUE;
import static org.lwjgl.glfw.GLFW.GLFW_VISIBLE;
import static org.lwjgl.glfw.GLFW.glfwCreateWindow;
Expand All @@ -44,6 +43,7 @@
import static org.lwjgl.glfw.GLFW.glfwSetKeyCallback;
import static org.lwjgl.glfw.GLFW.glfwSetWindowPos;
import static org.lwjgl.glfw.GLFW.glfwSetWindowShouldClose;
import static org.lwjgl.glfw.GLFW.glfwSetWindowSizeCallback;
import static org.lwjgl.glfw.GLFW.glfwSetWindowTitle;
import static org.lwjgl.glfw.GLFW.glfwSwapBuffers;
import static org.lwjgl.glfw.GLFW.glfwSwapInterval;
Expand All @@ -52,6 +52,7 @@
import static org.lwjgl.glfw.GLFW.glfwWindowShouldClose;
import org.lwjgl.glfw.GLFWKeyCallback;
import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.glfw.GLFWWindowSizeCallback;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GLCapabilities;
import static org.lwjgl.system.MemoryUtil.NULL;
Expand All @@ -63,21 +64,14 @@
*/
public class Window {

/**
* Stores the window handle.
*/
private final long id;

/**
* Key callback for the window.
*/
private final GLFWKeyCallback keyCallback;

/**
* Shows if vsync is enabled.
*/
private boolean vsync;

private int width, height;
private boolean hasResized;
private GLFWWindowSizeCallback windowSizeCallback;

/**
* Creates a GLFW window and its OpenGL context with the specified width,
* height and title.
Expand All @@ -88,8 +82,12 @@ public class Window {
* @param vsync Set to true, if you want v-sync
*/
public Window(int width, int height, CharSequence title, boolean vsync) {
this.width = width;
this.height = height;
this.vsync = vsync;

hasResized = false;

/* Creating a temporary window for getting the available OpenGL version */
glfwDefaultWindowHints();
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
Expand All @@ -113,9 +111,9 @@ public Window(int width, int height, CharSequence title, boolean vsync) {
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
} else {
throw new RuntimeException("Neither OpenGL 3.2 nor OpenGL 2.1 is "
+ "supported, you may want to update your graphics driver.");
+ "supported, you may want to update your graphics driver.");
}
glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);
// glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE);

/* Create window with specified OpenGL context */
id = glfwCreateWindow(width, height, title, NULL, NULL);
Expand All @@ -127,9 +125,8 @@ public Window(int width, int height, CharSequence title, boolean vsync) {
/* Center window on screen */
GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(id,
(vidmode.width() - width) / 2,
(vidmode.height() - height) / 2
);
(vidmode.width() - width) / 2,
(vidmode.height() - height) / 2);

/* Create OpenGL context */
glfwMakeContextCurrent(id);
Expand All @@ -150,6 +147,24 @@ public void invoke(long window, int key, int scancode, int action, int mods) {
}
};
glfwSetKeyCallback(id, keyCallback);

/* set resizing callbacks */
setResizeCallback(id);
}

private void setResizeCallback(long id) {
/* Set resize callback */
windowSizeCallback = new GLFWWindowSizeCallback() {

@Override
public void invoke(long window, int argWidth, int argHeight) {
width = argWidth;
height = argHeight;
hasResized = true;
}

};
glfwSetWindowSizeCallback(id, windowSizeCallback);
}

/**
Expand All @@ -174,8 +189,10 @@ public void setTitle(CharSequence title) {
* Updates the screen.
*/
public void update() {
hasResized = false;
glfwSwapBuffers(id);
glfwPollEvents();

}

/**
Expand All @@ -184,6 +201,7 @@ public void update() {
public void destroy() {
glfwDestroyWindow(id);
keyCallback.free();
windowSizeCallback.free();
}

/**
Expand All @@ -209,4 +227,15 @@ public boolean isVSyncEnabled() {
return this.vsync;
}

public int getWidth() {
return width;
}

public int getHeight() {
return height;
}

public boolean hasResized() {
return hasResized;
}
}