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

Author Topic: Help with clock()  (Read 5708 times)

0 Members and 1 Guest are viewing this topic.

JohnGreek

  • Newbie
  • *
  • Posts: 43
    • View Profile
Help with clock()
« on: September 13, 2009, 12:29:49 am »
Since i started learning SFML recently i wanted to "test" my self with a program(window) that simply closes after 3 seconds.
I spend many hours and even with help i can't get it done

Here's my code:

Code: [Select]
#include <SFML/Graphics.hpp>

int main()
{
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");
sf::Clock Clock;

while (App.IsOpened())
{
sf::Event Event;

while (App.GetEvent(Event))
{
    int Time = Clock.GetElapsedTime();
    Clock.Reset();
        if (Time==3)
            App.Close();

    if (Event.Type == sf::Event::Closed)
        App.Close();
}


App.Clear();
App.Display();
}

return EXIT_SUCCESS;

}


My thoughts are:

Maybe sf::RenderWindow() doesn't accept to be closed through time...?
Maybe  Clock's functions are not correctly placed in the code?
Maybe it's all wrong...

Anybody can help???

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Help with clock()
« Reply #1 on: September 13, 2009, 01:13:35 am »
Do the right thing is the right place at the right moment :
Code: [Select]

int main(int, char**) {
  sf::Window window(...);
  sf::Clock clock;

  While window open :
    -Process Event ;
    -if clock.GetElapsedTime() > 3.f then close window ;
    -Display ;

  return EXIT_SUCCESS;
}
SFML / OS X developer

JohnGreek

  • Newbie
  • *
  • Posts: 43
    • View Profile
Help with clock()
« Reply #2 on: September 13, 2009, 01:19:47 am »
I'm sorry but i'm not at that stage to understand most of this code...
-I am newbie and i'd like to help me in a more "newbie" way
-what is "then" command??? never seen it before
-"Process" never seen it too...
-lol never seen a while loop with "-" instead of brakets

P.S i just reached "Sprites tutorial"...

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6232
  • Thor Developer
    • View Profile
    • Bromeon
Help with clock()
« Reply #3 on: September 13, 2009, 03:47:27 am »
That's just pseudo-code, no valid C++. :)
(Pseudo-code is intended do express the program behaviour by describing it in more or less human language.)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: first SFML book

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Help with clock()
« Reply #4 on: September 13, 2009, 10:49:46 am »
Try to express this pseudo code into legal C++ : read it as if it were a text.
If you don't understand the meaning of any terms just ask.

BTW don't call a var with the same name that its type ( for example don't call your clock "Clock" because its type is "Clock" ) . You may experience some wired situations.

About the "if .. then .. ; " :
The common structure of "conditioned action" is :
Quote
IF [condition] is true
THEN execute [ACTION]
ELSE execute [ANOTHER ACTION]
FI

FI means the end of the "if" block. You can also call it "end if" if you prefer.
SFML / OS X developer

Spidyy

  • Sr. Member
  • ****
  • Posts: 493
    • View Profile
Help with clock()
« Reply #5 on: September 13, 2009, 11:54:20 am »
In your case, the Clock.Reset isn't necessary. It even avoid the Time to reach the desired value.

Each frame duration is about 0,0001 sec, and when you do a Clock.Reset, it reset the countdown to 0.

So it do that :


Code: [Select]
while (App.GetEvent(Event))
{
    int Time = Clock.GetElapsedTime(); // Retrieve the current time in a variable,
    Clock.Reset(); // Reset the time to 0
        if (Time==3) // Here, time have the value of the duration of your frame, so it never reach 3 except if you pause the programm for 3 seconde.
            App.Close();

    if (Event.Type == sf::Event::Closed)
        App.Close();
}


The solution : just avoid the Clock.Reset(). Also, use a > condition instead of ==. The time very rarely have an integer value (3.00000...), it's most often close to the value (3.00000154...) but rarely get at an integer value. Having a "superior at" condition guarantie you to detect the right time. =p

You should do :
Code: [Select]

while (App.GetEvent(Event))
{
    if (Clock.GetElapsedTime() > 3)
        App.Close();

    if (Event.Type == sf::Event::Closed)
        App.Close();
}


And if you want to reapeat an action, here you can use the Clock.Reset() function.

Code: [Select]

while (App.GetEvent(Event))
{
    if (Clock.GetElapsedTime() > 3)
    {
        //Doing something to repeat
        ...
        Clock.Reset();
    }

    if (Event.Type == sf::Event::Closed)
        App.Close();
}


And as Hiura said, there is a precise order to do things. It isn't mandatory, but rather logical. =p

