restructure and add server/client example

This commit is contained in:
2025-01-24 10:34:04 +01:00
parent 729b475135
commit ec7b293bd5
30 changed files with 299 additions and 15 deletions

View File

@@ -0,0 +1,226 @@
#include <cmath>
#include "canvas/BackGround.hpp"
#include "canvas/BackGroundColors.hpp"
#include "canvas/Circle.hpp"
#include "canvas/stb_perlin.h"
#include <raylib.h>
#include <rlgl.h>
#include <raymath.h>
constexpr static size_t numOfStarts = 150;
constexpr static float moonXOffset = 0.1f;
constexpr static float minSizeOfMoon = 0.1f;
constexpr static float maxSizeOfMoon = 0.15f;
constexpr static float maxYPosOfMoon = 0.80f;
constexpr static float bigRingRatio = 0.5f;
constexpr static float smallRingRatio = 0.25f;
constexpr static float bigRingBlend = 0.02f;
constexpr static float smallRingBlend = 0.05f;
constexpr static float colorRatio1 = 0.3f;
constexpr static float colorRatio2 = 0.7f;
constexpr static float mounten1min = 0.65f;
constexpr static float mounten1max = 0.85f;
constexpr static float mounten2min = 0.80f;
constexpr static float mounten2max = 0.90;
constexpr static float mounten3min = 0.90f;
constexpr static float mounten3max = 0.95f;
Color ColorAddValue(Color c, int add)
{
int r = std::clamp(c.r + add, 0, 255);
int g = std::clamp(c.g + add, 0, 255);
int b = std::clamp(c.b + add, 0, 255);
return {(unsigned char)r, (unsigned char)g, (unsigned char)b, c.a};
}
Color ColorAdd(Color c1, Color c2)
{
int r = std::clamp(c1.r + c2.r, 0, 255);
int g = std::clamp(c1.g + c2.g, 0, 255);
int b = std::clamp(c1.b + c2.b, 0, 255);
int a = std::clamp(c1.a + c2.a, 0, 255);
return {(unsigned char)r, (unsigned char)g, (unsigned char)b, (unsigned char)a};
}
// Public
void BackGround::init(int canvasSize)
{
this->canvasSize = canvasSize;
}
void BackGround::deinit()
{
}
void BackGround::draw(Dna *dna)
{
Circle::setSoftEdge(true);
m_dna = dna;
mountenSeed = dna->mountenSeed;
starSeed = dna->starSeed;
int time = getTime();
int colorSet = getColorSet();
BackGroundColors::setColor(colorSet, time);
if (colorSet == 3)
{
ClearBackground(BackGroundColors::backGroundColor);
drawStars();
}
else
{
DrawRectangleGradientV(0, 0, canvasSize, canvasSize, ColorAddValue(BackGroundColors::backGroundColor, 60), BackGroundColors::backGroundColor);
}
drawSun();
drawMounten(150, (int)(mounten1min * canvasSize), (int)(mounten1max * canvasSize), BackGroundColors::MountenColor1, 5);
drawMounten(100, (int)(mounten2min * canvasSize), (int)(mounten2max * canvasSize), BackGroundColors::MountenColor2, 3);
drawMounten(50, (int)(mounten3min * canvasSize), (int)(mounten3max * canvasSize), BackGroundColors::MountenColor3, 1);
}
void BackGround::drawStars()
{
rlSetTexture(1);
rlBegin(RL_TRIANGLES);
rlNormal3f(0.0f, 0.0f, 1.0f);
bool star = true;
for (size_t i = 0; i < numOfStarts; i++)
{
int x = mrand::getFloat(&starSeed) * canvasSize;
int y = mrand::getFloat(&starSeed) * canvasSize;
int weight = mrand::getFloat(&starSeed) * 3 + 1;
float alph = Normalize(mrand::getFloat(&starSeed), 0.04f, 0.8f);
Color color = ColorLerp(BackGroundColors::backGroundColor, BackGroundColors::starColor, alph);
rlColor4ub(color.r, color.g, color.b, color.a);
if (star)
{
// topLeft
rlVertex2f(x, y);
// bottomLeft
rlVertex2f(x, y + weight);
// topRight
rlVertex2f(x + weight, y);
}
else
{
// bottomRight
rlVertex2f(x + weight, y + weight);
// topRight
rlVertex2f(x + weight, y);
// bottomLeft
rlVertex2f(x, y + weight);
}
star = !star;
}
rlEnd();
rlSetTexture(0);
}
void BackGround::drawSun()
{
int r = ((m_dna->moonY / 255.0f * (maxSizeOfMoon - minSizeOfMoon)) + minSizeOfMoon) * canvasSize;
int xpos = Lerp(canvasSize * moonXOffset, canvasSize - canvasSize * moonXOffset, m_dna->moonX / 255.0f);
int ypos = Lerp(canvasSize * moonXOffset, maxYPosOfMoon * canvasSize, m_dna->moonY / 255.0f);
if (getColorSet() == 3)
{
Circle::setColor(BackGroundColors::moonColor);
r = (((m_dna->moonSize / 255.0f) * (maxSizeOfMoon - minSizeOfMoon)) + minSizeOfMoon) * canvasSize;
}
else
{
Color color = {0};
color.r = 255;
color.g = std::lerp(200, 50, m_dna->moonY / 255.0f);
color.b = std::lerp(50, 0, m_dna->moonY / 255.0f);
color.a = 255;
Circle::setColor(color);
}
Circle::draw(xpos, ypos, r);
}
void BackGround::drawMounten(size_t mountenSegments, int min, int max, Color color, float scale)
{
float x = 0;
float diff = (float)canvasSize / (mountenSegments - 1);
float ny = mrand::getFloat(&mountenSeed);
float nz = mrand::getFloat(&mountenSeed);
int offsetX = mrand::getFloat(&mountenSeed) * 255;
float nx = (float)(0 + offsetX) * (scale / (float)mountenSegments);
float p = stb_perlin_fbm_noise3(nx, ny, nz, 2.0f, 0.5f, 6);
float np = (p + 1.0f) / 2.0f;
int y = Lerp(min, max, np);
rlSetTexture(1);
rlBegin(RL_TRIANGLES);
rlNormal3f(0.0f, 0.0f, 1.0f);
rlColor4ub(color.r, color.g, color.b, color.a);
for (size_t i = 1; i <= mountenSegments; i++)
{
// topLeft
rlVertex2f(x, y);
// bottomLeft
rlVertex2f(x, canvasSize);
nx = (float)(i + offsetX) * (scale / (float)mountenSegments);
p = stb_perlin_fbm_noise3(nx, ny, nz, 2.0f, 0.5f, 6);
if (p < -1.0f)
p = -1.0f;
if (p > 1.0f)
p = 1.0f;
np = (p + 1.0f) / 2.0f;
y = Lerp(min, max, np);
x += diff;
// topRight
rlVertex2f(x, y);
// bottomRight
rlVertex2f(x, canvasSize);
// topRight
rlVertex2f(x, y);
rlVertex2f(x - diff, canvasSize);
}
rlEnd();
rlSetTexture(0);
}
int BackGround::getColorSet()
{
uint8_t colorSet = m_dna->colorSet;
if (colorSet < 64)
return 0;
if (colorSet < 128)
return 1;
if (colorSet < 192)
return 2;
return 3;
}
int BackGround::getTime()
{
uint8_t ret = m_dna->moonY;
if (ret < 64)
return 0;
if (ret < 128)
return 1;
if (ret < 192)
return 2;
return 3;
}

