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

Author Topic: Low framerate on SFML 2.0  (Read 3095 times)

0 Members and 1 Guest are viewing this topic.

Jeffrey

  • Newbie
  • *
  • Posts: 36
    • View Profile
Low framerate on SFML 2.0
« on: July 24, 2012, 06:44:40 am »
I'm trying to run a very basic SFML program that just has a static sprite and a movable sprite. They both use the loadFromFile() method to load the gif image for the sprite. My game loop is very basic:

Quote
    while(App.isOpen()) {
        // event checking
        while(App.pollEvent(event)) {
            switch (event.type) {
                case sf::Event::Closed:
                    App.close();
                    break;
                case sf::Event::KeyPressed:
                    // esc detection
                    if (event.key.code == sf::Keyboard::Escape)
                        App.close();
                    // wasd movements
                    if (event.key.code == sf::Keyboard::W) {
                        cube.move(0, -1);
                        if (cube.isColliding(&a))
                                cube.move(0, +1);
                    }
                    if (event.key.code == sf::Keyboard::S) {
                        cube.move(0, +1);
                        if (cube.isColliding(&a))
                                cube.move(0, -1);
                    }
                    if (event.key.code == sf::Keyboard::A) {
                        cube.move(-1, 0);
                        if (cube.isColliding(&a))
                                cube.move(+1, 0);
                    }
                    if (event.key.code == sf::Keyboard::D) {
                        cube.move(+1, 0);
                        if (cube.isColliding(&a))
                                cube.move(-1, 0);
                    }
                    break;
                default:
                    break;
            }
        }
        App.clear();
        // start drawing logic
        App.draw(cube.get());
        App.draw(a.get());
        // end drawing logic
        App.display();
    }

but the frame rate is really really low. What am I doing wrong?
Also why when I press W+A is doesn't move diagonally but only to the left or only up?
« Last Edit: July 24, 2012, 06:53:30 am by Jeffrey »

Jeffrey

  • Newbie
  • *
  • Posts: 36
    • View Profile
Re: Low framerate on SFML 2.0
« Reply #1 on: July 24, 2012, 07:16:19 am »
My game loop is now:

Quote
    // main game loop
    while(App.isOpen()) {
        // event checking
        while(App.pollEvent(event)) {
            switch (event.type) {
                case sf::Event::Closed:
                    App.close();
                    break;
                case sf::Event::KeyPressed:
                    // esc detection
                    if (event.key.code == sf::Keyboard::Escape)
                        App.close();
                    // wasd movements
                    if (event.key.code == sf::Keyboard::W)
                        key_w = true;
                    else if (event.key.code == sf::Keyboard::S)
                        key_s = true;
                    else if (event.key.code == sf::Keyboard::A)
                        key_a = true;
                    else if (event.key.code == sf::Keyboard::D)
                        key_d = true;
                    break;
                case sf::Event::KeyReleased:
                    // wasd movements
                    if (event.key.code == sf::Keyboard::W)
                        key_w = false;
                    else if (event.key.code == sf::Keyboard::S)
                        key_s = false;
                    else if (event.key.code == sf::Keyboard::A)
                        key_a = false;
                    else if (event.key.code == sf::Keyboard::D)
                        key_d = false;
                    break;
            }
           
            if (key_w) {
                cube.move(0, -1);
                if (cube.isColliding(&a))
                        cube.move(0, +1);
            }
            if (key_s) {
                cube.move(0, +1);
                if (cube.isColliding(&a))
                        cube.move(0, -1);
            }
            if (key_a) {
                cube.move(-1, 0);
                if (cube.isColliding(&a))
                        cube.move(+1, 0);
            }
            if (key_d) {
                cube.move(+1, 0);
                if (cube.isColliding(&a))
                        cube.move(-1, 0);
            }
        }
        App.clear();
        // start drawing logic
        App.draw(cube.get());
        App.draw(a.get());
        // end drawing logic
        App.display();
    }

But I've noticed an eve stranger thing: when I move the mouse (I guess because there are also the mouse movement events) the sprite move faster and the framerate increase. The more I move the mouse the more the framerate is highter.

Also now I've solved the first movement problem (diagonally movement) but I've another: when I press w+a, the sprite moves correctly towards the top left corner diagonally, but when I release, for example, w the sprite stops, even if my a key is pressed. Why is that so?
« Last Edit: July 24, 2012, 07:18:11 am by Jeffrey »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Low framerate on SFML 2.0
« Reply #2 on: July 24, 2012, 07:39:04 am »
sf::Keyboard::isKeyPressed() does exactly what you're doing with your booleans key_a, key_d, etc. Use it. ;)

Jeffrey

  • Newbie
  • *
  • Posts: 36
    • View Profile
Re: Low framerate on SFML 2.0
« Reply #3 on: July 24, 2012, 08:36:55 am »
Thanks, I didn't know about it. Implemented.

Game loop is now:

    // main game loop
    while(App.isOpen()) {
        // event checking
        while(App.pollEvent(event)) {
            switch (event.type) {
                case sf::Event::Closed:
                    App.close();
                    break;
            }
           
            if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
                cube.move(0, -1);
                if (cube.isColliding(&a))
                        cube.move(0, +1);
            }
            if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
                cube.move(0, +1);
                if (cube.isColliding(&a))
                        cube.move(0, -1);
            }
            if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
                cube.move(-1, 0);
                if (cube.isColliding(&a))
                        cube.move(+1, 0);
            }
            if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
                cube.move(+1, 0);
                if (cube.isColliding(&a))
                        cube.move(-1, 0);
            }
        }
        App.clear();
        // start drawing logic
        App.draw(cube.get());
        App.draw(a.get());
        // end drawing logic
        App.display();
    }
 

And all the problems with the movements have been solved. Now what about the framerate? Why is it so low normally and so fast when moving the mouse?
« Last Edit: July 24, 2012, 09:20:26 am by Jeffrey »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Low framerate on SFML 2.0
« Reply #4 on: July 24, 2012, 08:51:40 am »
Because all your logic is inside the event loop. So your game is updated only when something (such as a mouse move) happens.

By the way, you should use the [code=cpp] tag to format your code on the forum.
Laurent Gomila - SFML developer

Jeffrey

  • Newbie
  • *
  • Posts: 36
    • View Profile
Re: Low framerate on SFML 2.0
« Reply #5 on: July 24, 2012, 09:21:36 am »
Edited, and yes, that was the problem, thanks.

 

anything