SFML community forums
Help => System => Topic started by: snoozer87 on April 24, 2012, 11:44:41 pm
-
How would the procedure work in the tutorials for an external thread class (SFML 2.0), like the code below:
Thread.ccp
#include "globalInclude.h"
class ThreadClass{
public:
void run(){
//NEED to use a variable that is passed from introductionState.cpp
}
private:
sf::Uint8 mAlpha;
};
introductionState.ccp
//VARIABLE TO BE PASSED
sf::uint8 alpha
...
...
void IntroScreen::TransitionOut(){
ThreadClass threadc;
sf::Thread th(&ThreadClass::run,&threadc);
th.launch();
transition=true;
}
Best regards
-
Alright, I got it running but my thread is blocking the caller and so the caller waits on the thread to be finished, which makes it not a thread at all :p.
Caller (does the sfml drawing and stuff) introScreen.cpp
void IntroScreen::TransitionOut(){
if(!transition){
transition=true;
ThreadClass threadc(this);
sf::Thread th(&ThreadClass::run,&threadc);
th.launch();
}
}
void IntroScreen::setFadeAmount(sf::Uint8 alpha){
mfade_alpha=alpha;
}
Thread.h
#include "globalInclude.h"
#include "GameScreen.h"
class ThreadClass{
public:
ThreadClass(GameScreen* pScreen);
ThreadClass();
void run();
private:
sf::Uint8 mAlpha;
sf::Clock mFadeClock;
GameScreen* m_pScreen;
};
Thread.ccp
#include "ThreadClass.h"
ThreadClass::ThreadClass(GameScreen* pScreen){
m_pScreen=pScreen;
mAlpha=0;
}
ThreadClass::ThreadClass(){
mAlpha=0;
}
void ThreadClass::run(){
bool done=false;
while(!done){
if(mAlpha!=255){
if(mFadeClock.getElapsedTime().asMilliseconds()>500){
if((255-mAlpha)<60){
mAlpha=255;
done=true;
}else{
mAlpha+=60;
}
//m_pScreen->setFadeAmount(mAlpha);
mFadeClock.restart();
}
}else{
done=true;
}
}
}
Is this the right way to do it and if so, what am I doing wrong?
Best regards
-
Your thread object is local to a function, so it is destroyed (and the main thread waits for it) immediately after being created.
-
I've changed them to global variables, but for some reason it works in VS2010 release mode, but when I try the .Exe it doesn't work. I should clarify that the not working is the fact that the thread doesn't seem to start and thus the screen is not faded.
//Global variables
ThreadClass threadc(IntroScreen::Instance());
sf::Thread th(&ThreadClass::run,&threadc);
-
Avoid global variables, especially those that do something in their constructor and/or destructor (don't forget that the corresponding code will be executed before/after main() runs).
-
Alright, sry for bothering you so much, but how would you do it then. Because now I tried this and the exe still does nothing:
caller.h
...
private:
ThreadClass *threadc;
sf::Thread *th;
caller.cpp
void IntroScreen::Init(sf::RenderWindow* SFMLView1){
...
threadc=new ThreadClass(this);
th=new sf::Thread(&ThreadClass::run,threadc);
...
}
void IntroScreen::TransitionOut(){
if(!transition){
transition=true;
th->launch();
}
}
**EDIT
MY apologies, I forgot to add my fade.png to the release map :s. But is my code correct, or is there a cleaner and better way to do it?
-
Your code is ok ;)