Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Loading resources in a thread without making them global variables  (Read 2817 times)

0 Members and 1 Guest are viewing this topic.

snoozer87

  • Newbie
  • *
  • Posts: 13
    • View Profile
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

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Loading resources in a thread without making them global variables
« Reply #1 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.
Laurent Gomila - SFML developer

snoozer87

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Loading resources in a thread without making them global variables
« Reply #2 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...
« Last Edit: May 11, 2012, 01:46:44 pm by snoozer87 »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Loading resources in a thread without making them global variables
« Reply #3 on: May 11, 2012, 02:50:36 pm »
What does the debugger say?
Laurent Gomila - SFML developer