Timing
This commit is contained in:
@@ -18,30 +18,14 @@ private:
|
||||
int screenWidth, screenHeight;
|
||||
Canvas canvas;
|
||||
|
||||
int pos = 0;
|
||||
std::array<RenderTexture2D, 2> canvasTexure = {0};
|
||||
RenderTexture2D 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};
|
||||
UiUnit unit = {0};
|
||||
|
||||
DnaManagerData manager;
|
||||
|
||||
Rectangle likedTextBox;
|
||||
Rectangle genTextBox;
|
||||
Rectangle simTextBox;
|
||||
float simil = 100.0f;
|
||||
|
||||
Color appColor = BLUE;
|
||||
};
|
||||
|
||||
227
app/src/App.cpp
227
app/src/App.cpp
@@ -2,241 +2,118 @@
|
||||
#include <cmath>
|
||||
|
||||
#include "App.hpp"
|
||||
#include "timing.hpp"
|
||||
|
||||
#include <raylib.h>
|
||||
#include <raymath.h>
|
||||
|
||||
#define TOP (1 - pos)
|
||||
#define BOTTOM pos
|
||||
Font fontTtf;
|
||||
|
||||
Rectangle TextInSpace(Rectangle box, float textH, float textW, float margin, bool topOrBottom, bool leftOrRigth)
|
||||
void DrawTextB(const char *text, float posX, float posY, int fontSize, Color color)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
if (leftOrRigth)
|
||||
{
|
||||
ret.x = box.x + wm;
|
||||
}
|
||||
else
|
||||
{
|
||||
ret.x = box.x + box.width - wm - ret.width;
|
||||
}
|
||||
return ret;
|
||||
DrawTextEx(fontTtf, text, (Vector2){ posX, posY }, fontSize, 1, color);
|
||||
}
|
||||
|
||||
// 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)
|
||||
{
|
||||
fontTtf = LoadFontEx("res/CascadiaCode.ttf", 32, 0, 0);
|
||||
SetTextLineSpacing(16);
|
||||
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);
|
||||
}
|
||||
canvasTexure = LoadRenderTexture(screenWidth, screenWidth);
|
||||
|
||||
DnaStore::load(&manager);
|
||||
simil = Similarity::calc_similarity(manager.vector);
|
||||
upTex(Liked::tbd);
|
||||
while (!canvas.tick(canvasTexure[TOP]))
|
||||
while (!canvas.tick(canvasTexure))
|
||||
{
|
||||
// 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, true);
|
||||
|
||||
genTextBox = TextInSpace({0,
|
||||
posY + screenWidth,
|
||||
(float)screenWidth,
|
||||
posY},
|
||||
20.0f, 200.0f, textMargin, true, true);
|
||||
|
||||
simTextBox = TextInSpace({(float)screenWidth / 2.0f,
|
||||
likedTextBox.y,
|
||||
(float)screenWidth / 2.0f,
|
||||
likedTextBox.height},
|
||||
20.0f, 60.0f, textMargin, false, false);
|
||||
|
||||
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};
|
||||
|
||||
if(!DnaStore::ping()){
|
||||
appColor = RED;
|
||||
}
|
||||
destA = {0, posY, (float)screenWidth, (float)screenWidth};
|
||||
}
|
||||
|
||||
void App::upTex(Liked liked)
|
||||
{
|
||||
if (liked != Liked::tbd)
|
||||
{
|
||||
unit[TOP].liked = liked;
|
||||
bool ng = DnaManager::like(unit[TOP], &manager);
|
||||
unit.liked = liked;
|
||||
bool ng = DnaManager::like(unit, &manager);
|
||||
|
||||
if (ng)
|
||||
{
|
||||
DnaStore::saveGen(&manager);
|
||||
DnaManager::newGen(&manager);
|
||||
DnaStore::saveVec(&manager);
|
||||
simil = Similarity::calc_similarity(manager.vector);
|
||||
}
|
||||
DnaStore::saveData(&manager);
|
||||
}
|
||||
unit[TOP] = DnaManager::next(&manager);
|
||||
if (unit[TOP].dna != nullptr)
|
||||
unit = DnaManager::next(&manager);
|
||||
if (unit.dna != nullptr)
|
||||
{
|
||||
canvas.newGen(canvasTexure[TOP], unit[TOP].dna);
|
||||
canvas.newGen(canvasTexure, unit.dna);
|
||||
return;
|
||||
}
|
||||
|
||||
BeginTextureMode(canvasTexure[TOP]);
|
||||
BeginTextureMode(canvasTexure);
|
||||
ClearBackground(BLACK);
|
||||
DrawText("NEXT GEN", 10, 10, screenWidth/10, 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);
|
||||
}
|
||||
void App::update(){}
|
||||
|
||||
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);
|
||||
enum class Rend_type{
|
||||
every_frame,
|
||||
in_one_frame,
|
||||
over_frames,
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
constexpr Rend_type rend_type = Rend_type::over_frames;
|
||||
|
||||
void App::draw()
|
||||
{
|
||||
ClearBackground(appColor);
|
||||
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT))
|
||||
{
|
||||
TraceLog(LOG_INFO, "CLICK");
|
||||
upTex(Liked::yes);
|
||||
if constexpr (rend_type == Rend_type::in_one_frame){
|
||||
while (!canvas.tick(canvasTexure))
|
||||
{
|
||||
// wait to finish drawing
|
||||
}
|
||||
}
|
||||
}
|
||||
if constexpr (rend_type == Rend_type::over_frames){
|
||||
canvas.tick(canvasTexure);
|
||||
}
|
||||
ClearBackground(BLUE);
|
||||
|
||||
int fps = GetFPS();
|
||||
const char* fpstext = TextFormat("%04d FPS", fps);
|
||||
DrawTextB(fpstext, 10, 10, 36, BLACK);
|
||||
const char* ctext = TextFormat("%04ld Circles", canvas.tree.circles);
|
||||
DrawTextB(ctext, 10, 50, 36, BLACK);
|
||||
|
||||
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 *textGen = TextFormat("GEN %d: %d / %d", manager.generation, unit[TOP].index + 1, NUM_PER_GEN);
|
||||
DrawText(textGen, genTextBox.x, genTextBox.y, genTextBox.height, BLACK);
|
||||
const char *textProc = TextFormat("%.1f%%", simil);
|
||||
DrawText(textProc, simTextBox.x, simTextBox.y, simTextBox.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;
|
||||
if constexpr (rend_type == Rend_type::every_frame){
|
||||
canvas.newGen(canvasTexure, unit.dna);
|
||||
while (!canvas.tick(canvasTexure))
|
||||
{
|
||||
// wait to finish drawing
|
||||
}
|
||||
}
|
||||
DrawTexturePro(canvasTexure.texture, source, destA, origin, 360, WHITE);
|
||||
}
|
||||
|
||||
void App::deinit()
|
||||
{
|
||||
for (size_t i = 0; i < canvasTexure.size(); i++)
|
||||
{
|
||||
UnloadRenderTexture(canvasTexure[i]);
|
||||
}
|
||||
UnloadRenderTexture(canvasTexure);
|
||||
UnloadFont(fontTtf);
|
||||
canvas.deinit();
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
#include <raylib.h>
|
||||
#include "App.hpp"
|
||||
#include "timing.hpp"
|
||||
|
||||
#if defined(PLATFORM_WEB)
|
||||
#include <emscripten/emscripten.h>
|
||||
@@ -7,12 +8,14 @@
|
||||
|
||||
static App app;
|
||||
|
||||
Timer t;
|
||||
void UpdateDrawFrame()
|
||||
{
|
||||
app.update();
|
||||
t.reset_and_start();
|
||||
BeginDrawing();
|
||||
app.draw();
|
||||
EndDrawing();
|
||||
t.stop_and_print("to draw frame");
|
||||
}
|
||||
|
||||
int main(void)
|
||||
@@ -34,7 +37,7 @@ int main(void)
|
||||
#if defined(PLATFORM_WEB)
|
||||
emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
|
||||
#else
|
||||
SetTargetFPS(60);
|
||||
// SetTargetFPS(60);
|
||||
while (!WindowShouldClose())
|
||||
{
|
||||
UpdateDrawFrame();
|
||||
|
||||
Reference in New Issue
Block a user