115 lines
2.4 KiB
C++
115 lines
2.4 KiB
C++
#include <cmath>
|
|
#include "raylib.h"
|
|
#include "raymath.h"
|
|
#include "Tree.hpp"
|
|
#include "Math.hpp"
|
|
|
|
// Public
|
|
Tree::Tree(int size)
|
|
{
|
|
this->size = size;
|
|
target = LoadRenderTexture(size, size);
|
|
start.x = size / 2;
|
|
start.y = size;
|
|
|
|
branches.assign(11, {0});
|
|
}
|
|
|
|
Tree::~Tree()
|
|
{
|
|
}
|
|
|
|
void Tree::draw(Rectangle dest, float rotation)
|
|
{
|
|
Rectangle source = {0, 0, (float)target.texture.width, (float)-target.texture.height};
|
|
Vector2 origin = {0.0f, 0.0f};
|
|
rotation = 360 - rotation;
|
|
DrawTexturePro(target.texture, source, dest, origin, rotation, WHITE);
|
|
}
|
|
|
|
void Tree::newTree()
|
|
{
|
|
generateBranches();
|
|
|
|
BeginTextureMode(target);
|
|
ClearBackground(WHITE);
|
|
drawTree();
|
|
EndTextureMode();
|
|
}
|
|
|
|
void Tree::deinit()
|
|
{
|
|
UnloadRenderTexture(target);
|
|
}
|
|
|
|
// 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);
|
|
|
|
for (float i = 0; i < 1; i += fstep)
|
|
{
|
|
Vector2 point = Vector2Lerp(arg.start, end, i);
|
|
Color color = ColorLerp(branches[arg.dep - 1].color, branches[arg.dep].color, i);
|
|
DrawCircleV(point, thick, color);
|
|
}
|
|
return end;
|
|
}
|
|
|
|
void Tree::drawBranch()
|
|
{
|
|
DrawArgs arg = draw_calls.front();
|
|
if (arg.dep >= MAX_DEPTH)
|
|
return;
|
|
|
|
Vector2 next = drawLine();
|
|
|
|
float next_len = branches[arg.dep].lenghthRatio;
|
|
float sectors = branches[arg.dep].numOfBranches + 1;
|
|
float degres = 180.0f / sectors;
|
|
|
|
for (size_t i = 0; i < branches[arg.dep].numOfBranches; i++)
|
|
{
|
|
float newAngle = arg.angleDeg - 90 + (degres * (i + 1));
|
|
draw_calls.push_back((DrawArgs){next, newAngle, arg.lenghth * next_len, arg.dep + 1});
|
|
}
|
|
}
|
|
|
|
void Tree::drawTree()
|
|
{
|
|
draw_calls.push_back((DrawArgs){start, 0, (float)size / 4, 1});
|
|
|
|
while (!draw_calls.empty())
|
|
{
|
|
drawBranch();
|
|
draw_calls.pop_front();
|
|
}
|
|
}
|
|
|
|
void Tree::generateBranches()
|
|
{
|
|
for (size_t i = 0; i < MAX_DEPTH; i++)
|
|
{
|
|
uint8_t r = GetRandomValue(0, 255);
|
|
uint8_t g = GetRandomValue(0, 255);
|
|
uint8_t b = GetRandomValue(0, 255);
|
|
branches[i].color = (Color){r, g, b, 255};
|
|
|
|
branches[i].numOfBranches = GetRandomValue(1, 3);
|
|
|
|
branches[i].lenghthRatio = ((float)GetRandomValue(600, 700)) / 1000.0f;
|
|
}
|
|
|
|
branches[0].color = branches[1].color;
|
|
}
|