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

43
app/inc/App.hpp Normal file
View File

@@ -0,0 +1,43 @@
#include <array>
#include "canvas/Canvas.hpp"
#include "DnaStore.hpp"
class App
{
public:
App() = default;
~App() = default;
void init(int screenWidth, int screenHeight);
void update();
void draw();
void deinit();
private:
void upTex(Liked liked);
int screenWidth, screenHeight;
Canvas canvas;
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;
std::array<UiUnit, 2> unit = {0};
DnaManagerData manager;
Rectangle likedTextBox;
Rectangle genTextBox;
};

9
app/inc/DnaStore.hpp Normal file
View File

@@ -0,0 +1,9 @@
#include "values/DnaManager.hpp"
namespace DnaStore
{
void load(DnaManagerData *data);
void saveData(DnaManagerData *data);
void saveVec(DnaManagerData *data);
void saveGen(DnaManagerData *data);
} // namespace DnaStore

88
app/inc/sunShader.hpp Normal file
View File

@@ -0,0 +1,88 @@
const char *sun_shader_100 = "#version 100\n\
precision mediump float;\
varying vec2 fragTexCoord;\
varying vec4 fragColor;\
uniform vec3 color;\
uniform float sun_radius;\
uniform float start_transperency;\
vec2 offset = vec2(1.0, 1.0);\
float sun_end = 1.0;\
void main()\
{\
offset.x -= fragTexCoord.x * 2.0;\
offset.y -= fragTexCoord.y * 2.0;\
float radius = length(offset);\
if (radius < sun_radius) {\
gl_FragColor = vec4(color, 1.0);\
}\
else if (radius < sun_end) {\
float gradient = radius;\
gradient -= sun_radius;\
gradient = gradient / (sun_end - sun_radius) * start_transperency;\
gl_FragColor = vec4(color, start_transperency - gradient);\
}\
else {\
gl_FragColor = vec4(color, 0.0);\
}\
}";
unsigned char shaders_sun_330_fs[] = {
0x23, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x20, 0x33, 0x33, 0x30,
0x0a, 0x0a, 0x69, 0x6e, 0x20, 0x76, 0x65, 0x63, 0x32, 0x20, 0x66, 0x72,
0x61, 0x67, 0x54, 0x65, 0x78, 0x43, 0x6f, 0x6f, 0x72, 0x64, 0x3b, 0x0a,
0x69, 0x6e, 0x20, 0x76, 0x65, 0x63, 0x34, 0x20, 0x66, 0x72, 0x61, 0x67,
0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x3b, 0x0a, 0x6f, 0x75, 0x74, 0x20, 0x76,
0x65, 0x63, 0x34, 0x20, 0x66, 0x69, 0x6e, 0x61, 0x6c, 0x43, 0x6f, 0x6c,
0x6f, 0x72, 0x3b, 0x0a, 0x0a, 0x75, 0x6e, 0x69, 0x66, 0x6f, 0x72, 0x6d,
0x20, 0x76, 0x65, 0x63, 0x33, 0x20, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x3b,
0x0a, 0x75, 0x6e, 0x69, 0x66, 0x6f, 0x72, 0x6d, 0x20, 0x66, 0x6c, 0x6f,
0x61, 0x74, 0x20, 0x73, 0x75, 0x6e, 0x5f, 0x72, 0x61, 0x64, 0x69, 0x75,
0x73, 0x3b, 0x0a, 0x75, 0x6e, 0x69, 0x66, 0x6f, 0x72, 0x6d, 0x20, 0x66,
0x6c, 0x6f, 0x61, 0x74, 0x20, 0x73, 0x74, 0x61, 0x72, 0x74, 0x5f, 0x74,
0x72, 0x61, 0x6e, 0x73, 0x70, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x79, 0x3b,
0x0a, 0x76, 0x65, 0x63, 0x32, 0x20, 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74,
0x20, 0x3d, 0x20, 0x76, 0x65, 0x63, 0x32, 0x28, 0x31, 0x2e, 0x30, 0x66,
0x2c, 0x20, 0x31, 0x2e, 0x30, 0x66, 0x29, 0x3b, 0x0a, 0x66, 0x6c, 0x6f,
0x61, 0x74, 0x20, 0x73, 0x75, 0x6e, 0x5f, 0x65, 0x6e, 0x64, 0x20, 0x3d,
0x20, 0x31, 0x2e, 0x30, 0x66, 0x3b, 0x0a, 0x0a, 0x76, 0x6f, 0x69, 0x64,
0x20, 0x6d, 0x61, 0x69, 0x6e, 0x28, 0x29, 0x0a, 0x7b, 0x0a, 0x20, 0x20,
0x20, 0x20, 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x2e, 0x78, 0x20, 0x2d,
0x3d, 0x20, 0x66, 0x72, 0x61, 0x67, 0x54, 0x65, 0x78, 0x43, 0x6f, 0x6f,
0x72, 0x64, 0x2e, 0x78, 0x20, 0x2a, 0x20, 0x32, 0x3b, 0x0a, 0x20, 0x20,
0x20, 0x20, 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x2e, 0x79, 0x20, 0x2d,
0x3d, 0x20, 0x66, 0x72, 0x61, 0x67, 0x54, 0x65, 0x78, 0x43, 0x6f, 0x6f,
0x72, 0x64, 0x2e, 0x79, 0x20, 0x2a, 0x20, 0x32, 0x3b, 0x0a, 0x20, 0x20,
0x20, 0x20, 0x66, 0x6c, 0x6f, 0x61, 0x74, 0x20, 0x72, 0x61, 0x64, 0x69,
0x75, 0x73, 0x20, 0x3d, 0x20, 0x6c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x28,
0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20,
0x20, 0x69, 0x66, 0x28, 0x72, 0x61, 0x64, 0x69, 0x75, 0x73, 0x20, 0x3c,
0x20, 0x73, 0x75, 0x6e, 0x5f, 0x72, 0x61, 0x64, 0x69, 0x75, 0x73, 0x29,
0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x66, 0x69,
0x6e, 0x61, 0x6c, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x3d, 0x20, 0x76,
0x65, 0x63, 0x34, 0x28, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x2c, 0x20, 0x31,
0x2e, 0x30, 0x66, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x65,
0x6c, 0x73, 0x65, 0x20, 0x69, 0x66, 0x28, 0x72, 0x61, 0x64, 0x69, 0x75,
0x73, 0x20, 0x3c, 0x20, 0x73, 0x75, 0x6e, 0x5f, 0x65, 0x6e, 0x64, 0x29,
0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x66, 0x6c,
0x6f, 0x61, 0x74, 0x20, 0x67, 0x72, 0x61, 0x64, 0x69, 0x65, 0x6e, 0x74,
0x20, 0x3d, 0x20, 0x72, 0x61, 0x64, 0x69, 0x75, 0x73, 0x3b, 0x0a, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x67, 0x72, 0x61, 0x64, 0x69,
0x65, 0x6e, 0x74, 0x20, 0x2d, 0x3d, 0x20, 0x73, 0x75, 0x6e, 0x5f, 0x72,
0x61, 0x64, 0x69, 0x75, 0x73, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x67, 0x72, 0x61, 0x64, 0x69, 0x65, 0x6e, 0x74, 0x20,
0x3d, 0x20, 0x67, 0x72, 0x61, 0x64, 0x69, 0x65, 0x6e, 0x74, 0x20, 0x2f,
0x20, 0x28, 0x73, 0x75, 0x6e, 0x5f, 0x65, 0x6e, 0x64, 0x20, 0x2d, 0x20,
0x73, 0x75, 0x6e, 0x5f, 0x72, 0x61, 0x64, 0x69, 0x75, 0x73, 0x29, 0x20,
0x2a, 0x20, 0x73, 0x74, 0x61, 0x72, 0x74, 0x5f, 0x74, 0x72, 0x61, 0x6e,
0x73, 0x70, 0x65, 0x72, 0x65, 0x6e, 0x63, 0x79, 0x3b, 0x0a, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x66, 0x69, 0x6e, 0x61, 0x6c, 0x43,
0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x3d, 0x20, 0x76, 0x65, 0x63, 0x34, 0x28,
0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x2c, 0x20, 0x73, 0x74, 0x61, 0x72, 0x74,
0x5f, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x70, 0x65, 0x72, 0x65, 0x6e, 0x63,
0x79, 0x20, 0x2d, 0x20, 0x67, 0x72, 0x61, 0x64, 0x69, 0x65, 0x6e, 0x74,
0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x65, 0x6c, 0x73, 0x65,
0x7b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x66, 0x69,
0x6e, 0x61, 0x6c, 0x43, 0x6f, 0x6c, 0x6f, 0x72, 0x20, 0x3d, 0x20, 0x76,
0x65, 0x63, 0x34, 0x28, 0x63, 0x6f, 0x6c, 0x6f, 0x72, 0x2c, 0x20, 0x30,
0x2e, 0x30, 0x66, 0x29, 0x3b, 0x0a, 0x20, 0x20, 0x20, 0x20, 0x7d, 0x0a,
0x7d, 0x0a};

