change to draw body first
Using std::list to store what needs to be drawn
This commit is contained in:
parent
f6adb3e306
commit
3d9e6c9f7d
35
main.cpp
35
main.cpp
@ -1,6 +1,7 @@
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
#include <cstdint>
|
||||
#include <list>
|
||||
#include "raylib.h"
|
||||
#include "raymath.h"
|
||||
|
||||
@ -9,12 +10,10 @@
|
||||
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;
|
||||
}
|
||||
|
||||
@ -23,7 +22,16 @@ struct branch
|
||||
Color color;
|
||||
};
|
||||
|
||||
struct draw_args
|
||||
{
|
||||
Vector2 start;
|
||||
float angleDeg;
|
||||
float lenghth;
|
||||
int dep;
|
||||
};
|
||||
|
||||
static std::vector<branch> tree(MAX_DEPTH);
|
||||
static std::list<draw_args> draw_calls;
|
||||
|
||||
Vector2 draw_line(Vector2 start, float angleDeg, float lenghth, int dep)
|
||||
{
|
||||
@ -45,16 +53,27 @@ Vector2 draw_line(Vector2 start, float angleDeg, float lenghth, int dep)
|
||||
return end;
|
||||
}
|
||||
|
||||
void draw_branch(Vector2 start, float angle, float len, int dep)
|
||||
void draw_branch(draw_args arg)
|
||||
{
|
||||
if (dep >= MAX_DEPTH)
|
||||
if (arg.dep >= MAX_DEPTH)
|
||||
return;
|
||||
|
||||
Vector2 next = draw_line(start, angle, len, dep);
|
||||
Vector2 next = draw_line(arg.start, arg.angleDeg, arg.lenghth, arg.dep);
|
||||
|
||||
float next_len = 0.7f;
|
||||
draw_branch(next, angle + 45, len * next_len, dep + 1);
|
||||
draw_branch(next, angle - 45, len * next_len, dep + 1);
|
||||
draw_calls.push_back((draw_args){next, arg.angleDeg + 45, arg.lenghth * next_len, arg.dep + 1});
|
||||
draw_calls.push_back((draw_args){next, arg.angleDeg - 45, arg.lenghth * next_len, arg.dep + 1});
|
||||
}
|
||||
|
||||
void draw_tree(draw_args first)
|
||||
{
|
||||
draw_calls.push_back(first);
|
||||
|
||||
while (!draw_calls.empty())
|
||||
{
|
||||
draw_branch(draw_calls.front());
|
||||
draw_calls.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
void generate_tree()
|
||||
@ -89,7 +108,7 @@ int main(void)
|
||||
|
||||
BeginTextureMode(target);
|
||||
ClearBackground(WHITE);
|
||||
draw_branch(start, 0, textureHeight / 4, 1);
|
||||
draw_tree((draw_args){start, 0, textureHeight / 4, 1});
|
||||
EndTextureMode();
|
||||
|
||||
Rectangle source = {0, 0, (float)target.texture.width, (float)-target.texture.height};
|
||||
|
Loading…
x
Reference in New Issue
Block a user