View File

@@ -0,0 +1,104 @@
#include "canvas/BackGroundColors.hpp"
Color BackGroundColors::backGroundColor;
Color BackGroundColors::MountenColor1;
Color BackGroundColors::MountenColor2;
Color BackGroundColors::MountenColor3;
void BackGroundColors::setColor(int color, int time)
{
if (color == 3)
{
backGroundColor = {21, 34, 56, 255};
MountenColor1 = {28, 28, 38, 255};
MountenColor2 = {21, 21, 27, 255};
MountenColor3 = {17, 17, 20, 255};
return;
}
Color colors[3][4][4] = {
{
{
{90, 60, 60, 255},
{118, 91, 91, 255},
{154, 131, 131, 255},
{193, 161, 161, 255},
},
{
{90, 60, 60, 255},
{118, 91, 91, 255},
{154, 131, 131, 255},
{193, 161, 161, 255},
},
{
{70, 40, 40, 255},
{98, 71, 71, 255},
{134, 111, 111, 255},
{173, 141, 141, 255},
},
{
{60, 30, 30, 255},
{88, 61, 61, 255},
{124, 101, 101, 255},
{163, 131, 131, 255},
},
},
{
{
{103, 88, 51, 255},
{148, 127, 73, 255},
{186, 159, 92, 255},
{163, 154, 132, 255},
},
{
{103, 88, 51, 255},
{148, 127, 73, 255},
{186, 159, 92, 255},
{163, 154, 132, 255},
},
{
{83, 68, 31, 255},
{128, 107, 53, 255},
{166, 139, 72, 255},
{143, 134, 112, 255},
},
{
{73, 58, 21, 255},
{118, 97, 43, 255},
{156, 129, 62, 255},
{133, 124, 102, 255},
},
},
{
{
{77, 84, 99, 255},
{104, 126, 144, 255},
{152, 219, 206, 255},
{213, 240, 235, 255},
},
{
{81, 58, 36, 255},
{111, 98, 78, 255},
{180, 148, 119, 255},
{200, 165, 133, 255},
},
{
{67, 44, 22, 255},
{97, 84, 64, 255},
{166, 134, 105, 255},
{186, 151, 119, 255},
},
{
{46, 23, 1, 255},
{76, 63, 43, 255},
{145, 113, 84, 255},
{165, 130, 98, 255},
},
},
};
backGroundColor = colors[color][0][time];
MountenColor1 = colors[color][1][time];
MountenColor2 = colors[color][2][time];
MountenColor3 = colors[color][3][time];
}

