#include #include "canvas/Tree.hpp" #include "canvas/Circle.hpp" #include "Math.hpp" #include #include #define ITER_PER_FRAME 5000 constexpr int max_num_of_branches = 3; // Public void Tree::init(int size) { this->size = size; start.x = size / 2; start.y = size; } 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(); } bool Tree::tick() { size_t i = 0; while (!draw_calls.empty()) { drawBranch(); draw_calls.pop_front(); i++; if (i >= ITER_PER_FRAME) break; } return draw_calls.empty(); } // Private Vector2 Tree::drawLine() { DrawArgs arg = draw_calls.front(); arg.angleDeg += 180.0f; float angle = (arg.angleDeg * PI) / 180.0f; float nx = arg.lenghth * std::sin(angle); float ny = arg.lenghth * std::cos(angle); Vector2 end = {arg.start.x + nx, arg.start.y + ny}; float thick = 2.0; float fstep = 1.0 / ((arg.lenghth / thick) * 1.5); Color colorStart = get_start_color(arg.dep); 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(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; } void Tree::drawBranch() { DrawArgs arg = draw_calls.front(); if (arg.dep >= MAX_DEPTH) return; Vector2 next = drawLine(); float next_len = 0.7f; float sectors = get_num_of_branches(arg.dep) + 1; float degres = 180.0f / sectors; 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}; }