恒FPS的Andr​​oid OpenGLES [英] Constant FPS Android OpenGLES

查看:160
本文介绍了恒FPS的Andr​​oid OpenGLES的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好Android开发者,

Hello android developers,

我正在开发使用OpenGLES 1.0一个简单的游戏为Android在Eclipse中。我使用三星Galaxy S2的Andr​​oid(2.3)作为设备的发展。

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.

所以,我已成功创建GLSurfaceView并覆盖onDrawFrame()函数,我调用LogicUpdate(deltatime)函数和渲染()函数。

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.

我正的问题是与双核。如果我通过进入设置 - >省电,检查系统节能我意识到,渲染自动锁定在30 FPS禁用双核。但是,如果我能双核心通过取消系统节能我看到的渲染锁定在60帧,但手机变热,这水渠电池真快。

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.

这样的想法是让我的游戏运行在30 FPS节省电池。

So the idea is keep my game run at 30 FPS to save some battery.

所以,我用的是code波纹管做到这一点。

So to do this I use the code bellow.

在我做的逻辑更新我称之为和平code,记住这一切都是在onDrawFrame()。完成

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
    }                           
}

其中CONST_FPS = 30

Where CONST_FPS = 30

然后

StartTime = System.currentTimeMillis(); //

UpdateLogic(1.0 / CONST_FPS);
Render();

游戏在30帧非常平滑,主要是因为它不需要锁定的FPS。 但试图锁定60FPS到30 FPS当我口吃。我做了一些研究,发现了Thread.sleep()方法是不是precise。这是真的?我还有什么可以做的锁定60FPS为30FPS的时候,使游戏更加流畅。

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.

感谢您的回答...

推荐答案

您应该使用经过时间扩展所有的运动,使运动保持在不同的FPS速度流畅。你可以得到经过时间是这样的:

You should use elapsed time to scale all your movement, so that the movement stays smooth at varying FPS rates. You can get elapsed time like this:

long currentTime = System.currentTimeMillis();
float elapsed = (System.currentTimeMillis() - lastFrameTime) * .001f;//convert ms to seconds
lastFrameTime = currentTime; 

然后EX preSS以秒为单位的速度和更新的位置是这样的:

Then express your velocities in units per second, and update position like this:

sprite.x += (sprite.xspeed * elapsed);

这篇关于恒FPS的Andr​​oid OpenGLES的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