我制作了一款安卓游戏,将其作为主游戏循环:
// desired fps
private final static int MAX_FPS = 60;
// maximum number of frames to be skipped
private final static int MAX_FRAME_SKIPS = 5;
// the frame period
private final static int FRAME_PERIOD = 1000 / MAX_FPS;
int sleepTime = 0; // ms to sleep (<0 if we're behind)
int framesSkipped; // number of frames being skipped
private float dt = 0;
@Override
public void onDrawFrame(GL10 gl)
{
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT );
dt = game.gTimer.update(false);
doDraw(gl);
doUpdate(20); // <<-- UPDATE (Step = 20units)
sleepTime = (int)(FRAME_PERIOD - dt); // calculate sleep time
if (sleepTime > 0) { // if sleepTime > 0 we're OK
try
{
// send the thread to sleep for a short period
// very useful for battery saving
Thread.sleep(sleepTime);
} catch (InterruptedException e) {}
}
while (sleepTime < 0 && framesSkipped < MAX_FRAME_SKIPS) {
// we need to catch up - update without rendering
doUpdate(20); // <<-- UPDATE (Step = 20units)
// add frame period to check if in next frame
sleepTime += FRAME_PERIOD;
framesSkipped++;
}
gl.glFlush();
}
问题是它在快速设备(例如三星Galaxy S3)上运行速度较慢(约40fps !!!预期&gt; 50)并且在较慢的设备上运行速度较快(范围为42~70fps)(三星Galaxy mini,Allview AllDro)速度,LG Optimus One)
UPDATE - 渲染代码(3d对象/多维数据集):
public void render(GL10 gl, int texID)
{
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
//Bind texture in this case
gl.glBindTexture(GL10.GL_TEXTURE_2D, texID);
//Enable the vertex and texture state
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer[0]);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer[0]);
//Draw the vertices as triangles, based on the Index Buffer information
gl.glDrawElements(GL10.GL_TRIANGLES, indexBuffer[0].capacity(), GL10.GL_UNSIGNED_SHORT, indexBuffer[0]);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}
我只在内存中加载一个3d对象。 (在doDraw中呈现10次)。 我有一个ArrayList&lt;&gt;立方体/ 3d对象的3d位置和物理跟踪。