SimpleEngine/src/game.cpp

161 lines
5.0 KiB
C++
Raw Normal View History

//
// Created by SeanOMik on 7/2/2020.
// Github: https://github.com/SeanOMik
// Email: seanomik@gmail.com
//
#include "game.h"
#include "entity.h"
#include "event.h"
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);
}
simpleengine::Game::Game(const sf::Vector2u& window_size, const std::string& window_name) : simpleengine::Game(window_size.x, window_size.y, window_name) {
}
simpleengine::Game::~Game() {
delete window;
std::vector<Event*>::iterator it = events.begin();
while (it != events.end()) {
delete (*it);
it = events.erase(it);
}
}
void simpleengine::Game::UpdateSFMLEvents() {
sf::Event event;
while (window->pollEvent(event)) {
switch (event.type) {
case sf::Event::Closed:
2021-03-13 03:01:33 +00:00
close_event.Trigger();
window->close();
2021-03-12 23:33:48 +00:00
break;
case sf::Event::Resized:
2021-03-13 03:01:33 +00:00
resized_event.Trigger(event.size);
break;
2021-03-12 23:33:48 +00:00
case sf::Event::LostFocus:
2021-03-13 03:01:33 +00:00
lost_focus_event.Trigger();
break;
2021-03-12 23:33:48 +00:00
case sf::Event::GainedFocus:
2021-03-13 03:01:33 +00:00
gained_focus_event.Trigger();
break;
2021-03-12 23:33:48 +00:00
case sf::Event::TextEntered:
2021-03-13 03:01:33 +00:00
text_entered_event.Trigger(event.text);
break;
2021-03-12 23:33:48 +00:00
case sf::Event::KeyPressed:
2021-03-13 03:01:33 +00:00
key_pressed_event.Trigger(event.key);
break;
2021-03-12 23:33:48 +00:00
case sf::Event::KeyReleased:
2021-03-13 03:01:33 +00:00
key_released_event.Trigger(event.key);
break;
2021-03-12 23:33:48 +00:00
case sf::Event::MouseWheelMoved:
2021-03-13 03:01:33 +00:00
mouse_wheel_moved_event.Trigger(event.mouseWheel);
break;
2021-03-12 23:33:48 +00:00
case sf::Event::MouseWheelScrolled:
2021-03-13 03:01:33 +00:00
mouse_wheel_scrolled_event.Trigger(event.mouseWheelScroll);
break;
2021-03-12 23:33:48 +00:00
case sf::Event::MouseButtonPressed:
2021-03-13 03:01:33 +00:00
mouse_button_pressed_event.Trigger(event.mouseButton);
break;
2021-03-12 23:33:48 +00:00
case sf::Event::MouseButtonReleased:
2021-03-13 03:01:33 +00:00
mouse_button_released_event.Trigger(event.mouseButton);
break;
2021-03-12 23:33:48 +00:00
case sf::Event::MouseMoved:
2021-03-13 03:01:33 +00:00
mouse_move_event.Trigger(event.mouseMove);
break;
2021-03-12 23:33:48 +00:00
case sf::Event::MouseEntered:
2021-03-13 03:01:33 +00:00
mouse_entered_event.Trigger();
break;
2021-03-12 23:33:48 +00:00
case sf::Event::MouseLeft:
2021-03-13 03:01:33 +00:00
mouse_left_event.Trigger();
break;
2021-03-12 23:33:48 +00:00
case sf::Event::JoystickButtonPressed:
2021-03-13 03:01:33 +00:00
joy_btn_pressed_event.Trigger(event.joystickButton);
break;
2021-03-12 23:33:48 +00:00
case sf::Event::JoystickButtonReleased:
2021-03-13 03:01:33 +00:00
joy_btn_released_event.Trigger(event.joystickButton);
break;
2021-03-12 23:33:48 +00:00
case sf::Event::JoystickMoved:
2021-03-13 03:01:33 +00:00
joy_moved_event.Trigger(event.joystickMove);
break;
2021-03-12 23:33:48 +00:00
case sf::Event::JoystickConnected:
2021-03-13 03:01:33 +00:00
joy_connected_event.Trigger(event.joystickConnect);
break;
2021-03-12 23:33:48 +00:00
case sf::Event::JoystickDisconnected:
2021-03-13 03:01:33 +00:00
joy_disconnected_event.Trigger(event.joystickConnect);
break;
2021-03-12 23:33:48 +00:00
case sf::Event::TouchBegan:
2021-03-13 03:01:33 +00:00
touch_began_event.Trigger(event.touch);
break;
2021-03-12 23:33:48 +00:00
case sf::Event::TouchMoved:
2021-03-13 03:01:33 +00:00
touch_moved_event.Trigger(event.touch);
break;
2021-03-12 23:33:48 +00:00
case sf::Event::TouchEnded:
2021-03-13 03:01:33 +00:00
touch_ended_event.Trigger(event.touch);
break;
2021-03-12 23:33:48 +00:00
case sf::Event::SensorChanged:
2021-03-13 03:01:33 +00:00
sensor_event.Trigger(event.sensor);
break;
2021-03-12 23:33:48 +00:00
case sf::Event::Count:
break;
}
}
}
void simpleengine::Game::Update() {
delta_time = delta_time_clock.restart().asSeconds(); // Update delta time
UpdateSFMLEvents();
for (std::vector<Event*>::iterator it = events.begin(); it != events.end(); ) {
(*it)->Update(delta_time);
if ((*it)->IsDestroying()) {
delete (*it);
it = events.erase(it);
} else {
++it;
}
}
}
void simpleengine::Game::RenderWindow() {
window->clear();
RenderItems();
window->display();
}
void simpleengine::Game::RenderItems() {
for (std::vector<Event*>::iterator it = events.begin(); it != events.end(); it++) {
(*it)->Render(window);
}
}
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) {
events.emplace_back(event);
}
sf::RenderWindow* simpleengine::Game::GetWindow() {
return window;
}
void simpleengine::Game::ExitGame() {
window->close();
}