Compare commits

...

10 Commits

12 changed files with 282 additions and 93 deletions

3
.gitignore vendored
View File

@ -10,4 +10,5 @@ treender
*.sln
*.vcxproj*
Console*/
main
main
*.bin

View File

@ -43,12 +43,12 @@ int main(int argc, char const *argv[])
if (!std::filesystem::is_directory(RAYLIB_DIR))
{
command = {"wget", "https://github.com/raysan5/raylib/releases/download/5.0/raylib-5.0_linux_amd64.tar.gz"};
command = {"wget", "https://github.com/raysan5/raylib/releases/download/5.5/raylib-5.5_linux_amd64.tar.gz"};
run_command(command);
command = {"tar", "-xzvf", "raylib-5.0_linux_amd64.tar.gz"};
command = {"tar", "-xzvf", "raylib-5.5_linux_amd64.tar.gz"};
run_command(command);
std::filesystem::rename("raylib-5.0_linux_amd64", "raylib");
std::filesystem::remove_all("raylib-5.0_linux_amd64.tar.gz");
std::filesystem::rename("raylib-5.5_linux_amd64", "raylib");
std::filesystem::remove_all("raylib-5.5_linux_amd64.tar.gz");
}
if (!std::filesystem::is_directory(RAYLIB_DIR))

View File

@ -17,12 +17,27 @@ private:
int screenWidth, screenHeight;
Canvas canvas;
RenderTexture2D canvasTexure = {0};
Rectangle dest;
int pos = 0;
std::array<RenderTexture2D, 2> canvasTexure = {0};
float rotation = 0.0f;
Vector2 mouseStart;
bool validHit = false;
float len;
float ofset;
Liked topLiked = Liked::tbd;
Rectangle destA;
Rectangle destB;
Rectangle likeBox;
Rectangle disLikeBox;
Unit unit;
std::array<UiUnit, 2> unit = {0};
DnaManager manager;
Rectangle likedTextBox;
Rectangle genTextBox;
};

View File

@ -1,5 +1,5 @@
#include <raylib.h>
Color ColorLerp(Color c1, Color c2, float amount);
Color ColorAdd(Color c1, Color c2);
Color ColorAddValue(Color c, int add);
Color ColorAddValue(Color c, int add);
Rectangle TextInSpace(Rectangle box, float textH, float textW, float margin, bool topOrBottom);

View File

@ -27,22 +27,4 @@ private:
uint128 starSeed;
int canvasSize = 0;
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;
};

View File

@ -7,30 +7,40 @@
enum Liked
{
tbd,
yes,
no,
tbd
no
};
struct Unit
struct UiUnit
{
Dna *dna;
Liked liked;
int index;
};
struct NetUnit
{
uint128 hash;
uint32_t index;
Liked liked;
};
static_assert(24 == sizeof(NetUnit));
class DnaManager
{
public:
void init();
void deinit();
Unit next();
void like(Unit unit);
UiUnit next();
void like(UiUnit unit);
int generation;
private:
void saveData();
void saveVec();
void saveGen();
uint128 randSeed;
uint128 id;
int queued;

View File

@ -72,13 +72,7 @@ int sendFile()
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;
}
serv_addr.sin_addr = ipAddr;
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
@ -119,13 +113,6 @@ int sendBuffer()
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");

View File

