diff --git a/CMakeLists.txt b/CMakeLists.txt index 9247046..282d3b0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,8 +40,6 @@ set(SOURCES src/debug/grid_debug_layer.h src/game/input/input_mapper.cpp src/game/input/input_mapper.h - src/game/input/action_controller.cpp - src/game/input/action_controller.h src/game/input/direction.h src/game/input/direction.cpp src/game/player/player.cpp diff --git a/src/config.h b/src/config.h index 0f7f043..842ad34 100644 --- a/src/config.h +++ b/src/config.h @@ -8,7 +8,7 @@ // FPS #define FRAME_RATE 60 -#define FRAME_TIME 1.0f / FRAME_RATE +#define FRAME_TIME sf::Time(sf::seconds(1.0f / FRAME_RATE)) // Window settings #define ANTIALIASINGLEVEL 8 diff --git a/src/debug/grid_debug_layer.cpp b/src/debug/grid_debug_layer.cpp index a1eead6..2a92791 100644 --- a/src/debug/grid_debug_layer.cpp +++ b/src/debug/grid_debug_layer.cpp @@ -32,7 +32,7 @@ void GridDebugLayer::draw(sf::RenderWindow *window) const } } -void GridDebugLayer::update() +void GridDebugLayer::update(Game *game) { } diff --git a/src/debug/grid_debug_layer.h b/src/debug/grid_debug_layer.h index 1ed5cbb..9d806a5 100644 --- a/src/debug/grid_debug_layer.h +++ b/src/debug/grid_debug_layer.h @@ -11,7 +11,7 @@ public: ~GridDebugLayer(); void draw(sf::RenderWindow *window) const override; - void update() override; + void update(Game *game) override; private: std::vector marker; diff --git a/src/game/game.cpp b/src/game/game.cpp index 26cf66d..c1dcd45 100644 --- a/src/game/game.cpp +++ b/src/game/game.cpp @@ -5,16 +5,13 @@ #include "game.h" Game::Game(std::shared_ptr window) : window(std::move(window)), - gameObjects(), - actionController(new ActionController(this)) + gameObjects() { InputMapper::setGame(this); } Game::~Game() { - delete actionController; - for (auto &gameObject: gameObjects) { delete gameObject; @@ -68,6 +65,6 @@ void Game::update() { for (auto &gameObject: gameObjects) { - gameObject->update(); + gameObject->update(this); } } diff --git a/src/game/game.h b/src/game/game.h index e4bf7b4..df4aef4 100644 --- a/src/game/game.h +++ b/src/game/game.h @@ -5,10 +5,10 @@ #include #include "../logging/easylogging++.h" #include "game_object.h" -#include "input/action_controller.h" #include "input/input_mapper.h" +class GameObject; class ActionController; @@ -26,7 +26,6 @@ public: void addGameObject(GameObject *gameObject); std::shared_ptr window; - ActionController *actionController; private: std::vector gameObjects; diff --git a/src/game/game_object.h b/src/game/game_object.h index beec618..0b0c7f0 100644 --- a/src/game/game_object.h +++ b/src/game/game_object.h @@ -6,13 +6,16 @@ #include #include #include "../coordinates/translated_coordinates.h" +#include "game.h" + +class Game; class GameObject { public: GameObject(); virtual void draw(sf::RenderWindow *window) const = 0; - virtual void update() = 0; + virtual void update(Game *game) = 0; TranslatedCoordinates coordinates; }; diff --git a/src/game/input/action_controller.cpp b/src/game/input/action_controller.cpp deleted file mode 100644 index 2adf8c5..0000000 --- a/src/game/input/action_controller.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include "action_controller.h" - -ActionController::ActionController(Game *game) : game(game) -{ -} - -void ActionController::moveView(InputDirection direction) -{ - float stepSize = 100.0f; - auto delta = stepSize * Direction::getVector(direction); - - auto view = game->window->getView(); - view.move(delta); - game->window->setView(view); -} diff --git a/src/game/input/action_controller.h b/src/game/input/action_controller.h deleted file mode 100644 index d814bdf..0000000 --- a/src/game/input/action_controller.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef HOLESOME_ACTION_CONTROLLER_H -#define HOLESOME_ACTION_CONTROLLER_H - - -#include "../game.h" -#include "direction.h" - -class Game; - - -/** - * Holds all the actions that can be performed and executes them when requested. - */ -class ActionController -{ -public: - explicit ActionController(Game *game); - - void moveView(InputDirection direction); - -private: - Game *game; -}; - - -#endif //HOLESOME_ACTION_CONTROLLER_H diff --git a/src/game/input/input_mapper.cpp b/src/game/input/input_mapper.cpp index fcee78f..aa1fe78 100644 --- a/src/game/input/input_mapper.cpp +++ b/src/game/input/input_mapper.cpp @@ -24,7 +24,6 @@ void InputMapper::processEvents() game->exit(); break; case sf::Event::Resized: - game->window->setView(sf::View(sf::FloatRect(0, 0, event.size.width, event.size.height))); break; default: break; diff --git a/src/game/player/player.hpp b/src/game/player/player.hpp index 0875b79..7949e57 100644 --- a/src/game/player/player.hpp +++ b/src/game/player/player.hpp @@ -12,7 +12,7 @@ public: void draw(sf::RenderWindow *window) const override; - void update() override; + void update(Game *game) override; }; diff --git a/src/game/world/world_view.cpp b/src/game/world/world_view.cpp index 9fdb9a6..81e6fce 100644 --- a/src/game/world/world_view.cpp +++ b/src/game/world/world_view.cpp @@ -1,29 +1,46 @@ #include "world_view.h" -WorldView::WorldView(Game *game) +WorldView::WorldView() : view(nullptr) { - this->game = game; - view = game->window->getView(); } WorldView::~WorldView() { - + delete view; } void WorldView::draw(sf::RenderWindow *window) const { } -void WorldView::update() +void WorldView::update(Game *game) { - auto moveDirection = InputMapper::getInputDirection(); + if (view == nullptr) + { + initializeView(game); + } + // Update size + auto windowSize = game->window->getSize(); + sf::Vector2f viewSize = sf::Vector2f(windowSize.x, windowSize.y); + view->setSize(viewSize); + + // Update position + auto moveDirection = InputMapper::getInputDirection(); if (moveDirection != InputDirection::NONE) { - float stepSize = 100.0f * FRAME_TIME; + float stepSize = 100.0f * FRAME_TIME.asSeconds(); auto delta = stepSize * Direction::getVector(moveDirection); - this->view.move(delta); - game->window->setView(this->view); + this->view->move(delta); } + + // TODO: Only update if necessary + game->window->setView(*this->view); +} + +void WorldView::initializeView(Game *game) +{ + auto center = game->window->getView().getCenter(); + auto size = game->window->getView().getSize(); + view = new sf::View(center, size); } diff --git a/src/game/world/world_view.h b/src/game/world/world_view.h index 6e8cfc2..c7bcdf8 100644 --- a/src/game/world/world_view.h +++ b/src/game/world/world_view.h @@ -5,20 +5,21 @@ #include "../game_object.h" #include "../game.h" -class WorldView : GameObject +class WorldView : public GameObject { public: - explicit WorldView(Game *game); + explicit WorldView(); ~WorldView(); void draw(sf::RenderWindow *window) const override; - void update() override; + void update(Game *game) override; private: - sf::View view; - Game *game; + sf::View *view; + + void initializeView(Game *game); }; diff --git a/src/main.cpp b/src/main.cpp index 50c9d4b..501ce1d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,6 +3,7 @@ #include "logging/easylogging++.h" #include "game/game_factory.hpp" #include "debug/grid_debug_layer.h" +#include "game/world/world_view.h" INITIALIZE_EASYLOGGINGPP @@ -13,6 +14,7 @@ int main(int argc, char *argv[]) auto game = GameFactory::createWindowed("Holesome"); game->addGameObject(new GridDebugLayer(0, 50, 0, 50)); + game->addGameObject(new WorldView()); game->run(); } diff --git a/src/primitives/circle_object.cpp b/src/primitives/circle_object.cpp index 77a148f..19f5f99 100644 --- a/src/primitives/circle_object.cpp +++ b/src/primitives/circle_object.cpp @@ -15,7 +15,7 @@ void CircleObject::draw(sf::RenderWindow *window) const window->draw(circle); } -void CircleObject::update() +void CircleObject::update(Game *game) { } diff --git a/src/primitives/circle_object.h b/src/primitives/circle_object.h index 553932d..8e2f9cc 100644 --- a/src/primitives/circle_object.h +++ b/src/primitives/circle_object.h @@ -10,7 +10,7 @@ public: CircleObject(int radius, sf::Color color); void draw(sf::RenderWindow *window) const override; - void update() override; + void update(Game *game) override; private: int radius;