2020-07-03 03:48:58 +00:00
|
|
|
//
|
|
|
|
// Created by SeanOMik on 7/2/2020.
|
|
|
|
// Github: https://github.com/SeanOMik
|
|
|
|
// Email: seanomik@gmail.com
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "game.h"
|
2021-03-02 04:08:10 +00:00
|
|
|
#include "entity.h"
|
2020-07-03 03:48:58 +00:00
|
|
|
#include "event.h"
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
simpleengine::Game::Game(int w, int h, const std::string& window_name) {
|
|
|
|
// Create a render window
|
|
|
|
window = new sf::RenderWindow(sf::VideoMode(w, h), window_name);
|
|
|
|
}
|
|
|
|
|
2021-03-02 04:08:10 +00:00
|
|
|
simpleengine::Game::Game(const sf::Vector2u& window_size, const std::string& window_name) : simpleengine::Game(window_size.x, window_size.y, window_name) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-07-03 03:48:58 +00:00
|
|
|
simpleengine::Game::~Game() {
|
|
|
|
delete window;
|
|
|
|
|
2021-03-02 04:08:10 +00:00
|
|
|
/* while(!events.empty()) {
|
2020-07-03 03:48:58 +00:00
|
|
|
delete events.top();
|
|
|
|
events.pop();
|
2021-03-02 04:08:10 +00:00
|
|
|
} */
|
|
|
|
|
|
|
|
std::vector<Event*>::iterator it = events.begin();
|
|
|
|
while (it != events.end()) {
|
|
|
|
delete (*it);
|
|
|
|
it = events.erase(it);
|
2020-07-03 03:48:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void simpleengine::Game::UpdateSFMLEvents() {
|
|
|
|
sf::Event event;
|
|
|
|
while (window->pollEvent(event)) {
|
|
|
|
switch (event.type) {
|
|
|
|
case sf::Event::Closed:
|
|
|
|
window->close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void simpleengine::Game::Update() {
|
|
|
|
delta_time = delta_time_clock.restart().asSeconds(); // Update delta time
|
|
|
|
UpdateSFMLEvents();
|
|
|
|
|
2021-03-02 04:08:10 +00:00
|
|
|
for (std::vector<Event*>::iterator it = events.begin(); it != events.end(); ) {
|
|
|
|
(*it)->Update(delta_time);
|
2020-07-03 03:48:58 +00:00
|
|
|
|
2021-03-12 23:08:20 +00:00
|
|
|
if ((*it)->IsQuitting()) {
|
2021-03-02 04:08:10 +00:00
|
|
|
delete (*it);
|
|
|
|
it = events.erase(it);
|
|
|
|
} else {
|
|
|
|
++it;
|
2020-07-03 03:48:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void simpleengine::Game::RenderWindow() {
|
|
|
|
window->clear();
|
|
|
|
RenderItems();
|
|
|
|
window->display();
|
|
|
|
}
|
|
|
|
|
|
|
|
void simpleengine::Game::RenderItems() {
|
2021-03-02 04:08:10 +00:00
|
|
|
for (std::vector<Event*>::iterator it = events.begin(); it != events.end(); it++) {
|
|
|
|
(*it)->Render(window);
|
2020-07-03 03:48:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int simpleengine::Game::Run() {
|
|
|
|
sf::CircleShape shape(100);
|
|
|
|
shape.setFillColor(sf::Color::Green);
|
|
|
|
|
|
|
|
while (window->isOpen()) {
|
|
|
|
Update();
|
|
|
|
RenderWindow();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void simpleengine::Game::AddEvent(simpleengine::Event *event) {
|
2021-03-02 04:08:10 +00:00
|
|
|
events.emplace_back(event);
|
2020-07-03 03:48:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sf::RenderWindow* simpleengine::Game::GetWindow() {
|
|
|
|
return window;
|
|
|
|
}
|
|
|
|
|
|
|
|
void simpleengine::Game::ExitGame() {
|
|
|
|
window->close();
|
|
|
|
}
|