c++ - Texture.loadFromFile 降低了我的 fps。应该是 60 而不是 30fps

标签 c++ sfml

当我开始游戏时,fps 大约是 60,但按住“W”按钮时,我的 fps 下降到 30。松开那个按钮时,它再次变为 60。发生这种情况是因为代码行: 模型->加载("img/characters.png", rec).

如果我将像这样的注释行“//”角色将以 60fps 的速度平稳移动但不会转向顶部。

Entity *player;

void heroMovement()
{

if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
    sf::IntRect top(32, 224, 32, 32);

    this->player->Load("img/characters.png", top);

    calculateTileAnimation(0, 32, 64, top , this->player);

    velocity.y -= character_speed;

}

}
void calculateTileAnimation(int firstTile , int sizeTile , int 
lastTile,sf::IntRect rec , Entity *model)
{
    model->Load("img/characters.png", rec); // This line decreasing fps

    if (clock.getElapsedTime().asSeconds() < 0.3f)
    {
        rec.left += sizeTile;
        if (rec.left == lastTile)
            rec.left = firstTile;
        clock.restart();
    }
}


 void Load(std::string filename, sf::IntRect rec)
{
    this->texture->loadFromFile(filename, rec);
    this->setTexture(*this->texture);
}

需要修复它,按住按钮“w”字符应该在顶部打开并以 60fps 移动。

最佳答案

您在每次击键时从磁盘加载纹理。不。在游戏开始时从磁盘加载纹理(您可能知道那些加载屏幕)并将它们存储在变量中。

然后使用它们,例如在setTexture 方法中。

关于c++ - Texture.loadFromFile 降低了我的 fps。应该是 60 而不是 30fps,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54727371/

相关文章:

c++ - 如何在android项目中使用NDK?

c++ - SFML 的 'intersects' 函数总是返回 'true'

c++ - 编译有效,启动不是 - "standard path"?

c++ - sfml 编译错误 : NULL was not declared in this scope

用于对二维坐标数组进行排序并计算边界框的 C++ 函数

c++ - CGAL 二维 alpha 形状轮廓

带有 "infinite"参数的 c++ 模板

戈朗 : "go get github.com/.." errors with "cannot find header file" - where is this configured?

sfml - 如何在 Visual Studio Code 中链接 SFML 库?

c++ - C++中指针引用的生命周期