SFML community forums

Help => System => Topic started by: fatum on October 16, 2011, 01:30:13 am

Title: Callback a method inside of a class with sf::Thread?
Post by: fatum on October 16, 2011, 01:30:13 am
I've tried many different scenarios, but I can't seem to figure out.  Here's a basic example if what I'd like to do:

Code: [Select]

class Test
{
private:
ThreadHandle();
//
public:
Test();
void setup();
//
};


void Test::setup()
{
sf::Thread TestThread(&ThreadHandle);
TestThread.Launch();
}


I've also tried setting up the "TestThread" object in the header file, but it alerts me that "ThreadHandle" is not a data type.

Thanks for any help!
Title: Callback a method inside of a class with sf::Thread?
Post by: Nexus on October 16, 2011, 02:32:01 am
What do you want to do? Is ThreadHandle a member function of Test? I ask because the return type is missing, and the term "handle" usually refers to objects, not functions.

For member functions, you can use the following constructor:
Code: [Select]
template <typename C>  
Thread(void (C::*function)(), C* object)

See here (http://www.sfml-dev.org/documentation/2.0/classsf_1_1Thread.php). Then you have to pass &Test::ThreadHandle and this as arguments.
Title: Callback a method inside of a class with sf::Thread?
Post by: Ectance on November 09, 2011, 07:15:25 pm
I have :

Code: [Select]

class A
{
   private :
      sf::Thread *thread;
      bool running;
      void loop();
   public :
      A();
      ~A();
      void Start();
      void Stop();
    };



A::A()
{
   thread = new sf::Thread(&A::loop,this); // <- here is the error
   running = false;
}



A::~A()
{
   Stop();
   sf::Sleep(1.0f);
   delete thread;
}



void A::Start()
{
   running = true;
   thread->Launch();
}



void A::Stop()
{
   running = false;
}



void A::loop()
{
   while(running)
      // do sth
}



I get this error :
error C2664: 'sf::Thread::Thread(sf::Thread::FuncType,void *)' : cannot convert parameter 1 from 'void (__thiscall A::* )(void)' to 'sf::Thread::FuncType'
1>          There is no context in which this conversion is possible


( MS VC++ 2010 xpress )
Title: Callback a method inside of a class with sf::Thread?
Post by: Laurent on November 09, 2011, 08:13:28 pm
Are you using SFML 1.6?
Title: Callback a method inside of a class with sf::Thread?
Post by: Ectance on November 09, 2011, 08:51:20 pm
yes sir , im using 1.6
will 2.0 work fine ?
Title: Callback a method inside of a class with sf::Thread?
Post by: Laurent on November 09, 2011, 10:48:15 pm
The syntax that you use is for SFML 2.0 only. Read the documentation and tutorials ;)