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

Author Topic: Ubuntu - Speed Issues  (Read 1816 times)

0 Members and 2 Guests are viewing this topic.

Peter P.

  • Newbie
  • *
  • Posts: 2
    • View Profile
Ubuntu - Speed Issues
« on: November 26, 2015, 01:31:16 am »
Hello folks,

I recently started looking for a C++ Multimedia library and decided to give SFML a try. I have built SFML on Xubuntu 14.04 utilizing Cmake according to official websites instructions. So far so good. I have now implemented a very basic version of the first code example given in the book 'SFML game developement', which is a circle moving about when one of the 'wasd' keys is pressed.

Now this does work, however,  since I have not attempted to regulate the circle's speed, I would have expected it to move very rapidly. In reality it takes about 20 seconds for the circle to cross my screen.

I guess the program is very crudely implemented, also I do not have a dedicated graphics-card (my laptop has an Intel 4400 built in). Still I can't believe this sort of behavior is to be expected. I actually compiled the same program on another laptop running arch and the framerate increased about a hundredfold.

Is there anything I might be missing?

Here's the code, pretty much straight from the book.

//The game.h header
class Game
{
    public:
        Game();
        void run();

    private:
        void processEvents();
        void handlePlayerInput(sf::Keyboard::Key, bool);
        void update();
        void render();
   
        sf::RenderWindow mWindow;
        sf::CircleShape mPlayer;
       
        bool mIsMovingUp;
        bool mIsMovingDown;
        bool mIsMovingLeft;
        bool mIsMovingRight;
};


//The implementation
#include<SFML/Graphics.hpp>
#include "game.h"

Game::Game()
: mWindow(sf::VideoMode(640, 480), "SFML Application")
, mIsMovingUp(false), mIsMovingDown(false), mIsMovingLeft(false), mIsMovingRight(false)
, mPlayer()
{
    mWindow.setKeyRepeatEnabled(false);
       
    mPlayer.setRadius(40.f);
    mPlayer.setPosition(100.f, 100.f);
    mPlayer.setFillColor(sf::Color::Cyan);
}

void Game::run()
{
    while (mWindow.isOpen())
    {
        processEvents();
        update();
        render();
    }
}

void Game::render()
{
    mWindow.clear();
    mWindow.draw(mPlayer);
    mWindow.display();
}

void Game::processEvents()
{
    static sf::Event event;

    if (mWindow.pollEvent(event))
    {
        switch (event.type)
        {
            case sf::Event::KeyPressed:
                handlePlayerInput(event.key.code, true);
                break;
            case sf::Event::KeyReleased:
                handlePlayerInput(event.key.code, false);
                break;
            case sf::Event::Closed:
                mWindow.close();
                break;
        }
    }
}

void Game::handlePlayerInput(sf::Keyboard::Key key, bool isPressed)
{
    if (key == sf::Keyboard::W)
            mIsMovingUp = isPressed;

    else if (key == sf::Keyboard::S)
            mIsMovingDown = isPressed;

    else if (key == sf::Keyboard::A)
            mIsMovingLeft = isPressed;

    else if (key == sf::Keyboard::D)
            mIsMovingRight = isPressed;
}

void Game::update()
{
    sf::Vector2f movement(0.f, 0.f);

    if (mIsMovingUp)
        movement.y -= 1.0f;    
    if (mIsMovingDown)
        movement.y += 1.0f;
    if (mIsMovingLeft)
        movement.x -= 1.0f;
    if (mIsMovingRight)
        movement.x += 1.0f;

    mPlayer.move(movement);
}

//The main function
#include <SFML/Graphics.hpp>
#include "game.h"

int main()
{

    Game game;
    game.run();
   
    return 0;
}
 

SeriousITGuy

  • Full Member
  • ***
  • Posts: 123
  • Still learning...
    • View Profile
Re: Ubuntu - Speed Issues
« Reply #1 on: November 26, 2015, 02:25:47 pm »
Your problem is not connected to Ubuntu or your OS in general. The "problem" is this line in Game::Game()
mWindow.setKeyRepeatEnabled(false);

After this, if you press and hold a key, no additional key events are generated. Therefore you must release the key and press it again, to fire a new key-press event. With every key press you only move by 1.f pixel, and therefore you must press it very fast ;)
Do the additional chapters in the book and you will find more elegant solutions to this "problem". For more info google "fix your timestep".

SpeCter

  • Full Member
  • ***
  • Posts: 151
    • View Profile
Re: Ubuntu - Speed Issues
« Reply #2 on: November 26, 2015, 03:18:09 pm »
Your problem is not connected to Ubuntu or your OS in general. The "problem" is this line in Game::Game()
mWindow.setKeyRepeatEnabled(false);

After this, if you press and hold a key, no additional key events are generated. Therefore you must release the key and press it again, to fire a new key-press event. With every key press you only move by 1.f pixel, and therefore you must press it very fast ;)
Do the additional chapters in the book and you will find more elegant solutions to this "problem". For more info google "fix your timestep".

This is not entirely the case here ;)
He sets:
mIsMovingUp;
mIsMovingDown;
mIsMovingLeft;
mIsMovingRight;

He only needs to hold it pressed(and generate only one event) to move, since they only get set to false again if he releases the respective button.As long as they are true, update will move his sprite.

The main problem is that the percieved speed is dependend on his framerate(which is why he should google for "fix your timestep" as you said.
If his driver has vsync enabled by default(@60hz for example) this would mean that he needs 18 seconds to get from one side of the screen to the other with a resolution of 1920*1080.


Peter P.

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Ubuntu - Speed Issues
« Reply #3 on: November 26, 2015, 05:22:24 pm »
I did factor in my monitors refresh rate (I don't see how this would be related to vsync), still everything was a bit slow and not as fluent as it should be. (in retrospect the "hundredfold" increase on arch was a bit of an exxageration). I just finished building SFML on my main OS (Debian) and now it seems to work out fine.
That does the trick for me, still, thanks for the advice.

Edit: Wait a minute I believe my brain just malfunctioned, I now remember how vsync works :D
And it's not enabled by default things are going too fast for that. Still not "that" fast... are there any benchmark programs for SFML that I could try running to see how my computer performs overall?
« Last Edit: November 26, 2015, 05:35:41 pm by Peter P. »

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Ubuntu - Speed Issues
« Reply #4 on: November 26, 2015, 07:35:39 pm »
Just something to consider: are you testing release builds with compiler optimizations enabled or debug builds without?
If you are testing unoptimized debug builds I'd say you can just throw your results out the window. Only ever compare performance of optimized release builds. Depending on the code in question, the compiler in use, the OS, the runtime library and many many other things, release builds can easily provide orders of magnitude better performance than debug builds.