I had been trying to make a game engine and got nothing but problems. Here are the problems.
- Different speeds on each run
- Highly slowed rendering
- The game freezes after 5-10 min
And here’s my code.
/* Only run this in another thread */
public void gameLoop(){
long UPS = 1000/30;
long expectedTime = 1000/UPS; // 30ms
long elapsedTime = 0;
while (running){
long start = getCurrentTime(); // in ms
if (elapsedTime<=expectedTime){
updateGame(elapsedTime);
displayGame();
}
long end = getCurrentTime();
elapsedTime = end - start;
long sleepTime = expectedTime - elapsedTime;
if (sleepTime>0){
try {
Thread.sleep(sleepTime);
} catch (Exception e){}
}
}
}
Could anyone shed some light please?