In my program, I have a single image that will be displayed multiple times. In the tutorials, I read that a good way to handle such a situation is to make a static image that every sprite accesses. I took the code from the tutorial, and now I'm trying to use it, but I'm getting a few issues.
first let me post the code:
////////////VariableLabel.h
#include "headers.h"
#pragma once
class VariableLabel
{
public:
VariableLabel();
static bool Initialize(const std::string& ImageFile);
private:
static sf::Image image; // Each sprite object shares this single image
sf::Sprite Sprite; // But there is one sprite per variable label
double value;
};
////////////VariableLabel.cpp
#include "VariableLabel.h"
VariableLabel::VariableLabel()
{
Sprite.SetImage(image); // Every sprite uses the same image
}
bool VariableLabel::Initialize(const std::string& ImageFile)
{
return image.LoadFromFile(ImageFile);
}
My questions concerns the implementation of the constructor. If I declare an instance of VariableLabel before calling the static function "static bool Initialize(const std::string& ImageFile)", when I try to draw the sprite that the VariableLabel contains, nothing is drawn. I assume that this is because image is not initialized when the constructor of VariableLabel is called, so the sprite is never assigned an image.
If I modify my code a bit and create an instance of VariableLabel after calling the initialize function, things work properly. Is there anyway to modify the class so that the constructor is still able properly assign images to the sprites before calling the initialize function. I only ask because I would like the call the initialize function inside a function of a class that contains VariableClass objects as its members.
Thank you.
-Nick