gpt4 book ai didi

c++ - SFML游戏开发书籍——理解FPS计算

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:21:15 24 4
gpt4 key购买 nike

这是我的第一篇文章,所以我希望我做对了。

我目前正在研究 SFML 游戏开发手册。我试图了解介绍示例中 FPS 的计算方式。

我的游戏类是这样的:

#include "Game.h"

const sf::Time Game::TIME_PER_FRAME = sf::seconds(1.f/60.f);
const float Game::PLAYER_SPEED = 100.f;

Game::Game() : Window(sf::VideoMode(640,480), "ORGELLA") {

isMovingUp = false;
isMovingDown = false;
isMovingLeft = false;
isMovingRight = false;

Player.setRadius(40.f);
Player.setPosition(100.f, 100.f);
Player.setFillColor(sf::Color::Cyan);

font.loadFromFile(resourcePath() + "sansation.ttf");
statsText.setFont(font);
statsText.setPosition(5.f, 5.f);
statsText.setCharacterSize(10);
}

void Game::run() {
sf::Clock clock;
sf::Time timeSinceLastUpdate = sf::Time::Zero;


while (Window.isOpen()) {
sf::Time dt = clock.restart();
timeSinceLastUpdate += dt;

while (timeSinceLastUpdate > TIME_PER_FRAME) {
timeSinceLastUpdate -= TIME_PER_FRAME;

handleEvents();
update(TIME_PER_FRAME);
}

updateGameStats(dt);
render();
}
}

void Game::handleEvents() {
sf::Event event;
while (Window.pollEvent(event)) {
switch (event.type) {
case sf::Event::KeyPressed:
handlePlayerInput(event.key.code, true);
break;
case sf::Event::KeyReleased:
handlePlayerInput(event.key.code, false);
break;
case sf::Event::Closed:
Window.close();
break;
}
}
}

void Game::update(sf::Time dt) {
sf::Vector2f movement(0.f, 0.f);

if (isMovingUp)
movement.y -= PLAYER_SPEED;
if (isMovingDown)
movement.y += PLAYER_SPEED;
if(isMovingLeft)
movement.x -= PLAYER_SPEED;
if (isMovingRight)
movement.x += PLAYER_SPEED;

Player.move(movement * dt.asSeconds());
}

void Game::render() {
Window.clear();
Window.draw(Player);
Window.draw(statsText);
Window.display();
}

void Game::handlePlayerInput(sf::Keyboard::Key key, bool isPressed) {
switch (key) {
case sf::Keyboard::W:
isMovingUp = isPressed;
break;
case sf::Keyboard::S:
isMovingDown = isPressed;
break;
case sf::Keyboard::A:
isMovingLeft = isPressed;
break;
case sf::Keyboard::D:
isMovingRight = isPressed;
break;
}
}

void Game::updateGameStats(sf::Time dt) {
statsTotalTimeElapsed += dt ;
statsNumberOfFrames += 1;

if (statsTotalTimeElapsed >= sf::seconds(1.0f)) {
statsText.setString(
"FPS = " + to_string(statsNumberOfFrames) + "\n" +
"Time/Update = " + to_string(statsTotalTimeElapsed.asMicroseconds() / statsNumberOfFrames) + "us");

statsTotalTimeElapsed -= sf::seconds(1.0f);
statsNumberOfFrames = 0;
}
}

现在,我的问题是这样的。如何计算呈现到我屏幕上的 FPS。据我所知,FPS 上限为 60fps,但当我运行应用程序时,显示的 FPS 约为 500 - 1200 FPS。

我是计算错误还是只是没有正确理解?

我希望这个帖子没问题,我似乎找不到类似的问题。

任何建议或帮助将不胜感激。

最佳答案

给定一个帧速率(例如 60 FPS):

auto framerate = 60; // FPS
auto elapsedMillisecondsExpected = 1000 / framerate; // in milliseconds

在 SFML 中,您使用 sf::Clock 并在每一帧对其进行限制:

sf::Clock clock;

// frame logic

auto elapsedMilliseconds = clock.getElapsedTime().asMilliseconds();
if (elapsedMilliseconds < elapsedMillisecondsExpected) {
auto sleepMilliseconds = elapsedMillisecondsExpected - elapsedMilliseconds;
// sleep for sleepMilliseconds
}

关于c++ - SFML游戏开发书籍——理解FPS计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25948903/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com