When I start game fps is about 60 but on holding button "W" my fps decrease to 30.On release that button it became 60 again.This happens because line of code: model->Load("img/characters.png", rec).
If I'll comment line like this "//" character gonna move smoothly with 60fps but w/o turning to top.
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);
}
Need to fix it , on holding button "w" character should be turned on top and move with 60fps.
You load the textures from disk on every keystroke. Don't. Load the textures from disk at the start of your game (you probably know those loading screens) and store them in variables.
Then use them, for example in the setTexture
method.