Texture.loadFromFile 降低了我的 fps.Should 为 60 而不是 30fps

Texture.loadFromFile decreased my fps.Should be 60 instead of 30fps

当我开始游戏时 fps 大约是 60 但按住按钮 "W" 我的 fps 下降到 30.On 释放那个按钮它变成了 60 again.This 发生是因为代码行: 模型->加载("img/characters.png", rec).

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

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方法中。