From 19bb29027678db128aa33fc6e6e54b61c35c0bea Mon Sep 17 00:00:00 2001
From: Nikola Petrov <nikolape7@gmail.com>
Date: Thu, 22 Feb 2024 01:01:03 +0100
Subject: [PATCH] Add color to tree

---
 main.cpp | 33 ++++++++++++++++++++++++++++-----
 1 file changed, 28 insertions(+), 5 deletions(-)

diff --git a/main.cpp b/main.cpp
index 360117a..a8d20dd 100644
--- a/main.cpp
+++ b/main.cpp
@@ -1,7 +1,17 @@
 #include <cmath>
+#include <vector>
+#include <cstdint>
 #include "raylib.h"
+#include "raymath.h"
 
-#define MAX_DEPTH 10
+#define MAX_DEPTH 11
+
+struct branch
+{
+  Color color;
+};
+
+static std::vector<branch> tree(MAX_DEPTH);
 
 Vector2 draw_line(Vector2 start, float angleDeg, float lenghth, Color color)
 {
@@ -16,16 +26,27 @@ Vector2 draw_line(Vector2 start, float angleDeg, float lenghth, Color color)
 
 void draw_branch(Vector2 start, float angle, float len, int dep)
 {
-  Vector2 next = draw_line(start, angle, len, PURPLE);
-
-  if (len < MAX_DEPTH)
+  if (dep >= MAX_DEPTH)
     return;
 
+  Vector2 next = draw_line(start, angle, len, tree[dep].color);
+
   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);
 }
 
+void generate_tree()
+{
+  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);
+    tree[i].color = (Color){r, g, b, 255};
+  }
+}
+
 int main(void)
 {
   const int screenWidth = 800;
@@ -41,9 +62,11 @@ int main(void)
   RenderTexture2D target = LoadRenderTexture(textureWidth, textureHeight);
   Vector2 start = {textureWidth / 2, textureHeight};
 
+  generate_tree();
+
   BeginTextureMode(target);
   ClearBackground(WHITE);
-  draw_branch(start, 0, textureHeight / 4, 0);
+  draw_branch(start, 0, textureHeight / 4, 1);
   EndTextureMode();
 
   Rectangle source = {0, 0, (float)target.texture.width, (float)-target.texture.height};