View File

@@ -0,0 +1,34 @@
#include "canvas/Canvas.hpp"
#include "canvas/Circle.hpp"
void Canvas::init(int size)
{
backGround.init(size);
tree.init(size);
Circle::init();
}
void Canvas::newGen(RenderTexture2D &target, Dna *dna)
{
BeginTextureMode(target);
ClearBackground(WHITE);
backGround.draw(dna);
tree.draw(dna);
EndTextureMode();
}
bool Canvas::tick(RenderTexture2D &target)
{
BeginTextureMode(target);
bool ret = tree.tick();
EndTextureMode();
return ret;
}
void Canvas::deinit()
{
backGround.deinit();
Circle::deinit();
}

View File

@@ -0,0 +1,83 @@
#include "canvas/Circle.hpp"
#include "sunShader.hpp"
#include <raylib.h>
#include <rlgl.h>
RenderTexture2D Circle::target;
Shader Circle::shader;
float Circle::radius;
float Circle::start_transperency;
float Circle::c[3];
int Circle::radius_loc;
int Circle::start_transperency_loc;
int Circle::colorLoc;
void Circle::init()
{
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_WEB)
shader = LoadShaderFromMemory(0, sun_shader_100);
#else
shader = LoadShaderFromMemory(0, (const char *)shaders_sun_330_fs);
#endif
target = LoadRenderTexture(sizeTexute, sizeTexute);
radius = 0.6f;
radius_loc = GetShaderLocation(shader, "sun_radius");
SetShaderValue(shader, radius_loc, &radius, SHADER_UNIFORM_FLOAT);
start_transperency = 0.40f;
start_transperency_loc = GetShaderLocation(shader, "start_transperency");
SetShaderValue(shader, start_transperency_loc, &start_transperency, SHADER_UNIFORM_FLOAT);
c[0] = 1.0f;
c[1] = 1.0f;
c[2] = 1.0f;
colorLoc = GetShaderLocation(shader, "color");
SetShaderValue(shader, colorLoc, c, SHADER_UNIFORM_VEC3);
// resitev da ne postane tekstura okoli sonca transparentna in da se ne vidi nasledna slika
// https://github.com/raysan5/raylib/issues/3820
rlSetBlendFactorsSeparate(RL_SRC_ALPHA, RL_ONE_MINUS_SRC_ALPHA, RL_ONE, RL_ONE, RL_FUNC_ADD, RL_MAX);
}
void Circle::deinit()
{
UnloadShader(shader);
UnloadRenderTexture(target);
}
void Circle::setColor(Color color)
{
c[0] = color.r / 255.0f;
c[1] = color.g / 255.0f;
c[2] = color.b / 255.0f;
SetShaderValue(shader, colorLoc, c, SHADER_UNIFORM_VEC3);
}
void Circle::setSoftEdge(bool soft)
{
if (soft)
{
radius = 0.6f;
}
else
{
radius = 1.0f;
}
SetShaderValue(shader, radius_loc, &radius, SHADER_UNIFORM_FLOAT);
}
void Circle::draw(float x, float y, float size)
{
Rectangle dest = {x - size, y - size, size * 2, size * 2};
Rectangle source = {0, 0, (float)target.texture.width, (float)-target.texture.height};
Vector2 origin = {0.0f, 0.0f};
// zgorni komentar da se mesanje barv oklopi pravilno
BeginBlendMode(RL_BLEND_CUSTOM_SEPARATE);
BeginShaderMode(shader);
DrawTexturePro(target.texture, source, dest, origin, 0.0f, WHITE);
EndShaderMode();
EndBlendMode();
}