@ -7,18 +7,61 @@
#include <raylib.h>
#include <raymath.h>
#define TOP (1 - pos)
#define BOTTOM pos
// Dimentions for font size 20
// DISLIKE 83
// LIKE 46
// GEN 9999: 999/999 -> 200
constexpr float textMargin = 0.02f;
void App::init(int screenWidth, int screenHeight)
{
this->screenWidth = screenWidth;
this->screenHeight = screenHeight;
this->canvas.init(screenWidth);
canvasTexure = LoadRenderTexture(screenWidth, screenWidth);
// int s = MeasureText("GEN 9999: 999/999", 20);
// TraceLog(LOG_INFO, "%d", s);
for (size_t i = 0; i < canvasTexure.size(); i++)
{
canvasTexure[i] = LoadRenderTexture(screenWidth, screenWidth);
}
manager.init();
upTex(Liked::tbd);
while (!canvas.tick(canvasTexure[TOP]))
{
// wait to finish drawing
}
pos = 1 - pos;
upTex(Liked::tbd);
while (!canvas.tick(canvasTexure[TOP]))
{
// wait to finish drawing
}
pos = 1 - pos;
float posY = (screenHeight - screenWidth) / 2.0f;
float recPosX = screenWidth * 0.2f;
dest = {0, posY, (float)screenWidth, (float)screenWidth};
likedTextBox = TextInSpace({0,
0,
(float)screenWidth / 2.0f,
posY},
20.0f, 83.0f, textMargin, false);
genTextBox = TextInSpace({0,
posY + screenWidth,
(float)screenWidth,
posY},
20.0f, 200.0f, textMargin, true);
destB = {0, posY, (float)screenWidth, (float)screenWidth};
destA = destB;
float recPosX = screenWidth * 0.3f;
disLikeBox = {0, posY, (float)recPosX, (float)screenWidth};
likeBox = {screenWidth - recPosX, posY, (float)recPosX, (float)screenWidth};
}
@ -27,36 +70,74 @@ void App::upTex(Liked liked)
{
if (liked != Liked::tbd)
{
unit.liked = liked;
manager.like(unit);
unit[TOP].liked = liked;
manager.like(unit[TOP]);
}
unit = manager.next();
if (unit.dna != nullptr)
unit[TOP] = manager.next();
if (unit[TOP].dna != nullptr)
{
canvas.newGen(canvasTexure, unit.dna);
canvas.newGen(canvasTexure[TOP], unit[TOP].dna);
return;
}
BeginTextureMode(canvasTexure);
ClearBackground(WHITE);
DrawText("NEXT GEN", 10, 10, 20, BLACK);
BeginTextureMode(canvasTexure[TOP]);
ClearBackground(BLACK);
DrawText("NEXT GEN", 10, 10, 20, WHITE);
EndTextureMode();
}
void App::update()
{
bool isDone = canvas.tick(canvasTexure);
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) && isDone)
bool isDone = canvas.tick(canvasTexure[BOTTOM]);
Vector2 mousePosition = GetMousePosition();
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
{
Vector2 mouse = GetMousePosition();
if (CheckCollisionPointRec(mouse, disLikeBox))
mouseStart = mousePosition;
validHit = CheckCollisionPointRec(mouseStart, destA);
len = Vector2Distance(mouseStart, {destB.x, destB.y});
ofset = std::atan2(destB.x - mouseStart.x, destB.y - mouseStart.y);
}
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT) && validHit)
{
float dist = mousePosition.x - mouseStart.x;
float l = dist / screenWidth;
rotation = Lerp(45.0f, -45.0f, (l + 1) / 2);
float angle = ((rotation)*PI) / 180.0f;
angle += ofset;
Vector2 newCenter = {.x = len * std::sin(angle), .y = len * std::cos(angle)};
destA.x = newCenter.x + mousePosition.x;
destA.y = newCenter.y + mousePosition.y;
if (CheckCollisionPointRec(mousePosition, disLikeBox))
{
upTex(Liked::no);
topLiked = Liked::no;
}
if (CheckCollisionPointRec(mouse, likeBox))
else if (CheckCollisionPointRec(mousePosition, likeBox))
{
upTex(Liked::yes);
topLiked = Liked::yes;
}
else
{
topLiked = Liked::tbd;
}
}
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT) && validHit)
{
if (isDone)
{
if (topLiked != Liked::tbd)
{
upTex(topLiked);
pos = 1 - pos; // switch bottom and top
}
}
rotation = 0.0f;
destA = destB;
topLiked = Liked::tbd;
}
}
@ -64,17 +145,35 @@ void App::draw()
{
ClearBackground(BLUE);
Rectangle source = {0, 0, (float)canvasTexure.texture.width, (float)-canvasTexure.texture.height};
Rectangle source = {0, 0, (float)screenWidth, (float)-screenWidth};
Vector2 origin = {0.0f, 0.0f};
DrawTexturePro(canvasTexure.texture, source, dest, origin, 0.0f, WHITE);
const char *text = TextFormat("GEN %d: %d / %d", manager.generation, unit.index + 1, NUM_PER_GEN);
DrawText(text, dest.x + 10, dest.y - 30, 20, BLACK);
DrawTexturePro(canvasTexure[BOTTOM].texture, source, destB, origin, 0.0f, WHITE);
DrawTexturePro(canvasTexure[TOP].texture, source, destA, origin, 360 - rotation, WHITE);
const char *text = TextFormat("GEN %d: %d / %d", manager.generation, unit[TOP].index + 1, NUM_PER_GEN);
DrawText(text, genTextBox.x, genTextBox.y, genTextBox.height, BLACK);
switch (topLiked)
{
case Liked::yes:
DrawText("LIKED", likedTextBox.x, likedTextBox.y, likedTextBox.height, BLACK);
break;
case Liked::no:
DrawText("DISLIKE", likedTextBox.x, likedTextBox.y, likedTextBox.height, BLACK);
break;
default:
break;
}
}
void App::deinit()
{
UnloadRenderTexture(canvasTexure);
for (size_t i = 0; i < canvasTexure.size(); i++)
{
UnloadRenderTexture(canvasTexure[i]);
}
canvas.deinit();
manager.deinit();
}

