LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: vilm on January 22, 2004, 09:58:14

Title: cpu at 100%
Post by: vilm on January 22, 2004, 09:58:14
Hi all,
 Still getting to grips with lwjgl...  I find that my programs run at 100% cpu (according to the XP process monitor), is that normal?  e.g. the very simple starfield code i posted in http://www.puppygames.net/forums/viewtopic.php?t=393&sid=0d9e648c30023125d8a51f0bde411036

Is this just a case of me having a cheap video card (riva tnt)?  I assumed that Window.paint(); blocks until the end of a vertical refresh? (at least if vsynch enabled is on) and so the cpu will be free once it's done the tiny bit of game loop processing... i don't know :?  (processor is ~1.8Ghz p4 celeron btw)
 can someone explain?

Cheers,

- vilm
Title: cpu at 100%
Post by: princec on January 22, 2004, 11:37:33
Window.paint() should never be relied on to wait as vsync might not be enabled. I advise you spin on the hires timer (with a Thread.yield() in there) until your frame time is up too. That'll use a fair bit of CPU but it's pre-emptible CPU, so fairly harmless to multitasking.

I suspect the reason you have 100% at the moment is because you haven't got vsync. In windowed mode, you don't get vysnc (yet), so it's always best to put that hires spin in whatever.

Cas :)
Title: cpu at 100%
Post by: vilm on January 22, 2004, 13:34:43
Ok, thx for the info.
So, basically shouldn't assume that vsynch is available (does that mean i should always set it to false, in case it is switched on?).  Then:
- get the freq from the display mode
- get the timer resolution
- work out the number of ticks = 1 frame
(start game loop)
at the start of the game loop reset the hires timer
at the end of the game loop spin (with Thread.yield) until the timer is at 1 frame worth

does that sound right?  

cheers

 - vilm
Title: cpu at 100%
Post by: Morgrog on January 22, 2004, 13:49:12
ohhh, that's exactly what I wanted to read, I mean how to implement a framerate controller :)

now if I could only have a bit more info (skeleton code anyone? :twisted:) that'd be great! or just a few pointers
(you can't always have vsync running eh? specially since it's Windows specific and we are after all a xplatform community)

Also, what's a good frame rate 60? (that's the vsync value eh?)
Title: cpu at 100%
Post by: elias on January 22, 2004, 14:33:25
Vsync isn't windoze only at all - it works nicely in Mac and on linux with never nvidia drivers. And you shouldn't disable vsync just because you use a spin loop, because vsync eliminates the display tearing resulting from a frame update in the middle of the monitor refresh.
Title: cpu at 100%
Post by: vilm on January 22, 2004, 16:02:08
ok, so set vsynch to true, but also implement the spinning alg i mention above in case vsynch isn't enabled? is that right?

cheers,

- vilm
Title: cpu at 100%
Post by: elias on January 22, 2004, 17:09:09
Sounds about right.

- elias
Title: cpu at 100%
Post by: princec on January 22, 2004, 17:32:10
The AF code looks like this:

if (Window.isVSyncEnabled()) {
long now = Sys.getTime();
frameTime = (float)(now - then) / (float)Sys.getTimerResolution();
} else
frameTime = 0.0f;

if (Window.isDirty() || !cheese) {
GL.glFlush();
Window.paint();
}

if (hasSound()) {
soundPlayer.play();
}

float elapsed = 0.0f;

do {
long now = Sys.getTime();
elapsed = (float)(now - then) / (float)Sys.getTimerResolution();
if (frameTime == 0.0f)
frameTime = elapsed;
Thread.yield();
} while (elapsed < options.getFrameTime());


Cas :)
Title: cpu at 100%
Post by: vilm on January 23, 2004, 10:06:59
hey - appreciate the code snippet, thanks!

 wonder what the cheese variable is...(!)

cheers

- vilm
Title: cpu at 100%
Post by: princec on January 23, 2004, 11:02:27
"say cheese for the camera" :)

Cas :)