SFML community forums
Help => Graphics => Topic started by: wademcgillis on September 09, 2009, 09:44:14 pm
-
Why is it so slow?
Why does drawing the text dynamically slow it down so much?
void draw_text(int x, int y, char* text)
{
sf::String displayText = sf::String();
displayText.SetColor(sf::Color(globalsys::current_color->getRed(),globalsys::current_color->getGreen(),
globalsys::current_color->getBlue(),255*globalsys::current_alpha));
displayText.SetFont(globalsys::current_font.font);
displayText.SetSize(globalsys::current_font.size);
if (view_enabled)
displayText.Move(x-view_xview,y-view_yview);
else
displayText.Move(x,y);
displayText.SetText(text);
App.Draw(displayText);
}
-
What is "slow" to you? What is your configuration?
How do you use this function? Can you extract a minimal and complete example using this function that is slow?
-
Slow meaning it drops in frame rate by a lot when I draw large text. Like, drawing 1 piece of text at 12 pt is fine. But when I do 64pt there is a noticeable drop, and when I do 128pt it is awful.
http://www.wademcgillis.com/downloads/fps3D.zip
Right click to draw big text.
-
It shouldn't be so slow. I'd like to see your code, if possible.
-
I really bet you call that "draw_text" function each frame. Please tell me you're not. ;)
-
I am.
-
I really bet you call that "draw_text" function each frame. Please tell me you're not.
What's wrong with that? He's just setting up and drawing a graphical string. It's not as if he was loading the font on each call ;)
-
I really bet you call that "draw_text" function each frame. Please tell me you're not.
What's wrong with that? He's just setting up and drawing a graphical string. It's not as if he was loading the font on each call ;)
Well, that maybe gives you a hint on how the rest of his program is written. I think it's a total waste of resources and time.
wademcgillis, could you please show us the rest of your (relevant) code?
-
Did you profile it and find out what part of code is responsible for the bad frame rate?
And you're not testing in debug mode, are you?
-
Well, that maybe gives you a hint on how the rest of his program is written. I think it's a total waste of resources and time.
I agree. But it's perfectly ok to use SFML drawables like this, nothing is wasted.
From the (incomplete) source code I got in PM, I can find only one potential cause for the bad performances. What is the ptototype of the draw_set_font function?
If the font is passed by value rather than by reference, then you're copying the whole font (including its OpenGL texture) everytime you call it.
-
I agree. But it's perfectly ok to use SFML drawables like this, nothing is wasted.
You must be kidding. ;)
sf::String displayText = sf::String();
Standard constructor, assignment. Why not directly assign the text? (this may be optimized by your favorite compiler, but that doesn't mean you shouldn't do it right)
displayText.SetColor(sf::Color(globalsys::current_color->getRed(),globalsys::current_color->getGreen(),
globalsys::current_color->getBlue(),255*globalsys::current_alpha));
Absolute unnecessary sf::Color temporary.
displayText.SetFont(globalsys::current_font.font);
displayText.SetSize(globalsys::current_font.size);
Again, this can be done through the constructor, saves calls.
The problem is not within that single function. It's that when it's done everywhere like this (and I think it is), performance will decrease sooner or later.
But okay, it's getting off topic here. ;)
-
You must be kidding
Absolutely not ;)
All he does is assigning a few primitive types (either pointer, float or int). Internally, nothing happens until the call to Draw.
I know that this kind of design is globally bad, but some people prefer this C-like approach instead of a clean OO design. And SFML won't be slower for these people.
-
Oops. I forgot to send the other .h files.
-
I was right:
void draw_set_font(Font f)
{
globalsys::current_font = f;
}
You're creating / copying / destroying a huge amout of data and resources on every call.
This should be like this:
const sf::Font* current_font;
void draw_set_font(const Font& f)
{
globalsys::current_font = &f;
}
-
Oh wow... THANK YOU!