Hello android developers,
I am developing a simple game for Android in Eclipse using OpenGLES 1.0. I am using Samsung Galaxy S2 Android(2.3) as a device for development.
And I have a question about dual core and making frame rate constant.
So I have managed creating GLSurfaceView and override onDrawFrame() function where I call LogicUpdate(deltatime) function and Render() function.
Yes, all in single thread for now.
The problem I am getting is with dual core. If I disable dual core by going to Setting->Power saving and check System power saving I realize that rendering is automatically locked at 30 FPS. But if I enable dual core by unchecking System power saving I see that rendering is locked at 60 FPS but, phone gets hot and it drains battery really fast.
So the idea is keep my game run at 30 FPS to save some battery.
So to do this I use the code bellow.
Before I do logic update I call this peace of code, remember all this is done in onDrawFrame().
if( CONST_FPS > 0 && StartTime > 0 )
{
/////////////////////////////////////////////////////////////////
// Get frame time
////////////////////////////////////////////////////////////////
long endTime = System.currentTimeMillis();
long time = endTime - StartTime;
//
long wantedtime = 1000 / CONST_FPS;
//
long wait = 0;
if( time < wantedtime )
{
wait = wantedtime - time;
//
Thread.sleep(wait);
}
else
{
//Time to big game will slow down
}
}
Where CONST_FPS = 30
And then
StartTime = System.currentTimeMillis(); //
UpdateLogic(1.0 / CONST_FPS);
Render();
Gameplay at 30 FPS is very smooth mainly because it does not need to lock FPS. BUT, when trying to lock 60FPS to 30 FPS I get stuttering. I did some research and found out that Thread.Sleep() is not precise. Is this true? What else can I do to make gameplay more smooth when locking 60FPS to 30FPS.
Thanks for the answer ...