Save point for converion of colors

This commit is contained in:
Nikola Petrov 2024-12-30 15:36:54 +01:00
parent 28b3d334c1
commit 46a739ae28
8 changed files with 279 additions and 25 deletions

View File

@ -1,4 +1,5 @@
#include <raylib.h>
Color ColorLerp(Color c1, Color c2, float amount);
Color ColorAdd(Color c, int add);
Color ColorAdd(Color c1, Color c2);
Color ColorAddValue(Color c, int add);

View File

@ -25,12 +25,16 @@ public:
private:
Dna *m_dna;
uint128 branchSeed;
int size = 0;
Vector2 start = {0};
std::list<DrawArgs> draw_calls;
void generateBranches();
void drawBranch();
Vector2 drawLine();
inline uint8_t get_num_of_branches(uint8_t dep);
inline Color get_start_color(uint8_t dep);
inline Color get_end_color(uint8_t dep, Color &start);
};

View File

@ -28,9 +28,9 @@ struct Branch
uint8_t colorR;
uint8_t colorG;
uint8_t colorB;
uint8_t colorR_change;
uint8_t colorG_change;
uint8_t colorB_change;
int8_t colorR_change;
int8_t colorG_change;
int8_t colorB_change;
uint8_t color_parent;
uint8_t color_var;

142
random/netw.cpp Normal file
View File

@ -0,0 +1,142 @@
#include <raylib.h>
#include <string>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <fstream>
#include <cstring>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#define PORT 8080
#define BUFFER_SIZE 1024
struct in_addr ipAddr;
bool resolved = false;
bool resolveDNS()
{
const char *host = "petrovv.com";
struct addrinfo hints{}, *res, *p;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // AF_INET or AF_INET6 to force version
hints.ai_socktype = SOCK_STREAM;
int status = getaddrinfo(host, nullptr, &hints, &res);
if (status != 0)
{
return false;
}
for (p = res; p != nullptr; p = p->ai_next)
{
if (p->ai_family == AF_INET)
{ // IPv4
auto *ipv4 = (struct sockaddr_in *)p->ai_addr;
ipAddr = ipv4->sin_addr;
resolved = true;
}
break; // Just take the first address
}
freeaddrinfo(res);
return true;
}
int sendFile()
{
int sock = 0;
struct sockaddr_in serv_addr;
char buffer[BUFFER_SIZE] = {0};
std::ifstream file("example.txt", std::ios::binary);
if (!file)
{
// std::cerr << "Unable to open file" << std::endl;
TraceLog(LOG_ERROR, "Unable to open file");
return 1;
}
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
TraceLog(LOG_ERROR, "Socket creation error");
return 1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 and IPv6 addresses from text to binary form
if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0)
{
TraceLog(LOG_ERROR, "Invalid address/ Address not supported");
return 1;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
TraceLog(LOG_ERROR, "Connection Failed");
return 1;
}
while (file.read(buffer, BUFFER_SIZE))
{
send(sock, buffer, file.gcount(), 0);
}
// Send the remaining bytes if the file size is not a multiple of BUFFER_SIZE
send(sock, buffer, file.gcount(), 0);
TraceLog(LOG_INFO, "File sent successfully");
close(sock);
file.close();
return 0;
}
int sendBuffer()
{
if (!resolved)
return 1;
int sock = 0;
struct sockaddr_in serv_addr;
char buffer[BUFFER_SIZE] = {123, 123, 123};
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
TraceLog(LOG_ERROR, "Socket creation error");
return 1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
serv_addr.sin_addr = ipAddr;
// Convert IPv4 and IPv6 addresses from text to binary form
// if (inet_pton(AF_INET, "192.168.0.31", &serv_addr.sin_addr) <= 0)
// {
// TraceLog(LOG_ERROR, "Invalid address/ Address not supported");
// return 1;
// }
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
TraceLog(LOG_ERROR, "Connection Failed");
return 1;
}
send(sock, buffer, 3, 0);
TraceLog(LOG_INFO, "File sent successfully");
close(sock);
return 0;
}

68
random/server.cpp Normal file
View File

