SFML community forums
Help => General => Topic started by: starfruitinc 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.
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
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
-
Using std::cout is very slow, have you tried without it?
-
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?
-
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??
-
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.
-
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.