Chargement des textures pour ball et block

This commit is contained in:
maelle 2016-03-12 11:22:10 +01:00
parent 9214ff7ac5
commit 327d9411c9
9 changed files with 181 additions and 1 deletions

BIN
Images/texture_test.jpeg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

View File

@ -1,6 +1,14 @@
#include "ball.hpp" #include "ball.hpp"
void Ball::draw(sf::RenderWindow& window) { void Ball::draw(sf::RenderWindow& window) {
//chargement de la texture de test
if (!texture.loadFromFile("./Images/texture_test.jpeg"))
{
//erreur
}
shape.setTexture(&texture);
shape.setPosition(position); shape.setPosition(position);
window.draw(shape); window.draw(shape);
} }

49
ball.cpp~ Normal file
View File

@ -0,0 +1,49 @@
#include "ball.hpp"
void Ball::draw(sf::RenderWindow& window) {
shape.setPosition(position);
window.draw(shape);
}
sf::Vector2f Ball::getForces(State state) {
sf::Vector2f forces = PhysicsObject::getForces(state);
// déplacement de la balle après appui sur les touches de direction
if (state.goLeftKey) {
forces += sf::Vector2f(-Ball::MOVE, 0);
}
if (state.goRightKey) {
forces += sf::Vector2f(Ball::MOVE, 0);
}
// force d'attraction entre les balles et les blocs chargés
if (getCharge() != 0) {
for (unsigned int j = 0; j < state.objects.size(); j++) {
Object *attractive = state.objects[j];
if (attractive == this || attractive->getCharge() == 0) {
continue;
}
// vecteur allant de l'objet attirant vers l'objet considéré
sf::Vector2f attraction(position - attractive->getPosition());
// la norme de ce vecteur est la distance entre les objets
float distanceSquared = attraction.x * attraction.x +
attraction.y * attraction.y;
// normalisation du vecteur direction qui porte
// la force d'attraction, puis application de la norme
attraction /= std::sqrt(distanceSquared);
attraction *= Ball::ATTRACTION * (
(charge * attractive->getCharge()) /
distanceSquared
);
forces += attraction;
}
}
return forces;
}

View File

@ -8,6 +8,7 @@
class Ball : public PhysicsObject { class Ball : public PhysicsObject {
protected: protected:
sf::Texture texture;
sf::CircleShape shape; sf::CircleShape shape;
/** /**

40
ball.hpp~ Normal file
View File

@ -0,0 +1,40 @@
#ifndef __PTF_BALL_HPP__
#define __PTF_BALL_HPP__
#include <SFML/Graphics.hpp>
#include <iostream>
#include "state.hpp"
#include "physics_object.hpp"
class Ball : public PhysicsObject {
protected:
sf::CircleShape shape;
/**
* Calcule les forces appliquées à l'objet
*/
virtual sf::Vector2f getForces(State state);
static constexpr float ATTRACTION = 25000;
static constexpr float MOVE = 100;
public:
Ball(float x, float y) : PhysicsObject(x, y), shape(10 * mass) {
shape.setOrigin(sf::Vector2f(10 * mass, 10 * mass));
shape.setFillColor(sf::Color(255, 245, 131));
}
/**
* Dessine la balle dans la fenêtre donnée
*/
void draw(sf::RenderWindow& window);
/**
* Détermine la couche d'affichage de l'objet
*/
unsigned int getLayer() {
return 1;
}
};
#endif

View File

@ -1,16 +1,26 @@
#include "block.hpp" #include "block.hpp"
void Block::draw(sf::RenderWindow& window) { void Block::draw(sf::RenderWindow& window) {
//chargement de la texture de test
if (!texture.loadFromFile("./Images/texture_test.jpeg"))
{
//erreur
}
shape.setTexture(&texture);
if (charge > 0) { if (charge > 0) {
shape.setFillColor(sf::Color(0, 0, 255)); shape.setFillColor(sf::Color(0, 0, 255));
} else if (charge < 0) { } else if (charge < 0) {
shape.setFillColor(sf::Color(255, 0, 0)); shape.setFillColor(sf::Color(255, 0, 0));
} else { } else {
shape.setFillColor(sf::Color(0, 0, 0)); shape.setFillColor(sf::Color(75, 75, 75));
} }
shape.setPosition(position); shape.setPosition(position);
window.draw(shape); window.draw(shape);
} }
void Block::update(State state) { void Block::update(State state) {

29
block.cpp~ Normal file
View File

@ -0,0 +1,29 @@
#include "block.hpp"
void Block::draw(sf::RenderWindow& window) {
//chargement de la texture de test
if (!texture.loadFromFile("./Images/texture_test.jpeg"))
{
//erreur
}
shape.setTexture(&texture);
shape.setTextureRect(sf::IntRect(5, 5, 10, 10));
if (charge > 0) {
shape.setFillColor(sf::Color(0, 0, 255));
} else if (charge < 0) {
shape.setFillColor(sf::Color(255, 0, 0));
} else {
shape.setFillColor(sf::Color(75, 75, 75));
}
shape.setPosition(position);
window.draw(shape);
}
void Block::update(State state) {
// rien à mettre à jour
}

View File

@ -8,6 +8,7 @@
class Block : public Object { class Block : public Object {
protected: protected:
sf::Texture texture;
sf::RectangleShape shape; sf::RectangleShape shape;
public: public:

42
block.hpp~ Normal file
View File

@ -0,0 +1,42 @@
#ifndef __PTF_BLOCK_HPP__
#define __PTF_BLOCK_HPP__
#include <SFML/Graphics.hpp>
#include <iostream>
#include "object.hpp"
#include "state.hpp"
class Block : public Object {
protected:
sf::Texture texture;
sf::Sprite sprite;
sf::RectangleShape shape;
public:
static constexpr float GRID = 32;
Block(float x, float y) : Object(x, y), shape(sf::Vector2f(Block::GRID, Block::GRID)) {
shape.setOrigin(sf::Vector2f(Block::GRID / 2, Block::GRID / 2));
shape.setFillColor(sf::Color(0, 0, 0));
}
/**
* Dessin du bloc dans la fenêtre donnée
*/
void draw(sf::RenderWindow& window);
/**
* Met à jour l'objet juste avant le dessin d'une frame
* Reçoit l'état actuel du moteur
*/
void update(State state);
/**
* Détermine la couche d'affichage de l'objet
*/
unsigned int getLayer() {
return 0;
}
};
#endif