cpu at 100%

Started by vilm, January 22, 2004, 09:58:14

Previous topic - Next topic

vilm

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

princec

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 :)

vilm

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

Morgrog

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?)

elias

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.

vilm

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

elias

Sounds about right.

- elias

princec

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 :)

vilm

hey - appreciate the code snippet, thanks!

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

cheers

- vilm

princec

"say cheese for the camera" :)

Cas :)