Nettoyage du code (suppression #include inutiles, variables globales)

This commit is contained in:
Mattéo Delabre 2016-04-12 18:11:36 +02:00
parent 87dc7aa715
commit ca3d36bea8
35 changed files with 425 additions and 394 deletions

View File

@ -1,4 +0,0 @@
{
"cmd": "cd {PROJECT_PATH} && cmake {PROJECT_PATH} && make",
"name": "cmake"
}

View File

@ -6,6 +6,7 @@
#include "object.hpp"
class Game;
class ResourceManager;
class Level;
class Block : public Object {

View File

@ -1,27 +0,0 @@
#ifndef __SKIZZLE_CONSTANTS_HPP__
#define __SKIZZLE_CONSTANTS_HPP__
#include <SFML/System.hpp>
namespace Constants {
/**
* Constante d'attraction. Utilisée dans la formule
* pour le calcul de l'attraction coulombienne entre
* deux objets
*/
static const float ATTRACTION = 500000;
/**
* Correction positionnelle : pourcentage de correction
* et seuil de correction
*/
static const float CORRECTION_PERCENTAGE = .5f;
static const float CORRECTION_SLOP = .02f;
/**
* Taille de la grille des blocs en pixels
*/
static const float GRID = 32;
}
#endif

View File

@ -4,7 +4,8 @@
#include <SFML/Graphics.hpp>
#include <memory>
#include "block.hpp"
#include "game.hpp"
class Game;
class FinishBlock : public Block {
public:

View File

@ -2,7 +2,7 @@
#define __PTF_GAME_HPP__
#include "level.hpp"
#include "editor.hpp"
#include "widget_timer.hpp"
/**
* La classe Game gère l'affichage et les objets
@ -25,7 +25,6 @@ private:
sf::Time next_frame_time;
std::vector<Object::Ptr> pending_kill;
std::shared_ptr<Editor> return_state;
Mode mode;
DeathCause death_cause;

View File

@ -4,7 +4,9 @@
#include <SFML/Graphics.hpp>
#include <memory>
#include "block.hpp"
#include "game.hpp"
class Game;
enum class GravityDirection;
class GravityBlock : public Block {
public:

View File

@ -4,7 +4,8 @@
#include <SFML/Graphics.hpp>
#include <memory>
#include "block.hpp"
#include "game.hpp"
class Game;
class KillBlock : public Block {
public:

View File

@ -6,8 +6,8 @@
#include "state.hpp"
#include "object.hpp"
#include "player.hpp"
#include "manager.hpp"
#include "resource_manager.hpp"
class Manager;
// liste des directions de la gravité
enum class GravityDirection {NORTH, EAST, SOUTH, WEST};

View File

@ -2,10 +2,11 @@
#define __SKIZZLE_MANAGER_HPP__
#include "resource_manager.hpp"
#include "state.hpp"
#include <memory>
#include <stack>
class State;
/**
* Gestionnaire principal de tous les états, vues et
* ressources du jeu
@ -37,6 +38,11 @@ public:
*/
static const sf::Time FRAME_TIME;
/**
* Taille d'une case de la grille du jeu
*/
static const float GRID;
Manager();
/**

View File

@ -1,11 +1,12 @@
#ifndef __PTF_MENU_HPP__
#define __PTF_MENU_HPP__
#include "manager.hpp"
#include "state.hpp"
#include <functional>
#include <vector>
class Manager;
/**
* La classe Menu charge le menu du jeu
* et permet de choisir entre jouer, lire les règles

View File

@ -5,8 +5,6 @@
#include <fstream>
#include <memory>
#include "collision.hpp"
#include "manager.hpp"
#include "resource_manager.hpp"
class Level;
class Game;
@ -53,16 +51,6 @@ protected:
static void init(std::ifstream& file, Object::Ptr object);
public:
/**
* 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();

View File

@ -1,9 +1,10 @@
#ifndef __PTF_RULES_HPP__
#define __PTF_RULES_HPP__
#include "manager.hpp"
#include "state.hpp"
class Manager;
/**
* La classe Rules affiche les règles du jeu
*/
@ -26,5 +27,4 @@ public:
virtual void frame();
};
#endif

View File

@ -4,7 +4,6 @@
#include <SFML/Graphics.hpp>
class ResourceManager;
class Object;
class Manager;
/**

View File

@ -4,7 +4,8 @@
#include <SFML/Graphics.hpp>
#include <memory>
#include "block.hpp"
#include "game.hpp"
class Game;
class SwitchBlock : public Block {
public:

View File

@ -3,7 +3,8 @@
#include <SFML/Graphics.hpp>
#include <functional>
#include "manager.hpp"
class Manager;
/**
* Affiche un bouton pouvant être cliqué

View File

@ -4,7 +4,8 @@
#include <SFML/Graphics.hpp>
#include <functional>
#include "widget_button.hpp"
#include "manager.hpp"
class Manager;
/**
* Affiche le compteur de temps pouvant (ou non)

View File

@ -5,7 +5,8 @@
#include <functional>
#include "object.hpp"
#include "level.hpp"
#include "manager.hpp"
class Manager;
/**
* Représente un objet plaçable depuis la barre d'outils

View File

@ -2,17 +2,16 @@
#include "level.hpp"
#include "game.hpp"
#include "player.hpp"
#include "constants.hpp"
#include "resource_manager.hpp"
#include "manager.hpp"
const unsigned int Block::TYPE_ID = 2;
Block::Block() : Object() {
aabb = sf::FloatRect(
-Constants::GRID / 2,
-Constants::GRID / 2,
Constants::GRID,
Constants::GRID
-Manager::GRID / 2,
-Manager::GRID / 2,
Manager::GRID,
Manager::GRID
);
}
@ -90,7 +89,7 @@ sf::FloatRect Block::getAABB() const {
}
float Block::getRadius() const {
return Constants::GRID / 2;
return Manager::GRID / 2;
}
unsigned int Block::getTypeId() const {

View File

@ -7,6 +7,11 @@
#include <utility>
#include <cmath>
/**
* Définition des variables et fonctions globales internes
* (accessibles uniquement dans ce fichier)
*/
namespace {
/**
* Détermination des informations sur une collision entre
* un cercle et un rectangle
@ -179,6 +184,7 @@ bool AABBToAABB(CollisionData& data) {
return true;
}
}
CollisionData::CollisionData() {}
bool getCollisionData(CollisionData& data) {

View File

@ -1,10 +1,14 @@
#include "manager.hpp"
#include "editor.hpp"
#include "game.hpp"
#include "block.hpp"
#include "constants.hpp"
#include <cmath>
#include <algorithm>
/**
* Définition des variables et fonctions globales internes
* (accessibles uniquement dans ce fichier)
*/
namespace {
const sf::Color SELECT_RECT_COLOR = sf::Color(33, 33, 33, 40);
const sf::Color SELECT_RECT_BORDER_COLOR = sf::Color(33, 33, 33, 127);
const sf::Color ZONE_POINT_COLOR = sf::Color(140, 15, 15, 255);
@ -13,16 +17,17 @@ const sf::Color ZONE_BORDER_COLOR = sf::Color(200, 15, 15, 255);
const float WHEEL_SCROLL_SPEED = -7.f;
const float POINTER_SCROLL_SPEED = 5.f;
const int POINTER_SCROLL_PADDING = 10;
}
/**
* Arrondit le vecteur donné à une position
* sur la grille
*/
inline sf::Vector2f roundVectorToGrid(sf::Vector2f input) {
input /= Constants::GRID;
input /= Manager::GRID;
input.x = round(input.x);
input.y = round(input.y);
input *= Constants::GRID;
input *= Manager::GRID;
return input;
}

View File

@ -1,3 +1,4 @@
#include "resource_manager.hpp"
#include "finish_block.hpp"
#include "game.hpp"

View File

@ -1,8 +1,8 @@
#include <iostream>
#include <cmath>
#include "manager.hpp"
#include "game.hpp"
#include "player.hpp"
#include "constants.hpp"
Game::Game(Manager& manager) : Level(manager),
widget_timer(manager, false),

View File

@ -1,3 +1,4 @@
#include "resource_manager.hpp"
#include "gravity_block.hpp"
#include "game.hpp"

View File

@ -1,3 +1,4 @@
#include "resource_manager.hpp"
#include "kill_block.hpp"
#include "game.hpp"
#include "player.hpp"

View File

@ -1,4 +1,4 @@
#include "constants.hpp"
#include "manager.hpp"
#include "level.hpp"
#include "player.hpp"
#include "block.hpp"
@ -13,6 +13,11 @@
#include <functional>
#include <queue>
/**
* Définition des variables et fonctions globales internes
* (accessibles uniquement dans ce fichier)
*/
namespace {
/**
* Constante de gravité
*/
@ -107,8 +112,8 @@ void loadLevel(
file.read(reinterpret_cast<char*>(&pos_x), 4);
file.read(reinterpret_cast<char*>(&pos_y), 4);
pos_x *= Constants::GRID;
pos_y *= Constants::GRID;
pos_x *= Manager::GRID;
pos_y *= Manager::GRID;
zone.push_back(sf::Vector2f(pos_x, pos_y));
}
@ -144,6 +149,7 @@ void loadLevel(
object_callback(object_type_map[object_type](file));
}
}
}
Level::Level(Manager& manager) : State(manager) {
sf::Vector2u window_size = getWindow().getSize();
@ -228,8 +234,8 @@ void Level::save(std::string path) {
file.write(&control_points, 1);
for (int i = 0; i < control_points; i++) {
float pos_x = zone[i].x / Constants::GRID;
float pos_y = zone[i].y / Constants::GRID;
float pos_x = zone[i].x / Manager::GRID;
float pos_y = zone[i].y / Manager::GRID;
file.write(reinterpret_cast<char*>(&pos_x), 4);
file.write(reinterpret_cast<char*>(&pos_y), 4);

View File

@ -1,7 +1,9 @@
#include "state.hpp"
#include "manager.hpp"
const unsigned int Manager::FPS = 60;
const sf::Time Manager::FRAME_TIME = sf::seconds(1.f / Manager::FPS);
const float Manager::GRID = 32;
Manager::Manager() : title(sf::String(L"")) {
// préchargement des textures

View File

@ -1,11 +1,10 @@
#include "manager.hpp"
#include "menu.hpp"
#include "rules.hpp"
#include "editor.hpp"
#include "game.hpp"
#include <cmath>
const float MAX_WIDTH_PROPORTION = 1.f / 3.f;
Menu::Menu(Manager& manager) : State(manager) {
background.setTexture(getResourceManager().getTexture("bg_menu.tga"));
loadMainMenu();

View File

@ -1,28 +1,60 @@
#include "manager.hpp"
#include "object.hpp"
#include "game.hpp"
#include "constants.hpp"
#include "collision.hpp"
#include <cmath>
const unsigned int Object::PROP_MASS = 1;
/**
* Définition des variables et fonctions globales internes
* (accessibles uniquement dans ce fichier)
*/
namespace {
// identifiant de la propriété de masse et sa valeur par défaut
// (une masse de zéro représente une masse infinie)
const unsigned int PROP_MASS = 1;
const float DEFAULT_MASS = 0.f;
const unsigned int Object::PROP_CHARGE = 2;
// identifiant de la propriété de charge et sa valeur par défaut
const unsigned int PROP_CHARGE = 2;
const float DEFAULT_CHARGE = 0.f;
const unsigned int Object::PROP_RESTITUTION = 3;
// identifiant de la propriété de restitution et sa valeur par défaut
// (plus la restitution est forte, plus les objets rebondissent)
const unsigned int PROP_RESTITUTION = 3;
const float DEFAULT_RESTITUTION = 0.4f;
const unsigned int Object::PROP_STATIC_FRICTION = 4;
// identifiant du coefficient de frottement statique et sa valeur par défaut
// (coefficient proportionnel à la qté d'énergie nécessaire pour mettre
// en mouvement l'objet)
const unsigned int PROP_STATIC_FRICTION = 4;
const float DEFAULT_STATIC_FRICTION = 0.4f;
const unsigned int Object::PROP_DYNAMIC_FRICTION = 5;
// identifiant du coefficient de frottement dynamique et sa valeur par défaut
// (coefficient proportionnel aux pertes d'énergie en mouvement)
const unsigned int PROP_DYNAMIC_FRICTION = 5;
const float DEFAULT_DYNAMIC_FRICTION = 0.2f;
const unsigned int Object::PROP_LAYER = 6;
// identifiant de la propriété calque et sa valeur par défaut
// (les objets sur deux calques différents n'entrent pas en collision,
// et les objets sont dessinés par ordre de calque)
const unsigned int PROP_LAYER = 6;
const int DEFAULT_LAYER = 0;
// coefficient d'attraction. Proportionnel à la quantité d'énergie
// fournie par un objet chargé
const float ATTRACTION = 500000;
// coefficients de correction positionnelle permettant de réduire
// la visibilité des erreurs d'arrondi des flottants. Le pourcentage
// de correction indique la proportion de correction par rapport à la
// vitesse et le seuil indique le minimum de correction appliqué
const float CORRECTION_PERCENTAGE = .5f;
const float CORRECTION_SLOP = .02f;
}
Object::Object() :
acceleration(0, 0), velocity(0, 0), position(0, 0),
selected(false), inv_mass(-1.f),
// valeurs par défaut pour les propriétés
// de tous les objets du jeu
mass(DEFAULT_MASS),
charge(DEFAULT_CHARGE),
restitution(DEFAULT_RESTITUTION),
@ -40,7 +72,7 @@ void Object::init(std::ifstream& file, Object::Ptr object) {
file.read(reinterpret_cast<char*>(&pos_y), 4);
object->setPosition(sf::Vector2f(
pos_x * Constants::GRID, pos_y * Constants::GRID
pos_x * Manager::GRID, pos_y * Manager::GRID
));
// lecture des propriétés facultatives
@ -48,37 +80,37 @@ void Object::init(std::ifstream& file, Object::Ptr object) {
while (file.read(&prop_type, 1)) {
switch (prop_type) {
case Object::PROP_MASS:
case PROP_MASS:
float mass;
file.read(reinterpret_cast<char*>(&mass), 4);
object->setMass(mass);
break;
case Object::PROP_CHARGE:
case PROP_CHARGE:
float charge;
file.read(reinterpret_cast<char*>(&charge), 4);
object->setCharge(charge);
break;
case Object::PROP_RESTITUTION:
case PROP_RESTITUTION:
float restitution;
file.read(reinterpret_cast<char*>(&restitution), 4);
object->setRestitution(restitution);
break;
case Object::PROP_STATIC_FRICTION:
case PROP_STATIC_FRICTION:
float static_friction;
file.read(reinterpret_cast<char*>(&static_friction), 4);
object->setStaticFriction(static_friction);
break;
case Object::PROP_DYNAMIC_FRICTION:
case PROP_DYNAMIC_FRICTION:
float dynamic_friction;
file.read(reinterpret_cast<char*>(&dynamic_friction), 4);
object->setDynamicFriction(dynamic_friction);
break;
case Object::PROP_LAYER:
case PROP_LAYER:
char layer;
file.read(&layer, 1);
object->setLayer((int) layer - 127);
@ -93,8 +125,8 @@ void Object::init(std::ifstream& file, Object::Ptr object) {
void Object::save(std::ofstream& file) const {
// écriture de la position de l'objet
float pos_x = getPosition().x / Constants::GRID;
float pos_y = getPosition().y / Constants::GRID;
float pos_x = getPosition().x / Manager::GRID;
float pos_y = getPosition().y / Manager::GRID;
file.write(reinterpret_cast<const char*>(&pos_x), 4);
file.write(reinterpret_cast<const char*>(&pos_y), 4);
@ -103,37 +135,37 @@ void Object::save(std::ofstream& file) const {
char prop_type;
if (mass != DEFAULT_MASS) {
prop_type = Object::PROP_MASS;
prop_type = PROP_MASS;
file.write(&prop_type, 1);
file.write(reinterpret_cast<const char*>(&mass), 4);
}
if (charge != DEFAULT_CHARGE) {
prop_type = Object::PROP_CHARGE;
prop_type = PROP_CHARGE;
file.write(&prop_type, 1);
file.write(reinterpret_cast<const char*>(&charge), 4);
}
if (restitution != DEFAULT_RESTITUTION) {
prop_type = Object::PROP_RESTITUTION;
prop_type = PROP_RESTITUTION;
file.write(&prop_type, 1);
file.write(reinterpret_cast<const char*>(&restitution), 4);
}
if (static_friction != DEFAULT_STATIC_FRICTION) {
prop_type = Object::PROP_STATIC_FRICTION;
prop_type = PROP_STATIC_FRICTION;
file.write(&prop_type, 1);
file.write(reinterpret_cast<const char*>(&static_friction), 4);
}
if (dynamic_friction != DEFAULT_DYNAMIC_FRICTION) {
prop_type = Object::PROP_DYNAMIC_FRICTION;
prop_type = PROP_DYNAMIC_FRICTION;
file.write(&prop_type, 1);
file.write(reinterpret_cast<const char*>(&dynamic_friction), 4);
}
if (layer != DEFAULT_LAYER) {
prop_type = Object::PROP_LAYER;
prop_type = PROP_LAYER;
file.write(&prop_type, 1);
char write_layer = layer + 127;
@ -176,7 +208,7 @@ sf::Vector2f Object::getForces(const Game& game) const {
// normalisation du vecteur direction qui porte
// la force d'attraction, puis application de la norme
attraction /= std::sqrt(distance_squared);
attraction *= Constants::ATTRACTION * (
attraction *= ATTRACTION * (
(getCharge() * attractive->getCharge()) /
distance_squared
);
@ -294,9 +326,8 @@ void Object::solveCollision(Game& game, Object::Ptr obj, const sf::Vector2f& nor
}
void Object::positionalCorrection(Object::Ptr obj, const sf::Vector2f& normal, float depth) {
float position_correction = std::max(depth - Constants::CORRECTION_SLOP, 0.0f) /
(getMassInvert() + obj->getMassInvert()) *
Constants::CORRECTION_PERCENTAGE;
float position_correction = std::max(depth - CORRECTION_SLOP, 0.0f) /
(getMassInvert() + obj->getMassInvert()) * CORRECTION_PERCENTAGE;
setPosition(getPosition() - getMassInvert() * position_correction * normal);
obj->setPosition(obj->getPosition() + obj->getMassInvert() * position_correction * normal);

View File

@ -1,10 +1,6 @@
#include "manager.hpp"
#include "player.hpp"
#include "game.hpp"
#include "game.hpp"
#include "block.hpp"
#include "constants.hpp"
#include <iostream>
#include <array>
const unsigned int Player::TYPE_ID = 1;

View File

@ -2,9 +2,6 @@
#include <iostream>
#include <algorithm>
using dir_iter = boost::filesystem::directory_iterator;
using fs_path = boost::filesystem::path;
ResourceManager::ResourceManager() : preloaded(false),
music_volume(20), playing_state(false), current_music("") {
// initialisation de la musique en bouclage et au volume par défaut
@ -17,8 +14,8 @@ void ResourceManager::preload() {
return;
}
fs_path current = boost::filesystem::current_path();
dir_iter end;
boost::filesystem::path current = boost::filesystem::current_path();
boost::filesystem::directory_iterator end;
// on garde une référence aux chemins des différentes ressources
textures_path = current / "res/textures";
@ -27,7 +24,7 @@ void ResourceManager::preload() {
musics_path = current / "res/musics";
// préchargement de toutes les textures
for (dir_iter it(textures_path); it != end; ++it) {
for (boost::filesystem::directory_iterator it(textures_path); it != end; ++it) {
if (boost::filesystem::is_regular_file(it->path())) {
std::string full_path = boost::filesystem::canonical(it->path()).string();
std::string name = it->path().filename().string();
@ -47,7 +44,7 @@ void ResourceManager::preload() {
}
// préchargement de toutes les polices
for (dir_iter it(fonts_path); it != end; ++it) {
for (boost::filesystem::directory_iterator it(fonts_path); it != end; ++it) {
if (boost::filesystem::is_regular_file(it->path())) {
std::string full_path = boost::filesystem::canonical(it->path()).string();
std::string name = it->path().filename().string();

View File

@ -1,5 +1,5 @@
#include "rules.hpp"
#include <cmath>
#include "manager.hpp"
Rules::Rules(Manager& manager) : State(manager) {
background.setTexture(getResourceManager().getTexture("bg_rules.png"));

View File

@ -1,3 +1,4 @@
#include "manager.hpp"
#include "switch_block.hpp"
#include "game.hpp"

View File

@ -1,12 +1,19 @@
#include "manager.hpp"
#include "widget_button.hpp"
const unsigned int WidgetButton::ARROW_UP = 0;
const unsigned int WidgetButton::ARROW_DOWN = 1;
/**
* Définition des variables et fonctions globales internes
* (accessibles uniquement dans ce fichier)
*/
namespace {
const sf::Color ARROW_COLOR = sf::Color(33, 33, 33);
const sf::Color NORMAL_COLOR = sf::Color(230, 230, 230);
const sf::Color HOVER_COLOR = sf::Color(220, 220, 220);
const sf::Color ACTIVE_COLOR = sf::Color(190, 190, 190);
}
WidgetButton::WidgetButton(
Manager& manager, std::function<void(void)> click_cb,

View File

@ -1,5 +1,5 @@
#include "manager.hpp"
#include "widget_timer.hpp"
#include <cmath>
WidgetTimer::WidgetTimer(Manager& manager, bool can_change, std::function<void(int)> time_left_cb) :
manager(manager), can_change(can_change), time_left_cb(time_left_cb),

View File

@ -1,3 +1,4 @@
#include "manager.hpp"
#include "widget_toolbar.hpp"
#include "block.hpp"
#include "player.hpp"
@ -7,7 +8,14 @@
#include "kill_block.hpp"
#include <utility>
/**
* Définition des variables et fonctions globales internes
* (accessibles uniquement dans ce fichier)
*/
namespace {
const int PADDING = 8;
}
ToolbarCategory::Ptr WidgetToolbar::addCategory(sf::String name) {
auto cat = ToolbarCategory::Ptr(new ToolbarCategory);