View File

@ -6,16 +6,6 @@
#include <raymath.h>
Color ColorLerp(Color c1, Color c2, float amount)
{
Color ret{0};
ret.r = Clamp(Lerp(c1.r, c2.r, amount), 0, 255);
ret.g = Clamp(Lerp(c1.g, c2.g, amount), 0, 255);
ret.b = Clamp(Lerp(c1.b, c2.b, amount), 0, 255);
ret.a = Clamp(Lerp(c1.a, c2.a, amount), 0, 255);
return ret;
}
Color ColorAddValue(Color c, int add)
{
int r = std::clamp(c.r + add, 0, 255);
@ -33,4 +23,42 @@ Color ColorAdd(Color c1, Color c2)
int a = std::clamp(c1.a + c2.a, 0, 255);
return {(unsigned char)r, (unsigned char)g, (unsigned char)b, (unsigned char)a};
}
Rectangle TextInSpace(Rectangle box, float textH, float textW, float margin, bool topOrBottom)
{
float br = box.width / box.height;
float tr = textW / textH;
Rectangle ret = {0, 0, 0, 0};
float hm = box.height * margin;
float wm = box.width * margin;
ret.height = box.height - hm;
ret.width = box.width - wm;
if (br < tr)
{
// bolj kvadrat izracunaj visino iz sirine
ret.height = ret.width / tr;
}
else
{
// bolj podolgovat izracunaj sirino iz visine
ret.width = ret.height * tr;
}
if (topOrBottom)
{
ret.y = box.y + hm;
}
else
{
ret.y = box.y + box.height - hm - ret.height;
}
ret.x = box.x + wm;
return ret;
}

View File

