FPS problem on android
I have a small problem with my implementation on android. I'm rendering some stuff with OpenGL ES. And for each frame, I have to update my date. So I decided to put a maximum FPS to avoid useless computation.
But the thing is, whatever is whatever the number of frame per second I put, I don't see any difference. Even if I put 1 fps.
Here's a bit for my code:
long now = SystemClock.elapsedRealtime(), diff;
diff = now - CURRENT_TIME;
if (diff < MILLISECOND_PER_FRAME)
{
try
{
wait(MILLISECOND_PER_FRAME - diff);
}
catch(Exception e)
{}
now = SystemClock.elapsedRealtime();
diff = now - CURRENT_TIME;
}
CURRENT_TIME = now;
So, that piece of code doesn't seem to slow down the execution at all. Any suggestion ?
And, just i 开发者_开发技巧case anyone has doubt, for X fps, I compute MILLISECOND_PER_FRAME as
MILLISECOND_PER_FRAME = (int)(1000.0f / X);
SOLVED:
Okay, so I solved that problem. If any of you want to for a thread to sleep for a specific amount of millisecond, use Thread.sleep(millis);. This function affects the thread in which it's running. And now I see the big difference when I put 1 fps versus 25 fps.
Thanks to everyone who took a look at this question.
Cheers,
although you have solved the issue, it seems you are doing it from the wrong end. It would be better to leave rendering at it's speed and only update application state at certain intervals. That means no thread sleeping, just a condition to see if enough time had elapsed to make the update.
This seems like a cleaner solution. I'm not sure what effects will it have on your phone if you brake the rendering thread (because it works with graphics hardware, you actually may be stalling some other (system) threads as well). Not sure if that is a good thing to do.
On the other hand, for some applications not rendering anything may make sense (but would be implemented much better by returning from the rendering function immediately, without swapping front and back buffers - which i'm not sure is possible on Android).
精彩评论