First, proceed with the Event. If someone ask to close the window, it is useless to execute the rest of the instruction following.

Then do all the calculation operation modifying your present objects, taking the changes of the Event in account.

Last, display the result. =)

It is what Hiura was explaining with his pseudo-code.

JohnGreek

  • Newbie
  • *
  • Posts: 43
    • View Profile
Help with clock()
« Reply #6 on: September 13, 2009, 04:38:17 pm »
thanks for your replies, i replaced my code but still the application does a weird thing, instead of closing at > 3 it closes at random times(5,12 etc.) i'm kinda confused.

My code:
Code: [Select]


////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
////////////////////////////////////////////////////////////
int main()
{
    // Create the main rendering window
    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");

    // Start game loop
    while (App.IsOpened())
    {
        // Process events
        sf::Clock Timer;
        sf::Event Event;
        while (App.GetEvent(Event))
        {

        if (Timer.GetElapsedTime() >= 3)
            App.Close();


        if (Event.Type == sf::Event::Closed)
            App.Close();
        }

        // Display window contents on screen
        App.Clear();
        App.Display();
    }

    return EXIT_SUCCESS;
}


I am a little sick too and my head hurts so the code might have some stupid mistakes, plz note them if you see any.

Spidyy

  • Sr. Member
  • ****
  • Posts: 493
    • View Profile
Help with clock()
« Reply #7 on: September 13, 2009, 05:03:56 pm »
My bad, I made a mistake while reading and Copy/pasting the code. =p

Two things for that :

First : You create your clock inside the loop, it means at each frames, it is created, queried then destroyed. Just write your sf::Clock Timer; outside the loop. (just before the sf::RenderWindow). When you create your clock, the creation's time is stored somewhere, it mean it is Reset() automaticaly at the creation. =p

Second : the while(App.GetEven(Event)) is used to retrieve ALL the Events that have been received during the frame, it is to be used only with the Events, nothing else. You should try your Timer after retrieving all the events.

Your code become something like this :

Code: [Select]

////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
////////////////////////////////////////////////////////////
int main()
{
    // Create the main rendering window
    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");

    sf::Clock Timer;  // Our timer, self-initializing

    // Start game loop
    while (App.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }

       if (Timer.GetElapsedTime() >= 3)  // Quering the timer for the time
            App.Close();

        // Display window contents on screen
        App.Clear();
        App.Display();
    }

    return EXIT_SUCCESS;
}


Also, the application take some time to create the window, the timer start after the window is created. It mean the 3 secondes are the time while the window is displaying something. If you really want your program to last 3 seconde with the window creation time, juste initialize your timer before you create your window :
Code: [Select]

sf::Clock Timer;
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");


A simple way to do things properly :
- Declaring and initializing objets
- starting the game loop
- Retrieve and manage events
- Do all the calculation stuff (query timers, manage and move objects with the result of the previous events, ...)
- Draw the scene (after it is fully and clearly calculated.)

JohnGreek

  • Newbie
  • *
  • Posts: 43
    • View Profile
Help with clock()
« Reply #8 on: September 13, 2009, 05:09:14 pm »
It works! :)
i thought that the "if" condition was considered an event.. lol
and about the clock you are right, it was incorrect placed in the loop.
It troubled me a hell lot but i made it thanks to you guys
thank you :D

Quote from: "Spidyy"
Reset() automaticaly at the creation. =p

I didn't knew that :o

Spidyy

  • Sr. Member
  • ****
  • Posts: 493
    • View Profile
Help with clock()
« Reply #9 on: September 13, 2009, 05:27:55 pm »
If you look at the SFML References, you have the Clock.cpp, and inside, hoo what do we have...

Code: [Select]
Clock::Clock()
{
     Reset();
 }


A Reset() in the constructor. :P

I experienced many problems and many of them was because I didn't pay attention to the SFML references nor to the SFML sources. :x

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Help with clock()
« Reply #10 on: September 13, 2009, 10:03:03 pm »
Quote from: "Spidyy"
A simple way to do things properly :
- Declaring and initializing objets
- starting the game loop
- Retrieve and manage events
- Do all the calculation stuff (query timers, manage and move objects with the result of the previous events, ...)
- Draw the scene (after it is fully and clearly calculated.)
I do not agree with the first point with C++. In C code it's true. But that's a detail for beginner so let's skip this.
SFML / OS X developer

Spidyy

  • Sr. Member
  • ****
  • Posts: 493
    • View Profile
Help with clock()
« Reply #11 on: September 13, 2009, 10:20:54 pm »
Yeah juste declaring is enough. The point is to create the permanent objects outside the loop, not inside. =p

 

anything