diff --git a/.gitignore b/.gitignore index a206d9b..be04db3 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,5 @@ treender .vs/ *.sln *.vcxproj* -Console*/ \ No newline at end of file +Console*/ +main \ No newline at end of file diff --git a/inc/Math.hpp b/inc/Math.hpp index bf711d3..27cc60f 100644 --- a/inc/Math.hpp +++ b/inc/Math.hpp @@ -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); \ No newline at end of file +void drawTexureWithRotation(RenderTexture2D &target, Rectangle &dest, float rotation); \ No newline at end of file diff --git a/inc/values/mrand.hpp b/inc/values/mrand.hpp new file mode 100644 index 0000000..d8354b0 --- /dev/null +++ b/inc/values/mrand.hpp @@ -0,0 +1,8 @@ +#include + +namespace mrand +{ + void setSeed(unsigned long long seed); + int getValue(int min, int max); + float getFloat(); +} \ No newline at end of file diff --git a/src/App.cpp b/src/App.cpp index 5afa9f0..b1fbab9 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -3,6 +3,7 @@ #include "App.hpp" #include "Math.hpp" +#include "values/mrand.hpp" #include #include @@ -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(); } \ No newline at end of file diff --git a/src/Math.cpp b/src/Math.cpp index 76edafe..609cd66 100644 --- a/src/Math.cpp +++ b/src/Math.cpp @@ -2,6 +2,8 @@ #include #include "Math.hpp" +#include "values/mrand.hpp" + #include 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); } \ No newline at end of file diff --git a/src/canvas/BackGround.cpp b/src/canvas/BackGround.cpp index 709a4d7..443cdd5 100644 --- a/src/canvas/BackGround.cpp +++ b/src/canvas/BackGround.cpp @@ -3,6 +3,7 @@ #include "canvas/BackGround.hpp" #include "canvas/BackGroundColors.hpp" #include "Math.hpp" +#include "values/mrand.hpp" #include #include @@ -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); diff --git a/src/canvas/Canvas.cpp b/src/canvas/Canvas.cpp index 6c00f3d..a2ce14d 100644 --- a/src/canvas/Canvas.cpp +++ b/src/canvas/Canvas.cpp @@ -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(); } diff --git a/src/canvas/Tree.cpp b/src/canvas/Tree.cpp index e17a28c..69c1b79 100644 --- a/src/canvas/Tree.cpp +++ b/src/canvas/Tree.cpp @@ -3,6 +3,7 @@ #include #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; diff --git a/src/values/MyRand.cpp b/src/values/MyRand.cpp deleted file mode 100644 index 29df61b..0000000 --- a/src/values/MyRand.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include - -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; -} \ No newline at end of file diff --git a/src/values/RandBuffer.cpp b/src/values/RandBuffer.cpp index b4b36ed..b065190 100644 --- a/src/values/RandBuffer.cpp +++ b/src/values/RandBuffer.cpp @@ -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; diff --git a/src/values/mrand.cpp b/src/values/mrand.cpp new file mode 100644 index 0000000..c710d0a --- /dev/null +++ b/src/values/mrand.cpp @@ -0,0 +1,63 @@ +#include +#include +#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; + } +} \ No newline at end of file