9
app/inc/sys.hpp Normal file
View File

@@ -0,0 +1,9 @@
namespace sys
{
const char *transformFilePath(const char *filename);
size_t saveDataToFile(const char *filename, void *data, size_t size);
size_t loadDataFromFile(const char *filename, void *data, size_t size);
bool fileExists(const char *filename);
}

223
app/src/App.cpp Normal file
View File

@@ -0,0 +1,223 @@
#include <array>
#include <cmath>
#include "App.hpp"
#include <raylib.h>
#include <raymath.h>
#define TOP (1 - pos)
#define BOTTOM pos
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;
}
// 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);
// 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);
}
DnaStore::load(&manager);
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;
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};
}
void App::upTex(Liked liked)
{
if (liked != Liked::tbd)
{
unit[TOP].liked = liked;
bool ng = DnaManager::like(unit[TOP], &manager);
if (ng)
{
DnaStore::saveGen(&manager);
DnaManager::newGen(&manager);
DnaStore::saveVec(&manager);
}
DnaStore::saveData(&manager);
}
unit[TOP] = DnaManager::next(&manager);
if (unit[TOP].dna != nullptr)
{
canvas.newGen(canvasTexure[TOP], unit[TOP].dna);
return;
}
BeginTextureMode(canvasTexure[TOP]);
ClearBackground(BLACK);
DrawText("NEXT GEN", 10, 10, 20, WHITE);
EndTextureMode();
}
void App::update()
{
bool isDone = canvas.tick(canvasTexure[BOTTOM]);
Vector2 mousePosition = GetMousePosition();
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
{
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))
{
topLiked = Liked::no;
}
else if (CheckCollisionPointRec(mousePosition, likeBox))
{
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;
}
}
void App::draw()
{
ClearBackground(BLUE);
Rectangle source = {0, 0, (float)screenWidth, (float)-screenWidth};
Vector2 origin = {0.0f, 0.0f};
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()
{
for (size_t i = 0; i < canvasTexure.size(); i++)
{
UnloadRenderTexture(canvasTexure[i]);
}
canvas.deinit();
}

140
app/src/DnaStore.cpp Normal file
View File

@@ -0,0 +1,140 @@
#include <cstdio>
#include <ctime>
#include "sys.hpp"
#include "DnaStore.hpp"
#include <raylib.h>
#define ID_FILE_NAME "ID.bin"
#define DATA_FILE_NAME "DATA.bin"
#define VECTOR_FILE_NAME "VECTOR.bin"
void DnaStore::load(DnaManagerData *data)
{
if (sys::fileExists(ID_FILE_NAME))
{
sys::loadDataFromFile(ID_FILE_NAME, &data->id, sizeof(uint128));
}
else
{
data->id = mrand::getState(time(nullptr));
sys::saveDataToFile(ID_FILE_NAME, &data->id, sizeof(uint128));
}
if (sys::fileExists(DATA_FILE_NAME))
{
const char *filename = sys::transformFilePath(DATA_FILE_NAME);
FILE *file = fopen(filename, "rb");
if (file == NULL)
return;
fread(&data->randSeed, 1, sizeof(uint128), file);
fread(&data->generation, 1, sizeof(int), file);
fread(&data->showed, 1, sizeof(int), file);
data->queued = data->showed;
int size, tmp;
fread(&size, sizeof(size), 1, file);
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;
data->liked.push_back(tmp);
}
fread(&size, sizeof(size), 1, file);
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;
data->disliked.push_back(tmp);
}
fclose(file);
}
else
{
data->randSeed = data->id;
data->queued = 0;
data->showed = 0;
data->generation = 0;
}
data->vector.resize(NUM_PER_GEN);
if (sys::fileExists(VECTOR_FILE_NAME))
{
sys::loadDataFromFile(VECTOR_FILE_NAME, data->vector.data(), sizeof(Dna) * NUM_PER_GEN);
}
else
{
for (std::size_t i = 0; i < NUM_PER_GEN; i++)
{
DNA::newDna(&data->vector[i], &data->randSeed);
}
saveVec(data); // v primeru da prvic ne gres cez celo generacijo se vec ne shrani in bo drugacen ker se bo regeneriral z drugim rand seed
saveData(data); // save ce rand seed se je spremenil in v premeru user ugasne brez da like se rand seed ne shrani in se resetira v ID
}
}
void DnaStore::saveData(DnaManagerData *data)
{
const char *filename = sys::transformFilePath(DATA_FILE_NAME);
FILE *file = fopen(filename, "wb");
if (file == NULL)
return;
fwrite(&data->randSeed, 1, sizeof(uint128), file);
fwrite(&data->generation, 1, sizeof(int), file);
fwrite(&data->showed, 1, sizeof(int), file);
int tmp;
tmp = data->liked.size();
fwrite(&tmp, sizeof(tmp), 1, file);
fwrite(data->liked.data(), sizeof(int) * tmp, 1, file);
tmp = data->disliked.size();
fwrite(&tmp, sizeof(tmp), 1, file);
fwrite(data->disliked.data(), sizeof(tmp) * tmp, 1, file);
fclose(file);
}
void DnaStore::saveVec(DnaManagerData *data)
{
const char *filename = sys::transformFilePath(VECTOR_FILE_NAME);
FILE *file = fopen(filename, "wb");
if (file == NULL)
return;
fwrite(data->vector.data(), sizeof(Dna) * NUM_PER_GEN, 1, file);
fclose(file);
}
void DnaStore::saveGen(DnaManagerData *data)
{
std::vector<NetUnit> gen;
gen.resize(NUM_PER_GEN);
for (std::size_t i = 0; i < NUM_PER_GEN; i++)
{
gen[i].hash = ComputeCRC32((unsigned char *)&data->vector[i], sizeof(Dna));
gen[i].liked = Liked::tbd;
gen[i].index = i;
}
for (auto &&i : data->liked)
{
gen[i].liked = Liked::yes;
}
for (auto &&i : data->disliked)
{
gen[i].liked = Liked::no;
}
const char *fileName = TextFormat("gen%04d.bin", data->generation);
sys::saveDataToFile(fileName, gen.data(), sizeof(NetUnit) * NUM_PER_GEN);
}

