bigearsman
bigearsman

Reputation: 41

java game stuttering on faster cpu

I've been reading a book on android game programming (The beginner's guide to android game development) and well I kinda started making my own game.

It's a java game using the book's framework. (The book starts with making games in java before moving to android).

Anyways I made my game and it works smoothly on my laptop which has a modest i3 processor 2.4GHz, I pretty much did all my coding and testing on it.

I got a new desktop with an i5 3.3 GHz processor so I decided to try out my game there.

However, on my presumably much faster desktop the game seems to run very choppy. There seems to be a stutter and I have no idea what's causing it because I didn't change any code.

My deltas are consistent on both machines, I should be getting a smooth ~60fps but it is simply not smooth on the desktop.

I noticed reducing the max sleep time (basically raising the frame rate) did seem to help a bit, but all my functions are based on a maximum 60fps.

I'm not that great with this level of programming (you know with threads and so forth).

Any ideas on what might be the problem, I want to think it's something to do with the game loop and the faster processor.

Upvotes: 4

Views: 321

Answers (2)

Edwin Buck
Edwin Buck

Reputation: 70909

As far as why your faster CPU is stuttering, I'm going to guess it is because of the code in updateAndRender(...). Odds are that method both updates and renders, when you probably have many updates, but want to skip a few renderings.

With game loops and these kind of timing loops in general, keep in mind that your loop is going to both "run ahead" of the wanted time and "fall beahind" the wanted time.

This means that expecting your loop to "fall in line" is something of an unrealistic expectation. Once you sleep, you concede control to the Operating System. The Operating System might have you sleep for exactly the requested time, but odds are good that if it has nothing better to do, it might wake you up early, and if it is busy, it might wake you up late.

With this in mind, I would first determine how many "ticks" in the game you have slept for and update the world state. Then determine if you have slept long enough for a screen update, and if not, wait. If you have, then check to see if you need more than one, and if so, only do the "last" one. If you "skip" too many screen updates (4 is a good number) then just exit out in error indicating that the screen can't draw fast enough (15 fps with your goals).

Upvotes: 2

sh0rug0ru
sh0rug0ru

Reputation: 1626

The Thread.sleep method by itself is not a reliable way to maintain a framerate. The most accurate way to maintain framerate is to take a System.currentTimeMillis() measurement at the start of rendering, and then call Thread.sleep(1) in a loop after rendering until the delta from subsequent System.currentTimeMillis() measurements exceeds the framerate threshold.

See this answer for more details.

Upvotes: 1

Related Questions