How to Handle frame rates and synchronizing screen repaints
Posted
by
David Kroukamp
on Game Development
See other posts from Game Development
or by David Kroukamp
Published on 2012-11-07T20:19:49Z
Indexed on
2012/11/07
23:15 UTC
Read the original article
Hit count: 419
I would first off say sorry if the title is worded incorrectly.
Okay now let me give the scenario
I'm creating a 2 player fighting game, An average battle will include a Map (moving/still) and 2 characters (which are rendered by redrawing a varying amount of sprites one after the other).
Now at the moment I have a single game loop limiting me to a set number of frames per second (using Java):
Timer timer = new Timer(0, new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
long beginTime; //The time when the cycle begun
long timeDiff; //The time it took for the cycle to execute
int sleepTime; //ms to sleep (< 0 if we're behind)
int fps = 1000 / 40;
beginTime = System.nanoTime() / 1000000;
//execute loop to update check collisions and draw
gameLoop();
//Calculate how long did the cycle take
timeDiff = System.nanoTime() / 1000000 - beginTime;
//Calculate sleep time
sleepTime = fps - (int) (timeDiff);
if (sleepTime > 0) {//If sleepTime > 0 we're OK
((Timer)e.getSource()).setDelay(sleepTime);
}
}
});
timer.start();
in gameLoop()
characters are drawn to the screen ( a character holds an array of images which consists of their current sprites) every gameLoop()
call will change the characters current sprite to the next and loop if the end is reached.
But as you can imagine if a sprite is only 3 images in length than calling gameLoop()
40 times will cause the characters movement to be drawn 40/3=13 times. This causes a few minor anomilies in the sprited for some charcters
So my question is how would I go about delivering a set amount of frames per second in when I have 2 characters on screen with varying amount of sprites?
© Game Development or respective owner