I'm not sure but try removing sf::Font from
sf::Font CRender::openSans.loadFromFile("data/fonts/SourceSansPro-Regular.ttf");
You're not initializing that static sf::Font, you're calling one of it's methods.
You can't call a function during initialization of an object, you can only choose which constructor to use.
There's an easy workaround though, using the copy constructor:
sf::Font createFont()
{
sf::Font font;
font.loadFromFile("data/fonts/SourceSansPro-Regular.ttf");
return font;
}
sf::Font CRender::openSans(createFont());
But loading SFML resources at global scope is strongly disadvised, it may lead to undefined behaviour. You should rather add an init() function that you call at the beginning, to initialize your renderer's internal stuff.
That works too but it kind of abuses class slicing so.. :(
class MyFont : public sf::Font
{
public:
MyFont(const std::string& font_to_load){loadFromFile(font_to_load);}
};
I can't initialize static member in non-constructor function too.
Of course you can, what's the problem with that?
But in first place, why do you need this static font member? There's a much cleaner solution to your "default argument" issue:
void CRender::write(string & text, sf::RenderWindow & window, short int x, short int y, short int size)
{
write(text, window, x, y, size, openSans);
}
void CRender::write(string & text, sf::RenderWindow & window, short int x, short int y, short int size, sf::Font & font)
{
sf::Text temp(text, font, size);
temp.setPosition(x, y);
window.draw(temp);
}
And... this "C" design where everything's done with functions will not give you the best performances. Creating a new sf::Text object everytime you draw text is very expensive, especially since your texts will most likely never change. It's much more efficient to create all the resources and entities to draw at init time, update them when needed and then what's left to be done 60 times per second is only to draw them.
And if it comes to initialize static member in function VC 2008 gives me an error (doing it outside of any function works flawlessy). Well, maybe I'm just doing something wrong.
You still have to define (initialize) static instances in a cpp file outside any function, but then you can use these objects like any other, in any function.
class Blop
{
// declaration
static sf::Font font;
Blop();
};
// definition
sf::Font Blop::font;
Blop::Blop()
{
font.loadFromFile(...);
}
Thanks. So it looks like the best solution is to make simple container based resource manager with all needed sf::Text instances. Well I wanted easy solution to write, because setting all sf::Text objects at startup isn't very convenient and at one moment I may need 5 of them and at another i may need 30, so I think resource manager is the best way to go.
Managers can be ok for shared resources such as textures or sounds, but why would you need them for text? Can't each text be stored and initialized in its owner class? Low-level graphics entities are usually part of higher-level classes that have their own init/update/draw cycle.