Updating too fast!

Hey, I’m going to get straight to the point. I’m working on a game using the JOGL and when I run the game after loading it up for the first time it updates way too fast, but if I restarted the level it runs fine. I was thinking that maybe its a problem with the way I’m updating but I can’t really find the problem. Can anyone help me?

Heres my updating code:


private int MAX_FRAME_SKIPS = 5;

int fps =60; // target frame rate
int noDelays = 0;

long beforeTime, afterTime, timeDiff, sleepTime;
long overSleepTime = 0L;
long excess = 0L;
long period=(long) (1000000000.0f / (float) fps);

boolean running = false;


public void run()
{
	beforeTime = System.nanoTime(); // calculates the time before we start
									// rendering and updating the game state
	running=true;
	while(running) 
	{
		display(); // render 1 frame of the scene and update game state all in
					// one method
		
		afterTime = System.nanoTime(); // calculates the time after we've
										// rendered and updated the game state
		
		timeDiff = afterTime - beforeTime; // calculates the time it took to
											// render 1 frame of the scene and
											// update the game state
		
		sleepTime = (period - timeDiff) - overSleepTime; // figure out how
															// much time we have
															// to sleep
	
		if (sleepTime > 0) // is there some time left in this cycle
		{ 
			try 
			{
				
				
				sleep(sleepTime/1000000L); // convert nano -> ms
			}
			catch(InterruptedException ex){}
			overSleepTime =
				(System.nanoTime() - afterTime) - sleepTime; // how much did
																// we oversleep?
		}
		else // go to here if sleepTime <= 0; took longer than the allotted
				// time to render and update the game
		{ 
			excess -= sleepTime; // store excess time value
			overSleepTime = 0L;
			if (++noDelays >= NO_DELAYS_PER_YIELD) 
			{
				
				yield(); // give another thread a chance to run so we dont
							// take up all the cpu cycles
				
				noDelays = 0;
			}

		}
		beforeTime = System.nanoTime();// calculates the time before we start
										// rendering and updating the game state
		
		/*
		 * If frame animation is taking too long, update the game state without
		 * rendering it, to get the updates/sec nearer to the required FPS.
		 */
		
		int skips = 0;
		while((excess > period) && (skips < MAX_FRAME_SKIPS))
		{
			excess -= period;
			renderer.update(); // update game state but don't render the scene
			skips++;
		}
	}


}

Thanks!

Hi

I think you have make it sleep longer.

kuhnibert

I fixed the problem. The problem was that, when the game switched from the menu screen to the actual game itself some how the ‘excess’ variable gets screwed up. The excess variable tells how much time we have to do frame skipping. I fixed this by setting the excess variable to zero until you are actually in the game. Therefore no frame skipping takes place on the menu screens.

Just in case anyone else might need help with this I will post the updated code. Thanks to all!


private int MAX_FRAME_SKIPS = 5;

int fps =60; // target frame rate
int noDelays = 0;

long beforeTime, afterTime, timeDiff, sleepTime;
long overSleepTime = 0L;
long excess = 0L;
long period=(long) (1000000000.0f / (float) fps);

boolean running = false;


public void run()
{
	beforeTime = System.nanoTime(); // calculates the time before we start
									// rendering and updating the game state
	running=true;
	while(running) 
	{
		display(); // render 1 frame of the scene and update game state all in
					// one method
		
		afterTime = System.nanoTime(); // calculates the time after we've
										// rendered and updated the game state
		
		timeDiff = afterTime - beforeTime; // calculates the time it took to
											// render 1 frame of the scene and
											// update the game state
		
		sleepTime = (period - timeDiff) - overSleepTime; // figure out how
															// much time we have
															// to sleep
	
		if (sleepTime > 0) // is there some time left in this cycle
		{ 
			try 
			{
				
				
				sleep(sleepTime/1000000L); // convert nano -> ms
			}
			catch(InterruptedException ex){}
			overSleepTime =
				(System.nanoTime() - afterTime) - sleepTime; // how much did
																// we oversleep?
		}
		else // go to here if sleepTime <= 0; took longer than the allotted
				// time to render and update the game
		{ 
			excess -= sleepTime; // store excess time value
			overSleepTime = 0L;
			if (++noDelays >= NO_DELAYS_PER_YIELD) 
			{
				
				yield(); // give another thread a chance to run so we dont
							// take up all the cpu cycles
				
				noDelays = 0;
			}

		}
		beforeTime = System.nanoTime();// calculates the time before we start
										// rendering and updating the game state
		
		/*
		 * If frame animation is taking too long, update the game state without
		 * rendering it, to get the updates/sec nearer to the required FPS.
		 */
		
		int skips = 0;

           if(renderer.inGame)
           {
     		while((excess > period) && (skips < MAX_FRAME_SKIPS))
		{
			excess -= period;
			renderer.update(); // update game state but don't render the scene
			skips++;
		}
           }
           else
           {
           excess=0;
           }
 
	}


}