Hi everyone, I am new to SFML and I'm trying to make a small game that, each piece at a time, would turn into a very small scale 2D engine. Probably very ambitious but nonetheless a curiosity of mine since my teen years.
I am in a stage where I want to have the sprite of my character animated. I got it working, despite it being very rudimentary but now the problem is that the animation goes so fast it looks ridiculous. I can find no way to tune the sprite animation rate of my character.
I am aware that there are a lot of previous questions about the same topic but for the life of me I am unable to focus on what the problem is. I am aware I need some kind of "sprite frame counter" but I have no idea on how to implement it right now.
In my player.cpp class implementation i have the following function
void Player::animation(sf::Time& dtFrame) ///SIMPLE YET WORKING :) HOW TO REGULATE TIMING THO?
{
///NOTE: IT'S ALWAYS A GOOD IDEA TO USE INTEGER NUMBERS FOR EACH FRAME IN ORDER TO SATISFY EASILY THE CONDITIONS FOF SPRITE FRAME CHANGING.
if(TextureArea.left == 0.0) //When at the left-most, which is set by default in the sprite.
{
moverightFrame = true;
moveleftFrame = false;
}
else if( TextureArea.left == 4872 ) //When at the right-most of the sprite.
{
moveleftFrame = true;
moverightFrame = false;
}
if(moverightFrame)
TextureArea.left += 406;
if(moveleftFrame)
TextureArea.left = 0;
Sprite.setTextureRect(TextureArea); //Take next sprite according to the previous in the sequence as determined above.
}
This works but I say said it's ridiculous. Now...as you see I have a reference that takes the clock of the game loop basically. If everything in that function is wrapped in an if-statement of the kind:
if(dtFrame.AsSeconds()>0.0167)
{
// the code above here
}
Only then the animation slows down and looks beautiful. It is basically a character walking.
First of all, why 0.0167? I deduced it from the fact that my framerate is voluntarily capped at 60fps somewhere else in the code and that there must be some relationship to 1/60Hz ~ 0.0167 seconds.
Second problem, if the pc slows down so does the framerate temporarily then the whole animation looks awful again for a few seconds.
I must be doing something highly inefficient and/or ineffective.
Sorry if there is anything confusing. Thanks in advance.