Add Canvas

Canvas contains the RenderTexture2D
contains the Tree
and now it's eazy to add more modules
This commit is contained in:
Nikola Petrov 2024-03-09 01:03:44 +01:00
parent 45422940ad
commit 611605550d
6 changed files with 68 additions and 40 deletions

View File

@ -1,5 +1,5 @@
#include <vector> #include <vector>
#include "Tree.hpp" #include "Canvas.hpp"
class App class App
{ {
@ -15,7 +15,7 @@ public:
private: private:
int pos = 0; int pos = 0;
int screenWidth, screenHeight; int screenWidth, screenHeight;
std::vector<Tree> trees; std::vector<Canvas> canvases;
Vector2 center; Vector2 center;
float rotation = 0.0f; float rotation = 0.0f;
Rectangle destB; Rectangle destB;

18
inc/Canvas.hpp Normal file
View File

@ -0,0 +1,18 @@
#include "raylib.h"
#include "Tree.hpp"
class Canvas
{
public:
Canvas() = default;
~Canvas() = default;
void init(int size);
void newGen();
void draw(Rectangle dest, float rotation);
void deinit();
private:
Tree tree;
RenderTexture2D target = {0};
};

View File

@ -24,15 +24,13 @@ class Tree
{ {
public: public:
Tree(int size); Tree() = default;
~Tree(); ~Tree() = default;
void draw(Rectangle dest, float rotation); void init(int size);
void newTree(); void newGen();
void deinit();
private: private:
int size = 0; int size = 0;
RenderTexture2D target = {0};
Vector2 start = {0}; Vector2 start = {0};
std::vector<Branch> branches; std::vector<Branch> branches;
std::list<DrawArgs> draw_calls; std::list<DrawArgs> draw_calls;

View File

@ -10,17 +10,20 @@ void App::init(int screenWidth, int screenHeight)
{ {
this->screenWidth = screenWidth; this->screenWidth = screenWidth;
this->screenHeight = screenHeight; this->screenHeight = screenHeight;
trees.emplace_back(screenWidth); canvases.assign(2, {});
trees.emplace_back(screenWidth);
for (auto &&tree : trees) for (auto &&canvas : canvases)
tree.newTree(); {
canvas.init(screenWidth);
canvas.newGen();
}
center = {(float)screenWidth / 2, (float)screenHeight / 2}; center = {(float)screenWidth / 2, (float)screenHeight / 2};
float rotation = 0.0f; float rotation = 0.0f;
destA = CalculateRect(center, rotation, screenWidth, screenWidth); destA = CalculateRect(center, rotation, screenWidth, screenWidth);
destB = {destA.x, destA.y, (float)screenWidth, (float)screenWidth}; destB = {destA.x, destA.y, (float)screenWidth, (float)screenWidth};
} }
void App::update() void App::update()
{ {
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) if (IsMouseButtonDown(MOUSE_BUTTON_LEFT))
@ -35,7 +38,7 @@ void App::update()
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT))
{ {
trees[1 - pos].newTree(); canvases[1 - pos].newGen();
pos = 1 - pos; pos = 1 - pos;
rotation = 0.0f; rotation = 0.0f;
center.x = screenWidth / 2; center.x = screenWidth / 2;
@ -47,14 +50,14 @@ void App::update()
void App::draw() void App::draw()
{ {
ClearBackground(RED); ClearBackground(RED);
trees[pos].draw(destB, 0.0f); canvases[pos].draw(destB, 0.0f);
trees[1 - pos].draw(destA, rotation); canvases[1 - pos].draw(destA, rotation);
} }
void App::deinit() void App::deinit()
{ {
for (size_t i = 0; i < trees.size(); i++) for (size_t i = 0; i < canvases.size(); i++)
{ {
trees[i].deinit(); canvases[i].deinit();
} }
} }

30
src/Canvas.cpp Normal file
View File

@ -0,0 +1,30 @@
#include "Canvas.hpp"
void Canvas::init(int size)
{
tree.init(size);
target = LoadRenderTexture(size, size);
}
void Canvas::newGen()
{
BeginTextureMode(target);
ClearBackground(WHITE);
tree.newGen();
EndTextureMode();
}
void Canvas::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 Canvas::deinit()
{
UnloadRenderTexture(target);
}

View File

@ -5,41 +5,20 @@
#include "Math.hpp" #include "Math.hpp"
// Public // Public
Tree::Tree(int size) void Tree::init(int size)
{ {
this->size = size; this->size = size;
target = LoadRenderTexture(size, size);
start.x = size / 2; start.x = size / 2;
start.y = size; start.y = size;
branches.assign(11, {}); branches.assign(11, {});
} }
Tree::~Tree() void Tree::newGen()
{
}
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(); generateBranches();
BeginTextureMode(target);
ClearBackground(WHITE);
drawTree(); drawTree();
EndTextureMode();
}
void Tree::deinit()
{
UnloadRenderTexture(target);
} }
// Private // Private