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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - minimidge

Pages: [1]
1
General / Re: Generic Game Loop
« on: December 19, 2012, 03:12:45 am »
Oops I just realized leaving that conditional in there causes the ball to move slower than it should. I took it out and now it acts normally. I guess I sort of answered my own question. :)

2
General / Re: Generic Game Loop
« on: December 19, 2012, 03:02:27 am »
I've heard about the issues using sleep before. By saying I won't get a "fully fixed" frame rate do you mean it simply won't be 100% accurate? With setFramerateLimit(60) I get ~62 fps. That's fine with me if it's slightly off.

If I understand correctly I can modify it like so:
#include <SFML\Graphics.hpp>
using namespace sf;

int main()
{
    const float FPS = 60.0f; //The desired FPS. (The number of updates each second).
    bool redraw = true;      //Do I redraw everything on the screen?

    RenderWindow window(VideoMode(300, 300, 32), "Hello");
    window.setFramerateLimit(FPS);
    Clock clock;
    CircleShape circle(10.0f);
    circle.setOrigin(10.0f, 10.0f);
    circle.setPosition(0, 150.0f);

    Event ev;
    while (window.isOpen())
    {
        //Wait until 1/60th of a second has passed, then update everything.
        if (clock.getElapsedTime().asSeconds() >= 1.0f / FPS)
        {
            redraw = true; //We're ready to redraw everything
            circle.move(1.0f, 0);
            clock.restart();
        }

        //Handle input
        while (window.pollEvent(ev))
        {
            if (ev.type == Event::Closed) window.close();
        }

        //Draw stuff if ready
        if (redraw)
        {
            redraw = false;
            window.clear(Color(0, 0, 0));
            window.draw(circle);
            window.display();
        }
    }

    return 0;
}
 

Can I remove the
if (clock.getElapsedTime().asSeconds() >= 1.0f / FPS)
condition and the clock entirely, or will that cause the circle's speed to be FPS-dependent? I'm trying to understand exactly how much setFramerateLimit() does for me automatically.

Thank you for your time.  :)

3
General / Generic Game Loop
« on: December 18, 2012, 10:52:31 pm »
Hello. I'm relatively new to SFML, but I've dabbled around with Allegro and game engines for awhile. I'm trying to create a basic fixed time-step loop for a game in SFML. I based the loop off a tutorial for Allegro. You can see the tutorial here: http://fixbyproximity.com/2011/08/2d-game-dev-part-4-1-timing-our-game-loop-2/ . The sourcecode is below the video if you're interested.

Anyways, I want the game to act independent of FPS. I wrote a simple program below. I want the circle to take 5 seconds to go from the left side to the right side of the screen (It moves 1 pixel every 1/60th of a second).
#include <SFML\Graphics.hpp>
using namespace sf;

int main()
{
        const float FPS = 60.0f; //The desired FPS. (The number of updates each second).
        bool redraw = true;      //Do I redraw everything on the screen?

        RenderWindow window(VideoMode(300, 300, 32), "Hello");
        Clock clock;
        CircleShape circle(10.0f);
        circle.setOrigin(10.0f, 10.0f);
        circle.setPosition(0, 150.0f);

        Event ev;
        while (window.isOpen())
        {
                //Wait until 1/60th of a second has passed, then update everything.
                if (clock.getElapsedTime().asSeconds() >= 1.0f / FPS)
                {
                        redraw = true; //We're ready to redraw everything
                        circle.move(1.0f, 0);
                        clock.restart();
                }
                else //Sleep until next 1/60th of a second comes around
                {
                        Time sleepTime = seconds((1.0f / FPS) - clock.getElapsedTime().asSeconds());
                        sleep(sleepTime);
                }

                //Handle input
                while (window.pollEvent(ev))
                {
                        if (ev.type == Event::Closed) window.close();
                }

                //Draw stuff if ready
                if (redraw)
                {
                        redraw = false;
                        window.clear(Color(0, 0, 0));
                        window.draw(circle);
                        window.display();
                }
        }

        return 0;
}

Fraps reports 58-59 fps, and the circle takes 5 seconds to do its thing. If I remove the if(redraw) condition, Fraps reports around 110 fps, and the circle still takes 5 seconds (which is what I want).

My questions is: is the above code a good way to approach this? I'm just worried that I'll discover some flaw halfway into a game and have to change everything.

Pages: [1]
anything