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

Author Topic: Slow Rendering in SFML  (Read 2579 times)

0 Members and 1 Guest are viewing this topic.

starfruitinc

  • Newbie
  • *
  • Posts: 9
    • View Profile
Slow Rendering in SFML
« on: September 18, 2011, 06:16:40 pm »
Hello everyone,

I have been playing around with SFML for a game I am designing, but I came across a problem, of which I am unable to determine if it is the result of just my poor coding or if SFML is designed to be called a different way.

Currently, I have a class called Module and it is a container for some variables and a Sprite.

Code: [Select]

class Module {
    public:
        sf::Sprite s_module;
        std::vector<Module> children;
        void addModule();
        void moveFromSpeed(float, float);
        void drawAll(sf::RenderWindow &);
        void setImage(sf::Image & i_module);
};


Here are the methods

Code: [Select]
void Module::drawAll(sf::RenderWindow & App) {
    cout << "Drawing Self\t" << children.size() << " Children" << endl;
    App.Draw(s_module);
    cout << "Drawing Children" << endl;
    for (int i = 0; i < (int)children.size(); i++) {
        children[i].drawAll(App);
    }
}

void Module::addModule() {
    children.push_back(Module());
}

void Module::setImage(sf::Image & i_module) {

    //Set Sprite
    s_module.SetImage(i_module);
    s_module.SetBlendMode(sf::Blend::Alpha);
    s_module.SetPosition(x, y);
}

void Module::moveFromSpeed(float tvelx, float tvely) {
    //Uses the speed variables to move the ship
    x += tvelx;
    y += tvely;
    s_module.SetPosition(x,y);
    for (int i = 0; i < (int)children.size(); i++) {
        children[i].moveFromSpeed(tvelx, tvely);
    }
}


When I add Module classes to children, it slows down considerably. Around 30 children, it slows to maybe 5-10 fps.

Input is appreciated.

Thanks

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Slow Rendering in SFML
« Reply #1 on: September 18, 2011, 06:44:04 pm »
Using std::cout is very slow, have you tried without it?
Laurent Gomila - SFML developer

starfruitinc

  • Newbie
  • *
  • Posts: 9
    • View Profile
Slow Rendering in SFML
« Reply #2 on: September 18, 2011, 06:52:51 pm »
Wow. std::cout is VERY slow. I removed all lines with std::cout and there is a definite boost to the performance. Thank you very much :D

Is there a better way to output to the console without cout?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Slow Rendering in SFML
« Reply #3 on: September 18, 2011, 07:11:50 pm »
Usually we do this for debugging, so we don't care about performances.

Why do you want to print to the console so many times per second and keep good performances??
Laurent Gomila - SFML developer

Serapth

  • Full Member
  • ***
  • Posts: 105
    • View Profile
Slow Rendering in SFML
« Reply #4 on: September 18, 2011, 08:23:17 pm »
cout is not buffered on Windows.


Add:

char buf[1024];
setvbuf(stdout, buf, _IOLBF, 1024);


in your main() and you will see an order of magnitude increase in speed.  In my test, it went from 200FPS to 7-800FPS.

starfruitinc

  • Newbie
  • *
  • Posts: 9
    • View Profile
Slow Rendering in SFML
« Reply #5 on: September 29, 2011, 08:16:48 am »
It works very well, thank you very much for your help!

I'm using the output mainly for debugging right now, and I'm keeping a lot of verbose info on. I want it to run at full speed because it is a real time application. I will be removing the couts later.

 

anything