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

Author Topic: I can't get this to work.  (Read 2146 times)

0 Members and 2 Guests are viewing this topic.

Geners

  • Newbie
  • *
  • Posts: 9
    • View Profile
I can't get this to work.
« 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;
}
 
« Last Edit: November 28, 2013, 02:18:29 am by Geners »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: I can't get this to work.
« Reply #1 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.
« Last Edit: November 28, 2013, 02:17:02 am by G. »

Geners

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: I can't get this to work.
« Reply #2 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.


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?

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: I can't get this to work.
« Reply #3 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()
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

Geners

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: I can't get this to work.
« Reply #4 on: November 28, 2013, 02:47:49 am »
Ooooh, I got it, Thanks so much, That should work.

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: I can't get this to work.
« Reply #5 on: November 28, 2013, 02:54:17 am »

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...
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: I can't get this to work.
« Reply #6 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*:

 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.
« Last Edit: November 28, 2013, 01:18:07 pm by FRex »
Back to C++ gamedev with SFML in May 2023