SFML community forums

Help => General => Topic started by: Geners on November 28, 2013, 02:03:49 am

Title: I can't get this to work.
Post by: Geners on November 28, 2013, 02:03:49 am
So I made this program and I totally was able to grab sprites until I added code for animation. All of the sudden when I run it, it's not showing anything. Here is the code. Why doesn't it work?
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
//#include "Chardicks.h"
//#include "Keystrokes.h"

using namespace sf;
using namespace std;

Texture texture;
Sprite Charmander;

int main()
{
    RenderWindow window(VideoMode(640, 480), "FUCKING CHARMANDER!");

    while (window.isOpen())
    {
        Event event;
        while (window.pollEvent(event))
        {
            if (event.type == Event::Closed)
                window.close();
        }

                Clock TIMER;
                TIMER.restart();
                Time t2 = milliseconds(333);
                Time t1 = TIMER.getElapsedTime();
                int step = 0;

                TIMER.restart();

                if (t1 >= milliseconds(333))
                {
                        window.clear();
                        texture.loadFromFile("PokeSprites.png", IntRect(1,1,32,32));
                        Charmander.setTexture(texture);
                        step = 1;
                        TIMER.restart();
                        window.draw(Charmander);
                        window.display();

                }
                if (t1 >= t2)
                {
                        window.clear();
                        texture.loadFromFile("PokeSprites.png", IntRect(0,32,32,32));
                        Charmander.setTexture(texture);
                        step = 0;
                        TIMER.restart();
                        window.draw(Charmander);
                        window.display();
                }
    }

    return 0;
}
 
Title: Re: I can't get this to work.
Post by: G. on November 28, 2013, 02:11:49 am
                TIMER.restart();
                Time t1 = TIMER.getElapsedTime();
t1 is probably 0ms or close to 0ms.
So it is never >= milliseconds(333) or >= t2 (which is also milliseconds(333) lol.).

When posting pieces of code (especially long ones) in the forum, you have to surround it with code tags. It greatly improves readability and you'll be more likely to get answers.
(http://i.imgur.com/XoEkbUI.png)
Title: Re: I can't get this to work.
Post by: Geners on November 28, 2013, 02:21:03 am
                TIMER.restart();
                Time t1 = TIMER.getElapsedTime();
t1 is probably 0ms or close to 0ms.
So it is never >= milliseconds(333) or >= t2 (which is also milliseconds(333) lol.).

When posting pieces of code (especially long ones) in the forum, you have to surround it with code tags. It greatly improves readability and you'll be more likely to get answers.
(http://i.imgur.com/XoEkbUI.png)

Thanks, I changed it.

Doesn't getElapsedTime get the time of how long the program has been running so far? So wouldn't it never be 0ms?
Title: Re: I can't get this to work.
Post by: G. on November 28, 2013, 02:39:25 am
getElapsedTime returns the time since the last restart (or creation) of your sf::Clock.
Quote from: getElapsedTime() (http://www.sfml-dev.org/documentation/2.1/classsf_1_1Clock.php#a799feb6acb099b57b58d8d20984fce11)
This function returns the time elapsed since the last call to restart() (or the construction of the instance if restart() has not been called).
BTW, your clock is created every frame because you declared it inside the loop. So even if you don't restart it, it will always be 0 (or close to 0)

If you want to get the "time of how long the program has been running so far", declare your sf::Clock outside the main loop, and don't restart it. :p
Title: Re: I can't get this to work.
Post by: Geners on November 28, 2013, 02:47:49 am
Ooooh, I got it, Thanks so much, That should work.
Title: Re: I can't get this to work.
Post by: binary1248 on November 28, 2013, 02:54:17 am
(http://i.imgur.com/L1GaqCs.png)
This is why auto-complete and intellisense should be eradicated. They try to make it easier for people but end up making it harder for them. If only they would also display the documentation a la Eclipse + javadoc...
Title: Re: I can't get this to work.
Post by: FRex on November 28, 2013, 01:07:28 pm
Quote
This is why auto-complete and intellisense should be eradicated. They try to make it easier for people but end up making it harder for them.
Don't take away my toys because someone missuses, they'd misuse documentation too. :P

Quote
If only they would also display the documentation a la Eclipse + javadoc...
Some have to be able to do it, I know of one.
NetBeans from Oracle*:
(http://oi41.tinypic.com/22f58n.jpg)
 8)
Admittedly it fails for sf::Clock and sf::Time :'( because Linux and POSIX documentation take priority over comments but it's still great(and there might be some switch for that hidden somewhere).

* yes, that Oracle, makers of Java and javadoc also write an IDE in Java for many languages that is quite good for c++, yes, it's quite ironic.