@ -10,6 +10,24 @@
#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;
// Public
void BackGround::init(int canvasSize)
{

View File

@ -7,21 +7,25 @@
#include <raylib.h>
#define ID_FILE_NAME "ID.bin"
#define DATA_FILE_NAME "DATA.bin"
#define VECTOR_FILE_NAME "VECTOR.bin"
void DnaManager::init()
{
if (sys::fileExists("id"))
if (sys::fileExists(ID_FILE_NAME))
{
sys::loadDataFromFile("id", &id, sizeof(uint128));
sys::loadDataFromFile(ID_FILE_NAME, &id, sizeof(uint128));
}
else
{
id = mrand::getState(time(nullptr));
sys::saveDataToFile("id", &id, sizeof(uint128));
sys::saveDataToFile(ID_FILE_NAME, &id, sizeof(uint128));
}
if (sys::fileExists("data"))
if (sys::fileExists(DATA_FILE_NAME))
{
const char *filename = sys::transformFilePath("data");
const char *filename = sys::transformFilePath(DATA_FILE_NAME);
FILE *file = fopen(filename, "rb");
if (file == NULL)
@ -37,6 +41,8 @@ void DnaManager::init()
for (int i = 0; i < size; i++)
{
fread(&tmp, sizeof(tmp), 1, file);
if (tmp >= NUM_PER_GEN) // out of bounds error prevention if buffer was in the past biiger then its now
continue;
liked.push_back(tmp);
}
@ -44,6 +50,8 @@ void DnaManager::init()
for (int i = 0; i < size; i++)
{
fread(&tmp, sizeof(tmp), 1, file);
if (tmp >= NUM_PER_GEN) // out of bounds error prevention if buffer was in the past biiger then its now
continue;
disliked.push_back(tmp);
}
fclose(file);
@ -57,9 +65,9 @@ void DnaManager::init()
}
vector.resize(NUM_PER_GEN);
if (sys::fileExists("array"))
if (sys::fileExists(VECTOR_FILE_NAME))
{
sys::loadDataFromFile("array", vector.data(), sizeof(Dna) * NUM_PER_GEN);
sys::loadDataFromFile(VECTOR_FILE_NAME, vector.data(), sizeof(Dna) * NUM_PER_GEN);
}
else
{
@ -67,6 +75,7 @@ void DnaManager::init()
{
DNA::newDna(&vector[i], &randSeed);
}
saveVec();
}
}
@ -76,7 +85,7 @@ void DnaManager::deinit()
void DnaManager::saveData()
{
const char *filename = sys::transformFilePath("data");
const char *filename = sys::transformFilePath(DATA_FILE_NAME);
FILE *file = fopen(filename, "wb");
if (file == NULL)
@ -100,7 +109,7 @@ void DnaManager::saveData()
void DnaManager::saveVec()
{
const char *filename = sys::transformFilePath("array");
const char *filename = sys::transformFilePath(VECTOR_FILE_NAME);
FILE *file = fopen(filename, "wb");
if (file == NULL)
return;
@ -108,14 +117,19 @@ void DnaManager::saveVec()
fclose(file);
}
Unit DnaManager::next()
UiUnit DnaManager::next()
{
if (queued >= NUM_PER_GEN)
{
return {nullptr, Liked::tbd, -1};
}
Dna *ret = &vector[queued];
int index = queued++;
return {ret, Liked::tbd, index};
}
void DnaManager::like(Unit unit)
void DnaManager::like(UiUnit unit)
{
int found = -1;
if (unit.index == showed)
@ -125,7 +139,7 @@ void DnaManager::like(Unit unit)
if (found == -1)
{
TraceLog(LOG_ERROR, "NOT FOUND UNIT");
// RUN OUT OF GEN WAITING FOR NEW GEN
return;
}
if (unit.liked == Liked::yes)
@ -144,8 +158,9 @@ void DnaManager::like(Unit unit)
showed++;
if (showed == NUM_PER_GEN && queued == NUM_PER_GEN)
if (showed >= NUM_PER_GEN && 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 ==
{
saveGen();
newGen();
queued = 0;
showed = 0;
@ -155,6 +170,37 @@ void DnaManager::like(Unit unit)
saveData();
}
void DnaManager::saveGen()
{
std::vector<NetUnit> gen;
gen.resize(NUM_PER_GEN);
for (std::size_t i = 0; i < NUM_PER_GEN; i++)
{
unsigned int *hash = ComputeMD5((unsigned char *)&vector[i], sizeof(Dna));
gen[i].hash.a = hash[0];
gen[i].hash.b = hash[1];
gen[i].hash.c = hash[2];
gen[i].hash.d = hash[3];
gen[i].liked = Liked::tbd;
gen[i].index = i;
}
for (auto &&i : liked)
{
gen[i].liked = Liked::yes;
}
for (auto &&i : disliked)
{
gen[i].liked = Liked::no;
}
const char *fileName = TextFormat("gen%04d.bin", generation);
sys::saveDataToFile(fileName, gen.data(), sizeof(NetUnit) * NUM_PER_GEN);
}
void DnaManager::newGen()
{
if (liked.size() == 0)
@ -184,6 +230,7 @@ void DnaManager::newGen()
{
for (auto &&i : disliked)
{
size_t p1 = mrand::getValue(0, liked.size() - 1, &randSeed);
size_t p2 = mrand::getValue(0, liked.size() - 1, &randSeed);
while (p1 == p2)
@ -205,4 +252,6 @@ void DnaManager::newGen()
{
DNA::mutate(&vector[i], NUM_OF_MUT, &randSeed);
}
disliked.clear();
liked.clear();
}

BIN
treender.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB