无帧速率限制的移动C ++ SFML [英] Movement Without Framerate Limit C++ SFML

查看:151
本文介绍了无帧速率限制的移动C ++ SFML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在用SFML制作游戏,但是我一直在不受帧率限制的情况下陷入困境.现在,我弄清楚如何在所有计算机上获得一致帧率的唯一方法是使用

I'm working an making a game in SFML right now, but I am getting stuck on movement without a framerate limit. Right now the only way I have figured out how to get consistent framerate on all computers is by using

window.setFramerateLimit(30);

我想找到一种没有帧率上限的方法,以便在更好的计算机上看起来更好,这样,如果有人计算机速度真的很慢,他们仍然可以玩游戏.最好的方法是什么.

I want to find a way to not have a framerate cap so it does look better on better computers and so that if anybody has a really slow computer, they can still play the game. What would be the best way to do this.

推荐答案

您应该将自上一帧以来经过的时间传递给需要绘制的对象,然后计算该对象必须移动的空间,例如这个:

You should pass the time that has elapsed since the last frame to the object that needs to be drawn, and then calculate the space the object has to move, like this:

sf::Clock clock;
int speed = 300;


//Draw func that should be looped
void Draw()
{
    sf::Time elapsedTime = clock.restart();
    float tempSpeed = elapsedTime.asSeconds() * speed;
    drawnObject.move(tempSpeed, 0);
    drawnObject.draw(window);
}

这样,无论FPS大小如何,"drawnObject"每秒都会向右移动300(像素?)

This way, the 'drawnObject' will move 300 (pixels?) per second to the right regardless of the FPS

这篇关于无帧速率限制的移动C ++ SFML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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