I needed a way to easily use same font and colors throughout my UI. Every UI component like button or textbox has its own class with render function. At first I made the fonts and colors as parameters of the render function but it seemed stupid to include them in every single one function call. Therefore I made a header which declares them:
#pragma once
#include <SFML\Graphics.hpp>
extern sf::Color baseColor;
extern sf::Color darkAccent;
extern sf::Color lightAccent;
extern sf::Color colorAccent;
extern sf::Font baseUiFont;
The header is included in every .cpp file that needs the variables.
Then I define them in the beginning of my main() like so:
sf::Color baseColor(50, 50, 50);
sf::Color darkAccent(30, 30, 30);
sf::Color lighAccent(70, 70, 70);
sf::Color colorAccent(255, 140, 0);
sf::Font baseUiFont;
baseUiFont.loadFromFile("Resources/OpenSans-Regular.ttf");
The problem is that this does not work. I get two errors:
Error LNK2001 unresolved external symbol "class sf::Color baseColor" (?baseColor@@3VColor@sf@@A)
Error LNK1120 1 unresolved externals
I tested similar declaration and definition of a simple int variable and it did work. Therefore I think that the problem is in the way SFML works. Is there a solution to this? All I need is a simple way to pass these variables to every object that needs them.
you can wrap them in a struct:
In some 'globals.h' file
typedef struct ui{
struct color{
sf::Color base;
sf::Color darkAccent;
sf::Color lightAccent;
sf::Color accent;
}color;
sf::Font font;
};
extern struct ui;
In a cpp file:
struct ui;
void initialize(){
// init the ui...
}
Then initialize it somewhere in your application (in the main if it's a very small app that doesn't need better organization).
Globals aren't a bad if they are used for global things... just group them into structs or classes to avoid too much 'extern' keywords