treender/src/canvas/BackGround.cpp
Nikola Petrov e62b0eb6bf Change fro, RL_QUADS to RL_TRIANGLES
for web support
2024-03-31 00:35:33 +01:00

114 lines
2.8 KiB
C++

#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_TRIANGLES);
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
rlVertex2f(x, y);
// bottomLeft
rlVertex2f(x, y + weight);
// topRight
rlVertex2f(x + weight, y);
// bottomRight
rlVertex2f(x + weight, y + weight);
// topRight
rlVertex2f(x + weight, y);
// bottomLeft
rlVertex2f(x, y + weight);
}
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_TRIANGLES);
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
rlVertex2f(x, y);
// bottomLeft
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;
// topRight
rlVertex2f(x, y);
// bottomRight
rlVertex2f(x, size);
// topRight
rlVertex2f(x, y);
rlVertex2f(x - diff, size);
}
rlEnd();
rlSetTexture(0);
}