167 lines
4.1 KiB
C++
167 lines
4.1 KiB
C++
#include <cmath>
|
|
|
|
#include "canvas/BackGround.hpp"
|
|
#include "canvas/BackGroundColors.hpp"
|
|
#include "canvas/Circle.hpp"
|
|
#include "Math.hpp"
|
|
#include "values/mrand.hpp"
|
|
|
|
#include "stb_perlin.h"
|
|
|
|
#include <raylib.h>
|
|
#include <rlgl.h>
|
|
#include <raymath.h>
|
|
|
|
// Public
|
|
void BackGround::init(int canvasSize)
|
|
{
|
|
this->canvasSize = canvasSize;
|
|
}
|
|
|
|
void BackGround::deinit()
|
|
{
|
|
}
|
|
|
|
void BackGround::draw(Dna *dna)
|
|
{
|
|
Circle::setSoftEdge(true);
|
|
m_dna = dna;
|
|
mountenSeed = dna->mountenSeed;
|
|
starSeed = dna->starSeed;
|
|
|
|
BackGroundColors::setColor(m_dna->colorSet, m_dna->time);
|
|
|
|
if (m_dna->colorSet == 3)
|
|
{
|
|
ClearBackground(BackGroundColors::backGroundColor);
|
|
drawStars();
|
|
}
|
|
else
|
|
{
|
|
DrawRectangleGradientV(0, 0, canvasSize, canvasSize, ColorAdd(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->moon.y * (maxSizeOfMoon - minSizeOfMoon)) + minSizeOfMoon) * canvasSize;
|
|
int xpos = Lerp(canvasSize * moonXOffset, canvasSize - canvasSize * moonXOffset, m_dna->moon.x);
|
|
int ypos = Lerp(canvasSize * moonXOffset, maxYPosOfMoon * canvasSize, m_dna->moon.y);
|
|
|
|
if (m_dna->colorSet == 3)
|
|
{
|
|
Circle::setColor(BackGroundColors::moonColor);
|
|
r = ((m_dna->moon.size * (maxSizeOfMoon - minSizeOfMoon)) + minSizeOfMoon) * canvasSize;
|
|
}
|
|
else
|
|
{
|
|
Color color = {0};
|
|
color.r = 255;
|
|
color.g = std::lerp(200, 50, m_dna->moon.y);
|
|
color.b = std::lerp(50, 0, m_dna->moon.y);
|
|
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);
|
|
} |