Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Can someone help me understand how this bit of clock code works?  (Read 7706 times)

0 Members and 1 Guest are viewing this topic.

carton83

  • Newbie
  • *
  • Posts: 38
    • View Profile
I'm following tutorials by CodingMadeEasy for SFML 2 and he went over using sf::Clock to keep the framerate of an animation consistent.  My issue is that he didn't really explain how it works.  He may come back to this at a later time but not knowing exactly how this works is bugging the crap out of me.

sf::Clock clock;
int frameCounter, switchFrame = 100, frameSpeed = 500;
frameCounter += frameSpeed * clock.restart().asSeconds();

if(frameCounter >= switchFrame)
{

    frameCounter = 0;
           
    //Code to update sprite frame

}

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Can someone help me understand how this bit of clock code works?
« Reply #1 on: January 05, 2014, 12:28:59 am »
Why do we suddenly have such an influx of users from CodingMadeEasy? :-\
His tutorials are not good at all, lots of mistakes, bad C++, bad and wrong explanations - see here and here.

The code piece here shows this as well... I'm not sure what his logic behind this really is, but it's not such a good way imho. It's way more helpful to work with "animation frames per second".
The code here uses a random speed factor then multiplies that with the frametime (I think) and adds it up. Once the counter has reached another random number (switchFrame), the if condition will be met and I guess the frame gets updated?

There's a nice AnimatedSprite class on the SFML Wiki. And as alternative, you can also take a look at Thor's animation module.
« Last Edit: January 05, 2014, 12:31:12 am by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

carton83

  • Newbie
  • *
  • Posts: 38
    • View Profile
Re: Can someone help me understand how this bit of clock code works?
« Reply #2 on: January 05, 2014, 12:37:01 am »
I personally need some sort of video based tutorial or I just lose interest. CME, while certainly not great, is all that I can find.

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: Can someone help me understand how this bit of clock code works?
« Reply #3 on: January 05, 2014, 01:39:24 am »
CME, while certainly not great, is all that I can find.

If you really want to learn SFML with a video tutorial you should have a look at the tutorials from SuperV1234: http://en.sfml-dev.org/forums/index.php?topic=13677.0 . They are quite nice with "good" c++.


AlexAUT

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: Can someone help me understand how this bit of clock code works?
« Reply #4 on: January 05, 2014, 09:16:46 am »
If your attention span doesn't go beyond video tutorials, C++ might not be for you. C++ is very complex and can't be learned by watching videos, because there are no videos that into all the important details.
I rather suggest you get yourself a good C++ book or look a easier languages. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

wintertime

  • Sr. Member
  • ****
  • Posts: 255
    • View Profile
Re: Can someone help me understand how this bit of clock code works?
« Reply #5 on: January 05, 2014, 01:20:18 pm »
Shouldn't it be the other way around anyway?
I mean if I would watch a video tutorial its like wasting most of the time waiting to hear some relevant/interesting thing and if it gets told its like a few words that are over faster than needed to remember them before the irrelevant stuff continues to distract from that. And you can not skip parts because there is no indication of where some interesting part in the video would be. And you have no way of judging the quality without having wasted the time to watch it in whole already and its difficult to edit errors, which means its probably lower quality.
When reading something you can skim it and get easily to the interesting parts without missing them, then carefully read these parts without a time constraint to understand them. If needed its possible to go back to an earlier part easily.

carton83

  • Newbie
  • *
  • Posts: 38
    • View Profile
Re: Can someone help me understand how this bit of clock code works?
« Reply #6 on: January 06, 2014, 03:01:36 am »
Everybody is different Winter.  I'll gladly read documentation to get the specifics of how "X" works, but I enjoy listening to someone going over the general bits more than just reading a tutorial.  That said I also have experience in other languages and that helps me spot questionable code.  Sure, I'm still a bit fresh to C++ but I'm not completely unfamiliar to programming in general.

eXpl0it3r:

Thanks for the link to the AnimatedSprite class.  My only issue with it is how you have to set up each and every single frame.  In games that have a lot of sprites with a lot of frames that would very quickly become a huge pain.  There has gotta be a simpler, cleaner way of doing that. I'm not sure how Thor does it but I'll look into it later when I have the time.

eigenbom

  • Full Member
  • ***
  • Posts: 228
    • View Profile
Re: Can someone help me understand how this bit of clock code works?
« Reply #7 on: January 06, 2014, 03:47:27 am »
Thanks for the link to the AnimatedSprite class.  My only issue with it is how you have to set up each and every single frame.  In games that have a lot of sprites with a lot of frames that would very quickly become a huge pain.  There has gotta be a simpler, cleaner way of doing that. I'm not sure how Thor does it but I'll look into it later when I have the time.

The code in the wiki is the first step at getting some animation happening. You'd automate this for a proper game. For example, I use the TexturePacker software to generate my sprite sheets and frame info as .json, which I then read in at runtime.

krzat

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: Can someone help me understand how this bit of clock code works?
« Reply #8 on: January 06, 2014, 10:03:09 pm »
Thanks for the link to the AnimatedSprite class.  My only issue with it is how you have to set up each and every single frame.  In games that have a lot of sprites with a lot of frames that would very quickly become a huge pain.  There has gotta be a simpler, cleaner way of doing that. I'm not sure how Thor does it but I'll look into it later when I have the time.

   
if (!m_isPaused && m_animation)
    {
        // add delta time
        m_currentTime += deltaTime;

        // if current time is bigger then the frame time advance one frame
        if (m_currentTime >= m_frameTime)
        {
 
This code (from AnimatedSprite) does the same thing as the code you posted. Only difference is that they use time directly (which makes perfect sense).

I'm not sure what you mean by "huge pain".
SFML.Utils - useful extensions for SFML.Net

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Can someone help me understand how this bit of clock code works?
« Reply #9 on: January 30, 2014, 08:48:30 pm »
I think reading the "Fix your time step" http://gafferongames.com/game-physics/fix-your-timestep/ article (and the other articles in that series) would do you good regarding understanding that time/clock code :)

 

anything