glfwGetKey сode is executed many times

Started by Viking_S_Puzom, May 03, 2020, 17:35:48

Previous topic - Next topic

Viking_S_Puzom

Hello everyone
I just start to study LWJGL using this tutorial https://www.youtube.com/watch?v=2Q7K2Ma5u1U&list=PLILiqflMilIxta2xKk2EftiRHD4nQGW0u&index=3

And i had two problems:

First, when i press W key, this part of code run 4 times, and my object shifted by 2f instead 0.5. (If i use some like System.out.println"Hello", instead " x += 0.5f;", "Hello" will type 4 times )

if(glfwGetKey(win, GLFW_KEY_W) == GL_TRUE){
                x += 0.5f;
            }


Second problem:
If i try to put breakpoint on this part of code for debug, when i start "debug" in intellij idea, created window "does not respond". And i can`t do some input and debug my first problem.

Please help me, and thanks in advance=)


All code:

import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.opengl.GL;
 
public class Main {
 
    public Main(){
        if(glfwInit() != true){
            System.err.println("GLFW Failed to initialize!");
            System.exit(1);
        }
 
        long win = glfwCreateWindow(640, 480, "Snake", 0, 0);
 
        glfwShowWindow(win);
 
        glfwMakeContextCurrent(win);
 
        GL.createCapabilities();
 
        float x = 0;
 
 
        while (!glfwWindowShouldClose(win)){
            glfwPollEvents();
 
            if(glfwGetKey(win, GLFW_KEY_W) == GL_TRUE){
                x += 0.5f;
            }
 
            glClear(GL_COLOR_BUFFER_BIT);
 
            glBegin(GL_QUADS);
 
                glColor4f(1, 0, 0, 0);
                glVertex2f(-0.5f + x, 0.5f);
 
                glColor4f(0, 1, 0, 0);
                glVertex2f(0.5f + x, 0.5f);
 
                glColor4f(0, 0, 1, 0);
                glVertex2f(0.5f + x, -0.5f);
 
                glColor4f(1, 1, 1, 0);
                glVertex2f(-0.5f + x, -0.5f);
            glEnd();
 
            glfwSwapBuffers(win);
        }
 
        glfwTerminate();
    }
 
    public static void main(String[] args){
        new Main();
    }
 
}

KaiHH

About the first problem:
Well, you are querying the state of the key every frame. So, given a refresh rate of 60Hz, then your loop will query the state of the key 60 times per second. If you hold the key down for a duration of 66 milliseconds, then four iterations of your loop will see that button pressed down.

About the second problem:
The reason why the window becomes unresponsive is because when you suspend the program execution via the debugger, then window messages sent by the OS will not be processed anymore (which happens by calling glfwPollEvents()). So the process does not react anymore to anything the OS is asking of it, and the OS will therefore assume the process has died and will indicate that to the user by overlaying any window the process has created.

Viking_S_Puzom

Quote from: KaiHH on May 03, 2020, 18:18:02
About the first problem:
Well, you are querying the state of the key every frame. So, given a refresh rate of 60Hz, then your loop will query the state of the key 60 times per second. If you hold the key down for a duration of 66 milliseconds, then four iterations of your loop will see that button pressed down.

About the second problem:
The reason why the window becomes unresponsive is because when you suspend the program execution via the debugger, then window messages sent by the OS will not be processed anymore (which happens by calling glfwPollEvents()). So the process does not react anymore to anything the OS is asking of it, and the OS will therefore assume the process has died and will indicate that to the user by overlaying any window the process has created.

Thank you so much, KaiHH. Really, as you said, the problem was in the button holding time
Please tell me if there is a way to debug such windows?