Dear community,
At the moment I am trying to load my resources in separate thread so that I can show a loading screen in the mean time, everything works, however I had to make all resources global variables (textures/sprites/...). This is probably very bad practice and I would like to know how work with namespaces and threads.
To clarify, this is my current method and it works:
RulesScreen.h
#ifndef _RULESSCREEN_H_
#define _RULESSCREEN_H_
#define MAXPANELS 4
#include "GameScreen.h"
class RulesScreen : public GameScreen{
public:
...
protected:
...
private:
.....
sf::Thread *th;
....
};
#endif
RulesScreen.cpp
//GLOBAL VARIABLES
sf::Texture t_mBackground;
sf::Sprite s_mBackground;
bool loaded; //LOADED BOOLEAN, IF THE SCREEN IS LOADED ALREADY, DON'T LOAD IT AGAIN
void loadThread(){
if(!loaded){
//LOADING TEXTURES
t_mBackground.loadFromFile(".//Resources//globalbackground.png");
//LOAD TEXTURES INTO SPRITES
s_mBackground.setTexture(t_mBackground);
//SET SPRITE POSITION
s_mBackground.setPosition(0,0);
loaded=true;
}
}
RulesScreen::RulesScreen(){
th=new sf::Thread(&loadThread);
}
void RulesScreen::Init(sf::RenderWindow* SFMLView1){
th->launch();
}
However, when I change this code to the more conventional method:
RULESCREEN.H
...
private:
sf::Texture t_mBackground;
sf::Sprite s_mBackground;
bool loaded; //LOADED BOOLEAN, IF THE SCREEN IS LOADED ALREADY, DON'T LOAD IT AGAIN
...
void loadThread();
RULESSCREEN.CPP
RulesScreen::RulesScreen(){
th=new sf::Thread(&RulesScreen::loadThread);
}
void RulesScreen::loadThread(){
if(!loaded){
//LOADING TEXTURES
t_mBackground.loadFromFile(".//Resources//globalbackground.png");
//LOAD TEXTURES INTO SPRITES
s_mBackground.setTexture(t_mBackground);
//SET SPRITE POSITION
s_mBackground.setPosition(0,0);
loaded=true;
}
}
void RulesScreen::Init(sf::RenderWindow* SFMLView1){
th->launch();
}
Then I get this error:
error C2064: term does not evaluate to a function taking 0 arguments
Any ideas on how to do this the right way?
Best regards
Yannick