Reformat to folow style

This commit is contained in:
Nikola Petrov 2024-03-03 22:31:15 +01:00
parent 8b3eb1cb7e
commit 462f27aa26
4 changed files with 79 additions and 55 deletions

20
Readme.md Normal file
View File

@ -0,0 +1,20 @@
# Code style
## Naming Conventions:
- PascalCase:
- Functions
- Class names
- Struct names
- camelCase:
- Class attributes
- Class methods
- Struct attributes
- Variables
## Class Structure:
- Header (.h) Files:
- Declare public methods and attributes first, followed by private members.
- Implementation (.cpp) Files:
- Implement methods in the same order as declared in the corresponding header file.

View File

@ -1,19 +1,18 @@
#include "raylib.h"
#include "raylib.h";
#include <cstdint> #include <cstdint>
#include <vector> #include <vector>
#include <list> #include <list>
#define MAX_DEPTH 11 #define MAX_DEPTH 11
struct branch struct Branch
{ {
Color color; Color color;
uint8_t numOfBranches; uint8_t numOfBranches;
float lenghthRatio; float lenghthRatio;
}; };
struct draw_args struct DrawArgs
{ {
Vector2 start; Vector2 start;
float angleDeg; float angleDeg;
@ -27,17 +26,18 @@ class Tree
public: public:
Tree(int size); Tree(int size);
~Tree(); ~Tree();
void Draw(Rectangle dest); void draw(Rectangle dest, float rotation);
void new_tree(); void newTree();
void generate_tree();
void draw_tree(draw_args first);
void draw_branch(draw_args arg);
Vector2 draw_line(Vector2 start, float angleDeg, float lenghth, int dep);
private: private:
int size = 0; int size = 0;
RenderTexture2D target = {0}; RenderTexture2D target = {0};
Vector2 start = {0}; Vector2 start = {0};
std::vector<branch> tree; std::vector<Branch> branches;
std::list<draw_args> draw_calls; std::list<DrawArgs> draw_calls;
void generateBranches();
void drawTree();
void drawBranch();
Vector2 drawLine();
}; };

View File

@ -10,21 +10,19 @@ int main(void)
SetTargetFPS(60); SetTargetFPS(60);
{ {
Tree tre(800); Tree tree(800);
tre.new_tree(); tree.newTree();
Rectangle dest = {0, 0, screenWidth, screenHeight}; Rectangle dest = {0, 0, screenWidth, screenHeight};
float rotation = 0.0f;
while (!WindowShouldClose()) while (!WindowShouldClose())
{ {
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
{ {
tre.new_tree(); tree.newTree();
} }
BeginDrawing(); BeginDrawing();
tre.Draw(dest); tree.draw(dest, rotation);
EndDrawing(); EndDrawing();
} }
} }

View File

