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

Author Topic: slow things down a bit  (Read 3038 times)

0 Members and 2 Guests are viewing this topic.

newbie123

  • Newbie
  • *
  • Posts: 27
    • View Profile
slow things down a bit
« on: January 08, 2012, 03:20:27 pm »
Hello
I'm trying to make a counter from 0 to 170 and then from 170 to 0. I got it working but its counting very fast. Is there a way to slow the counting down a bit.

I tried to use Clock.GetTime(); but the problem is that the clock count from 0 to 170 for example but i want it to also count backwords from 170 to 0.

here what i got

Code: [Select]

#include <SFML\Graphics.hpp>
#include <iostream>

int main()
{
sf::RenderWindow Window(sf::VideoMode(800, 632, 32), "Tower Defence");
sf::Image Image;
sf::Sprite Sprite;

Image.LoadFromFile("light.png");
Sprite.SetImage(Image);
Sprite.SetPosition(0, 32);
Sprite.SetColor(sf::Color(14,25,52));

sf::Clock Clock;
int Time = 0;
bool up = true;

while(Window.IsOpened())
{
sf::Event Event;
Window.SetFramerateLimit(150);
Window.UseVerticalSync(true);
while (Window.GetEvent(Event))
{
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
Window.Close();

}


if( up )
{
++Time;
}
else
{
--Time;
}

if( Time >= 170 )
up = false;

if( Time <= 0 )
up = true;


Sprite.SetColor(sf::Color(15,10,63, Time));
std::cout<<Time<<"\n";

Window.Clear();
Window.Draw(Sprite);
Window.Display();
}
return 0;
}
[quote][/quote]

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
slow things down a bit
« Reply #1 on: January 08, 2012, 03:40:55 pm »
Do something like this to also make the clock count backwards (this is an example code, not exactly what you have to write):
Code: [Select]

sf::Clock Clock;
int Time = 0;

while (...)
{
    if (up)
    {
        Time += Clock.GetElapsedTime();
        Clock.Reset();
    }
    else
    {
        Time -= Clock.GetElapsedTime()
        Clock.Reset();
    }
}
TGUI: C++ SFML GUI

newbie123

  • Newbie
  • *
  • Posts: 27
    • View Profile
slow things down a bit
« Reply #2 on: January 08, 2012, 03:53:25 pm »
Quote from: "texus"
Do something like this to also make the clock count backwards (this is an example code, not exactly what you have to write):
Code: [Select]

sf::Clock Clock;
int Time = 0;

while (...)
{
    if (up)
    {
        Time += Clock.GetElapsedTime();
        Clock.Reset();
    }
    else
    {
        Time -= Clock.GetElapsedTime()
        Clock.Reset();
    }
}


well i did this but its still going fast
Code: [Select]

int Time = 0;
bool up = true;

while(Window.IsOpened())
{
if( up )
{
Time += Clock.GetElapsedTime();
}
else
{
Time -= Clock.GetElapsedTime();
Clock.Reset();
}

if( Time >= 170 )
up = false;

if( Time <= 0 )
up = true;


Sprite.SetColor(sf::Color(15,10,63, Time));
std::cout<<Time<<"\n";
}

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
slow things down a bit
« Reply #3 on: January 08, 2012, 03:59:55 pm »
You only reset the clock when up is false, also do this when it is true.


There are two ways I see to fix the problem:

1) Does it has to be 170?  If not then you can slow it down by letting it count to e.g. 17000.

2) Change it to something like this:
Code: [Select]
if (up)
{
    if (Clock.GetElapsedTime() > 25)
    {
        ++Time;
        Clock.Reset();
    }
}

By changing the 25 you can adjust the speed.
TGUI: C++ SFML GUI

newbie123

  • Newbie
  • *
  • Posts: 27
    • View Profile
slow things down a bit
« Reply #4 on: January 08, 2012, 04:11:03 pm »
Quote from: "texus"
You only reset the clock when up is false, also do this when it is true.


There are two ways I see to fix the problem:

1) Does it has to be 170?  If not then you can slow it down by letting it count to e.g. 17000.

2) Change it to something like this:
Code: [Select]
if (up)
{
    if (Clock.GetElapsedTime() > 25)
    {
        ++Time;
        Clock.Reset();
    }
}

By changing the 25 you can adjust the speed.


if i reset clock when its false and true the time variable will stay at 0 and will not increment.

1) yes it has to be 170 because I'm changing the transparency of an image using time. I'm creating a darkness / night effect.

2) i can't do it this way because the program will wait 25 second till it start incrementing time.

what i want to do is increment "time" every second till it reach 170 and then decrement it till it reach 0 and so on, to create my night effect.

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
slow things down a bit
« Reply #5 on: January 08, 2012, 04:34:27 pm »
I tested the following code with SFML2 and it should work.
I think that if u are using SFML 1.6 that you should change the 1000 into a 1.

Code: [Select]
while(Window.IsOpened())
{
    if( up )
    {
        if (Clock.GetElapsedTime() >= 1000)
        {
            ++Time;
            Clock.Reset();
        }
    }
    else
    {
        if (Clock.GetElapsedTime() >= 1000)
        {
            --Time;
            Clock.Reset();
        }
    }

    if( Time >= 170 )
        up = false;

    if( Time <= 0 )
        up = true;
           
}
TGUI: C++ SFML GUI

newbie123

  • Newbie
  • *
  • Posts: 27
    • View Profile
slow things down a bit
« Reply #6 on: January 08, 2012, 04:38:47 pm »
Quote from: "texus"
I tested the following code with SFML2 and it should work.
I think that if u are using SFML 1.6 that you should change the 1000 into a 1.

Code: [Select]
while(Window.IsOpened())
{
    if( up )
    {
        if (Clock.GetElapsedTime() >= 1000)
        {
            ++Time;
            Clock.Reset();
        }
    }
    else
    {
        if (Clock.GetElapsedTime() >= 1000)
        {
            --Time;
            Clock.Reset();
        }
    }

    if( Time >= 170 )
        up = false;

    if( Time <= 0 )
        up = true;
           
}


wow thank you so very much its working perfectly.
but i could sworn that i have tried this way before posting and it didn't work. hmmm...

anyways thank you very much it working like a charm. :D