From 0b4f524ff9f0bb64399af0a3c7af14269e4fd87a Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Sat, 16 Nov 2002 19:46:18 +0000 Subject: [PATCH] Added Sys timer support --- src/native/linux/Game.java | 6 +++++- src/native/linux/org_lwjgl_Sys.c | 22 +++++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/native/linux/Game.java b/src/native/linux/Game.java index 78f217a4..cd83514a 100644 --- a/src/native/linux/Game.java +++ b/src/native/linux/Game.java @@ -118,10 +118,12 @@ public final class Game { if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) finished = true;*/ Keyboard.read(); - if (Keyboard.getNumKeyboardEvents() > 0) { + for (int i = 0; i < Keyboard.getNumKeyboardEvents(); i++) { Keyboard.next(); if (Keyboard.key == Keyboard.KEY_ESCAPE && Keyboard.state) finished = true; + if (Keyboard.key == Keyboard.KEY_T && Keyboard.state) + System.out.println("Current time: " + Sys.getTime()); } } @@ -149,6 +151,8 @@ public final class Game { Keyboard.create(); Keyboard.enableBuffer(); Mouse.create(); + Sys.setTime(0); + System.out.println("Timer resolution: " + Sys.getTimerResolution()); // Go into orthographic projection mode. gl.matrixMode(GL.PROJECTION); gl.loadIdentity(); diff --git a/src/native/linux/org_lwjgl_Sys.c b/src/native/linux/org_lwjgl_Sys.c index 687b08b8..acb8a104 100644 --- a/src/native/linux/org_lwjgl_Sys.c +++ b/src/native/linux/org_lwjgl_Sys.c @@ -39,6 +39,7 @@ * @version $Revision$ */ +#include #include "org_lwjgl_Sys.h" long int hires_timer_freq; // Hires timer frequency @@ -78,6 +79,20 @@ JNIEXPORT jlong JNICALL Java_org_lwjgl_Sys_getTimerResolution return hires_timer_freq; } +long queryTime(void) { + struct timeval tv; + if (gettimeofday(&tv, NULL) == -1) { +#ifdef _DEBUG + printf("Could not read current time\n"); +#endif + } + long result = tv.tv_sec * 1000000l + tv.tv_usec; +#ifdef _DEBUG + printf("Current time (native): %ld\n", result); +#endif + return result; +} + /* * Class: org_lwjgl_Sys * Method: getTime @@ -86,7 +101,7 @@ JNIEXPORT jlong JNICALL Java_org_lwjgl_Sys_getTimerResolution JNIEXPORT jlong JNICALL Java_org_lwjgl_Sys_getTime (JNIEnv * env, jclass clazz) { -// QueryPerformanceCounter((LARGE_INTEGER*) &hires_timer); + hires_timer = queryTime(); hires_timer -= hires_timer_start; return hires_timer; } @@ -99,8 +114,9 @@ JNIEXPORT jlong JNICALL Java_org_lwjgl_Sys_getTime JNIEXPORT void JNICALL Java_org_lwjgl_Sys_setTime (JNIEnv * env, jclass clazz, jlong startTime) { -/* QueryPerformanceFrequency((LARGE_INTEGER*) &hires_timer_freq); - QueryPerformanceCounter((LARGE_INTEGER*) &hires_timer_start);*/ + hires_timer_start = queryTime(); + // We don't have a real resolution so assume highest possible + hires_timer_freq = 1000000; hires_timer_start -= startTime; }