Search code examples
javaopengllwjgl

LWJGL Game Loop not working


I am trying to make a simple game in LWJGL 3 but I am stuck in place with my main game loop not working;

float fps = 60;
double ns = 1000000000 / fps;
long last = System.nanoTime();
double delta = 0;

while(glfwWindowShouldClose(window) != GL_TRUE){
    delta += (System.nanoTime() - last) / ns;
    System.out.println(delta);
    last = System.nanoTime();
    while(delta-- >= 1){
        update();
    }
    render();
}

I added the print to test what is going on and I noticed that delta is always less than zero, which shouldn't ever happen;

I am using LWJGL 3 with Java 1.8 SE


Solution

  • Why do you think that it is impossible to get negative values?

    This expression

    while(delta-- >= 1)
    

    will decrease delta by 1 at least once, no matter if the loop is taken or not.