diff --git a/inc/App.hpp b/inc/App.hpp index 0162b5c..8899d45 100644 --- a/inc/App.hpp +++ b/inc/App.hpp @@ -1,5 +1,5 @@ #include -#include "Tree.hpp" +#include "Canvas.hpp" class App { @@ -15,7 +15,7 @@ public: private: int pos = 0; int screenWidth, screenHeight; - std::vector trees; + std::vector canvases; Vector2 center; float rotation = 0.0f; Rectangle destB; diff --git a/inc/Canvas.hpp b/inc/Canvas.hpp new file mode 100644 index 0000000..7087ef9 --- /dev/null +++ b/inc/Canvas.hpp @@ -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}; +}; diff --git a/inc/Tree.hpp b/inc/Tree.hpp index 6a713d8..b8b0dd5 100644 --- a/inc/Tree.hpp +++ b/inc/Tree.hpp @@ -24,15 +24,13 @@ class Tree { public: - Tree(int size); - ~Tree(); - void draw(Rectangle dest, float rotation); - void newTree(); - void deinit(); + Tree() = default; + ~Tree() = default; + void init(int size); + void newGen(); private: int size = 0; - RenderTexture2D target = {0}; Vector2 start = {0}; std::vector branches; std::list draw_calls; diff --git a/src/App.cpp b/src/App.cpp index 8c04266..41a8f2b 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -10,17 +10,20 @@ void App::init(int screenWidth, int screenHeight) { this->screenWidth = screenWidth; this->screenHeight = screenHeight; - trees.emplace_back(screenWidth); - trees.emplace_back(screenWidth); + canvases.assign(2, {}); - for (auto &&tree : trees) - tree.newTree(); + for (auto &&canvas : canvases) + { + canvas.init(screenWidth); + canvas.newGen(); + } center = {(float)screenWidth / 2, (float)screenHeight / 2}; float rotation = 0.0f; destA = CalculateRect(center, rotation, screenWidth, screenWidth); destB = {destA.x, destA.y, (float)screenWidth, (float)screenWidth}; } + void App::update() { if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) @@ -35,7 +38,7 @@ void App::update() if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) { - trees[1 - pos].newTree(); + canvases[1 - pos].newGen(); pos = 1 - pos; rotation = 0.0f; center.x = screenWidth / 2; @@ -47,14 +50,14 @@ void App::update() void App::draw() { ClearBackground(RED); - trees[pos].draw(destB, 0.0f); - trees[1 - pos].draw(destA, rotation); + canvases[pos].draw(destB, 0.0f); + canvases[1 - pos].draw(destA, rotation); } 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(); } } \ No newline at end of file diff --git a/src/Canvas.cpp b/src/Canvas.cpp new file mode 100644 index 0000000..7fcda7e --- /dev/null +++ b/src/Canvas.cpp @@ -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); +} \ No newline at end of file diff --git a/src/Tree.cpp b/src/Tree.cpp index e1b147f..11c1f87 100644 --- a/src/Tree.cpp +++ b/src/Tree.cpp @@ -5,41 +5,20 @@ #include "Math.hpp" // Public -Tree::Tree(int size) +void Tree::init(int size) { this->size = size; - target = LoadRenderTexture(size, size); start.x = size / 2; start.y = size; branches.assign(11, {}); } -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() +void Tree::newGen() { generateBranches(); - BeginTextureMode(target); - ClearBackground(WHITE); drawTree(); - EndTextureMode(); -} - -void Tree::deinit() -{ - UnloadRenderTexture(target); } // Private