SFML community forums

Help => System => Topic started by: snoozer87 on May 11, 2012, 01:13:34 pm

Title: Loading resources in a thread without making them global variables
Post by: snoozer87 on May 11, 2012, 01:13:34 pm
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
Code: [Select]
#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
Code: [Select]
//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
Code: [Select]
...
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
Code: [Select]
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
Title: Re: Loading resources in a thread without making them global variables
Post by: Laurent on May 11, 2012, 01:31:46 pm
Have a closer look at the example which uses a member function, in the sf::Thread API documentation.
Title: Re: Loading resources in a thread without making them global variables
Post by: snoozer87 on May 11, 2012, 01:44:08 pm
Ok, I tried this now
Code: [Select]
RulesScreen::RulesScreen(){
sf::Thread th(&RulesScreen::loadThread,this);
}

But now I get an access violation here:

Code: [Select]
void RulesScreen::Init(sf::RenderWindow* SFMLView1){
....
m_pSFML_VIEW=SFMLView1;
if(!loaded){
th->launch();
}
m_pSFML_VIEW->setActive(); //ACCESS VIOLATION
}
I'm probably partly on the right track here...
Title: Re: Loading resources in a thread without making them global variables
Post by: Laurent on May 11, 2012, 02:50:36 pm
What does the debugger say?