Removed unnecessary Renderer abstraction

This commit is contained in:
Maximilian Giller 2023-04-28 22:59:24 +02:00
parent e19972ae7c
commit 7cb2fd2e24
8 changed files with 89 additions and 98 deletions

View file

@ -15,8 +15,6 @@ include_directories(${EIGEN3_INCLUDE_DIR})
# Set up your project's source files # Set up your project's source files
set(SOURCES set(SOURCES
src/main.cpp src/main.cpp
src/game/renderer.h
src/game/renderer.cpp
src/game/game_object.cpp src/game/game_object.cpp
src/game/game_object.h src/game/game_object.h
src/game/game.cpp src/game/game.cpp
@ -32,7 +30,8 @@ set(SOURCES
src/game/input_handler.h src/game/input_handler.h
src/primitives/circle_object.cpp src/primitives/circle_object.cpp
src/primitives/circle_object.h src/primitives/circle_object.h
) src/game/game_factory.cpp
src/game/game_factory.hpp)
# Add an executable target # Add an executable target
add_executable(Holesome ${SOURCES}) add_executable(Holesome ${SOURCES})

View file

@ -4,56 +4,70 @@
#include <utility> #include <utility>
#include <SFML/Window/Event.hpp> #include <SFML/Window/Event.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include "game.h" #include "game.h"
Game::Game(std::shared_ptr<Renderer> renderer) : renderer(std::move(renderer)), isRunning(false), gameObjects() { Game::Game(std::shared_ptr<sf::RenderWindow> window) : window(std::move(window)), isRunning(false), gameObjects()
{
} }
void Game::run() { void Game::run()
if (isRunning) { {
if (isRunning)
{
LOG(WARNING) << "Game is already running"; LOG(WARNING) << "Game is already running";
return; return;
} }
isRunning = true; isRunning = true;
while (isRunning) { while (isRunning)
{
renderFrame(); renderFrame();
// Process any events that have occurred since the last iteration // Process any events that have occurred since the last iteration
sf::Event event{}; sf::Event event{};
while (renderer->pollEvent(event)) { while (window->pollEvent(event))
{
// If the event is to close the window, then close it // If the event is to close the window, then close it
if (event.type == sf::Event::Closed) { if (event.type == sf::Event::Closed)
{
exit(); 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(); exit();
} }
} }
} }
} }
void Game::exit() { void Game::exit()
{
isRunning = false; isRunning = false;
renderer->close(); window->close();
} }
void Game::renderFrame() { void Game::renderFrame()
for (auto &gameObject: gameObjects) { {
renderer->draw(gameObject); for (auto &gameObject: gameObjects)
{
gameObject->draw(*window, sf::RenderStates::Default);
} }
renderer->display(); window->display();
} }
Game::~Game() { Game::~Game()
for (auto &gameObject: gameObjects) { {
for (auto &gameObject: gameObjects)
{
delete gameObject; delete gameObject;
} }
} }
void Game::addGameObject(GameObject *gameObject) { void Game::addGameObject(GameObject *gameObject)
{
gameObjects.push_back(gameObject); gameObjects.push_back(gameObject);
} }

View file

@ -7,18 +7,17 @@
#include <memory> #include <memory>
#include "renderer.h"
#include "../util/easylogging++.h" #include "../util/easylogging++.h"
#include "game_object.h" #include "game_object.h"
class Game { class Game {
private: private:
std::shared_ptr<Renderer> renderer; std::shared_ptr<sf::RenderWindow> window;
std::vector<GameObject *> gameObjects; std::vector<GameObject *> gameObjects;
bool isRunning = false; bool isRunning = false;
public: public:
explicit Game(std::shared_ptr<Renderer> renderer); explicit Game(std::shared_ptr<sf::RenderWindow> window);
~Game(); ~Game();
void run(); void run();

32
src/game/game_factory.cpp Normal file
View file

@ -0,0 +1,32 @@
//
// Created by max on 28.04.23.
//
#include <SFML/Graphics/RenderWindow.hpp>
#include "game_factory.hpp"
std::shared_ptr<Game> GameFactory::createWindowed(const std::string &title, int width, int height)
{
auto window = std::make_shared<sf::RenderWindow>(sf::VideoMode(width, height), title);
return std::make_shared<Game>(window);
}
std::shared_ptr<Game> 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<sf::RenderWindow>(fullScreenMode, title, sf::Style::Fullscreen);
return std::make_shared<Game>(window);
}

17
src/game/game_factory.hpp Normal file
View file

@ -0,0 +1,17 @@
#ifndef HOLESOME_GAME_FACTORY_HPP
#define HOLESOME_GAME_FACTORY_HPP
#include <string>
#include <memory>
#include "game.h"
class GameFactory
{
public:
static std::shared_ptr<Game> createWindowed(const std::string &title, int width, int height);
static std::shared_ptr<Game> createFullscreen(const std::string &title);
};
#endif //HOLESOME_GAME_FACTORY_HPP

View file

@ -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> Renderer::createWindow(const std::string &title, int width, int height) {
auto renderer = std::make_shared<Renderer>();
renderer->window = new sf::RenderWindow(sf::VideoMode(width, height), title);
return renderer;
}
std::shared_ptr<Renderer> 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>();
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);
}

View file

@ -1,27 +0,0 @@
#ifndef HOLESOME_RENDERER_H
#define HOLESOME_RENDERER_H
#include <SFML/Graphics/RenderWindow.hpp>
#include "../util/easylogging++.h"
class Renderer {
public:
static std::shared_ptr<Renderer> createWindow(const std::string &title, int width = 960, int height = 540);
static std::shared_ptr<Renderer> 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

View file

@ -3,15 +3,17 @@
#include "util/easylogging++.h" #include "util/easylogging++.h"
#include "game/game.h" #include "game/game.h"
#include "primitives/circle_object.h" #include "primitives/circle_object.h"
#include "game/game_factory.hpp"
INITIALIZE_EASYLOGGINGPP INITIALIZE_EASYLOGGINGPP
int main(int argc, char *argv[]) { int main(int argc, char *argv[])
{
START_EASYLOGGINGPP(argc, 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();
} }