Réduction de l'utilisation de la mémoire dans les fonctions critiques
This commit is contained in:
		
							parent
							
								
									bd9aa54275
								
							
						
					
					
						commit
						4cc2bfa197
					
				| 
						 | 
				
			
			@ -3,7 +3,7 @@
 | 
			
		|||
 | 
			
		||||
#include <SFML/Graphics.hpp>
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include "state.hpp"
 | 
			
		||||
#include "engine_state.hpp"
 | 
			
		||||
#include "physics_object.hpp"
 | 
			
		||||
 | 
			
		||||
class Ball : public PhysicsObject {
 | 
			
		||||
| 
						 | 
				
			
			@ -14,7 +14,7 @@ protected:
 | 
			
		|||
    /**
 | 
			
		||||
     * Calcule les forces appliquées à l'objet
 | 
			
		||||
     */
 | 
			
		||||
    virtual sf::Vector2f getForces(State state);
 | 
			
		||||
    virtual sf::Vector2f getForces(EngineState& state);
 | 
			
		||||
 | 
			
		||||
    static constexpr float ATTRACTION = 25000;
 | 
			
		||||
    static constexpr float MOVE = 100;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -4,7 +4,7 @@
 | 
			
		|||
#include <SFML/Graphics.hpp>
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include "object.hpp"
 | 
			
		||||
#include "state.hpp"
 | 
			
		||||
#include "engine_state.hpp"
 | 
			
		||||
 | 
			
		||||
class Block : public Object {
 | 
			
		||||
protected:
 | 
			
		||||
| 
						 | 
				
			
			@ -27,7 +27,7 @@ public:
 | 
			
		|||
     * Met à jour l'objet juste avant le dessin d'une frame
 | 
			
		||||
     * Reçoit l'état actuel du moteur
 | 
			
		||||
     */
 | 
			
		||||
    void update(State state);
 | 
			
		||||
    void update(EngineState& state);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Détermine la couche d'affichage de l'objet
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,9 +1,9 @@
 | 
			
		|||
#ifndef __PTF_ENGINE_HPP__
 | 
			
		||||
#define __PTF_ENGINE_HPP__
 | 
			
		||||
 | 
			
		||||
#include <vector>
 | 
			
		||||
#include <SFML/Graphics.hpp>
 | 
			
		||||
#include "object.hpp"
 | 
			
		||||
#include "engine_state.hpp"
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * La classe principale Engine coordonne les éléments
 | 
			
