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

Author Topic: Thread not working on a loop game  (Read 3536 times)

0 Members and 1 Guest are viewing this topic.

stvince

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Thread not working on a loop game
« on: April 20, 2014, 08:47:39 pm »
Hello, I'm triing to use the sfml thread's, and the folowing basic exemple works perfectly:
void            thread_func(__attribute__((unused))int *i)
  {
    while (1)
      {
        std::cout << "ça alterne" << std::endl;
        sleep(1);
      }
  }

int             main()
{
  int           i;

  i = 5;
  sf::Thread thread(&thread_func, &i);
  thread.launch();
    while (1)
      {
        std::cout << "ça devrait alterner" << std::endl;
        sleep (1);
      }
}

But, when i'm triing to do exactly the same thing with a window: create a thread on an event, it stop the main thread. does anyone know why? Is the window use different? Am I doing a stupid and basic mistake?

I've done the following code, not commented, but verry basic: Main.cpp, Game.cpp, Game.hpp

This is the Game.hpp
# include <SFML/Window.hpp>
# include <SFML/Graphics.hpp>
# include <iostream>

# define FRAMERATE 60.f
# define TIMER sf::seconds(1.f / FRAMERATE)

class Game
{
public:
  Game();
  ~Game();
  bool                  init();
  void                  loop();
private:
  void                  myexit();
  void                  update();
  void                  refresh();
  void                  event();
  void                  fps();
private:
  sf::Time              _time;
  sf::Clock             _clock;
  sf::Clock             _clockfps;

  sf::RenderWindow      _window;
  sf::Event             _event;

  unsigned int          _width;
  unsigned int          _height;
  bool                  _game;

  int                   _frames;
  int                   _fps;
  sf::Time              _timefps;
};

void                    thread_func_test(int *i);
the Game.cpp:
#include "Client/Game.hpp"
//#include "Client/Particles.hpp"

Game::Game()
{
  _fps = 0;
  _width = 800;
  _height = 600;
  _game = true;
  _frames = 0;
}

Game::~Game()
{
}

bool                    Game::init()
{
  //_window.create(sf::VideoMode::getDesktopMode(), "window", sf::Style::Close);
  _window.create((sf::VideoMode(_width, _height)), "window", sf::Style::Close);
  _window.setActive(false);
  return (true);
}

void                    Game::refresh()
{
  _frames++;
  _window.display();
}

void                    thread_func_test(__attribute__((unused))int *i)
{
  while (1);
}

void                    Game::event()
{
  while (_window.pollEvent(_event))
    {
      if (_event.key.code == sf::Keyboard::Escape)
          _game = false;
      if (_event.type == sf::Event::Closed)
        _game = false;
      if (_event.mouseButton.button == sf::Mouse::Right)
        {
          int           i = 5;
          sf::Thread    thread(&thread_func_test, &i);
          thread.launch();
        }
    }
}

void                    Game::myexit()
{
  _window.close();
}

void                    Game::fps()
{
  _timefps += _clockfps.restart();
  if (_timefps >= sf::seconds(1.f))
    {
      _fps = _frames;
      std::cout << "fps: " << _fps << std::endl;
      _timefps = sf::Time::Zero;
      _frames = 0;
    }
}

void                    Game::update()
{
  //rien pour le moment je debug;
  std::cout << "pouet" << std::endl;
}

void                    Game::loop()
{
  _time = sf::Time::Zero;
  while (_game)
    {
      _time += _clock.restart();
      event();
      fps();
      if (_time >= TIMER)
        {
          _window.clear();
          update();
          refresh();
          _time = sf::Time::Zero;
        }
      else
        sf::sleep(TIMER - _time);
    }
  myexit();
}
and the Main.cpp:
#include "Client/Game.hpp"

int             main()
{
  Game          game;
  if (game.init())
    game.loop();
  return (0);
}
I compile with:
-W -Wextra -Wall -Werror -lsfml-system -lsfml-window -lsfml-graphics

The problem is that the main thread is not still running when I click on the right mouse.
Does anyone can help me please?
Thanks you

ps: i just notice that this post should be on the window topic :/
« Last Edit: April 20, 2014, 09:02:05 pm by stvince »

Raincode

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: Thread not working on a loop game
« Reply #1 on: April 21, 2014, 10:47:17 am »
Doc suggests to use the standard library facilities for threads. Just sayin.

stvince

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Thread not working on a loop game
« Reply #2 on: April 21, 2014, 12:23:59 pm »
thanks: i solved the problem using a pointer:
  sf::Thread *thread;
  thread = new sf::Thread(&thread_func_test, &i);
  thread->launch();

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Thread not working on a loop game
« Reply #3 on: April 21, 2014, 12:44:53 pm »
You know... reading the tutorial and/or the documentation helps in cases like these. The problem you have is exactly the one that is described in the tutorial and reading the documentation along with a bit of C++ knowledge would help you solve it as well. Your current solution will cause a memory leak, so if you care about leaks I suggest you think of a better solution or resort to using the standard library threading facilities as Raincode already suggested.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

stvince

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Thread not working on a loop game
« Reply #4 on: April 21, 2014, 12:57:00 pm »
thanks you a lot, i add a sf::Thread *_thread as a class variable, and it work well.
Does anyone know if its possible to call _window.draw(...) from differents threads?

 

anything