I was just wondering if you could set textured sprites as global variables using SFML 2.0. I need to draw a sprite to the screen through a function other than the int main() function. However, I cannot seem to declare and define textures and sprites outside of a function. Here's some sample code similar to what I had:
#include <SFML/Graphics.hpp>
sf::Sprite spritename;
sf::Texture texturename;
texturename.loadFromFile("texture.png");
spritename.setTexture(texturename);
int main()
{
//code here
}
However, the compiler would say the texturename on line 4 and the spritename on line 5 had no storage class or type specifier.
Initialize your global variables inside the main function. You'll still be able to access them in any other functions defined in this cpp.
You probably don't want the as global variables, you are best to store them somewhere scoped and appropriate, and pass them to other functions that require them for use.
sf::Sprite spritename;
sf::Texture texturename;
int main()
{
texturename.loadFromFile("texture.png");
spritename.setTexture(texturename);
}