LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: chaosfox on July 23, 2011, 15:01:58

Title: getting events timestamp
Post by: chaosfox on July 23, 2011, 15:01:58

Hi there,

so, I'm trying to get the most accurate time of the key press, the main idea is getting System.nanoTime() or similar when we poll the keyboard, the problem is that the loop can't be running "free", we limit the loop with Display.sync so when the player press a key while the gameloop is at Display.sync there's input lag, in an attempt to solve that I decided to use a separate thread to poll the keyboard free of the gameloop, the code looks like this:


startRender {
[..GL inits..]
keyboardThread = new KeyboardThread(keyboardQueue); // ConcurrentLinkedQueue<KeyEvent>
keyboardThread.start();

gameLoop();
}

gameloop {
        gameRunning = true;
        while (gameRunning) {

            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
            GL11.glLoadIdentity();

            GL11.glScalef(scale_x, scale_y, 1);
            callback.frameRendering();

            Display.update(false);
            keyboardThread.processMessages();

            if (Display.isCloseRequested() || Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
                destroy();
            }
            if(sync_rate > 0)Display.sync(sync_rate);
        }
        Display.destroy();
}

private class KeyboardThread extends Thread {
volatile boolean active = true;
ConcurrentLinkedQueue<KeyEvent> queue;

public KeyboardThread(ConcurrentLinkedQueue<KeyEvent> queue) {
this.queue = queue;
}
@Override
public void run() {
while(active) {
processMessages();
try {sleep(5);} catch (InterruptedException ex) {}
}
}
public synchronized void processMessages() {
Display.processMessages();
while(Keyboard.next()) {
queue.offer(new KeyEvent(Keyboard.getEventKey(),SystemTimer.getTime()));
}
}
public void kill() {
active = false;
}
}


This works better than getting the time inside the gameloop, the problem is that sometimes, on linux, java dies right after the rendering start, there's no exception on the output just this low level message:
java: xcb_io.c:140: dequeue_pending_request: Assertion `req == dpy->xcb->pending_requests' failed.

Is what I'm doing right ? Is there any other way I can achieve this ?

Thank you for your time.
Title: Re: getting events timestamp
Post by: Fool Running on July 25, 2011, 13:00:47
To my knowledge, the Keyboard class is not thread-safe. It needs to be used on the same thread that Display.Update is run on. However, I am not sure of that.

Does Keyboard.getEventNanoseconds() not give you what you want? It is designed to give you the time when the event was generated, not the time you poll it. According to the documentation, the time returned is relative, but if you could find out what it it relative to, you might be able to get what you want.
Although, I'll admit I haven't actually used it myself. :P
Title: Re: getting events timestamp
Post by: chaosfox on July 25, 2011, 18:24:13
Quote from: Fool Running on July 25, 2011, 13:00:47
Does Keyboard.getEventNanoseconds() not give you what you want? It is designed to give you the time when the event was generated, not the time you poll it. According to the documentation, the time returned is relative, but if you could find out what it it relative to, you might be able to get what you want.
Although, I'll admit I haven't actually used it myself. :P

Yes ! actually using getEventNanoseconds() would be perfect if I had a reference to which adjust my game time to, but the documentation says I shouldn't rely on it except to compare with other key events so I'm lost on what to do..