219 lines
5.4 KiB
C++
219 lines
5.4 KiB
C++
#include <cmath>
|
|
|
|
#include "canvas/BackGround.hpp"
|
|
#include "canvas/BackGroundColors.hpp"
|
|
#include "canvas/Circle.hpp"
|
|
#include "canvas/stb_perlin.h"
|
|
|
|
#include <raylib.h>
|
|
#include <rlgl.h>
|
|
#include <raymath.h>
|
|
|
|
constexpr static size_t numOfStarts = 150;
|
|
constexpr static float moonXOffset = 0.1f;
|
|
constexpr static float minSizeOfMoon = 0.1f;
|
|
constexpr static float maxSizeOfMoon = 0.15f;
|
|
constexpr static float maxYPosOfMoon = 0.80f;
|
|
constexpr static float mounten1min = 0.65f;
|
|
constexpr static float mounten1max = 0.85f;
|
|
constexpr static float mounten2min = 0.80f;
|
|
constexpr static float mounten2max = 0.90;
|
|
constexpr static float mounten3min = 0.90f;
|
|
constexpr static float mounten3max = 0.95f;
|
|
|
|
Color ColorAddValue(Color c, int add)
|
|
{
|
|
int r = std::clamp(c.r + add, 0, 255);
|
|
int g = std::clamp(c.g + add, 0, 255);
|
|
int b = std::clamp(c.b + add, 0, 255);
|
|
|
|
return {(unsigned char)r, (unsigned char)g, (unsigned char)b, c.a};
|
|
}
|
|
|
|
Color ColorAdd(Color c1, Color c2)
|
|
{
|
|
int r = std::clamp(c1.r + c2.r, 0, 255);
|
|
int g = std::clamp(c1.g + c2.g, 0, 255);
|
|
int b = std::clamp(c1.b + c2.b, 0, 255);
|
|
int a = std::clamp(c1.a + c2.a, 0, 255);
|
|
|
|
return {(unsigned char)r, (unsigned char)g, (unsigned char)b, (unsigned char)a};
|
|
}
|
|
|
|
// Public
|
|
void BackGround::init(int canvasSize)
|
|
{
|
|
this->canvasSize = canvasSize;
|
|
}
|
|
|
|
void BackGround::deinit()
|
|
{
|
|
}
|
|
|
|
void BackGround::draw(Dna *dna)
|
|
{
|
|
m_dna = dna;
|
|
mountenSeed = dna->mountenSeed;
|
|
starSeed = dna->starSeed;
|
|
int time = getTime();
|
|
int colorSet = getColorSet();
|
|
BackGroundColors::setColor(colorSet, time);
|
|
|
|
if (colorSet == 3)
|
|
{
|
|
ClearBackground(BackGroundColors::backGroundColor);
|
|
drawStars();
|
|
}
|
|
else
|
|
{
|
|
DrawRectangleGradientV(0, 0, canvasSize, canvasSize, ColorAddValue(BackGroundColors::backGroundColor, 60), BackGroundColors::backGroundColor);
|
|
}
|
|
|
|
drawSun();
|
|
drawMounten(150, (int)(mounten1min * canvasSize), (int)(mounten1max * canvasSize), BackGroundColors::MountenColor1, 5);
|
|
drawMounten(100, (int)(mounten2min * canvasSize), (int)(mounten2max * canvasSize), BackGroundColors::MountenColor2, 3);
|
|
drawMounten(50, (int)(mounten3min * canvasSize), (int)(mounten3max * canvasSize), BackGroundColors::MountenColor3, 1);
|
|
}
|
|
|
|
void BackGround::drawStars()
|
|
{
|
|
rlSetTexture(1);
|
|
rlBegin(RL_TRIANGLES);
|
|
rlNormal3f(0.0f, 0.0f, 1.0f);
|
|
bool star = true;
|
|
|
|
for (size_t i = 0; i < numOfStarts; i++)
|
|
{
|
|
int x = mrand::getFloat(&starSeed) * canvasSize;
|
|
int y = mrand::getFloat(&starSeed) * canvasSize;
|
|
int weight = mrand::getFloat(&starSeed) * 3 + 1;
|
|
float alph = Normalize(mrand::getFloat(&starSeed), 0.04f, 0.8f);
|
|
Color color = ColorLerp(BackGroundColors::backGroundColor, BackGroundColors::starColor, alph);
|
|
|
|
rlColor4ub(color.r, color.g, color.b, color.a);
|
|
if (star)
|
|
{
|
|
// topLeft
|
|
rlVertex2f(x, y);
|
|
// bottomLeft
|
|
rlVertex2f(x, y + weight);
|
|
// topRight
|
|
rlVertex2f(x + weight, y);
|
|
}
|
|
else
|
|
{
|
|
// bottomRight
|
|
rlVertex2f(x + weight, y + weight);
|
|
// topRight
|
|
rlVertex2f(x + weight, y);
|
|
// bottomLeft
|
|
rlVertex2f(x, y + weight);
|
|
}
|
|
star = !star;
|
|
}
|
|
rlEnd();
|
|
rlSetTexture(0);
|
|
}
|
|
|
|
void BackGround::drawSun()
|
|
{
|
|
int r = ((m_dna->moonY / 255.0f * (maxSizeOfMoon - minSizeOfMoon)) + minSizeOfMoon) * canvasSize;
|
|
int xpos = Lerp(canvasSize * moonXOffset, canvasSize - canvasSize * moonXOffset, m_dna->moonX / 255.0f);
|
|
int ypos = Lerp(canvasSize * moonXOffset, maxYPosOfMoon * canvasSize, m_dna->moonY / 255.0f);
|
|
|
|
if (getColorSet() == 3)
|
|
{
|
|
Circle::setColor(BackGroundColors::moonColor);
|
|
r = (((m_dna->moonSize / 255.0f) * (maxSizeOfMoon - minSizeOfMoon)) + minSizeOfMoon) * canvasSize;
|
|
}
|
|
else
|
|
{
|
|
Color color = {0};
|
|
color.r = 255;
|
|
color.g = std::lerp(200, 50, m_dna->moonY / 255.0f);
|
|
color.b = std::lerp(50, 0, m_dna->moonY / 255.0f);
|
|
color.a = 255;
|
|
|
|
Circle::setColor(color);
|
|
}
|
|
Circle::draw(xpos, ypos, r);
|
|
}
|
|
|
|
void BackGround::drawMounten(size_t mountenSegments, int min, int max, Color color, float scale)
|
|
{
|
|
float x = 0;
|
|
float diff = (float)canvasSize / (mountenSegments - 1);
|
|
|
|
float ny = mrand::getFloat(&mountenSeed);
|
|
float nz = mrand::getFloat(&mountenSeed);
|
|
|
|
int offsetX = mrand::getFloat(&mountenSeed) * 255;
|
|
float nx = (float)(0 + offsetX) * (scale / (float)mountenSegments);
|
|
|
|
float p = stb_perlin_fbm_noise3(nx, ny, nz, 2.0f, 0.5f, 6);
|
|
|
|
float np = (p + 1.0f) / 2.0f;
|
|
|
|
int y = Lerp(min, max, np);
|
|
rlSetTexture(1);
|
|
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, canvasSize);
|
|
|
|
nx = (float)(i + offsetX) * (scale / (float)mountenSegments);
|
|
|
|
p = stb_perlin_fbm_noise3(nx, ny, nz, 2.0f, 0.5f, 6);
|
|
|
|
if (p < -1.0f)
|
|
p = -1.0f;
|
|
if (p > 1.0f)
|
|
p = 1.0f;
|
|
|
|
np = (p + 1.0f) / 2.0f;
|
|
|
|
y = Lerp(min, max, np);
|
|
x += diff;
|
|
// topRight
|
|
rlVertex2f(x, y);
|
|
|
|
// bottomRight
|
|
rlVertex2f(x, canvasSize);
|
|
// topRight
|
|
rlVertex2f(x, y);
|
|
|
|
rlVertex2f(x - diff, canvasSize);
|
|
}
|
|
rlEnd();
|
|
rlSetTexture(0);
|
|
}
|
|
|
|
int BackGround::getColorSet()
|
|
{
|
|
uint8_t colorSet = m_dna->colorSet;
|
|
if (colorSet < 64)
|
|
return 0;
|
|
if (colorSet < 128)
|
|
return 1;
|
|
if (colorSet < 192)
|
|
return 2;
|
|
return 3;
|
|
}
|
|
|
|
int BackGround::getTime()
|
|
{
|
|
uint8_t ret = m_dna->moonY;
|
|
if (ret < 64)
|
|
return 0;
|
|
if (ret < 128)
|
|
return 1;
|
|
if (ret < 192)
|
|
return 2;
|
|
return 3;
|
|
} |