diff --git a/inc/App.hpp b/inc/App.hpp new file mode 100644 index 0000000..aa7445f --- /dev/null +++ b/inc/App.hpp @@ -0,0 +1,23 @@ +#include +#include "Tree.hpp" + +class App +{ + +public: + App(int screenWidth, int screenHeight); + ~App() = default; + void init(); + void update(); + void draw(); + void deinit(); + +private: + int pos = 0; + int screenWidth, screenHeight; + std::array trees; + Vector2 center; + float rotation = 0.0f; + Rectangle destB; + Rectangle destA; +}; diff --git a/src/App.cpp b/src/App.cpp new file mode 100644 index 0000000..13a016c --- /dev/null +++ b/src/App.cpp @@ -0,0 +1,51 @@ +#include + +#include "App.hpp" +#include "Math.hpp" + +#include "raylib.h" +#include "raymath.h" + +App::App(int screenWidth, int screenHeight) : screenWidth(screenWidth), screenHeight(screenHeight), trees({Tree(screenWidth), Tree(screenWidth)}) +{ +} + +void App::init() +{ + for (auto &&tree : trees) + tree.newTree(); + + center = {(float)screenWidth / 2, (float)screenHeight / 2}; + float rotation = 0.0f; + destA = CalculateRect(center, rotation, screenWidth, screenWidth); + destB = {destA.x, destA.y, (float)screenWidth, (float)screenWidth}; +} +void App::update() +{ + if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) + { + int mouseX = GetMouseX(); + center.x = mouseX; + center.y = GetMouseY(); + float l = (float)mouseX / screenWidth; + rotation = Lerp(45.0f, -45.0f, l); + destA = CalculateRect(center, rotation, screenWidth, screenWidth); + } + + if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) + { + trees[1 - pos].newTree(); + pos = 1 - pos; + rotation = 0.0f; + center.x = screenWidth / 2; + center.y = screenHeight / 2; + destA = CalculateRect(center, rotation, screenWidth, screenWidth); + } +} +void App::draw() +{ + ClearBackground(RED); + trees[pos].draw(destB, 0.0f); + trees[1 - pos].draw(destA, rotation); +} +void App::deinit() {} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 1528d37..4191300 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,10 +1,5 @@ -#include #include "raylib.h" -#include "raymath.h" -#include "Tree.hpp" -#include "Math.hpp" - -#define MAX_TREES 2 +#include "App.hpp" int main(void) { @@ -21,42 +16,13 @@ int main(void) #endif SetTargetFPS(60); { - std::array trees = {Tree(screenWidth), Tree(screenWidth)}; - - for (auto &&tree : trees) - tree.newTree(); - int pos = 0; - - Vector2 center = {(float)screenWidth / 2, (float)screenHeight / 2}; - float rotation = 0.0f; - Rectangle destA = CalculateRect(center, rotation, screenWidth, screenWidth); - Rectangle destB = {destA.x, destA.y, (float)screenWidth, (float)screenWidth}; - + App app(screenWidth, screenHeight); + app.init(); while (!WindowShouldClose()) { - if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) - { - int mouseX = GetMouseX(); - center.x = mouseX; - center.y = GetMouseY(); - float l = (float)mouseX / screenWidth; - rotation = Lerp(45.0f, -45.0f, l); - destA = CalculateRect(center, rotation, screenWidth, screenWidth); - } - - if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT)) - { - trees[1 - pos].newTree(); - pos = 1 - pos; - rotation = 0.0f; - center.x = screenWidth / 2; - center.y = screenHeight / 2; - destA = CalculateRect(center, rotation, screenWidth, screenWidth); - } + app.update(); BeginDrawing(); - ClearBackground(RED); - trees[pos].draw(destB, 0.0f); - trees[1 - pos].draw(destA, rotation); + app.draw(); EndDrawing(); } }