From 2bcf699b9e11ccf848393882257fc3986bd28e45 Mon Sep 17 00:00:00 2001 From: HampusM Date: Sun, 27 Feb 2022 15:24:07 +0100 Subject: add game & vector2 --- src/CMakeLists.txt | 2 ++ src/bootstrap.cpp | 12 +++++++++++ src/engine/graphics/vector2.cpp | 39 +++++++++++++++++++++++++++++++++ src/engine/graphics/vector2.hpp | 48 +++++++++++++++++++++++++++++++++++++++++ src/game/game.cpp | 9 ++++++++ src/game/game.hpp | 16 ++++++++++++++ src/game_of_life.cpp | 6 ++++++ src/interfaces/game.hpp | 7 ++++++ src/interfaces/vector2.hpp | 44 +++++++++++++++++++++++++++++++++++++ 9 files changed, 183 insertions(+) create mode 100644 src/engine/graphics/vector2.cpp create mode 100644 src/engine/graphics/vector2.hpp create mode 100644 src/game/game.cpp create mode 100644 src/game/game.hpp create mode 100644 src/interfaces/game.hpp create mode 100644 src/interfaces/vector2.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 54ee94f..7e668ed 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -13,6 +13,8 @@ file(GLOB SOURCES bootstrap.cpp conversion.cpp argument_parser.cpp + game/game.cpp + engine/graphics/vector2.cpp randomization/generator.cpp randomization/seed_generator.cpp DI/object_type.cpp) diff --git a/src/bootstrap.cpp b/src/bootstrap.cpp index 85498bc..ce07621 100644 --- a/src/bootstrap.cpp +++ b/src/bootstrap.cpp @@ -2,10 +2,14 @@ // Interfaces #include "interfaces/argument_parser.hpp" +#include "interfaces/game.hpp" #include "interfaces/randomization.hpp" +#include "interfaces/vector2.hpp" // Implementations #include "argument_parser.hpp" +#include "engine/graphics/vector2.hpp" +#include "game/game.hpp" #include "randomization/generator.hpp" #include "randomization/seed_generator.hpp" @@ -17,6 +21,7 @@ Container bootstrap() auto container = Container(); container.bind().to(); + container.bind().to(); container.bind().to_factory( [](const unsigned int &seed) @@ -32,5 +37,12 @@ Container bootstrap() std::make_shared(std::make_unique())); }); + container.bind().to_factory( + [](const IVector2Options &options) + { + return std::dynamic_pointer_cast( + std::make_shared(options)); + }); + return container; } diff --git a/src/engine/graphics/vector2.cpp b/src/engine/graphics/vector2.cpp new file mode 100644 index 0000000..dfdf4bd --- /dev/null +++ b/src/engine/graphics/vector2.cpp @@ -0,0 +1,39 @@ +#include "vector2.hpp" + +Vector2::Vector2(const IVector2Options &options) : _x(options.x), _y(options.y) {} + +unsigned int Vector2::x() const +{ + return _x; +} + +void Vector2::x(unsigned int x) +{ + _x = x; +} + +unsigned int Vector2::y() const +{ + return _y; +} + +void Vector2::y(unsigned int y) +{ + _y = y; +} + +const IVector2 &Vector2::operator+=(const IVector2 &vector2) +{ + _x += vector2.x(); + _y += vector2.y(); + + return *this; +} + +const IVector2 &Vector2::operator-=(const IVector2 &vector2) +{ + _x -= vector2.x(); + _y -= vector2.y(); + + return *this; +} diff --git a/src/engine/graphics/vector2.hpp b/src/engine/graphics/vector2.hpp new file mode 100644 index 0000000..c8c6349 --- /dev/null +++ b/src/engine/graphics/vector2.hpp @@ -0,0 +1,48 @@ +#pragma once + +#include "interfaces/vector2.hpp" + +#include + +/** + * A 2D Vector. + */ +class Vector2 : public IVector2 +{ +public: + /** + * Creates a 2D vector. + */ + explicit Vector2(const IVector2Options &options); + + /** + * Returns the X coordinate. + */ + [[nodiscard]] unsigned int x() const override; + + /** + * Sets the X coordinate. + * + * @param x A new X coordinate + */ + void x(unsigned int x) override; + + /** + * Returns the Y coordinate. + */ + [[nodiscard]] unsigned int y() const override; + + /** + * Sets the Y coordinate. + * + * @param Y A new Y coordinate + */ + void y(unsigned int y) override; + + const IVector2 &operator+=(const IVector2 &vector2) override; + const IVector2 &operator-=(const IVector2 &vector2) override; + +private: + unsigned int _x; + unsigned int _y; +}; diff --git a/src/game/game.cpp b/src/game/game.cpp new file mode 100644 index 0000000..e785e18 --- /dev/null +++ b/src/game/game.cpp @@ -0,0 +1,9 @@ +#include "game.hpp" + +#include + +Game::Game(IVector2Factory vector2_factory) : _vector2_factory(std::move(vector2_factory)) +{ +} + +void Game::run() {} diff --git a/src/game/game.hpp b/src/game/game.hpp new file mode 100644 index 0000000..4c7e5e1 --- /dev/null +++ b/src/game/game.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include "DI/auto_wirable.hpp" +#include "interfaces/game.hpp" +#include "interfaces/vector2.hpp" + +class Game : public IGame, public AutoWirable +{ +public: + explicit Game(IVector2Factory vector2_factory); + + void run() override; + +private: + IVector2Factory _vector2_factory; +}; diff --git a/src/game_of_life.cpp b/src/game_of_life.cpp index 9f7b019..4d7ec52 100644 --- a/src/game_of_life.cpp +++ b/src/game_of_life.cpp @@ -1,5 +1,6 @@ #include "bootstrap.hpp" #include "interfaces/argument_parser.hpp" +#include "interfaces/game.hpp" #include "interfaces/randomization.hpp" #include @@ -20,10 +21,15 @@ int main(int argc, char *argv[]) if (parsed_arguments.random_gen == nullptr) { auto seed_generator_factory = container.get(); + auto random_number_generator_factory = container.get(); parsed_arguments.random_gen = random_number_generator_factory(seed_generator_factory()->random_seed()); } + + auto game = container.get(); + + game->run(); } diff --git a/src/interfaces/game.hpp b/src/interfaces/game.hpp new file mode 100644 index 0000000..f9d851b --- /dev/null +++ b/src/interfaces/game.hpp @@ -0,0 +1,7 @@ +#pragma once + +class IGame +{ +public: + virtual void run() = 0; +}; diff --git a/src/interfaces/vector2.hpp b/src/interfaces/vector2.hpp new file mode 100644 index 0000000..a58686f --- /dev/null +++ b/src/interfaces/vector2.hpp @@ -0,0 +1,44 @@ +#pragma once + +#include +#include + +class IVector2 +{ +public: + /** + * Returns the X coordinate. + */ + [[nodiscard]] virtual unsigned int x() const = 0; + + /** + * Sets the X coordinate. + * + * @param x A new X coordinate + */ + virtual void x(unsigned int x) = 0; + + /** + * Returns the Y coordinate. + */ + [[nodiscard]] virtual unsigned int y() const = 0; + + /** + * Sets the Y coordinate. + * + * @param Y A new Y coordinate + */ + virtual void y(unsigned int y) = 0; + + virtual const IVector2 &operator+=(const IVector2 &vector2) = 0; + virtual const IVector2 &operator-=(const IVector2 &vector2) = 0; +}; + +struct IVector2Options +{ + unsigned int x; + unsigned int y; +}; + +using IVector2Factory = + std::function(const IVector2Options &options)>; -- cgit v1.2.3-18-g5258