2016-03-04 15:29:31 +00:00
|
|
|
#include "engine.hpp"
|
2016-03-09 18:21:38 +00:00
|
|
|
#include "state.hpp"
|
2016-03-04 15:29:31 +00:00
|
|
|
#include <cmath>
|
2016-03-08 18:52:55 +00:00
|
|
|
#include <queue>
|
2016-03-04 15:29:31 +00:00
|
|
|
|
2016-03-10 09:40:50 +00:00
|
|
|
Engine::Engine() : window(
|
2016-03-11 15:11:30 +00:00
|
|
|
sf::VideoMode(400, 300), "Projet CMI",
|
2016-03-10 09:40:50 +00:00
|
|
|
sf::Style::Default & ~sf::Style::Resize,
|
|
|
|
sf::ContextSettings(0, 0, 2)
|
|
|
|
), goLeftKey(false), goRightKey(false) {
|
2016-03-04 15:29:31 +00:00
|
|
|
window.setVerticalSyncEnabled(true);
|
2016-03-09 18:33:43 +00:00
|
|
|
}
|
2016-03-04 15:29:31 +00:00
|
|
|
|
2016-03-09 18:33:43 +00:00
|
|
|
void Engine::start() {
|
2016-03-04 15:29:31 +00:00
|
|
|
// boucle d'événements sur la fenêtre
|
|
|
|
while (window.isOpen()) {
|
|
|
|
sf::Event event;
|
|
|
|
|
|
|
|
// traitement des évènements reçus
|
|
|
|
while (window.pollEvent(event)) {
|
|
|
|
// fermeture de la fenêtre
|
|
|
|
if (event.type == sf::Event::Closed) {
|
|
|
|
window.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
// une touche a été enfoncée
|
|
|
|
if (event.type == sf::Event::KeyPressed) {
|
|
|
|
if (event.key.code == sf::Keyboard::Left) {
|
|
|
|
goLeftKey = 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.key.code == sf::Keyboard::Right) {
|
|
|
|
goRightKey = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-09 18:21:38 +00:00
|
|
|
update();
|
2016-03-04 15:29:31 +00:00
|
|
|
draw();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-12 18:45:16 +00:00
|
|
|
void Engine::addObject(Object& object) {
|
|
|
|
objects.push_back(&object);
|
2016-03-09 18:35:40 +00:00
|
|
|
}
|
|
|
|
|
2016-03-09 18:21:38 +00:00
|
|
|
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;
|
|
|
|
|
2016-03-08 18:52:55 +00:00
|
|
|
// demande la mise à jour de tous les objets du jeu
|
2016-03-09 18:42:09 +00:00
|
|
|
for (unsigned int i = 0; i < objects.size(); i++) {
|
2016-03-08 21:50:42 +00:00
|
|
|
objects[i]->update(state);
|
2016-03-04 15:29:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Engine::draw() {
|
2016-03-08 18:52:55 +00:00
|
|
|
// efface la scène précédente et dessine la couche de fond
|
2016-03-04 15:29:31 +00:00
|
|
|
window.clear(sf::Color(66, 165, 245));
|
|
|
|
|
2016-03-08 18:52:55 +00:00
|
|
|
// chargement de la file d'affichage des objets
|
2016-03-10 21:47:53 +00:00
|
|
|
std::priority_queue<Object*, std::vector<Object*>, ObjectCompare> display_queue;
|
2016-03-08 18:52:55 +00:00
|
|
|
|
2016-03-09 18:42:09 +00:00
|
|
|
for (unsigned int i = 0; i < objects.size(); i++) {
|
2016-03-08 18:52:55 +00:00
|
|
|
display_queue.push(objects[i]);
|
2016-03-04 15:29:31 +00:00
|
|
|
}
|
|
|
|
|
2016-03-08 18:52:55 +00:00
|
|
|
// dessin des objets de la file d'affichage couche par couche
|
|
|
|
while (!display_queue.empty()) {
|
2016-03-08 21:50:42 +00:00
|
|
|
display_queue.top()->draw(window);
|
|
|
|
display_queue.pop();
|
2016-03-04 15:29:31 +00:00
|
|
|
}
|
2016-03-10 18:18:50 +00:00
|
|
|
|
|
|
|
window.display();
|
2016-03-04 15:29:31 +00:00
|
|
|
}
|