diff --git a/.vscode/launch.json b/.vscode/launch.json index 665e2de..bba8801 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -27,7 +27,7 @@ "ignoreFailures": true } ], - "preLaunchTask": "build" + "preLaunchTask": "build-deb" } ] } \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 7b78eeb..94db072 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -10,11 +10,11 @@ ] }, { - "label": "clean", + "label": "build-deb", "type": "shell", "command": "make", "args": [ - "clean" + "main" ] } ] diff --git a/inc/Math.hpp b/inc/Math.hpp index e607c19..d1a42d5 100644 --- a/inc/Math.hpp +++ b/inc/Math.hpp @@ -1,12 +1,5 @@ #include "raylib.h" #include "raymath.h" -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; -} \ No newline at end of file +Color ColorLerp(Color c1, Color c2, float amount); +Rectangle CalculateRect(Vector2 center, float rotation, float width, float height); \ No newline at end of file diff --git a/main.cpp b/main.cpp index cbff358..25b7ca9 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,7 @@ #include "raylib.h" #include "Tree.hpp" +#include "Math.hpp" +#include int main(void) { @@ -19,21 +21,36 @@ int main(void) Tree tree(800); tree.newTree(); - int yPos = (screenHeight - screenWidth) / 2; - Rectangle dest = {0, (float)yPos, (float)screenWidth, (float)screenWidth}; + Vector2 center = {(float)screenWidth / 2, (float)screenHeight / 2}; float rotation = 0.0f; + Rectangle dest = CalculateRect(center, rotation, screenWidth, screenWidth); + 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(); + rotation = 0.0f; + center.x = screenWidth / 2; + center.y = screenHeight / 2; + dest = CalculateRect(center, rotation, screenWidth, screenWidth); } BeginDrawing(); - ClearBackground(WHITE); + ClearBackground(RED); tree.draw(dest, rotation); EndDrawing(); } } + CloseWindow(); return 0; } \ No newline at end of file diff --git a/src/Math.cpp b/src/Math.cpp new file mode 100644 index 0000000..87c4185 --- /dev/null +++ b/src/Math.cpp @@ -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; +} \ No newline at end of file diff --git a/src/Tree.cpp b/src/Tree.cpp index eaca11b..f4d1ed0 100644 --- a/src/Tree.cpp +++ b/src/Tree.cpp @@ -24,6 +24,7 @@ void Tree::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); }