restructure and add server/client example
This commit is contained in:
169
shared/src/TcpSocket.cpp
Normal file
169
shared/src/TcpSocket.cpp
Normal file
@@ -0,0 +1,169 @@
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <netdb.h>
|
||||
|
||||
#include <string>
|
||||
#include <functional>
|
||||
#include <cerrno>
|
||||
#include <cstring>
|
||||
#include <thread>
|
||||
|
||||
namespace TcpSocket
|
||||
{
|
||||
|
||||
void setTimeout(int seconds, int sock)
|
||||
{
|
||||
struct timeval tv;
|
||||
tv.tv_sec = seconds;
|
||||
tv.tv_usec = 0;
|
||||
|
||||
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv));
|
||||
setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(tv));
|
||||
}
|
||||
|
||||
ssize_t sendt(int sock, const void *bytes, size_t byteslength) { return send(sock, bytes, byteslength, 0); }
|
||||
|
||||
ssize_t recvt(int sock, void *bytes, size_t byteslength) { return recv(sock, bytes, byteslength, 0); }
|
||||
|
||||
void closet(int sock)
|
||||
{
|
||||
shutdown(sock, SHUT_RDWR);
|
||||
close(sock);
|
||||
}
|
||||
|
||||
std::string remoteAddress(sockaddr_in &address)
|
||||
{
|
||||
char ip[INET_ADDRSTRLEN];
|
||||
inet_ntop(AF_INET, &(address.sin_addr), ip, INET_ADDRSTRLEN);
|
||||
|
||||
return std::string(ip);
|
||||
}
|
||||
|
||||
int remotePort(sockaddr_in &address) { return ntohs(address.sin_port); }
|
||||
|
||||
int connectt(const char *host, uint16_t port)
|
||||
{
|
||||
struct addrinfo hints, *res, *it;
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = AF_INET;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
|
||||
// Get address info from DNS
|
||||
int status = getaddrinfo(host, NULL, &hints, &res);
|
||||
if (status != 0)
|
||||
{
|
||||
// onError(errno, "Invalid address." + std::string(gai_strerror(status)));
|
||||
return -1;
|
||||
}
|
||||
|
||||
sockaddr_in address;
|
||||
for (it = res; it != NULL; it = it->ai_next)
|
||||
{
|
||||
if (it->ai_family == AF_INET)
|
||||
{ // IPv4
|
||||
memcpy((void *)(&address), (void *)it->ai_addr, sizeof(sockaddr_in));
|
||||
break; // for now, just get the first ip (ipv4).
|
||||
}
|
||||
}
|
||||
|
||||
freeaddrinfo(res);
|
||||
|
||||
int sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
|
||||
if (sock == -1)
|
||||
{
|
||||
// onError(errno, "Socket creating error.");
|
||||
return -2;
|
||||
}
|
||||
|
||||
address.sin_family = AF_INET;
|
||||
address.sin_port = htons(port);
|
||||
|
||||
// setTimeout(5, sock);
|
||||
|
||||
// Try to connect.
|
||||
status = connect(sock, (const sockaddr *)&address, sizeof(sockaddr_in));
|
||||
if (status == -1)
|
||||
{
|
||||
// onError(errno, "Connection failed to the host.");
|
||||
close(sock);
|
||||
return -3;
|
||||
}
|
||||
return sock;
|
||||
}
|
||||
|
||||
typedef std::function<void(int sock, sockaddr_in newSocketInfo)> OnNewConnectionCallBack;
|
||||
|
||||
int listent(const char *host, uint16_t port, OnNewConnectionCallBack callback)
|
||||
{
|
||||
sockaddr_in address;
|
||||
|
||||
int sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
|
||||
if (sock == -1)
|
||||
{
|
||||
// onError(errno, "Socket creating error.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int opt = 1;
|
||||
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int));
|
||||
setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(int));
|
||||
|
||||
int status = inet_pton(AF_INET, host, &address.sin_addr);
|
||||
switch (status)
|
||||
{
|
||||
case -1:
|
||||
close(sock);
|
||||
// onError(errno, "Invalid address. Address type not supported.");
|
||||
return -2;
|
||||
case 0:
|
||||
close(sock);
|
||||
// onError(errno, "AF_INET is not supported. Please send message to developer.");
|
||||
return -3;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
address.sin_family = AF_INET;
|
||||
address.sin_port = htons(port);
|
||||
|
||||
if (bind(sock, (const sockaddr *)&address, sizeof(address)) == -1)
|
||||
{
|
||||
// onError(errno, "Cannot bind the socket.");
|
||||
close(sock);
|
||||
return -4;
|
||||
}
|
||||
if (listen(sock, 20) == -1)
|
||||
{
|
||||
// onError(errno, "Error: Server can't listen the socket.");
|
||||
close(sock);
|
||||
return -5;
|
||||
}
|
||||
|
||||
sockaddr_in newSocketInfo;
|
||||
socklen_t newSocketInfoLength = sizeof(newSocketInfo);
|
||||
|
||||
int newSocketFileDescriptor = -1;
|
||||
while (true)
|
||||
{
|
||||
newSocketFileDescriptor = accept(sock, (sockaddr *)&newSocketInfo, &newSocketInfoLength);
|
||||
if (newSocketFileDescriptor == -1)
|
||||
{
|
||||
if (errno == EBADF || errno == EINVAL)
|
||||
return -6;
|
||||
|
||||
return -7;
|
||||
}
|
||||
|
||||
std::thread t(callback, newSocketFileDescriptor, newSocketInfo);
|
||||
t.detach();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
226
shared/src/canvas/BackGround.cpp
Normal file
226
shared/src/canvas/BackGround.cpp
Normal 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;
|
||||
}
|
104
shared/src/canvas/BackGroundColors.cpp
Normal file
104
shared/src/canvas/BackGroundColors.cpp
Normal 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];
|
||||
}
|
34
shared/src/canvas/Canvas.cpp
Normal file
34
shared/src/canvas/Canvas.cpp
Normal 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();
|
||||
}
|
83
shared/src/canvas/Circle.cpp
Normal file
83
shared/src/canvas/Circle.cpp
Normal 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
206
shared/src/canvas/Tree.cpp
Normal 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;
|
||||
}
|
65
shared/src/values/Dna.cpp
Normal file
65
shared/src/values/Dna.cpp
Normal file
@@ -0,0 +1,65 @@
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
|
||||
#include "values/Dna.hpp"
|
||||
|
||||
namespace DNA
|
||||
{
|
||||
|
||||
void newDna(Dna *dna, uint128 *state)
|
||||
{
|
||||
uint8_t *array = (uint8_t *)dna;
|
||||
for (size_t i = 0; i < sizeof(Dna); i++)
|
||||
{
|
||||
array[i] = mrand::getValue(0, 255, state);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
void makeChild(Dna *p1, Dna *p2, Dna *c, uint128 *state)
|
||||
{
|
||||
uint8_t *p1a = (uint8_t *)p1;
|
||||
uint8_t *p2a = (uint8_t *)p2;
|
||||
uint8_t *ca = (uint8_t *)c;
|
||||
for (size_t i = 0; i < sizeof(Dna); i++)
|
||||
{
|
||||
int val = mrand::getValue(0, 1, state);
|
||||
if (val == 0)
|
||||
{
|
||||
ca[i] = p1a[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
ca[i] = p2a[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void clone(Dna *p1, Dna *c, uint128 *state)
|
||||
{
|
||||
uint8_t *p1a = (uint8_t *)p1;
|
||||
|
||||
uint8_t *ca = (uint8_t *)c;
|
||||
for (size_t i = 0; i < sizeof(Dna); i++)
|
||||
{
|
||||
int val = mrand::getValue(0, 1, state);
|
||||
if (val == 0)
|
||||
{
|
||||
ca[i] = p1a[i];
|
||||
}
|
||||
else
|
||||
{
|
||||
ca[i] = mrand::getValue(0, 255, state);
|
||||
}
|
||||
}
|
||||
}
|
||||
void mutate(Dna *dna, uint32_t num, uint128 *state)
|
||||
{
|
||||
uint8_t *array = (uint8_t *)dna;
|
||||
for (size_t i = 0; i < num; i++)
|
||||
{
|
||||
int pos = mrand::getValue(0, sizeof(Dna), state);
|
||||
array[pos] = mrand::getValue(0, 255, state);
|
||||
}
|
||||
}
|
||||
}
|
112
shared/src/values/DnaManager.cpp
Normal file
112
shared/src/values/DnaManager.cpp
Normal file
@@ -0,0 +1,112 @@
|
||||
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
#include "values/DnaManager.hpp"
|
||||
|
||||
UiUnit DnaManager::next(DnaManagerData *data)
|
||||
{
|
||||
if (data->queued >= NUM_PER_GEN)
|
||||
{
|
||||
return {nullptr, Liked::tbd, -1};
|
||||
}
|
||||
|
||||
Dna *ret = &data->vector[data->queued];
|
||||
int index = data->queued++;
|
||||
return {ret, Liked::tbd, index};
|
||||
}
|
||||
|
||||
bool DnaManager::like(UiUnit unit, DnaManagerData *data)
|
||||
{
|
||||
int found = -1;
|
||||
if (unit.index == data->showed)
|
||||
{
|
||||
found = data->showed;
|
||||
}
|
||||
|
||||
if (found == -1)
|
||||
{
|
||||
// RUN OUT OF GEN WAITING FOR NEW GEN
|
||||
return false;
|
||||
}
|
||||
if (unit.liked == Liked::yes)
|
||||
{
|
||||
data->liked.push_back(found);
|
||||
}
|
||||
else if (unit.liked == Liked::no)
|
||||
{
|
||||
data->disliked.push_back(found);
|
||||
}
|
||||
else
|
||||
{
|
||||
// could be infinite loop if something went wrong and user lost UiUnit and next is returning null thinking that it queued everiting
|
||||
// maybe return true to move on and maybe tread Liked::tbd as Liked::no
|
||||
return false;
|
||||
}
|
||||
|
||||
data->showed++;
|
||||
|
||||
if (data->showed >= NUM_PER_GEN && data->queued >= NUM_PER_GEN) // if buffer was biger in the past showed could be more then NUM_PER_GEN so its changed to >= insted of ==
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void DnaManager::newGen(DnaManagerData *data)
|
||||
{
|
||||
data->queued = 0;
|
||||
data->showed = 0;
|
||||
data->generation += 1;
|
||||
if (data->liked.size() == 0)
|
||||
{
|
||||
for (std::size_t i = 0; i < NUM_PER_GEN; i++)
|
||||
{
|
||||
DNA::newDna(&data->vector[i], &data->randSeed);
|
||||
}
|
||||
data->disliked.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
if (data->liked.size() == 1)
|
||||
{
|
||||
int front = data->liked.front();
|
||||
|
||||
for (auto &&i : data->disliked)
|
||||
{
|
||||
DNA::clone(&data->vector[front], &data->vector[i], &data->randSeed);
|
||||
}
|
||||
|
||||
data->disliked.clear();
|
||||
data->liked.clear();
|
||||
}
|
||||
|
||||
if (data->liked.size() >= 2)
|
||||
{
|
||||
for (auto &&i : data->disliked)
|
||||
{
|
||||
|
||||
size_t p1 = mrand::getValue(0, data->liked.size() - 1, &data->randSeed);
|
||||
size_t p2 = mrand::getValue(0, data->liked.size() - 1, &data->randSeed);
|
||||
while (p1 == p2)
|
||||
{
|
||||
p2 = mrand::getValue(0, data->liked.size(), &data->randSeed);
|
||||
}
|
||||
|
||||
p1 = data->liked[p1];
|
||||
p2 = data->liked[p2];
|
||||
Dna *p1p = &data->vector[p1];
|
||||
Dna *p2p = &data->vector[p2];
|
||||
Dna *c = &data->vector[i];
|
||||
|
||||
DNA::makeChild(p1p, p2p, c, &data->randSeed);
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < NUM_PER_GEN; i++)
|
||||
{
|
||||
DNA::mutate(&data->vector[i], NUM_OF_MUT, &data->randSeed);
|
||||
}
|
||||
data->disliked.clear();
|
||||
data->liked.clear();
|
||||
}
|
63
shared/src/values/mrand.cpp
Normal file
63
shared/src/values/mrand.cpp
Normal file
@@ -0,0 +1,63 @@
|
||||
#include <cinttypes>
|
||||
#include <algorithm>
|
||||
#include "values/mrand.hpp"
|
||||
|
||||
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 &rprand_seed)
|
||||
{
|
||||
uint64_t z = (rprand_seed += 0x9e3779b97f4a7c15);
|
||||
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
|
||||
z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
|
||||
return z ^ (z >> 31);
|
||||
}
|
||||
|
||||
namespace mrand
|
||||
{
|
||||
uint128 getState(unsigned long long seed)
|
||||
{
|
||||
uint64_t rprand_seed = (uint64_t)seed; // Set SplitMix64 seed for further use
|
||||
|
||||
uint128 rprand_state;
|
||||
|
||||
// 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.a = (uint32_t)(rprand_splitmix64(rprand_seed) & 0xffffffff);
|
||||
rprand_state.b = (uint32_t)((rprand_splitmix64(rprand_seed) & 0xffffffff00000000) >> 32);
|
||||
rprand_state.c = (uint32_t)(rprand_splitmix64(rprand_seed) & 0xffffffff);
|
||||
rprand_state.d = (uint32_t)((rprand_splitmix64(rprand_seed) & 0xffffffff00000000) >> 32);
|
||||
return rprand_state;
|
||||
}
|
||||
|
||||
int getValue(int min, int max, uint128 *state)
|
||||
{
|
||||
int value = my_rprand_xoshiro((uint32_t *)state) % (std::abs(max - min) + 1) + min;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
float getFloat(uint128 *state)
|
||||
{
|
||||
return my_rprand_xoshiro((uint32_t *)state) / 4294967295.0f;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user