48
app/src/main.cpp Normal file
View File

@@ -0,0 +1,48 @@
#include <raylib.h>
#include "App.hpp"
#if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h>
#endif
static App app;
void UpdateDrawFrame()
{
app.update();
BeginDrawing();
app.draw();
EndDrawing();
}
int main(void)
{
char name[] = "treender";
int screenWidth = 600;
int screenHeight = 800;
#ifdef PLATFORM_ANDROID
InitWindow(0, 0, name);
screenWidth = GetScreenWidth();
screenHeight = GetScreenHeight();
#else
InitWindow(screenWidth, screenHeight, name);
#endif
app.init(screenWidth, screenHeight);
#if defined(PLATFORM_WEB)
emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
#else
SetTargetFPS(60);
while (!WindowShouldClose())
{
UpdateDrawFrame();
}
#endif
app.deinit();
CloseWindow();
return 0;
}

61
app/src/sys.cpp Normal file
View File

@@ -0,0 +1,61 @@
#include <stdio.h>
#include <unistd.h>
#include "sys.hpp"
#include <raylib.h>
#if defined(PLATFORM_ANDROID)
#include <raymob.h>
#endif
namespace sys
{
const char *transformFilePath(const char *filename)
{
#if defined(PLATFORM_ANDROID)
return TextFormat(
"%s/%s", GetAndroidApp()->activity->internalDataPath, filename);
#else
return filename;
#endif
}
size_t saveDataToFile(const char *filename, void *data, size_t size)
{
const char *saveFilePath = transformFilePath(filename);
FILE *file = fopen(saveFilePath, "wb");
if (file == NULL)
return false;
size_t ret = fwrite(data, 1, size, file);
fclose(file);
return ret;
}
size_t loadDataFromFile(const char *filename, void *data, size_t size)
{
const char *saveFilePath = transformFilePath(filename);
FILE *file = fopen(saveFilePath, "rb");
if (file == NULL)
return false;
size_t ret = fread(data, 1, size, file);
fclose(file);
return ret;
}
bool fileExists(const char *filename)
{
const char *saveFilePath = transformFilePath(filename);
bool result = false;
if (access(saveFilePath, F_OK) != -1)
result = true;
return result;
}
}