Extract RenderTexture from Canvas

For easyer managment of texures and potential buffers
This commit is contained in:
Nikola Petrov 2024-08-12 22:39:52 +02:00
parent 2f4ad253a1
commit d7afa1afc9
6 changed files with 27 additions and 31 deletions

View File

@ -1,4 +1,4 @@
#include <vector> #include <array>
#include "canvas/Canvas.hpp" #include "canvas/Canvas.hpp"
class App class App
@ -15,7 +15,8 @@ public:
private: private:
int pos = 0; int pos = 0;
int screenWidth, screenHeight; int screenWidth, screenHeight;
std::vector<Canvas> canvases; Canvas canvas;
std::array< RenderTexture2D, 2> canvasTexure = { 0 };
float rotation = 0.0f; float rotation = 0.0f;
Rectangle destB; Rectangle destB;
Rectangle destA; Rectangle destA;

View File

@ -4,3 +4,5 @@ Color ColorLerp(Color c1, Color c2, float amount);
Color ColorAdd(Color c, int add); Color ColorAdd(Color c, int add);
Vector2 CalculateVector(float rotation, float offSet, float len); Vector2 CalculateVector(float rotation, float offSet, float len);
float GetRandomFloat(); float GetRandomFloat();
void drawTexureWithRotation(RenderTexture2D& target, Rectangle& dest, float rotation);

View File

@ -9,12 +9,9 @@ public:
Canvas() = default; Canvas() = default;
~Canvas() = default; ~Canvas() = default;
void init(int size); void init(int size);
void newGen(); void newGen(RenderTexture2D& target);
void draw(Rectangle dest, float rotation);
void deinit();
private: private:
BackGround backGround; BackGround backGround;
Tree tree; Tree tree;
RenderTexture2D target = {0};
}; };

View File

@ -11,14 +11,16 @@ void App::init(int screenWidth, int screenHeight)
{ {
this->screenWidth = screenWidth; this->screenWidth = screenWidth;
this->screenHeight = screenHeight; 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); canvasTexure[i] = LoadRenderTexture(screenWidth, screenWidth);
canvas.newGen();
} }
canvas.newGen(canvasTexure[0]);
canvas.newGen(canvasTexure[1]);
float posY = (screenHeight - screenWidth) / 2; float posY = (screenHeight - screenWidth) / 2;
destA = {0, posY, (float)screenWidth, (float)screenWidth}; destA = {0, posY, (float)screenWidth, (float)screenWidth};
destB = destA; destB = destA;
@ -47,7 +49,7 @@ void App::update()
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT) && validHit) if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT) && validHit)
{ {
canvases[1 - pos].newGen(); canvas.newGen(canvasTexure[1 - pos]);
pos = 1 - pos; pos = 1 - pos;
rotation = 0.0f; rotation = 0.0f;
destA = destB; destA = destB;
@ -57,14 +59,14 @@ void App::update()
void App::draw() void App::draw()
{ {
ClearBackground(RED); ClearBackground(RED);
canvases[pos].draw(destB, 0.0f); drawTexureWithRotation(canvasTexure[pos], destB, 0.0f);
canvases[1 - pos].draw(destA, rotation); drawTexureWithRotation(canvasTexure[1 - pos], destA, rotation);
} }
void App::deinit() 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]);
} }
} }

View File

@ -36,3 +36,11 @@ float GetRandomFloat()
{ {
return GetRandomValue(0, 100000) / 100000.0f; 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);
}

View File

@ -4,10 +4,9 @@ void Canvas::init(int size)
{ {
backGround.init(size); backGround.init(size);
tree.init(size); tree.init(size);
target = LoadRenderTexture(size, size);
} }
void Canvas::newGen() void Canvas::newGen(RenderTexture2D& target)
{ {
BeginTextureMode(target); BeginTextureMode(target);
ClearBackground(WHITE); ClearBackground(WHITE);
@ -17,16 +16,3 @@ void Canvas::newGen()
EndTextureMode(); 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);
}