skizzle/src/engine.cpp

82 lines
2.2 KiB
C++
Raw Normal View History

2016-03-04 15:29:31 +00:00
#include "engine.hpp"
#include <cmath>
#include <queue>
2016-03-04 15:29:31 +00:00
Engine::Engine() : window(
2016-03-18 17:41:18 +00:00
sf::VideoMode(800, 600), "Projet CMI",
sf::Style::Default & ~sf::Style::Resize,
sf::ContextSettings(0, 0, 2)
) {
2016-03-04 15:29:31 +00:00
window.setVerticalSyncEnabled(true);
}
2016-03-04 15:29:31 +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();
}
// suivi de l'enfoncement et du relâchement des touches
2016-03-04 15:29:31 +00:00
if (event.type == sf::Event::KeyPressed) {
state.keys[event.key.code] = true;
2016-03-04 15:29:31 +00:00
}
if (event.type == sf::Event::KeyReleased) {
state.keys[event.key.code] = false;
2016-03-04 15:29:31 +00:00
}
}
state.delta = clock.restart().asSeconds();
update();
2016-03-04 15:29:31 +00:00
draw();
}
}
void Engine::addObject(Object& object) {
state.objects.push_back(&object);
}
void Engine::update() {
// demande la mise à jour de tous les objets du jeu
for (unsigned int i = 0; i < state.objects.size(); i++) {
state.objects[i]->update(state);
2016-03-04 15:29:31 +00:00
}
// gère les collisions entre les objets
for (unsigned int i = 0; i < state.objects.size(); i++) {
Object* objA = state.objects[i];
for (unsigned int j = i + 1; j < state.objects.size(); j++) {
Object* objB = state.objects[j];
objA->collide(*objB);
}
}
2016-03-04 15:29:31 +00:00
}
void Engine::draw() {
// 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));
// chargement de la file d'affichage des objets
std::priority_queue<Object*, std::vector<Object*>, ObjectCompare> display_queue;
for (unsigned int i = 0; i < state.objects.size(); i++) {
display_queue.push(state.objects[i]);
2016-03-04 15:29:31 +00:00
}
// dessin des objets de la file d'affichage couche par couche
while (!display_queue.empty()) {
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
}