diff --git a/inc/App.hpp b/inc/App.hpp index 440c132..6a744d3 100644 --- a/inc/App.hpp +++ b/inc/App.hpp @@ -1,4 +1,4 @@ -#include +#include #include "canvas/Canvas.hpp" class App @@ -15,7 +15,8 @@ public: private: int pos = 0; int screenWidth, screenHeight; - std::vector canvases; + Canvas canvas; + std::array< RenderTexture2D, 2> canvasTexure = { 0 }; float rotation = 0.0f; Rectangle destB; Rectangle destA; diff --git a/inc/Math.hpp b/inc/Math.hpp index f583320..bf711d3 100644 --- a/inc/Math.hpp +++ b/inc/Math.hpp @@ -3,4 +3,6 @@ Color ColorLerp(Color c1, Color c2, float amount); Color ColorAdd(Color c, int add); Vector2 CalculateVector(float rotation, float offSet, float len); -float GetRandomFloat(); \ No newline at end of file +float GetRandomFloat(); + +void drawTexureWithRotation(RenderTexture2D& target, Rectangle& dest, float rotation); \ No newline at end of file diff --git a/inc/canvas/Canvas.hpp b/inc/canvas/Canvas.hpp index 388bd53..ff0191f 100644 --- a/inc/canvas/Canvas.hpp +++ b/inc/canvas/Canvas.hpp @@ -9,12 +9,9 @@ public: Canvas() = default; ~Canvas() = default; void init(int size); - void newGen(); - void draw(Rectangle dest, float rotation); - void deinit(); + void newGen(RenderTexture2D& target); private: BackGround backGround; Tree tree; - RenderTexture2D target = {0}; }; diff --git a/src/App.cpp b/src/App.cpp index de0d259..4cb96a7 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -11,13 +11,15 @@ void App::init(int screenWidth, int screenHeight) { this->screenWidth = screenWidth; this->screenHeight = screenHeight; - canvases.assign(2, {}); + this->canvas.init(screenWidth); - for (auto &&canvas : canvases) + for (size_t i = 0; i < canvasTexure.size(); i++) { - canvas.init(screenWidth); - canvas.newGen(); + canvasTexure[i] = LoadRenderTexture(screenWidth, screenWidth); } + + canvas.newGen(canvasTexure[0]); + canvas.newGen(canvasTexure[1]); float posY = (screenHeight - screenWidth) / 2; destA = {0, posY, (float)screenWidth, (float)screenWidth}; @@ -47,7 +49,7 @@ void App::update() if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT) && validHit) { - canvases[1 - pos].newGen(); + canvas.newGen(canvasTexure[1 - pos]); pos = 1 - pos; rotation = 0.0f; destA = destB; @@ -57,14 +59,14 @@ void App::update() void App::draw() { ClearBackground(RED); - canvases[pos].draw(destB, 0.0f); - canvases[1 - pos].draw(destA, rotation); + drawTexureWithRotation(canvasTexure[pos], destB, 0.0f); + drawTexureWithRotation(canvasTexure[1 - pos], destA, rotation); } void App::deinit() { - for (size_t i = 0; i < canvases.size(); i++) + for (size_t i = 0; i < canvasTexure.size(); i++) { - canvases[i].deinit(); + UnloadRenderTexture(canvasTexure[i]); } } \ No newline at end of file diff --git a/src/Math.cpp b/src/Math.cpp index 7c6a877..76edafe 100644 --- a/src/Math.cpp +++ b/src/Math.cpp @@ -36,3 +36,11 @@ float GetRandomFloat() { return GetRandomValue(0, 100000) / 100000.0f; } + +void drawTexureWithRotation(RenderTexture2D& target, 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); +} \ No newline at end of file diff --git a/src/canvas/Canvas.cpp b/src/canvas/Canvas.cpp index 0e0cee6..c70041a 100644 --- a/src/canvas/Canvas.cpp +++ b/src/canvas/Canvas.cpp @@ -4,10 +4,9 @@ void Canvas::init(int size) { backGround.init(size); tree.init(size); - target = LoadRenderTexture(size, size); } -void Canvas::newGen() +void Canvas::newGen(RenderTexture2D& target) { BeginTextureMode(target); ClearBackground(WHITE); @@ -17,16 +16,3 @@ void Canvas::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