LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: aboogesnickle on January 18, 2016, 17:34:12

Title: LWJGL 3.0.0 Beta move or resize window not working right
Post by: aboogesnickle on January 18, 2016, 17:34:12
I made a fps and ups counter that works fine until you move or resize the window. After that it just goes crazy.

public static void run() {
long lastTime = System.nanoTime();
long now;
long timer = System.currentTimeMillis();
double delta = 0;
int frames = 0;
int updates = 0;
while (running) {
if (Window.shouldClose() == true) {
stop();
}
now = System.nanoTime();
delta += (now - lastTime) / Final.NS;
lastTime = now;
while (delta >= 1) {
update();
updates++;
delta--;
}
render();
frames++;
if (System.currentTimeMillis() - timer > 1000) {
timer += 1000;
System.out.println(updates + " ups, " + frames + " fps");
updates = 0;
frames = 0;
}
}
dispose();
}


This is the counter code for the game loop.

package engine;

import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWCursorPosCallback;
import org.lwjgl.glfw.GLFWFramebufferSizeCallback;
import org.lwjgl.glfw.GLFWKeyCallback;
import org.lwjgl.glfw.GLFWMouseButtonCallback;
import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.glfw.GLFWWindowSizeCallback;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL11;
import org.lwjgl.system.MemoryUtil;

import engine.input.Cursor;
import engine.input.Keyboard;
import engine.input.Mouse;
import engine.util.Final;

public class Window {

private static String title = Final.TITLE;
private static long id;
private static int width = Final.WINDOW_WIDTH;
private static int height = Final.WINDOW_HEIGHT;
private static boolean fullscreen = Final.WINDOW_FULLSCREEN;
private static boolean vsync = Final.WINDOW_VSYNC;
private static boolean visible = Final.WINDOW_VISIBILITY;
private static boolean resizable = Final.WINDOW_RESIZABLE;

private static final GLFWFramebufferSizeCallback FRAMEBUFFER_SIZE_CALLBACK = new GLFWFramebufferSizeCallback(){
@Override
public void invoke(long window, int width, int height) {
GL11.glViewport(0, 0, width, height);
}
};
private static final GLFWWindowSizeCallback WINDOW_SIZE_CALLBACK = new GLFWWindowSizeCallback(){
@Override
public void invoke(long window, int width, int height) {
Window.width = width;
Window.height = height;
}
};
private static final GLFWKeyCallback KEY_CALLBACK = new Keyboard();;
private static final GLFWMouseButtonCallback MOUSE_BUTTON_CALLBACK = new Mouse();
private static final GLFWCursorPosCallback CURSOR_POS_CALLBACK = new Cursor();

public Window() {
if (GLFW.glfwInit() != GLFW.GLFW_TRUE) {
throw new IllegalStateException("Unable to initialize GLFW");
}
GLFW.glfwDefaultWindowHints();
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, GLFW.GLFW_TRUE);
GLFW.glfwWindowHint(GLFW.GLFW_VISIBLE, visible ? GLFW.GLFW_TRUE : GLFW.GLFW_FALSE);
GLFW.glfwWindowHint(GLFW.GLFW_RESIZABLE, resizable ? GLFW.GLFW_TRUE : GLFW.GLFW_FALSE);
id = GLFW.glfwCreateWindow(width, height, title, fullscreen ? GLFW.glfwGetPrimaryMonitor() : MemoryUtil.NULL, MemoryUtil.NULL);
if (id == MemoryUtil.NULL) {
GLFW.glfwTerminate();
throw new RuntimeException("Failed to create the GLFW window");
}
GLFWVidMode vidmode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor());
GLFW.glfwSetWindowPos(id, (vidmode.width() - width) / 2, (vidmode.height() - height) / 2);
GLFW.glfwSwapInterval(vsync ? 1 : 0);
GLFW.glfwMakeContextCurrent(id);
FRAMEBUFFER_SIZE_CALLBACK.set(id);
WINDOW_SIZE_CALLBACK.set(id);
KEY_CALLBACK.set(id);
MOUSE_BUTTON_CALLBACK.set(id);
CURSOR_POS_CALLBACK.set(id);
GL.createCapabilities();
}

public static void update() {
GLFW.glfwSwapBuffers(id);
GLFW.glfwPollEvents();
}

public static void dispose() {
GLFW.glfwDestroyWindow(id);
FRAMEBUFFER_SIZE_CALLBACK.release();
WINDOW_SIZE_CALLBACK.release();
KEY_CALLBACK.release();
MOUSE_BUTTON_CALLBACK.release();
CURSOR_POS_CALLBACK.release();
GLFW.glfwTerminate();
}

public static void setWidth(int width) {
Window.width = width;
}

public static int getWidth() {
return width;
}

public static void setHeight(int height) {
Window.height = height;
}

public static int getHeight() {
return height;
}

public static void setTitle(String title) {
Window.title = title;
}

public static void setVsync(boolean vsync) {
Window.vsync = vsync;
GLFW.glfwSwapInterval(vsync ? 1 : 0);
}

public static void setVisiblity(boolean visible) {
Window.visible = visible;
if(visible){
GLFW.glfwShowWindow(id);
}else{
GLFW.glfwHideWindow(id);
}
}

public static long getID() {
return id;
}

public static boolean shouldClose() {
return GLFW.glfwWindowShouldClose(id) == GLFW.GLFW_TRUE;
}
}


This is the Window class I am using
Title: Re: LWJGL 3.0.0 Beta move or resize window not working right
Post by: Kai on January 18, 2016, 19:20:13
Redirect: http://www.java-gaming.org/topics/lwjgl-3-ups-fps-counter-goes-crazy/37050/view.html