@ -0,0 +1,68 @@
#include <iostream>
#include <fstream>
#include <cstring>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#define PORT 8080
#define BUFFER_SIZE 1024
// use this https://github.com/eminfedar/async-sockets-cpp/tree/master
int main()
{
int server_fd, new_socket;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[BUFFER_SIZE] = {0};
std::ofstream file("received_example.txt", std::ios::binary);
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
{
std::cerr << "Socket failed" << std::endl;
return 1;
}
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt)))
{
std::cerr << "setsockopt" << std::endl;
return 1;
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0)
{
std::cerr << "Bind failed" << std::endl;
return 1;
}
if (listen(server_fd, 3) < 0)
{
std::cerr << "Listen failed" << std::endl;
return 1;
}
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t *)&addrlen)) < 0)
{
std::cerr << "Accept failed" << std::endl;
return 1;
}
int valread;
while ((valread = recv(new_socket, buffer, BUFFER_SIZE, 0)) > 0)
{
file.write(buffer, valread);
}
std::cout << "File received successfully" << std::endl;
close(new_socket);
close(server_fd);
file.close();
return 0;
}

View File

@ -16,11 +16,21 @@ Color ColorLerp(Color c1, Color c2, float amount)
return ret;
}
Color ColorAdd(Color c, int add)
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};
}

View File

@ -38,7 +38,7 @@ void BackGround::draw(Dna *dna)
}
else
{
DrawRectangleGradientV(0, 0, canvasSize, canvasSize, ColorAdd(BackGroundColors::backGroundColor, 60), BackGroundColors::backGroundColor);
DrawRectangleGradientV(0, 0, canvasSize, canvasSize, ColorAddValue(BackGroundColors::backGroundColor, 60), BackGroundColors::backGroundColor);
}
drawSun();

View File

@ -9,6 +9,8 @@
#define ITER_PER_FRAME 5000
constexpr int max_num_of_branches = 3;
// Public
void Tree::init(int size)
{
@ -22,6 +24,7 @@ void Tree::draw(Dna *dna)
Circle::setSoftEdge(false);
m_dna = dna;
branchSeed = dna->branchSeed;
draw_calls.push_back({start, 0, (float)size / 4, 1});
tick();
}
@ -56,34 +59,24 @@ Vector2 Tree::drawLine()
float thick = 2.0;
float fstep = 1.0 / ((arg.lenghth / thick) * 1.5);
Color colorParent = {
m_dna->branches[arg.dep - 1].colorR,
m_dna->branches[arg.dep - 1].colorG,
m_dna->branches[arg.dep - 1].colorB,
255};
Color colorStart = get_start_color(arg.dep);
Color colorM = {
m_dna->branches[arg.dep].colorR,
m_dna->branches[arg.dep].colorG,
m_dna->branches[arg.dep].colorB,
255};
Color colorEnd = get_end_color(arg.dep, colorStart);
for (float i = 0; i < 1; i += fstep)
{
Vector2 point = Vector2Lerp(arg.start, end, i);
Color color = ColorLerp(colorParent, colorM, i);
Color color = ColorLerp(colorStart, colorEnd, i);
DrawCircleV(point, thick, 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
}
return end;
}
inline uint8_t get_num_of_branches(uint8_t count)
{
return ((float)count / 255.0f) * 3 + 1;
}
void Tree::drawBranch()
{
DrawArgs arg = draw_calls.front();
@ -93,12 +86,48 @@ void Tree::drawBranch()
Vector2 next = drawLine();
float next_len = 0.7f;
float sectors = get_num_of_branches(m_dna->branches[arg.dep].branch_count) + 1;
float sectors = get_num_of_branches(arg.dep) + 1;
float degres = 180.0f / sectors;
for (size_t i = 0; i < get_num_of_branches(m_dna->branches[arg.dep].branch_count); i++)
for (size_t i = 0; i < get_num_of_branches(arg.dep); i++)
{
float newAngle = arg.angleDeg - 90 + (degres * (i + 1));
draw_calls.push_back({next, newAngle, arg.lenghth * next_len, arg.dep + 1});
}
}
inline uint8_t Tree::get_num_of_branches(uint8_t dep)
{
return ((float)m_dna->branches[dep].branch_count / 255.0f) * max_num_of_branches + 1;
}
inline Color Tree::get_start_color(uint8_t dep)
{
Color ret = {
m_dna->branches[dep].colorR,
m_dna->branches[dep].colorG,
m_dna->branches[dep].colorB,
255};
if (dep > 0)
{
Color parent = {
m_dna->branches[dep - 1].colorR,
m_dna->branches[dep - 1].colorG,
m_dna->branches[dep - 1].colorB,
255};
float mix = ((float)m_dna->branches[dep].color_parent) / 255.0f;
ret = ColorLerp(ret, parent, mix);
}
return ret;
}
inline Color Tree::get_end_color(uint8_t dep, Color &start)
{
return {
start.r + m_dna->branches[dep].colorR_change,
start.g + m_dna->branches[dep].colorG_change,
start.b + m_dna->branches[dep].colorB_change,
255};
}