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

Author Topic: [SOLVED]Need sf::Clock to be more specific  (Read 2318 times)

0 Members and 1 Guest are viewing this topic.

Chuckleluck

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
[SOLVED]Need sf::Clock to be more specific
« on: November 23, 2011, 04:56:12 am »
Hello,
I'm working on a program and I need my character's feet to move back and forth while he's walking.  I've got everything working with sf::Clock. Here's a snippet of my code:
Code: [Select]
void MovePlayer(int x, int y, int Player)
{
    switch(Player)
    {
        case 1:
            g_sPlayerHead.Move(x, y);
            g_sPlayerBody.Move(x, y);
            g_sPlayerFeet.Move(x, y);
            g_sPlayerArms.Move(x, y);
            int timer = FootImgClock.GetElapsedTime();
            switch(timer)
            {
                case 0:
                    g_sPlayerFeet.SetSubRect(g_rcFeetMveA);
                    break;
                case 1:
                    g_sPlayerFeet.SetSubRect(g_rcFeetMveB);
                    break;
                case 2:
                    FootImgClock.Reset();
                    break;
            }
        default:
            break;
    }
}


The feet move too slow for my liking, once every second or so.  How do I fix this?  Is there a version of sf::Clock that measures in miliseconds?

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
[SOLVED]Need sf::Clock to be more specific
« Reply #1 on: November 23, 2011, 05:28:01 am »
The measured time is a float representing the ammount of seconds that has passed. By putting it into an integer, you are cutting the smaller parts and taking only the integer part. If you want milliseconds, just multiply the float by 1000 before truncationg it into an integer.

kolofsson

  • Full Member
  • ***
  • Posts: 100
    • View Profile
[SOLVED]Need sf::Clock to be more specific
« Reply #2 on: November 23, 2011, 10:18:26 am »
Quote from: "Tex Killer"
The measured time is a float representing the ammount of seconds that has passed. By putting it into an integer, you are cutting the smaller parts and taking only the integer part. If you want milliseconds, just multiply the float by 1000 before truncationg it into an integer.


In SFML2 GetElapsedTime is a Uint32 measuring time in milliseconds. I think the X and Y should be floats not ints.

If you want an accurate timer, do not reset it, instead calculate the difference

Code: [Select]
last_frame = FootImgClock.GetElapsedTime();
while (running)
{
    frame_time = FootImgClock.GetElapsedTime() - last_frame;
    last_frame = FootImgClock.GetElapsedTime();
    Player.Move(frame_time, 0);
}

Chuckleluck

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
[SOLVED]Need sf::Clock to be more specific
« Reply #3 on: November 23, 2011, 05:12:47 pm »
Can someone show me what I should do to modify the code I posted?  I don't have much experience with floats.  I'm also using SFML 1.6, not 2.

Tex Killer

  • Full Member
  • ***
  • Posts: 242
    • View Profile
[SOLVED]Need sf::Clock to be more specific
« Reply #4 on: November 23, 2011, 05:28:58 pm »
Change
Code: [Select]
int timer = FootImgClock.GetElapsedTime();
to
Code: [Select]
int timer = FootImgClock.GetElapsedTime() * 2;
and you will have 2 animation frames per second, instead of one.
But I think you should study more of the basics before attempting to do something.

Chuckleluck

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
[SOLVED]Need sf::Clock to be more specific
« Reply #5 on: November 23, 2011, 05:36:41 pm »
Nevermind.  I figured it out  :D
For anyone who's interested, this is how I edited the code:
Code: [Select]
void MovePlayer(int x, int y, int Player)
{
    switch(Player)
    {
        case 1:
            g_sPlayerHead.Move(x, y);
            g_sPlayerBody.Move(x, y);
            g_sPlayerFeet.Move(x, y);
            g_sPlayerArms.Move(x, y);
            float ftimer = FootImgClock.GetElapsedTime();
            if(ftimer <= .3)
                g_sPlayerFeet.SetSubRect(g_rcFeetMveA);
            else if(ftimer >= .4 && ftimer <= .7)
                g_sPlayerFeet.SetSubRect(g_rcFeetMveB);
            else if(ftimer >= .9)
                FootImgClock.Reset();
        default:
            break;
    }
}

sbroadfoot90

  • Jr. Member
  • **
  • Posts: 77
    • View Profile
[SOLVED]Need sf::Clock to be more specific
« Reply #6 on: November 23, 2011, 09:43:36 pm »
There's nothing much to floats (that's sort of a lie). For your purposes, think of a float as something that can store a decimal number to about 8 significant figures. A double can store about 16 significant figures.

kolofsson

  • Full Member
  • ***
  • Posts: 100
    • View Profile
[SOLVED]Need sf::Clock to be more specific
« Reply #7 on: November 23, 2011, 10:29:27 pm »
Quote from: "sbroadfoot90"
There's nothing much to floats (that's sort of a lie). For your purposes, think of a float as something that can store a decimal number to about 8 significant figures. A double can store about 16 significant figures.


That double precision is worthless when you're losing the fractions of a pixel each frame of not using floats.