Add random generator that accepts state for repetability
This commit is contained in:
parent
bbc8b33224
commit
2112878ca7
3
.gitignore
vendored
3
.gitignore
vendored
@ -9,4 +9,5 @@ treender
|
||||
.vs/
|
||||
*.sln
|
||||
*.vcxproj*
|
||||
Console*/
|
||||
Console*/
|
||||
main
|
@ -3,6 +3,5 @@
|
||||
Color ColorLerp(Color c1, Color c2, float amount);
|
||||
Color ColorAdd(Color c, int add);
|
||||
Vector2 CalculateVector(float rotation, float offSet, float len);
|
||||
float GetRandomFloat();
|
||||
|
||||
void drawTexureWithRotation(RenderTexture2D& target, Rectangle& dest, float rotation);
|
||||
void drawTexureWithRotation(RenderTexture2D &target, Rectangle &dest, float rotation);
|
8
inc/values/mrand.hpp
Normal file
8
inc/values/mrand.hpp
Normal file
@ -0,0 +1,8 @@
|
||||
#include <inttypes.h>
|
||||
|
||||
namespace mrand
|
||||
{
|
||||
void setSeed(unsigned long long seed);
|
||||
int getValue(int min, int max);
|
||||
float getFloat();
|
||||
}
|
@ -3,6 +3,7 @@
|
||||
|
||||
#include "App.hpp"
|
||||
#include "Math.hpp"
|
||||
#include "values/mrand.hpp"
|
||||
|
||||
#include <raylib.h>
|
||||
#include <raymath.h>
|
||||
@ -13,11 +14,13 @@ void App::init(int screenWidth, int screenHeight)
|
||||
this->screenHeight = screenHeight;
|
||||
this->canvas.init(screenWidth);
|
||||
|
||||
mrand::setSeed((unsigned int)time(NULL));
|
||||
|
||||
for (size_t i = 0; i < canvasTexure.size(); i++)
|
||||
{
|
||||
canvasTexure[i] = LoadRenderTexture(screenWidth, screenWidth);
|
||||
}
|
||||
|
||||
|
||||
canvas.newGen(canvasTexure[0]);
|
||||
canvas.newGen(canvasTexure[1]);
|
||||
|
||||
@ -67,7 +70,7 @@ void App::deinit()
|
||||
{
|
||||
for (size_t i = 0; i < canvasTexure.size(); i++)
|
||||
{
|
||||
UnloadRenderTexture(canvasTexure[i]);
|
||||
UnloadRenderTexture(canvasTexure[i]);
|
||||
}
|
||||
canvas.deinit();
|
||||
}
|
13
src/Math.cpp
13
src/Math.cpp
@ -2,6 +2,8 @@
|
||||
#include <algorithm>
|
||||
|
||||
#include "Math.hpp"
|
||||
#include "values/mrand.hpp"
|
||||
|
||||
#include <raymath.h>
|
||||
|
||||
Color ColorLerp(Color c1, Color c2, float amount)
|
||||
@ -32,15 +34,10 @@ Vector2 CalculateVector(float rotation, float offSet, float len)
|
||||
.y = len * std::cos(angle)};
|
||||
}
|
||||
|
||||
float GetRandomFloat()
|
||||
void drawTexureWithRotation(RenderTexture2D &target, Rectangle &dest, float rotation)
|
||||
{
|
||||
return GetRandomValue(0, 100000) / 100000.0f;
|
||||
}
|
||||
|
||||
void drawTexureWithRotation(RenderTexture2D& target, Rectangle& dest, float rotation)
|
||||
{
|
||||
Rectangle source = { 0, 0, (float)target.texture.width, (float)-target.texture.height };
|
||||
Vector2 origin = { 0.0f, 0.0f };
|
||||
Rectangle source = {0, 0, (float)target.texture.width, (float)-target.texture.height};
|
||||
Vector2 origin = {0.0f, 0.0f};
|
||||
rotation = 360 - rotation;
|
||||
DrawTexturePro(target.texture, source, dest, origin, rotation, WHITE);
|
||||
}
|
@ -3,6 +3,7 @@
|
||||
#include "canvas/BackGround.hpp"
|
||||
#include "canvas/BackGroundColors.hpp"
|
||||
#include "Math.hpp"
|
||||
#include "values/mrand.hpp"
|
||||
|
||||
#include <raylib.h>
|
||||
#include <rlgl.h>
|
||||
@ -24,8 +25,8 @@ void BackGround::newGen()
|
||||
{
|
||||
starBuff.reset();
|
||||
mountenBuff.reset();
|
||||
m_moon = {GetRandomFloat(), GetRandomFloat(), GetRandomFloat()};
|
||||
colorSet = GetRandomValue(0, 3);
|
||||
m_moon = {mrand::getFloat(), mrand::getFloat(), mrand::getFloat()};
|
||||
colorSet = mrand::getValue(0, 3);
|
||||
time = floor(Remap(m_moon.y, 0, 1, 4, 0));
|
||||
|
||||
BackGroundColors::setColor(colorSet, time);
|
||||
|
@ -6,7 +6,7 @@ void Canvas::init(int size)
|
||||
tree.init(size);
|
||||
}
|
||||
|
||||
void Canvas::newGen(RenderTexture2D& target)
|
||||
void Canvas::newGen(RenderTexture2D &target)
|
||||
{
|
||||
BeginTextureMode(target);
|
||||
ClearBackground(WHITE);
|
||||
@ -17,7 +17,7 @@ void Canvas::newGen(RenderTexture2D& target)
|
||||
EndTextureMode();
|
||||
}
|
||||
|
||||
void Canvas::deinit()
|
||||
void Canvas::deinit()
|
||||
{
|
||||
backGround.deinit();
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
#include <raymath.h>
|
||||
#include "canvas/Tree.hpp"
|
||||
#include "Math.hpp"
|
||||
#include "values/mrand.hpp"
|
||||
|
||||
// Public
|
||||
void Tree::init(int size)
|
||||
@ -60,7 +61,7 @@ void Tree::drawBranch()
|
||||
for (size_t i = 0; i < branches[arg.dep].numOfBranches; i++)
|
||||
{
|
||||
float newAngle = arg.angleDeg - 90 + (degres * (i + 1));
|
||||
draw_calls.push_back({ next, newAngle, arg.lenghth * next_len, arg.dep + 1 });
|
||||
draw_calls.push_back({next, newAngle, arg.lenghth * next_len, arg.dep + 1});
|
||||
}
|
||||
}
|
||||
|
||||
@ -79,14 +80,14 @@ void Tree::generateBranches()
|
||||
{
|
||||
for (size_t i = 0; i < MAX_DEPTH; i++)
|
||||
{
|
||||
uint8_t r = GetRandomValue(0, 255);
|
||||
uint8_t g = GetRandomValue(0, 255);
|
||||
uint8_t b = GetRandomValue(0, 255);
|
||||
uint8_t r = mrand::getValue(0, 255);
|
||||
uint8_t g = mrand::getValue(0, 255);
|
||||
uint8_t b = mrand::getValue(0, 255);
|
||||
branches[i].color = {r, g, b, 255};
|
||||
|
||||
branches[i].numOfBranches = GetRandomValue(1, 3);
|
||||
branches[i].numOfBranches = mrand::getValue(1, 3);
|
||||
|
||||
branches[i].lenghthRatio = ((float)GetRandomValue(600, 700)) / 1000.0f;
|
||||
branches[i].lenghthRatio = ((float)mrand::getValue(600, 700)) / 1000.0f;
|
||||
}
|
||||
|
||||
branches[0].color = branches[1].color;
|
||||
|
@ -1,25 +0,0 @@
|
||||
#include <inttypes.h>
|
||||
|
||||
static uint32_t rprand_state[4] = {0};
|
||||
|
||||
static inline uint32_t my_rotate_left(const uint32_t x, int k)
|
||||
{
|
||||
return (x << k) | (x >> (32 - k));
|
||||
}
|
||||
|
||||
uint32_t my_rprand_xoshiro(void)
|
||||
{
|
||||
const uint32_t result = my_rotate_left(rprand_state[1] * 5, 7) * 9;
|
||||
const uint32_t t = rprand_state[1] << 9;
|
||||
|
||||
rprand_state[2] ^= rprand_state[0];
|
||||
rprand_state[3] ^= rprand_state[1];
|
||||
rprand_state[1] ^= rprand_state[2];
|
||||
rprand_state[0] ^= rprand_state[3];
|
||||
|
||||
rprand_state[2] ^= t;
|
||||
|
||||
rprand_state[3] = my_rotate_left(rprand_state[3], 11);
|
||||
|
||||
return result;
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
#include "values/RandBuffer.hpp"
|
||||
#include "Math.hpp"
|
||||
#include "values/mrand.hpp"
|
||||
|
||||
void RandBuffer::reset()
|
||||
{
|
||||
@ -13,7 +13,7 @@ float RandBuffer::next()
|
||||
{
|
||||
return ((float *)m_buffer.buffer)[m_pos++];
|
||||
}
|
||||
float ret = GetRandomFloat();
|
||||
float ret = mrand::getFloat();
|
||||
m_buffer.add_end((uint8_t *)&ret, sizeof(float));
|
||||
m_pos++;
|
||||
return ret;
|
||||
|
63
src/values/mrand.cpp
Normal file
63
src/values/mrand.cpp
Normal file
@ -0,0 +1,63 @@
|
||||
#include <inttypes.h>
|
||||
#include <algorithm>
|
||||
#include "values/mrand.hpp"
|
||||
|
||||
static uint32_t rprand_state[4] = {0};
|
||||
static uint64_t rprand_seed = 0;
|
||||
|
||||
static inline uint32_t my_rotate_left(const uint32_t x, int k)
|
||||
{
|
||||
return (x << k) | (x >> (32 - k));
|
||||
}
|
||||
|
||||
uint32_t my_rprand_xoshiro(uint32_t state[4])
|
||||
{
|
||||
const uint32_t result = my_rotate_left(state[1] * 5, 7) * 9;
|
||||
const uint32_t t = state[1] << 9;
|
||||
|
||||
state[2] ^= state[0];
|
||||
state[3] ^= state[1];
|
||||
state[1] ^= state[2];
|
||||
state[0] ^= state[3];
|
||||
|
||||
state[2] ^= t;
|
||||
|
||||
state[3] = my_rotate_left(state[3], 11);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
uint64_t rprand_splitmix64()
|
||||
{
|
||||
uint64_t z = (rprand_seed += 0x9e3779b97f4a7c15);
|
||||
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
|
||||
z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
|
||||
return z ^ (z >> 31);
|
||||
}
|
||||
|
||||
namespace mrand
|
||||
{
|
||||
void setSeed(unsigned long long seed)
|
||||
{
|
||||
rprand_seed = (uint64_t)seed; // Set SplitMix64 seed for further use
|
||||
|
||||
// To generate the Xoshiro128** state, we use SplitMix64 generator first
|
||||
// We generate 4 pseudo-random 64bit numbers that we combine using their LSB|MSB
|
||||
rprand_state[0] = (uint32_t)(rprand_splitmix64() & 0xffffffff);
|
||||
rprand_state[1] = (uint32_t)((rprand_splitmix64() & 0xffffffff00000000) >> 32);
|
||||
rprand_state[2] = (uint32_t)(rprand_splitmix64() & 0xffffffff);
|
||||
rprand_state[3] = (uint32_t)((rprand_splitmix64() & 0xffffffff00000000) >> 32);
|
||||
}
|
||||
|
||||
int getValue(int min, int max)
|
||||
{
|
||||
int value = my_rprand_xoshiro(rprand_state) % (std::abs(max - min) + 1) + min;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
float getFloat()
|
||||
{
|
||||
return my_rprand_xoshiro(rprand_state) / 4294967295.0f;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user