Move files
BackGround, Canvas, Tree to dir canvas
This commit is contained in:
114
src/canvas/BackGround.cpp
Normal file
114
src/canvas/BackGround.cpp
Normal file
@@ -0,0 +1,114 @@
|
||||
#include "canvas/BackGround.hpp"
|
||||
#include "Math.hpp"
|
||||
|
||||
#include "raylib.h"
|
||||
#include "rlgl.h"
|
||||
#include "raymath.h"
|
||||
#include "external/stb_perlin.h"
|
||||
|
||||
// Public
|
||||
void BackGround::init(int size)
|
||||
{
|
||||
this->size = size;
|
||||
}
|
||||
|
||||
void BackGround::newGen()
|
||||
{
|
||||
ClearBackground(backGroundColor);
|
||||
starts();
|
||||
moon();
|
||||
mounten(20, 0.3, (int)(mounten1 * size), (int)(mounten2 * size), mountenColor);
|
||||
mounten(21, 0.3, (int)(mounten2 * size), (int)(mounten3 * size), ColorLerp(mountenColor, BLACK, 0.1f));
|
||||
mounten(23, 0.3, (int)(mounten3 * size), (int)(mounten4 * size), ColorLerp(mountenColor, BLACK, 0.3f));
|
||||
}
|
||||
|
||||
void BackGround::starts()
|
||||
{
|
||||
rlSetTexture(texShapes.id);
|
||||
rlBegin(RL_QUADS);
|
||||
rlNormal3f(0.0f, 0.0f, 1.0f);
|
||||
|
||||
for (size_t i = 1; i <= numOfStarts; i++)
|
||||
{
|
||||
int x = GetRandomValue(0, size);
|
||||
int y = GetRandomValue(0, size);
|
||||
int weight = GetRandomValue(1, 3);
|
||||
|
||||
float alph = ((float)GetRandomValue(10, 200)) / 255.0f;
|
||||
Color color = ColorLerp(backGroundColor, starColor, alph);
|
||||
|
||||
rlColor4ub(color.r, color.g, color.b, color.a);
|
||||
|
||||
// topLeft
|
||||
rlTexCoord2f(0, 0);
|
||||
rlVertex2f(x, y);
|
||||
// bottomLeft
|
||||
rlTexCoord2f(0, 1);
|
||||
rlVertex2f(x, y + weight);
|
||||
|
||||
// bottomRight
|
||||
rlTexCoord2f(1, 1);
|
||||
rlVertex2f(x + weight, y + weight);
|
||||
// topRight
|
||||
rlTexCoord2f(1, 0);
|
||||
rlVertex2f(x + weight, y);
|
||||
}
|
||||
rlEnd();
|
||||
rlSetTexture(0);
|
||||
}
|
||||
|
||||
void BackGround::moon()
|
||||
{
|
||||
int xpos = GetRandomValue(100, size - 100);
|
||||
int ypos = GetRandomValue(100, (int)(maxYPosOfMoon * size));
|
||||
int r = GetRandomValue((int)(minSizeOfMoon * size), (int)(maxSizeOfMoon * size));
|
||||
|
||||
DrawCircle(xpos, ypos, r + 20, ColorLerp(backGroundColor, moonColor, 0.02f));
|
||||
DrawCircle(xpos, ypos, r + 10, ColorLerp(backGroundColor, moonColor, 0.05f));
|
||||
DrawCircle(xpos, ypos, r, moonColor);
|
||||
}
|
||||
|
||||
void BackGround::mounten(size_t mountenSegments, float scale, int min, int max, Color color)
|
||||
{
|
||||
// get random offset in the perlin noise
|
||||
int offsetX = GetRandomValue(20, 1000);
|
||||
int offsetY = GetRandomValue(20, 1000);
|
||||
|
||||
int x = 0;
|
||||
int diff = size / (mountenSegments - 1);
|
||||
|
||||
float nx = (float)offsetX * scale;
|
||||
float p = stb_perlin_fbm_noise3(nx, offsetY, 1.0f, 2.0f, 0.5f, 6);
|
||||
p = (p + 1.0f) / 2.0f;
|
||||
int y = Lerp(min, max, p);
|
||||
|
||||
rlSetTexture(texShapes.id);
|
||||
rlBegin(RL_QUADS);
|
||||
rlNormal3f(0.0f, 0.0f, 1.0f);
|
||||
rlColor4ub(color.r, color.g, color.b, color.a);
|
||||
|
||||
for (size_t i = 1; i <= mountenSegments; i++)
|
||||
{
|
||||
// topLeft
|
||||
rlTexCoord2f(0, 0);
|
||||
rlVertex2f(x, y);
|
||||
// bottomLeft
|
||||
rlTexCoord2f(0, 1);
|
||||
rlVertex2f(x, size);
|
||||
|
||||
nx = (float)(i + offsetX) * scale;
|
||||
p = stb_perlin_fbm_noise3(nx, offsetY, 1.0f, 2.0f, 0.5f, 6);
|
||||
p = (p + 1.0f) / 2.0f;
|
||||
y = Lerp(min, max, p);
|
||||
x += diff;
|
||||
|
||||
// bottomRight
|
||||
rlTexCoord2f(1, 1);
|
||||
rlVertex2f(x, size);
|
||||
// topRight
|
||||
rlTexCoord2f(1, 0);
|
||||
rlVertex2f(x, y);
|
||||
}
|
||||
rlEnd();
|
||||
rlSetTexture(0);
|
||||
}
|
||||
32
src/canvas/Canvas.cpp
Normal file
32
src/canvas/Canvas.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#include "canvas/Canvas.hpp"
|
||||
|
||||
void Canvas::init(int size)
|
||||
{
|
||||
backGround.init(size);
|
||||
tree.init(size);
|
||||
target = LoadRenderTexture(size, size);
|
||||
}
|
||||
|
||||
void Canvas::newGen()
|
||||
{
|
||||
BeginTextureMode(target);
|
||||
ClearBackground(WHITE);
|
||||
|
||||
backGround.newGen();
|
||||
tree.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);
|
||||
}
|
||||
93
src/canvas/Tree.cpp
Normal file
93
src/canvas/Tree.cpp
Normal file
@@ -0,0 +1,93 @@
|
||||
#include <cmath>
|
||||
#include "raylib.h"
|
||||
#include "raymath.h"
|
||||
#include "canvas/Tree.hpp"
|
||||
#include "Math.hpp"
|
||||
|
||||
// Public
|
||||
void Tree::init(int size)
|
||||
{
|
||||
this->size = size;
|
||||
start.x = size / 2;
|
||||
start.y = size;
|
||||
|
||||
branches.assign(11, {});
|
||||
}
|
||||
|
||||
void Tree::newGen()
|
||||
{
|
||||
generateBranches();
|
||||
|
||||
drawTree();
|
||||
}
|
||||
|
||||
// Private
|
||||
|
||||
Vector2 Tree::drawLine()
|
||||
{
|
||||
DrawArgs arg = draw_calls.front();
|
||||
|
||||
arg.angleDeg += 180.0f;
|
||||
float angle = (arg.angleDeg * PI) / 180.0f;
|
||||
float nx = arg.lenghth * std::sin(angle);
|
||||
float ny = arg.lenghth * std::cos(angle);
|
||||
Vector2 end = {arg.start.x + nx, arg.start.y + ny};
|
||||
|
||||
float thick = 2.0;
|
||||
float fstep = 1.0 / ((arg.lenghth / thick) * 1.5);
|
||||
|
||||
for (float i = 0; i < 1; i += fstep)
|
||||
{
|
||||
Vector2 point = Vector2Lerp(arg.start, end, i);
|
||||
Color color = ColorLerp(branches[arg.dep - 1].color, branches[arg.dep].color, i);
|
||||
DrawCircleV(point, thick, color);
|
||||
}
|
||||
return end;
|
||||
}
|
||||
|
||||
void Tree::drawBranch()
|
||||
{
|
||||
DrawArgs arg = draw_calls.front();
|
||||
if (arg.dep >= MAX_DEPTH)
|
||||
return;
|
||||
|
||||
Vector2 next = drawLine();
|
||||
|
||||
float next_len = branches[arg.dep].lenghthRatio;
|
||||
float sectors = branches[arg.dep].numOfBranches + 1;
|
||||
float degres = 180.0f / sectors;
|
||||
|
||||
for (size_t i = 0; i < branches[arg.dep].numOfBranches; i++)
|
||||
{
|
||||
float newAngle = arg.angleDeg - 90 + (degres * (i + 1));
|
||||
draw_calls.push_back((DrawArgs){next, newAngle, arg.lenghth * next_len, arg.dep + 1});
|
||||
}
|
||||
}
|
||||
|
||||
void Tree::drawTree()
|
||||
{
|
||||
draw_calls.push_back((DrawArgs){start, 0, (float)size / 4, 1});
|
||||
|
||||
while (!draw_calls.empty())
|
||||
{
|
||||
drawBranch();
|
||||
draw_calls.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
void Tree::generateBranches()
|
||||
{
|
||||
for (size_t i = 0; i < MAX_DEPTH; i++)
|
||||
{
|
||||
uint8_t r = GetRandomValue(0, 255);
|
||||
uint8_t g = GetRandomValue(0, 255);
|
||||
uint8_t b = GetRandomValue(0, 255);
|
||||
branches[i].color = (Color){r, g, b, 255};
|
||||
|
||||
branches[i].numOfBranches = GetRandomValue(1, 3);
|
||||
|
||||
branches[i].lenghthRatio = ((float)GetRandomValue(600, 700)) / 1000.0f;
|
||||
}
|
||||
|
||||
branches[0].color = branches[1].color;
|
||||
}
|
||||
Reference in New Issue
Block a user