Bonjour,
Alors je me suis mis au c++ il n'y a pas très longtemps et depuis quelques heures j'essaie de faire un Pong...
Le problème est ma classe Player qui ne se dessine pas, la compilation et l'exe se lance mais la fenêtre est noire :
- Player.h
#ifndef PLAYER_H
#define PLAYER_H
#include <SFML/Graphics.hpp>
class Player : public sf::Drawable
{
public:
Player(int pX, int pY, int pId, sf::Color pColor);
sf::RectangleShape getShape();
void movement();
int getId();
virtual ~Player();
protected:
private:
sf::RectangleShape shape;
int id;
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
};
#endif // PLAYER_H
- Player.cpp
#include "Player.h"
#include <SFML/Graphics.hpp>
#include <iostream>
Player::Player(int pX, int pY, int pId, sf::Color pColor = sf::Color::Blue)
{
sf::RectangleShape shape(sf::Vector2f(35, 120));
//int id = pId;
shape.setFillColor(pColor);
shape.setPosition(sf::Vector2f(pX, pY));
std::cout << "player"<<pId << std::endl;
std::cout << "PosX : "<<pX << std::endl;
std::cout << "PosY : "<<pY << std::endl;
}
void Player::movement(){
if(getId() == 1){
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
getShape().move(0, 0.1);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
getShape().move(0, -0.1);
}else {
if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
getShape().move(0, 0.1);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Z))
getShape().move(0, -0.1);
}
}
int Player::getId(){
return id;
}
sf::RectangleShape Player::getShape(){
return shape;
};
void Player::draw(sf::RenderTarget& target, sf::RenderStates states)const{
target.draw(shape, states);
}
Player::~Player()
{
//dtor
}
main.cpp
#include <SFML/Graphics.hpp>
#include <iostream>
#include "include/Player.h"
using namespace std;
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML Testing !");
Player player1(20, 50, 1, sf::Color::Blue);
Player player2(740, 400, 2, sf::Color::Red);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
window.close();
break;
}
}
player1.movement();
player2.movement();
window.clear();
window.draw(player1);
window.draw(player2);
window.display();
}
return 0;
}
Je vous remercie d'avance !