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

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