Bonjour
Voilà mon problème je souhaite faire des projectiles donc j'utilise un thread. Mais je n'arrive pas à intégrer un thread dans ma scène principale afin de lier ma classe projectile malgré avoir regardé la doc sur les threads ...
Voici mon code :
#include "Projectile.h"
Projectile::Projectile()
{
Projectile_Blue.setSize(sf::Vector2f(1, 1));
Projectile_Blue.setPosition(0, 400);
Projectile_Blue.setFillColor(sf::Color::Blue);
}
void Projectile::Move(sf::RenderWindow &window)
{
sf::FloatRect ProjectileBox(Projectile_Blue.getPosition(), Projectile_Blue.getSize());
bool the_end = true;
while(the_end)
{
if (ProjectileBox.contains(window.getSize().x, Projectile_Blue.getPosition().y))
the_end = false;
Projectile_Blue.move(5, 0);
sf::sleep(sf::milliseconds(25));
}
}
void Projectile::setPosition(int x, int y)
{
Projectile_Blue.setPosition(x, y);
}
int Projectile::getPositionX(){ return Projectile_Blue.getPosition().x; }
int Projectile::getPositionY(){ return Projectile_Blue.getPosition().y; }
#ifndef PROJECTILE_H
#define PROJECTILE_H
#include <SFML/Graphics.hpp>
#include <iostream>
class Projectile : public sf::Drawable
{
public:
Projectile();
void Move(sf::RenderWindow &window);
void setPosition(int x, int y);
int getPositionX();
int getPositionY();
private:
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
{
target.draw(Projectile_Blue, states);
}
sf::RectangleShape Projectile_Blue;
};
#endif // PROJECTILE_H
#include "Scene.h"
sf::Clock horloge;
sf::Time tempsFrame;
Scene::Scene()
{
sf::RenderWindow window(sf::VideoMode(), "Warrior Of Space", sf::Style::Fullscreen);
window.setKeyRepeatEnabled(true);
window.setFramerateLimit(60);
sf::Texture image;
if(!image.loadFromFile("Ressources/Background.png"))
std::cout<<"Error"<<std::endl;
sf::Sprite background(image);
Spaceship spaceship;
Projectile projectile;
Gui gui(&spaceship);
sf::View camera(sf::FloatRect(0, 0, 800, 600));
sf::Thread t_projectileMove(std::bind(&Projectile::Move, &projectile, &window));
float timer;
while (window.isOpen())
{
timer += m_tempsFrame.asSeconds();
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
t_projectileMove.terminate();
}
if (event.type == sf::Event::Resized)
{
sf::FloatRect visibleArea(0, 0, event.size.width, event.size.height);
window.setView(sf::View(visibleArea));
}
if ( (event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape) )
{
window.close();
t_projectileMove.terminate();
}
if ( (event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Right) )
{
spaceship.Move(5, 0);
}
if ( (event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Left) )
{
spaceship.Move(-5, 0);
}
if ( (event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Z) )
{
spaceship.addHealth(+5);
}
if ( (event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::A) )
{
spaceship.addHealth(-5);
}
if ( (event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Space) )
{
t_projectileMove.terminate();
t_projectileMove.launch();
}
}
std::cout << spaceship.getHealth() << std::endl;
window.clear();
window.draw(background);
window.draw(spaceship);
window.draw(projectile);
window.draw(gui);
gui.Update(&spaceship);
m_tempsFrame = m_horloge.restart();
window.setView(camera);
window.display();
}
}
#ifndef SCENE_H
#define SCENE_H
#include <SFML/Graphics.hpp>
#include <iostream>
#include <functional>
#include "Spaceship.h"
#include "Projectile.h"
#include "Gui.h"
class Scene
{
public:
Scene();
private:
sf::Clock m_horloge;
sf::Time m_tempsFrame;
};
#endif // SCENE_H