treender/src/BackGround.cpp
Nikola Petrov e6f102266c Add BackGround
add stary night
2024-03-09 17:18:17 +01:00

109 lines
2.2 KiB
C++

#include "BackGround.hpp"
#include "raylib.h"
#include "rlgl.h"
#include "external/stb_perlin.h"
// Public
void BackGround::init(int size)
{
this->size = size;
}
void BackGround::newGen()
{
ClearBackground({21, 34, 56, 255});
starts();
// mounten();
}
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 alph = GetRandomValue(10, 100);
int weight = GetRandomValue(1, 3);
rlColor4ub(starColor.r, starColor.g, starColor.b, alph);
// 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::mounten()
{
int len = 20;
int offsetX = GetRandomValue(20, 1000);
int offsetY = GetRandomValue(20, 1000);
float scale = 1;
std::vector<float> data(len);
for (size_t i = 0; i < data.size(); i++)
{
float nx = (float)(i + offsetX) * (scale / (float)len);
float ny = (float)(offsetY) * (scale / (float)1);
float p = stb_perlin_fbm_noise3(nx, ny, 1.0f, 2.0f, 0.5f, 6);
if (p < -1.0f)
p = -1.0f;
if (p > 1.0f)
p = 1.0f;
data[i] = p;
}
drawMounten(data, 500, 100, DARKBLUE);
}
void BackGround::drawMounten(std::vector<float> &data, int line, int varience, Color color)
{
int x = 0;
int diff = size / (data.size() - 1);
rlSetTexture(texShapes.id);
rlBegin(RL_QUADS);
rlNormal3f(0.0f, 0.0f, 1.0f);
rlColor4ub(color.r, color.g, color.b, color.a);
int pervY = line + (data[0] * varience);
for (size_t i = 1; i <= data.size(); i++)
{
// topLeft
rlTexCoord2f(0, 0);
rlVertex2f(x, pervY);
// bottomLeft
rlTexCoord2f(0, 1);
rlVertex2f(x, size);
x += diff;
pervY = line + (data[i] * varience);
// bottomRight
rlTexCoord2f(1, 1);
rlVertex2f(x, size);
// topRight
rlTexCoord2f(1, 0);
rlVertex2f(x, pervY);
}
rlEnd();
rlSetTexture(0);
}