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"
class App
@ -15,7 +15,8 @@ public:
private:
int pos = 0;
int screenWidth, screenHeight;
std::vector<Canvas> canvases;
Canvas canvas;
std::array< RenderTexture2D, 2> canvasTexure = { 0 };
float rotation = 0.0f;
Rectangle destB;
Rectangle destA;

View File

@ -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();
float GetRandomFloat();
void drawTexureWithRotation(RenderTexture2D& target, Rectangle& dest, float rotation);

View File

@ -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};
};

View File

@ -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]);
}
}

View File

@ -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);
}

View File

@ -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);
}