Add basic rotation

Rotate texuture based on mouse position
This commit is contained in:
Nikola Petrov 2024-03-05 01:51:26 +01:00
parent acb478f970
commit e5f9e8d7f3
6 changed files with 54 additions and 16 deletions

2
.vscode/launch.json vendored
View File

@ -27,7 +27,7 @@
"ignoreFailures": true "ignoreFailures": true
} }
], ],
"preLaunchTask": "build" "preLaunchTask": "build-deb"
} }
] ]
} }

4
.vscode/tasks.json vendored
View File

@ -10,11 +10,11 @@
] ]
}, },
{ {
"label": "clean", "label": "build-deb",
"type": "shell", "type": "shell",
"command": "make", "command": "make",
"args": [ "args": [
"clean" "main"
] ]
} }
] ]

View File

@ -1,12 +1,5 @@
#include "raylib.h" #include "raylib.h"
#include "raymath.h" #include "raymath.h"
Color ColorLerp(Color c1, Color c2, float amount) Color ColorLerp(Color c1, Color c2, float amount);
{ Rectangle CalculateRect(Vector2 center, float rotation, float width, float height);
Color ret{0};
ret.r = Clamp(Lerp(c1.r, c2.r, amount), 0, 255);
ret.g = Clamp(Lerp(c1.g, c2.g, amount), 0, 255);
ret.b = Clamp(Lerp(c1.b, c2.b, amount), 0, 255);
ret.a = Clamp(Lerp(c1.a, c2.a, amount), 0, 255);
return ret;
}

View File

@ -1,5 +1,7 @@
#include "raylib.h" #include "raylib.h"
#include "Tree.hpp" #include "Tree.hpp"
#include "Math.hpp"
#include <cmath>
int main(void) int main(void)
{ {
@ -19,21 +21,36 @@ int main(void)
Tree tree(800); Tree tree(800);
tree.newTree(); tree.newTree();
int yPos = (screenHeight - screenWidth) / 2; Vector2 center = {(float)screenWidth / 2, (float)screenHeight / 2};
Rectangle dest = {0, (float)yPos, (float)screenWidth, (float)screenWidth};
float rotation = 0.0f; float rotation = 0.0f;
Rectangle dest = CalculateRect(center, rotation, screenWidth, screenWidth);
while (!WindowShouldClose()) while (!WindowShouldClose())
{ {
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) if (IsMouseButtonDown(MOUSE_BUTTON_LEFT))
{
int mouseX = GetMouseX();
center.x = mouseX;
float l = (float)mouseX / screenWidth;
rotation = Lerp(45.0f, -45.0f, l);
dest = CalculateRect(center, rotation, screenWidth, screenWidth);
}
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT))
{ {
tree.newTree(); tree.newTree();
rotation = 0.0f;
center.x = screenWidth / 2;
center.y = screenHeight / 2;
dest = CalculateRect(center, rotation, screenWidth, screenWidth);
} }
BeginDrawing(); BeginDrawing();
ClearBackground(WHITE); ClearBackground(RED);
tree.draw(dest, rotation); tree.draw(dest, rotation);
EndDrawing(); EndDrawing();
} }
} }
CloseWindow(); CloseWindow();
return 0; return 0;
} }

27
src/Math.cpp Normal file
View File

@ -0,0 +1,27 @@
#include "Math.hpp"
Color ColorLerp(Color c1, Color c2, float amount)
{
Color ret{0};
ret.r = Clamp(Lerp(c1.r, c2.r, amount), 0, 255);
ret.g = Clamp(Lerp(c1.g, c2.g, amount), 0, 255);
ret.b = Clamp(Lerp(c1.b, c2.b, amount), 0, 255);
ret.a = Clamp(Lerp(c1.a, c2.a, amount), 0, 255);
return ret;
}
Rectangle CalculateRect(Vector2 center, float rotation, float width, float height)
{
Rectangle ret = {0, 0, width, height};
width /= 2; // a
height /= 2; // b
float len = std::sqrt(std::pow(width, 2) + std::pow(height, 2)); // c
rotation += 180.0f; // add 180 to point 0 up
float angle = (rotation * PI) / 180.0f; // from degrees to radians
angle += std::atan(width / height); // add angle alfa
float nx = len * std::sin(angle);
float ny = len * std::cos(angle);
ret.x = center.x + nx;
ret.y = center.y + ny;
return ret;
}

View File

@ -24,6 +24,7 @@ void Tree::draw(Rectangle dest, float rotation)
{ {
Rectangle source = {0, 0, (float)target.texture.width, (float)-target.texture.height}; Rectangle source = {0, 0, (float)target.texture.width, (float)-target.texture.height};
Vector2 origin = {0.0f, 0.0f}; Vector2 origin = {0.0f, 0.0f};
rotation = 360 - rotation;
DrawTexturePro(target.texture, source, dest, origin, rotation, WHITE); DrawTexturePro(target.texture, source, dest, origin, rotation, WHITE);
} }