Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting games to run at a consistent frame (cycle) rate?

 
Hank Haroldson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Currently, to have my game run at a consistent frame (or should I say cycle) rate, I:
1) Determine the Cycles Per Second I need, for this example 40
2) After each cycle is complete, I have the game Sleep for the remainder of time allocated to that cycle.

Problem is, this process seems to create a big performance hit on my game. Even if I jack up the Cycles Per Second, the game still runs slow. However, if I removed this code, the game flys way too fast (in the hundreds of Cycles Per Second). Is there a better way to accomplish this?
 
Ramender Mall
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

As far as increasing the FPS is concerned...I saw a game, which really did not respond well with very high FPS, as developer had used the FPS in an equation of his game, which was giving this negative effect...
if you could post the portion of the code that deals with your thread's sleeping for the remaining time, the problem may get sorted out...

Ramy...
[ January 12, 2006: Message edited by: Ramender Mall ]
 
Matt Wil
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the exact same problem as Hank.. Here is the slow code:

long timeSinceStart = (cycleStartTime - System.currentTimeMillis());
if (timeSinceStart < MS_PER_FRAME){
try {
Thread.sleep(MS_PER_FRAME - timeSinceStart);
} catch(java.lang.InterruptedException e){}
}
 
Eduardo Marques
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you considered the use of TimerTask and Timer from the MIDP API?
I believe that Timer.scheduleAtFixedRate() can produce the intended behaviour: the TimerTask procedure will run with a fixed time rate.
See the MIDP documentation for more details.


[ January 15, 2006: Message edited by: Eduardo Marques ]
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!