getting events timestamp

Started by chaosfox, July 23, 2011, 15:01:58

Previous topic - Next topic

chaosfox


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.

Fool Running

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
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

chaosfox

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..