Hello Guest

LWJGL3: Input delay

  • 2 Replies
  • 4423 Views
LWJGL3: Input delay
« on: September 30, 2016, 17:11:18 »
Hey,
to start with lwjgl i render a simple square on the screen witch should be moved by WASD. This is working fine but when i stop pressing the key the square stops moving immediately and then moves again for a second or something (depends on how long i pressed the key) and stops moving after that again. This delay is generated by my extended GLFWKeyCallback class, when I print the action variable of the invoke methode i get somethink like this:

2
2
2
2
0 <-- this is the point I stopped pressing the key
1
2 <-- but it behaves as if the key would still be pressed
2
2
2
2
2
0 <-- until it realizes again that i stopped pressing the button

I don't know if this is a bug or just a fault by me but it is quite annoying.

This is the simple input class I am using like you find it in every tutorial:
Code: [Select]
package input;

import static org.lwjgl.glfw.GLFW.*;
import org.lwjgl.glfw.GLFWKeyCallback;

public class Input extends GLFWKeyCallback {
private static boolean[] keys = new boolean[600];

@Override
public void invoke(long window, int key, int scancode, int action, int mods) {
keys[key] = action != GLFW_RELEASE;
System.out.println(action);
}

public static boolean isKeyDown(int key) {
return keys[key];
}
}

*

Offline spasi

  • *****
  • 2261
    • WebHotelier
Re: LWJGL3: Input delay
« Reply #1 on: October 01, 2016, 19:39:58 »
I cannot reproduce this. The sequence I see is 1, 2, 2, ..., 0.

Could you share some details about your system? (OS/arch, LWJGL version, JVM version) Could you prepare a minimal standalone program that reproduces the issue for you?

Re: LWJGL3: Input delay
« Reply #2 on: October 02, 2016, 12:54:42 »
OS: Linux Mint 18
Java Version: 1.8.0_91
LWJGL Version: 3.0.0 build 90
OpenGL Version: 4.5.0 NVIDIA 361.42

This is waht's happen if I run the source below code: https://youtu.be/g2hXiK_44Es

Code: [Select]
package delay;
import org.lwjgl.*;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;

import static org.lwjgl.glfw.Callbacks.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryUtil.*;

public class Main {
private Input input;
private long window;

public void run() {

try {
init();
loop();

glfwFreeCallbacks(window);
glfwDestroyWindow(window);
} finally {
glfwTerminate();
glfwSetErrorCallback(null).free();
}
}

private void init() {
GLFWErrorCallback.createPrint(System.err).set();

if ( !glfwInit() )
throw new IllegalStateException("Unable to initialize GLFW");

glfwDefaultWindowHints();
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);

int WIDTH = 1280;
int HEIGHT = 720;

window = glfwCreateWindow(WIDTH, HEIGHT, "Input delay!", NULL, NULL);
if ( window == NULL )
throw new RuntimeException("Failed to create the GLFW window");

glfwSetKeyCallback(window,input = new Input());

GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
glfwSetWindowPos(
window,
(vidmode.width() - WIDTH) / 2,
(vidmode.height() - HEIGHT) / 2
);

glfwMakeContextCurrent(window);
glfwSwapInterval(1);

glfwShowWindow(window);
}

private void loop() {
GL.createCapabilities();
System.out.println("Java Version: " + System.getProperty("java.version"));
System.out.println("LWJGL Version: " + Version.getVersion());
System.out.println("OpenGL Version: " +glGetString(GL_VERSION));

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

while ( !glfwWindowShouldClose(window) ) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

if(input.isKeyDown(GLFW_KEY_SPACE)){
glClearColor(1.0f,0.0f,0.0f,0.0f);
}else{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}
glfwSwapBuffers(window);

glfwPollEvents();
}
}

public static void main(String[] args) {
new Main().run();
}

}

Code: [Select]
package delay;


import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWKeyCallback;

public class Input extends GLFWKeyCallback {

public boolean[] keys = new boolean[65536];

@Override
public void invoke(long window, int key, int scancode, int action, int mods) {
keys[key] = action != GLFW.GLFW_RELEASE;
System.out.println(action);
}

public boolean isKeyDown(int keycode) {
return keys[keycode];
}

}

Thank's for your effort