当我开始游戏时,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/