I am writing a splash screen class to display while loading. I want it to be created on the main thread and then display itself on a separate thread until I tell it to quit.
This was working fine. However, I also want to display an image in this splash screen window, so I need an sf::Sprite. However, whenever I add the sf::Sprite, the program crashes when it's destructor is called (saying Windows has triggered a breakpoint).
Here is my code; any help fixing this problem would be greatly appreciated
class SplashScreen {
sf::Image Image;
sf::Font Font;
sf::Thread* Thread;
bool Running;
unsigned Width, Height;
const std::string Caption, Heading;
void Execute()
{
sf::Sprite bg(Image);
bg.Scale((float)Width/(float)Image.GetWidth(), (float)Height/(float)Image.GetHeight());
Running = true;
sf::RenderWindow Window(sf::VideoMode(Width, Height, 32), Caption, sf::Style::None);
while (Running)
{
Window.Draw(bg);
Window.Display();
}
}
public:
SplashScreen(unsigned Width, unsigned Height, const std::string& Heading, const std::string& Caption, const std::string& ImageFilename, const std::string& FontFilename = "arial.ttf")
: Width(Width), Height(Height), Caption(Caption), Heading(Heading)
{
if (!Image.LoadFromFile(ImageFilename))
throw Exceptions::BadFile(ImageFilename);
if (!Font.LoadFromFile(FontFilename))
throw Exceptions::BadFile(FontFilename);
Thread = new (std::nothrow) sf::Thread(&SplashScreen::Execute, this);
if (!Thread)
throw Exceptions::BadAllocation();
}
~SplashScreen()
{
Stop();
delete Thread;
}
void Run()
{
Thread->Launch();
}
void Stop()
{
Running = false;
Thread->Wait();
}
};