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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Offerus

Pages: [1]
1
Window / Re: RenderWindow as a parameter of a member function in a thread
« on: November 08, 2017, 10:35:15 am »
Oh, i think we had this one in the lecture at any time, but i never needed it. I think it's the lack of experience  ;)

Thanks, your suggestions really helped!

2
Window / Re: RenderWindow as a parameter of a member function in a thread
« on: November 08, 2017, 10:12:00 am »
Thats true. Then i have to rearrange the program and create a new class for managing all the monsters, instead of letting every monster manage itself.

By the way, is there a smarter solution for creating multiple monsters, all with individual positions and health points, than writing:

Monster M1;
Monster M2;
Monster M3;
Monster M4;
Monster M5;
      .
      .
      .

M1.m_run() // m_run is the member function to move the monster
M2.m_run()
M3.m_run()
M4.m_run()
M5.m_run()
      .
      .
      .
 

3
Window / Re: RenderWindow as a parameter of a member function in a thread
« on: November 08, 2017, 09:08:15 am »
@dabbertorres
Until now i couldn't figure out how to pass the argument, even with your link. I tried several variations of this:

#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <iostream>
#include <thread>

class ClassWithThread
{
public:

    ClassWithThread(sf::RenderWindow& window)
    : m_thread(&ClassWithThread::f, this)
    {
                i = 4;
                m_window = window;
    }

    void f()
    {
        std::cout << i << std::endl;
    }

private:
        sf::RenderWindow& m_window;
    std::thread m_thread;
    int i;
};

int main (){
        // create the window
       sf::RenderWindow window(sf::VideoMode(1920, 1080), "MyWindow");
        ClassWithThread x(window);

        x.f();
return 0;
}
 

@eXpl0it3r
I would like to avoid threads, but i thought it is my only option: I wrote a game with monsters one has to destroy. So i wrote a class for the monster and it moves on the gamefield. But now i want two ( or better 20  ;) ) monsters simultaneously. If i call the member function for moving monsters one after another, they are not moving at the same time :) . So i thougt every monster needs its own thread.

4
Window / RenderWindow as a parameter of a member function in a thread
« on: November 07, 2017, 11:20:21 pm »
Hello everyone,
I'm trying to start a member function in a new thread, which works fine, but i can not manage to use a render window as a parameter of the member function. Here is an small example of what i'm struggling with:

This code works perfectly fine:
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <iostream>
#include <thread>

class ClassWithThread
{
public:

    ClassWithThread()
    : m_thread(&ClassWithThread::f, this)
    {
                i = 4;
    }

    void f()
    {
        std::cout << i << std::endl;
    }

private:

    std::thread m_thread;
    int i;
};

int main (){
        ClassWithThread x;
        x.f();
return 0;
}
 

But if i try to use a render window inside the member function, i get a long error message that i don't really understand:

#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <iostream>
#include <thread>

class ClassWithThread
{
public:

    ClassWithThread()
    : m_thread(&ClassWithThread::f, this)
    {
                i = 4;
    }

    void f(sf::RenderWindow& window)
    {
        std::cout << i << std::endl;
    }

private:

    std::thread m_thread;
    int i;
};

int main (){
        ClassWithThread x;
        // create the window
        sf::RenderWindow window(sf::VideoMode(1920, 1080), "MyWindow");
        x.f(window);
return 0;
}
 

With the error (sorry, some of it is in german):
(click to show/hide)

I already tried to commit the window as a pointer or as a reference. I'm using linux debian testing with g++ 4:7.2.0-1d1 and libsfml 2.4.

Has anyone an idea what i'm doing wrong?

Pages: [1]