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

时间:2019-02-16 20:30:52

标签: c++ sfml

当我开始游戏时,fps约为60,但是在按住“ W”按钮时,我的fps降低到30。释放该按钮时,fps又变为60。 model-> Load(“ 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的速度移动。

1 个答案:

答案 0 :(得分:2)

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

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