diff options
author | HampusM <hampus@hampusmat.com> | 2022-02-27 15:24:07 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-06-13 17:56:53 +0200 |
commit | 2bcf699b9e11ccf848393882257fc3986bd28e45 (patch) | |
tree | a7ea1033984a857812297b0b41bd322c57a3b23f | |
parent | 5864e5abc43b201c3801fa39a2fcaf9e3a9e8914 (diff) |
add game & vector2
-rw-r--r-- | src/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/bootstrap.cpp | 12 | ||||
-rw-r--r-- | src/engine/graphics/vector2.cpp | 39 | ||||
-rw-r--r-- | src/engine/graphics/vector2.hpp | 48 | ||||
-rw-r--r-- | src/game/game.cpp | 9 | ||||
-rw-r--r-- | src/game/game.hpp | 16 | ||||
-rw-r--r-- | src/game_of_life.cpp | 6 | ||||
-rw-r--r-- | src/interfaces/game.hpp | 7 | ||||
-rw-r--r-- | src/interfaces/vector2.hpp | 44 |
9 files changed, 183 insertions, 0 deletions
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<IArgumentParser>().to<ArgumentParser>(); + container.bind<IGame>().to<Game>(); container.bind<IRandomNumberGeneratorFactory>().to_factory( [](const unsigned int &seed) @@ -32,5 +37,12 @@ Container bootstrap() std::make_shared<SeedGenerator>(std::make_unique<std::random_device>())); }); + container.bind<IVector2Factory>().to_factory( + [](const IVector2Options &options) + { + return std::dynamic_pointer_cast<IVector2>( + std::make_shared<Vector2>(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 <memory> + +/** + * 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 <iostream> + +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<IGame, Game, IVector2Factory> +{ +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 <getopt.h> @@ -20,10 +21,15 @@ int main(int argc, char *argv[]) if (parsed_arguments.random_gen == nullptr) { auto seed_generator_factory = container.get<ISeedGeneratorFactory>(); + auto random_number_generator_factory = container.get<IRandomNumberGeneratorFactory>(); parsed_arguments.random_gen = random_number_generator_factory(seed_generator_factory()->random_seed()); } + + auto game = container.get<IGame>(); + + 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 <functional> +#include <memory> + +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<std::shared_ptr<IVector2>(const IVector2Options &options)>; |