Lecture des objets depuis un fichier

This commit is contained in:
Mattéo Delabre 2016-04-02 15:18:29 +02:00
parent 57f7772b76
commit 9fb90a0371
7 changed files with 119 additions and 28 deletions

View File

@ -2,7 +2,6 @@
#define __PTF_BLOCK_HPP__
#include <SFML/Graphics.hpp>
#include <fstream>
#include "object.hpp"
class Block : public Object {
@ -15,7 +14,7 @@ public:
*/
static const unsigned int TYPE_ID;
Block(float x, float y);
Block();
virtual ~Block();
/**

View File

@ -2,6 +2,7 @@
#define __PTF_OBJECT_HPP__
#include <SFML/Graphics.hpp>
#include <fstream>
#include <memory>
#include "collision.hpp"
#include "manager.hpp"
@ -17,9 +18,9 @@ private:
sf::VertexArray acceleration_line;
sf::VertexArray velocity_line;
mutable float inv_mass;
float mass;
mutable float inv_mass;
float charge;
float restitution;
float static_friction;
@ -33,9 +34,25 @@ protected:
virtual sf::Vector2f getForces(const Manager& manager, const std::vector<ObjectPtr>& objects) const;
public:
Object(float x, float y);
/**
* Identifiants uniques des propriétés communes modifiables
*/
static const unsigned int PROP_MASS;
static const unsigned int PROP_CHARGE;
static const unsigned int PROP_RESTITUTION;
static const unsigned int PROP_STATIC_FRICTION;
static const unsigned int PROP_DYNAMIC_FRICTION;
static const unsigned int PROP_LAYER;
Object();
virtual ~Object();
/**
* Charge les propriétés communes à tous les objets
* depuis le fichier donné dans l'objet donné
*/
static void load(std::ifstream& file, std::shared_ptr<Object> object);
/**
* Dessine l'objet dans la fenêtre donnée
*/

View File

@ -2,7 +2,6 @@
#define __PTF_PLAYER_HPP__
#include <SFML/Graphics.hpp>
#include <fstream>
#include "object.hpp"
class Player : public Object {
@ -22,7 +21,7 @@ public:
*/
static const unsigned int TYPE_ID;
Player(float x, float y);
Player();
virtual ~Player();
/**

Binary file not shown.

View File

@ -3,9 +3,9 @@
#include "constants.hpp"
#include "resource_manager.hpp"
const unsigned int Block::TYPE_ID = 1;
const unsigned int Block::TYPE_ID = 2;
Block::Block(float x, float y) : Object(x, y) {
Block::Block() : Object() {
// par défaut, les blocs ne sont pas déplaçables et ont
// donc une masse infinie, représentée par 0
setMass(0.f);
@ -17,7 +17,13 @@ Block::Block(float x, float y) : Object(x, y) {
Block::~Block() {}
std::shared_ptr<Object> Block::load(std::ifstream& file) {
return std::shared_ptr<Object>(new Block(10, 10));
std::shared_ptr<Object> object = std::shared_ptr<Object>(new Block);
// lecture des propriétés communes des objets
Object::load(file, object);
file.seekg(1, file.cur);
return object;
}
void Block::draw(Manager& manager) {

View File

@ -2,20 +2,90 @@
#include "constants.hpp"
#include "collision.hpp"
Object::Object(float x, float y) :
acceleration(0, 0), velocity(0, 0), position(x, y),
const unsigned int Object::PROP_MASS = 1;
const unsigned int Object::PROP_CHARGE = 2;
const unsigned int Object::PROP_RESTITUTION = 3;
const unsigned int Object::PROP_STATIC_FRICTION = 4;
const unsigned int Object::PROP_DYNAMIC_FRICTION = 5;
const unsigned int Object::PROP_LAYER = 6;
Object::Object() :
acceleration(0, 0), velocity(0, 0), position(0, 0),
acceleration_line(sf::Lines, 2),
velocity_line(sf::Lines, 2),
mass(Constants::DEFAULT_MASS),
inv_mass(1.f / Constants::DEFAULT_MASS),
charge(Constants::DEFAULT_CHARGE),
restitution(Constants::DEFAULT_RESTITUTION),
inv_mass(-1.f),
// valeurs par défaut pour les propriétés
// de tous les objets du jeu
mass(1.f), charge(0.f),
restitution(0.4f),
static_friction(0.4f),
dynamic_friction(0.2f),
layer(Constants::DEFAULT_LAYER) {}
layer(0) {}
Object::~Object() {}
void Object::load(std::ifstream& file, std::shared_ptr<Object> object) {
// lecture de la position de l'objet
float pos_x, pos_y;
file.read(reinterpret_cast<char*>(&pos_x), sizeof(pos_x));
file.read(reinterpret_cast<char*>(&pos_y), sizeof(pos_y));
object->setPosition(sf::Vector2f(
pos_x * Constants::GRID, pos_y * Constants::GRID
));
// lecture des propriétés facultatives
char prop_type = -1;
while (file.read(&prop_type, 1)) {
switch (prop_type) {
case Object::PROP_MASS:
float mass;
file.read(reinterpret_cast<char*>(&mass), sizeof(mass));
object->setMass(mass);
break;
case Object::PROP_CHARGE:
float charge;
file.read(reinterpret_cast<char*>(&charge), sizeof(charge));
object->setCharge(charge);
break;
case Object::PROP_RESTITUTION:
float restitution;
file.read(reinterpret_cast<char*>(&restitution), sizeof(restitution));
object->setRestitution(restitution);
break;
case Object::PROP_STATIC_FRICTION:
float static_friction;
file.read(reinterpret_cast<char*>(&static_friction), sizeof(static_friction));
object->setStaticFriction(static_friction);
break;
case Object::PROP_DYNAMIC_FRICTION:
float dynamic_friction;
file.read(reinterpret_cast<char*>(&dynamic_friction), sizeof(dynamic_friction));
object->setDynamicFriction(dynamic_friction);
break;
case Object::PROP_LAYER:
char layer;
file.read(&layer, 1);
object->setLayer((int) layer - 127);
break;
default:
// propriété de type inconnu : on recule
// d'un octet et on sort
file.seekg(-1, file.cur);
return;
}
}
}
sf::Vector2f Object::getForces(
const Manager& manager, const std::vector<ObjectPtr>& objects
) const {

View File

@ -4,9 +4,9 @@
#include <array>
#include <iostream>
const unsigned int Player::TYPE_ID = 0;
const unsigned int Player::TYPE_ID = 1;
Player::Player(float x, float y) : Object(x, y) {
Player::Player() : Object() {
// déplacement de l'origine au centre de la balle
sprite.setOrigin(sf::Vector2f(getRadius(), getRadius()));
}
@ -14,19 +14,19 @@ Player::Player(float x, float y) : Object(x, y) {
Player::~Player() {}
std::shared_ptr<Object> Player::load(std::ifstream& file) {
float pos_x, pos_y;
std::shared_ptr<Object> object = std::shared_ptr<Object>(new Player);
std::shared_ptr<Player> player = std::dynamic_pointer_cast<Player>(object);
file.read(reinterpret_cast<char*>(&pos_x), sizeof(pos_x));
file.read(reinterpret_cast<char*>(&pos_y), sizeof(pos_y));
// lecture du numéro de joueur
char player_number;
file.read(&player_number, 1);
player->setPlayerNumber(player_number);
pos_x *= Constants::GRID;
pos_y *= Constants::GRID;
// lecture des propriétés communes des objets
Object::load(file, object);
file.seekg(1, file.cur);
std::shared_ptr<Player> player =
std::shared_ptr<Player>(new Player(pos_x, pos_y));
// player->setPlayerNumber(i);
return std::dynamic_pointer_cast<Object>(player);
return object;
}
sf::Vector2f Player::getForces(const Manager& manager, const std::vector<ObjectPtr>& objects) const {