From 62ade4de04d18507b9759c051163457a27ba74eb Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Fri, 12 Mar 2021 21:27:45 -0600 Subject: [PATCH] Create simpleengine::Random class, remove simpleengine::random namespace --- examples/snake/src/main.cpp | 7 +++++-- include/simpleengine/async_event.h | 2 +- include/simpleengine/game.h | 5 ----- include/simpleengine/random.h | 21 ++++++++++++++++++--- src/random.cpp | 13 ------------- 5 files changed, 24 insertions(+), 24 deletions(-) delete mode 100644 src/random.cpp diff --git a/examples/snake/src/main.cpp b/examples/snake/src/main.cpp index 494355f..6be8510 100644 --- a/examples/snake/src/main.cpp +++ b/examples/snake/src/main.cpp @@ -154,8 +154,11 @@ public: target->draw(shape); } + // Find a new location for the snake food. void Relocate() { - sf::Vector2f pos(simpleengine::random::RandomInt(0, window_size.x), simpleengine::random::RandomInt(0, window_size.y)); + simpleengine::Random random; + sf::Vector2f pos(random.NextInRange(0, window_size.x), random.NextInRange(0, window_size.y)); + // Make sure its on the 15 pixel grid. pos.x -= (int) pos.x % 15; pos.y -= (int) pos.y % 15; @@ -293,7 +296,7 @@ int main(int argc, char *argv[]) { score_text.setString("0"); score_text.setCharacterSize(24); score_text.setFillColor(sf::Color::Green); - score_text.setPosition(window_size.x - 50, 0); + score_text.setPosition(window_size.x - 50, 5); auto snake_player = std::make_shared(window_size, score_text, std::ref(game)); auto snake_food = std::make_shared(window_size, snake_player); diff --git a/include/simpleengine/async_event.h b/include/simpleengine/async_event.h index 4b1d790..43d9f75 100644 --- a/include/simpleengine/async_event.h +++ b/include/simpleengine/async_event.h @@ -56,7 +56,7 @@ namespace simpleengine { cond_var.notify_all(); } - virtual void Render(sf::RenderTarget* target) = 0; + virtual void Render(sf::RenderTarget* target) { }; protected: std::mutex mutex; std::condition_variable cond_var; diff --git a/include/simpleengine/game.h b/include/simpleengine/game.h index d7f437f..cd6ba1e 100644 --- a/include/simpleengine/game.h +++ b/include/simpleengine/game.h @@ -20,11 +20,6 @@ namespace simpleengine { class Event; - /* template - auto Deduce(F f) -> std::void_t{ - std::function{f}; - } */ - class Game { public: Game(int w, int h, const std::string& window_name); diff --git a/include/simpleengine/random.h b/include/simpleengine/random.h index 8ac7b95..bb4c667 100644 --- a/include/simpleengine/random.h +++ b/include/simpleengine/random.h @@ -8,10 +8,25 @@ #define SIMPLEENGINE_RANDOM_H #include +#include namespace simpleengine { - namespace random { - int RandomInt(int min = std::numeric_limits::min(), int max = std::numeric_limits::max()); - } + template + class Random { + private: + RandomDevice rd; + Generator gen; + public: + Random() { + this->gen = Generator(rd()); + } + + template> + T NextInRange(T min, T max) { + Dist dist(min, max); + + return dist(gen); + } + }; } #endif //SIMPLEENGINE_RANDOM_H \ No newline at end of file diff --git a/src/random.cpp b/src/random.cpp deleted file mode 100644 index bded16a..0000000 --- a/src/random.cpp +++ /dev/null @@ -1,13 +0,0 @@ -#include "random.h" - -#include - -namespace simpleengine { - int random::RandomInt(int min, int max) { - std::random_device rd; - std::mt19937 gen(rd()); - std::uniform_int_distribution<> distr(min, max); - - return distr(gen); - } -} \ No newline at end of file