From 7cb2fd2e24f6310164aa93f2fa4800bfe8a4fb60 Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Fri, 28 Apr 2023 22:59:24 +0200 Subject: [PATCH] Removed unnecessary Renderer abstraction --- CMakeLists.txt | 5 ++--- src/game/game.cpp | 46 +++++++++++++++++++++++++-------------- src/game/game.h | 5 ++--- src/game/game_factory.cpp | 32 +++++++++++++++++++++++++++ src/game/game_factory.hpp | 17 +++++++++++++++ src/game/renderer.cpp | 45 -------------------------------------- src/game/renderer.h | 27 ----------------------- src/main.cpp | 10 +++++---- 8 files changed, 89 insertions(+), 98 deletions(-) create mode 100644 src/game/game_factory.cpp create mode 100644 src/game/game_factory.hpp delete mode 100644 src/game/renderer.cpp delete mode 100644 src/game/renderer.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 4051362..ce314ec 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,8 +15,6 @@ include_directories(${EIGEN3_INCLUDE_DIR}) # Set up your project's source files set(SOURCES src/main.cpp - src/game/renderer.h - src/game/renderer.cpp src/game/game_object.cpp src/game/game_object.h src/game/game.cpp @@ -32,7 +30,8 @@ set(SOURCES src/game/input_handler.h src/primitives/circle_object.cpp src/primitives/circle_object.h - ) + src/game/game_factory.cpp + src/game/game_factory.hpp) # Add an executable target add_executable(Holesome ${SOURCES}) diff --git a/src/game/game.cpp b/src/game/game.cpp index 3d5d9ad..801be24 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -4,56 +4,70 @@ #include #include +#include #include "game.h" -Game::Game(std::shared_ptr renderer) : renderer(std::move(renderer)), isRunning(false), gameObjects() { +Game::Game(std::shared_ptr window) : window(std::move(window)), isRunning(false), gameObjects() +{ } -void Game::run() { - if (isRunning) { +void Game::run() +{ + if (isRunning) + { LOG(WARNING) << "Game is already running"; return; } isRunning = true; - while (isRunning) { + while (isRunning) + { renderFrame(); // Process any events that have occurred since the last iteration sf::Event event{}; - while (renderer->pollEvent(event)) { + while (window->pollEvent(event)) + { // If the event is to close the window, then close it - if (event.type == sf::Event::Closed) { + if (event.type == sf::Event::Closed) + { exit(); } - if (event.key.code == sf::Keyboard::Escape || event.key.code == sf::Keyboard::Q) { + if (event.key.code == sf::Keyboard::Escape || event.key.code == sf::Keyboard::Q) + { exit(); } } } } -void Game::exit() { +void Game::exit() +{ isRunning = false; - renderer->close(); + window->close(); } -void Game::renderFrame() { - for (auto &gameObject: gameObjects) { - renderer->draw(gameObject); +void Game::renderFrame() +{ + for (auto &gameObject: gameObjects) + { + gameObject->draw(*window, sf::RenderStates::Default); } - renderer->display(); + window->display(); } -Game::~Game() { - for (auto &gameObject: gameObjects) { +Game::~Game() +{ + for (auto &gameObject: gameObjects) + { delete gameObject; } } -void Game::addGameObject(GameObject *gameObject) { +void Game::addGameObject(GameObject *gameObject) +{ gameObjects.push_back(gameObject); } diff --git a/src/game/game.h b/src/game/game.h index d2bcec4..e47f5b6 100644 --- a/src/game/game.h +++ b/src/game/game.h @@ -7,18 +7,17 @@ #include -#include "renderer.h" #include "../util/easylogging++.h" #include "game_object.h" class Game { private: - std::shared_ptr renderer; + std::shared_ptr window; std::vector gameObjects; bool isRunning = false; public: - explicit Game(std::shared_ptr renderer); + explicit Game(std::shared_ptr window); ~Game(); void run(); diff --git a/src/game/game_factory.cpp b/src/game/game_factory.cpp new file mode 100644 index 0000000..5b95535 --- /dev/null +++ b/src/game/game_factory.cpp @@ -0,0 +1,32 @@ +// +// Created by max on 28.04.23. +// + +#include +#include "game_factory.hpp" + + +std::shared_ptr GameFactory::createWindowed(const std::string &title, int width, int height) +{ + auto window = std::make_shared(sf::VideoMode(width, height), title); + return std::make_shared(window); +} + +std::shared_ptr GameFactory::createFullscreen(const std::string &title) +{ + sf::VideoMode fullScreenMode; + + auto availableModes = sf::VideoMode::getFullscreenModes(); + if (availableModes.empty()) + { + LOG(INFO) << "No fullscreen modes available, falling back to Desktop Mode."; + fullScreenMode = sf::VideoMode::getDesktopMode(); + } else + { + fullScreenMode = availableModes[0]; + fullScreenMode.bitsPerPixel = sf::VideoMode::getDesktopMode().bitsPerPixel; + } + + auto window = std::make_shared(fullScreenMode, title, sf::Style::Fullscreen); + return std::make_shared(window); +} \ No newline at end of file diff --git a/src/game/game_factory.hpp b/src/game/game_factory.hpp new file mode 100644 index 0000000..a4ee58e --- /dev/null +++ b/src/game/game_factory.hpp @@ -0,0 +1,17 @@ +#ifndef HOLESOME_GAME_FACTORY_HPP +#define HOLESOME_GAME_FACTORY_HPP + + +#include +#include +#include "game.h" + +class GameFactory +{ +public: + static std::shared_ptr createWindowed(const std::string &title, int width, int height); + static std::shared_ptr createFullscreen(const std::string &title); + +}; + +#endif //HOLESOME_GAME_FACTORY_HPP diff --git a/src/game/renderer.cpp b/src/game/renderer.cpp deleted file mode 100644 index 82c1f48..0000000 --- a/src/game/renderer.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include "renderer.h" - -void Renderer::draw(sf::Drawable *drawable) { - window->draw(*drawable); -} - -void Renderer::close() { - window->close(); -} - -Renderer::~Renderer() { - delete window; -} - -std::shared_ptr Renderer::createWindow(const std::string &title, int width, int height) { - auto renderer = std::make_shared(); - renderer->window = new sf::RenderWindow(sf::VideoMode(width, height), title); - return renderer; -} - -std::shared_ptr Renderer::createFullscreen(const std::string &title) { - sf::VideoMode fullScreenMode; - - auto availableModes = sf::VideoMode::getFullscreenModes(); - if (availableModes.empty()) { - LOG(INFO) << "No fullscreen modes available, falling back to Desktop Mode."; - fullScreenMode = sf::VideoMode::getDesktopMode(); - } else { - fullScreenMode = availableModes[0]; - fullScreenMode.bitsPerPixel = sf::VideoMode::getDesktopMode().bitsPerPixel; - } - - auto renderer = std::make_shared(); - renderer->window = new sf::RenderWindow(fullScreenMode, title, sf::Style::Fullscreen); - return renderer; -} - -void Renderer::display() { - window->display(); - window->clear(sf::Color::Black); -} - -bool Renderer::pollEvent(sf::Event &event) { - return window->pollEvent(event); -} diff --git a/src/game/renderer.h b/src/game/renderer.h deleted file mode 100644 index 49e8c71..0000000 --- a/src/game/renderer.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef HOLESOME_RENDERER_H -#define HOLESOME_RENDERER_H - -#include -#include "../util/easylogging++.h" - -class Renderer { -public: - static std::shared_ptr createWindow(const std::string &title, int width = 960, int height = 540); - - static std::shared_ptr createFullscreen(const std::string &title); - - ~Renderer(); - - bool pollEvent(sf::Event &event); - - void draw(sf::Drawable *drawable); - void display(); - - void close(); - -private: - sf::RenderWindow *window; -}; - - -#endif //HOLESOME_RENDERER_H diff --git a/src/main.cpp b/src/main.cpp index 7c00bc2..c639e96 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,15 +3,17 @@ #include "util/easylogging++.h" #include "game/game.h" #include "primitives/circle_object.h" +#include "game/game_factory.hpp" INITIALIZE_EASYLOGGINGPP -int main(int argc, char *argv[]) { +int main(int argc, char *argv[]) +{ START_EASYLOGGINGPP(argc, argv); - auto game = Game(Renderer::createFullscreen("Holesome")); + auto game = GameFactory::createFullscreen("Holesome"); - game.addGameObject(new CircleObject(50, sf::Color::Red)); + game->addGameObject(new CircleObject(50, sf::Color::Red)); - game.run(); + game->run(); }