Create simpleengine::Random class, remove simpleengine::random namespace

This commit is contained in:
SeanOMik 2021-03-12 21:27:45 -06:00
parent f63d596438
commit 62ade4de04
No known key found for this signature in database
GPG Key ID: CA09E5BE1F32728A
5 changed files with 24 additions and 24 deletions

View File

@ -154,8 +154,11 @@ public:
target->draw(shape); target->draw(shape);
} }
// Find a new location for the snake food.
void Relocate() { void Relocate() {
sf::Vector2f pos(simpleengine::random::RandomInt(0, window_size.x), simpleengine::random::RandomInt(0, window_size.y)); simpleengine::Random<int> random;
sf::Vector2f pos(random.NextInRange(0, window_size.x), random.NextInRange(0, window_size.y));
// Make sure its on the 15 pixel grid. // Make sure its on the 15 pixel grid.
pos.x -= (int) pos.x % 15; pos.x -= (int) pos.x % 15;
pos.y -= (int) pos.y % 15; pos.y -= (int) pos.y % 15;
@ -293,7 +296,7 @@ int main(int argc, char *argv[]) {
score_text.setString("0"); score_text.setString("0");
score_text.setCharacterSize(24); score_text.setCharacterSize(24);
score_text.setFillColor(sf::Color::Green); 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<SnakePlayerEntity>(window_size, score_text, std::ref(game)); auto snake_player = std::make_shared<SnakePlayerEntity>(window_size, score_text, std::ref(game));
auto snake_food = std::make_shared<SnakeFoodEntity>(window_size, snake_player); auto snake_food = std::make_shared<SnakeFoodEntity>(window_size, snake_player);

View File

@ -56,7 +56,7 @@ namespace simpleengine {
cond_var.notify_all(); cond_var.notify_all();
} }
virtual void Render(sf::RenderTarget* target) = 0; virtual void Render(sf::RenderTarget* target) { };
protected: protected:
std::mutex mutex; std::mutex mutex;
std::condition_variable cond_var; std::condition_variable cond_var;

View File

@ -20,11 +20,6 @@
namespace simpleengine { namespace simpleengine {
class Event; class Event;
/* template<typename F>
auto Deduce(F f) -> std::void_t<decltype(std::function{f})>{
std::function{f};
} */
class Game { class Game {
public: public:
Game(int w, int h, const std::string& window_name); Game(int w, int h, const std::string& window_name);

View File

@ -8,10 +8,25 @@
#define SIMPLEENGINE_RANDOM_H #define SIMPLEENGINE_RANDOM_H
#include <limits> #include <limits>
#include <random>
namespace simpleengine { namespace simpleengine {
namespace random { template<typename T, class RandomDevice = std::random_device, class Generator = std::mt19937>
int RandomInt(int min = std::numeric_limits<int>::min(), int max = std::numeric_limits<int>::max()); class Random {
private:
RandomDevice rd;
Generator gen;
public:
Random() {
this->gen = Generator(rd());
} }
template<class Dist = std::uniform_int_distribution<>>
T NextInRange(T min, T max) {
Dist dist(min, max);
return dist(gen);
}
};
} }
#endif //SIMPLEENGINE_RANDOM_H #endif //SIMPLEENGINE_RANDOM_H

View File

@ -1,13 +0,0 @@
#include "random.h"
#include <random>
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);
}
}