206
shared/src/canvas/Tree.cpp Normal file
View File

@@ -0,0 +1,206 @@
#include <cmath>
#include "canvas/Tree.hpp"
#include "canvas/Circle.hpp"
#include <raylib.h>
#include <raymath.h>
#define ITER_PER_FRAME 5000
constexpr int maxColorChange = 15;
constexpr int minColorChange = -15;
constexpr float colorParentMix = 0.6f;
constexpr int maxSize = 20;
constexpr int minSize = 2;
constexpr int maxSizeVar = 5;
constexpr int minSizeVar = -5;
constexpr int maxSizeChange = 5;
constexpr int MinSizeChange = -5;
constexpr int sizes[] = {2, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
static_assert(sizeof(sizes) / sizeof(int) == MAX_POSIBLE_DEPTH);
float lengths[MAX_DEPTH];
constexpr float maxAngles[] = {5.0f, 5.0f, 5.0f, 10.0f, 10.0f, 10.0f, 15.0f, 15.0f, 20.0f, 20.0f, 20.0f};
static_assert(sizeof(maxAngles) / sizeof(float) == MAX_POSIBLE_DEPTH);
void calculateLevels(int canvasSize)
{
lengths[0] = canvasSize / 4.0f;
for (size_t i = 1; i < MAX_DEPTH; i++)
{
lengths[i] = lengths[i - 1] * 0.7f;
}
}
// Public
void Tree::init(int size)
{
this->canvasSize = size;
start.x = size / 2;
start.y = size;
calculateLevels(size);
}
void Tree::draw(Dna *dna)
{
Circle::setSoftEdge(false);
m_dna = dna;
branchSeed = dna->branchSeed;
drawCalls.push_back({start, 180.0f, 0});
tick();
}
bool Tree::tick()
{
size_t i = 0;
while (!drawCalls.empty())
{
drawBranch();
drawCalls.pop_front();
i++;
if (i >= ITER_PER_FRAME)
break;
}
return drawCalls.empty();
}
// Private
void Tree::drawBranch()
{
DrawArgs arg = drawCalls.front();
if (arg.dep == MAX_DEPTH)
return;
float angleVar = getAngleVar(arg);
float angle = ((arg.angleDeg + angleVar) * PI) / 180.0f;
float length = getLength(arg);
float nx = length * std::sin(angle);
float ny = length * std::cos(angle);
Vector2 end = {arg.start.x + nx, arg.start.y + ny};
int sizeStart = getStartSize(arg);
int sizeEnd = getEndSize(arg, sizeStart);
float fstep = 1.0 / ((length / sizeStart) * 2.0f);
Color colorStart = getStartColor(arg);
Color colorEnd = getEndColor(arg.dep, colorStart);
for (float i = 0; i < 1; i += fstep)
{
Vector2 point = Vector2Lerp(arg.start, end, i);
Color color = ColorLerp(colorStart, colorEnd, i);
int size = Lerp(sizeStart, sizeEnd, i);
DrawCircleV(point, size, color); // Fester on the phone to call DrawCircle insted of the Circle shader
// Circle::setColor(color);
// Circle::draw(point.x, point.y, thick); // TODO Change to BeginShaderMode and EndShaderMode only onece
// use
// DrawRectangleGradientEx
}
// add more branches to draw
if (arg.dep + 1 >= MAX_DEPTH)
return;
float sectors = getNumOfBranches(arg.dep) + 1;
float degres = 180.0f / sectors;
for (size_t i = 0; i < getNumOfBranches(arg.dep); i++)
{
float newAngle = arg.angleDeg - 90 + (degres * (i + 1));
drawCalls.push_back({end, newAngle, arg.dep + 1, colorEnd, sizeEnd});
}
}
inline size_t Tree::getNumOfBranches(int dep)
{
if (m_dna->branches[dep].branchCount < 128)
return 2;
else
return 3;
}
inline Color Tree::getStartColor(DrawArgs &arg)
{
Color ret = {
m_dna->branches[arg.dep].colorR,
m_dna->branches[arg.dep].colorG,
m_dna->branches[arg.dep].colorB,
255};
if (arg.dep > 0)
{
ret = ColorLerp(ret, arg.parent, colorParentMix);
}
int colorVar = Remap(m_dna->branches[arg.dep].colorVar, 0, 255, minColorChange, maxColorChange);
ret.r += colorVar * mrand::getFloat(&branchSeed);
ret.g += colorVar * mrand::getFloat(&branchSeed);
ret.b += colorVar * mrand::getFloat(&branchSeed);
return ret;
}
inline Color Tree::getEndColor(int dep, Color &start)
{
uint8_t r = start.r + m_dna->branches[dep].colorR_change;
uint8_t g = start.g + m_dna->branches[dep].colorG_change;
uint8_t b = start.b + m_dna->branches[dep].colorB_change;
return {r, g, b, 255};
}
inline int Tree::getStartSize(DrawArgs &arg)
{
int size = Remap(m_dna->branches[arg.dep].size, 0, 255, minSize, maxSize);
size += Remap(m_dna->branches[arg.dep].sizeVar, 0, 255, minSizeVar, maxSizeVar) * mrand::getFloat(&branchSeed);
if (arg.dep > 0)
{
float sizeParent = m_dna->branches[arg.dep].sizeParent / 255.0f;
size = std::lerp(size, arg.size, sizeParent);
}
float mixLevel = m_dna->branches[arg.dep].sizeLevel / 255.0f;
size = std::lerp(size, sizes[MAX_DEPTH - arg.dep - 1], mixLevel);
if (size < 1)
size = 1;
return size;
}
inline int Tree::getEndSize(DrawArgs &arg, int start)
{
int size = Remap(m_dna->branches[arg.dep].sizeChange, 0, 255, MinSizeChange, maxSizeChange);
size += start;
if (size < 1)
size = 1;
return size;
}
inline float Tree::getLength(DrawArgs &arg)
{
float lenght = lengths[arg.dep];
float lenghtRatio = Remap(m_dna->branches[arg.dep].length, 0, 255, 0.5f, 1.3f);
lenght *= lenghtRatio;
float lenghtVar = Remap(m_dna->branches[arg.dep].lengthVar, 0, 255, -0.15f, 0.15f);
lenght += lenght * lenghtVar * mrand::getFloat(&branchSeed);
if (lenght < 1)
lenght = 1;
return lenght;
}
inline float Tree::getAngleVar(DrawArgs &arg)
{
float angleVar = Remap(m_dna->branches[arg.dep].branchAngleVar, 0, 255, 0.0f, maxAngles[arg.dep]);
angleVar = Lerp(angleVar, -angleVar, mrand::getFloat(&branchSeed));
return angleVar;
}