@ -4,6 +4,7 @@
#include "Math.hpp" #include "Math.hpp"
#include <cmath> #include <cmath>
// Public
Tree::Tree(int size) Tree::Tree(int size)
{ {
this->size = size; this->size = size;
@ -11,7 +12,7 @@ Tree::Tree(int size)
start.x = size / 2; start.x = size / 2;
start.y = size; start.y = size;
tree.assign(11, {0}); branches.assign(11, {0});
} }
Tree::~Tree() Tree::~Tree()
@ -19,85 +20,90 @@ Tree::~Tree()
UnloadRenderTexture(target); UnloadRenderTexture(target);
} }
void Tree::Draw(Rectangle dest) void Tree::draw(Rectangle dest, float rotation)
{ {
Rectangle source = {0, 0, (float)target.texture.width, (float)-target.texture.height}; Rectangle source = {0, 0, (float)target.texture.width, (float)-target.texture.height};
Vector2 origin = {0.0f, 0.0f}; Vector2 origin = {0.0f, 0.0f};
DrawTexturePro(target.texture, source, dest, origin, 0.0f, WHITE); DrawTexturePro(target.texture, source, dest, origin, rotation, WHITE);
} }
Vector2 Tree::draw_line(Vector2 start, float angleDeg, float lenghth, int dep) void Tree::newTree()
{ {
angleDeg += 180.0f; generateBranches();
float angle = (angleDeg * PI) / 180.0f;
float nx = lenghth * std::sin(angle); BeginTextureMode(target);
float ny = lenghth * std::cos(angle); ClearBackground(WHITE);
Vector2 end = {start.x + nx, start.y + ny}; drawTree();
EndTextureMode();
}
// 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 thick = 2.0;
float fstep = 1.0 / ((lenghth / thick) * 1.5); float fstep = 1.0 / ((arg.lenghth / thick) * 1.5);
for (float i = 0; i < 1; i += fstep) for (float i = 0; i < 1; i += fstep)
{ {
Vector2 point = Vector2Lerp(start, end, i); Vector2 point = Vector2Lerp(arg.start, end, i);
Color color = ColorLerp(tree[dep - 1].color, tree[dep].color, i); Color color = ColorLerp(branches[arg.dep - 1].color, branches[arg.dep].color, i);
DrawCircleV(point, thick, color); DrawCircleV(point, thick, color);
} }
return end; return end;
} }
void Tree::draw_branch(draw_args arg) void Tree::drawBranch()
{ {
DrawArgs arg = draw_calls.front();
if (arg.dep >= MAX_DEPTH) if (arg.dep >= MAX_DEPTH)
return; return;
Vector2 next = draw_line(arg.start, arg.angleDeg, arg.lenghth, arg.dep); Vector2 next = drawLine();
float next_len = tree[arg.dep].lenghthRatio; float next_len = branches[arg.dep].lenghthRatio;
float sectors = tree[arg.dep].numOfBranches + 1; float sectors = branches[arg.dep].numOfBranches + 1;
float degres = 180.0f / sectors; float degres = 180.0f / sectors;
for (size_t i = 0; i < tree[arg.dep].numOfBranches; i++) for (size_t i = 0; i < branches[arg.dep].numOfBranches; i++)
{ {
float newAngle = arg.angleDeg - 90 + (degres * (i + 1)); float newAngle = arg.angleDeg - 90 + (degres * (i + 1));
draw_calls.push_back((draw_args){next, newAngle, arg.lenghth * next_len, arg.dep + 1}); draw_calls.push_back((DrawArgs){next, newAngle, arg.lenghth * next_len, arg.dep + 1});
} }
} }
void Tree::draw_tree(draw_args first) void Tree::drawTree()
{ {
draw_calls.push_back(first); draw_calls.push_back((DrawArgs){start, 0, size / 4, 1});
while (!draw_calls.empty()) while (!draw_calls.empty())
{ {
draw_branch(draw_calls.front()); drawBranch();
draw_calls.pop_front(); draw_calls.pop_front();
} }
} }
void Tree::generate_tree() void Tree::generateBranches()
{ {
for (size_t i = 0; i < MAX_DEPTH; i++) for (size_t i = 0; i < MAX_DEPTH; i++)
{ {
uint8_t r = GetRandomValue(0, 255); uint8_t r = GetRandomValue(0, 255);
uint8_t g = GetRandomValue(0, 255); uint8_t g = GetRandomValue(0, 255);
uint8_t b = GetRandomValue(0, 255); uint8_t b = GetRandomValue(0, 255);
tree[i].color = (Color){r, g, b, 255}; branches[i].color = (Color){r, g, b, 255};
tree[i].numOfBranches = GetRandomValue(1, 3); branches[i].numOfBranches = GetRandomValue(1, 3);
tree[i].lenghthRatio = ((float)GetRandomValue(600, 700)) / 1000.0f; branches[i].lenghthRatio = ((float)GetRandomValue(600, 700)) / 1000.0f;
} }
tree[0].color = tree[1].color; branches[0].color = branches[1].color;
} }
void Tree::new_tree()
{
generate_tree();
BeginTextureMode(target);
ClearBackground(WHITE);
draw_tree((draw_args){start, 0, size / 4, 1});
EndTextureMode();
}