Make app class

to handle everithing and easy porting
This commit is contained in:
Nikola Petrov 2024-03-06 11:16:19 +01:00
parent cf006e05e0
commit 05d0f63535
3 changed files with 79 additions and 39 deletions

23
inc/App.hpp Normal file
View File

@ -0,0 +1,23 @@
#include <array>
#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<Tree, 2> trees;
Vector2 center;
float rotation = 0.0f;
Rectangle destB;
Rectangle destA;
};

51
src/App.cpp Normal file
View File

@ -0,0 +1,51 @@
#include <array>
#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() {}

View File

@ -1,10 +1,5 @@
#include <array>
#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<Tree, MAX_TREES> 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();
}
}