		||||
| 
						 | 
				
			
			@ -13,11 +13,7 @@ class Engine {
 | 
			
		|||
private:
 | 
			
		||||
    sf::Clock clock;
 | 
			
		||||
    sf::RenderWindow window;
 | 
			
		||||
 | 
			
		||||
    bool goLeftKey;
 | 
			
		||||
    bool goRightKey;
 | 
			
		||||
 | 
			
		||||
    std::vector<Object*> objects;
 | 
			
		||||
    EngineState state;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Met à jour les objets du jeu pour
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,7 +1,8 @@
 | 
			
		|||
#ifndef __PTF_STATE_HPP__
 | 
			
		||||
#define __PTF_STATE_HPP__
 | 
			
		||||
#ifndef __PTF_ENGINE_STATE_HPP__
 | 
			
		||||
#define __PTF_ENGINE_STATE_HPP__
 | 
			
		||||
 | 
			
		||||
#include <vector>
 | 
			
		||||
#include <array>
 | 
			
		||||
 | 
			
		||||
// pré-déclaration de Object pour éviter les erreurs de compilation
 | 
			
		||||
// Object est définie dans object.hpp
 | 
			
		||||
| 
						 | 
				
			
			@ -12,11 +13,15 @@ class Object;
 | 
			
		|||
 * Cette structure est passée aux objets pour qu'ils se
 | 
			
		||||
 * mettent à jour en fonction de cet état
 | 
			
		||||
 */
 | 
			
		||||
struct State {
 | 
			
		||||
struct EngineState {
 | 
			
		||||
    std::vector<Object*> objects;
 | 
			
		||||
    bool goLeftKey;
 | 
			
		||||
    bool goRightKey;
 | 
			
		||||
    std::array<bool, sf::Keyboard::KeyCount> keys;
 | 
			
		||||
    float delta;
 | 
			
		||||
 | 
			
		||||
    EngineState() {
 | 
			
		||||
        // aucune touche n'est enfoncée au démarrage
 | 
			
		||||
        keys.fill(false);
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
| 
						 | 
				
			
			@ -2,7 +2,7 @@
 | 
			
		|||
#define __PTF_OBJECT_HPP__
 | 
			
		||||
 | 
			
		||||
#include <SFML/Graphics.hpp>
 | 
			
		||||
#include "state.hpp"
 | 
			
		||||
#include "engine_state.hpp"
 | 
			
		||||
 | 
			
		||||
class Object {
 | 
			
		||||
protected:
 | 
			
		||||
| 
						 | 
				
			
			@ -22,7 +22,7 @@ public:
 | 
			
		|||
     * Met à jour l'objet juste avant le dessin d'une frame
 | 
			
		||||
     * Reçoit l'état actuel du moteur
 | 
			
		||||
     */
 | 
			
		||||
    virtual void update(State state) = 0;
 | 
			
		||||
    virtual void update(EngineState& state) = 0;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Détermine la couche d'affichage de l'objet
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -4,7 +4,7 @@
 | 
			
		|||
#include <SFML/Graphics.hpp>
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include "object.hpp"
 | 
			
		||||
#include "state.hpp"
 | 
			
		||||
#include "engine_state.hpp"
 | 
			
		||||
 | 
			
		||||
class PhysicsObject : public Object {
 | 
			
		||||
protected:
 | 
			
		||||
| 
						 | 
				
			
			@ -17,7 +17,7 @@ protected:
 | 
			
		|||
    /**
 | 
			
		||||
     * Calcule les forces appliquées à l'objet
 | 
			
		||||
     */
 | 
			
		||||
    virtual sf::Vector2f getForces(State state);
 | 
			
		||||
    virtual sf::Vector2f getForces(EngineState& state);
 | 
			
		||||
 | 
			
		||||
    static constexpr float GRAVITY = 20;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -35,7 +35,7 @@ public:
 | 
			
		|||
     * Met à jour la physique de l'objet juste avant le dessin d'une frame
 | 
			
		||||
     * Reçoit l'état actuel du moteur
 | 
			
		||||
     */
 | 
			
		||||
    void update(State state);
 | 
			
		||||
    void update(EngineState& state);
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Récupère la vitesse de l'objet
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -14,15 +14,15 @@ void Ball::draw(sf::RenderWindow& window) {
 | 
			
		|||
    window.draw(shape);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
sf::Vector2f Ball::getForces(State state) {
 | 
			
		||||
sf::Vector2f Ball::getForces(EngineState& state) {
 | 
			
		||||
    sf::Vector2f forces = PhysicsObject::getForces(state);
 | 
			
		||||
 | 
			
		||||
    // déplacement de la balle après appui sur les touches de direction
 | 
			
		||||
    if (state.goLeftKey) {
 | 
			
		||||
    if (state.keys[sf::Keyboard::Left]) {
 | 
			
		||||
        forces += sf::Vector2f(-Ball::MOVE, 0);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (state.goRightKey) {
 | 
			
		||||
    if (state.keys[sf::Keyboard::Right]) {
 | 
			
		||||
        forces += sf::Vector2f(Ball::MOVE, 0);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -20,6 +20,6 @@ void Block::draw(sf::RenderWindow& window) {
 | 
			
		|||
    window.draw(shape);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Block::update(State state) {
 | 
			
		||||
void Block::update(EngineState& state) {
 | 
			
		||||
    // rien à mettre à jour
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,5 +1,4 @@
 | 
			
		|||
#include "engine.hpp"
 | 
			
		||||
#include "state.hpp"
 | 
			
		||||
#include <cmath>
 | 
			
		||||
#include <queue>
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -7,7 +6,7 @@ Engine::Engine() : window(
 | 
			
		|||
    sf::VideoMode(400, 300), "Projet CMI",
 | 
			
		||||
    sf::Style::Default & ~sf::Style::Resize,
 | 
			
		||||
    sf::ContextSettings(0, 0, 2)
 | 
			
		||||
), goLeftKey(false), goRightKey(false) {
 | 
			
		||||
) {
 | 
			
		||||
    window.setVerticalSyncEnabled(true);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -23,28 +22,17 @@ void Engine::start() {
 | 
			
		|||
                window.close();
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            // une touche a été enfoncée
 | 
			
		||||
            // suivi de l'enfoncement et du relâchement des touches
 | 
			
		||||
            if (event.type == sf::Event::KeyPressed) {
 | 
			
		||||
                if (event.key.code == sf::Keyboard::Left) {
 | 
			
		||||
                    goLeftKey = true;
 | 
			
		||||
                state.keys[event.key.code] = true;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
                if (event.key.code == sf::Keyboard::Right) {
 | 
			
		||||
                    goRightKey = true;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            // une touche a été relâchée
 | 
			
		||||
            if (event.type == sf::Event::KeyReleased) {
 | 
			
		||||
                if (event.key.code == sf::Keyboard::Left) {
 | 
			
		||||
                    goLeftKey = false;
 | 
			
		||||
                state.keys[event.key.code] = false;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
                if (event.key.code == sf::Keyboard::Right) {
 | 
			
		||||
                    goRightKey = false;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        state.delta = clock.restart().asSeconds();
 | 
			
		||||
 | 
			
		||||
        update();
 | 
			
		||||
        draw();
 | 
			
		||||
| 
						 | 
				
			
			@ -52,21 +40,13 @@ void Engine::start() {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
void Engine::addObject(Object& object) {
 | 
			
		||||
    objects.push_back(&object);
 | 
			
		||||
    state.objects.push_back(&object);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Engine::update() {
 | 
			
		||||
    // calcul du temps écoulé depuis la dernière frame
 | 
			
		||||
    // et création de l'objet state stockant l'état du moteur
 | 
			
		||||
    State state;
 | 
			
		||||
    state.delta = clock.restart().asSeconds();
 | 
			
		||||
    state.goLeftKey = goLeftKey;
 | 
			
		||||
    state.goRightKey = goRightKey;
 | 
			
		||||
    state.objects = objects;
 | 
			
		||||
 | 
			
		||||
    // demande la mise à jour de tous les objets du jeu
 | 
			
		||||
    for (unsigned int i = 0; i < objects.size(); i++) {
 | 
			
		||||
        objects[i]->update(state);
 | 
			
		||||
    for (unsigned int i = 0; i < state.objects.size(); i++) {
 | 
			
		||||
        state.objects[i]->update(state);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -77,8 +57,8 @@ void Engine::draw() {
 | 
			
		|||
    // chargement de la file d'affichage des objets
 | 
			
		||||
    std::priority_queue<Object*, std::vector<Object*>, ObjectCompare> display_queue;
 | 
			
		||||
 | 
			
		||||
    for (unsigned int i = 0; i < objects.size(); i++) {
 | 
			
		||||
        display_queue.push(objects[i]);
 | 
			
		||||
    for (unsigned int i = 0; i < state.objects.size(); i++) {
 | 
			
		||||
        display_queue.push(state.objects[i]);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // dessin des objets de la file d'affichage couche par couche
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -15,7 +15,7 @@ void PhysicsObject::draw(sf::RenderWindow& window) {
 | 
			
		|||
    window.draw(accelLine);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void PhysicsObject::update(State state) {
 | 
			
		||||
void PhysicsObject::update(EngineState& state) {
 | 
			
		||||
    // intégration de la vitesse dans la position
 | 
			
		||||
    position += velocity * state.delta;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -24,6 +24,15 @@ void PhysicsObject::update(State state) {
 | 
			
		|||
    velocity += acceleration * state.delta;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
sf::Vector2f PhysicsObject::getForces(EngineState& state) {
 | 
			
		||||
    sf::Vector2f forces(0, 0);
 | 
			
		||||
 | 
			
		||||
    // force de gravité
 | 
			
		||||
    forces += sf::Vector2f(0, PhysicsObject::GRAVITY);
 | 
			
		||||
 | 
			
		||||
    return forces;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
sf::Vector2f PhysicsObject::getVelocity() {
 | 
			
		||||
    return velocity;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -39,14 +48,3 @@ int PhysicsObject::getMass() {
 | 
			
		|||
void PhysicsObject::setMass(int set_mass) {
 | 
			
		||||
    mass = set_mass;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
sf::Vector2f PhysicsObject::getForces(State state) {
 | 
			
		||||
    sf::Vector2f forces(0, 0);
 | 
			
		||||
 | 
			
		||||
    // force de gravité
 | 
			
		||||
    forces += sf::Vector2f(0, PhysicsObject::GRAVITY);
 | 
			
		||||
 | 
			
		||||
    // TODO: collisions entre objets
 | 
			
		||||
 | 
			
		||||
    return forces;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue