diplomska_naloga/src/BackGround.cpp
2024-03-09 22:47:57 +01:00

124 lines
2.7 KiB
C++

#include "BackGround.hpp"
#include "Math.hpp"
#include "raylib.h"
#include "rlgl.h"
#include "external/stb_perlin.h"
// Public
void BackGround::init(int size)
{
this->size = size;
mountenData.assign(mountenDataCap, 0);
}
void BackGround::newGen()
{
ClearBackground(backGroundColor);
starts();
moon();
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 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, maxYPosOfMoon);
int r = GetRandomValue(minSizeOfMoon, maxSizeOfMoon);
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()
{
int len = 20;
int offsetX = GetRandomValue(20, 1000);
int offsetY = GetRandomValue(20, 1000);
float sc = (4 / (float)len);
for (size_t i = 0; i < mountenData.size(); i++)
{
float nx = (float)(i + offsetX) * sc;
float p = stb_perlin_fbm_noise3(nx, offsetY, 1.0f, 2.0f, 0.5f, 6);
if (p < -1.0f)
p = -1.0f;
if (p > 1.0f)
p = 1.0f;
// mountenData[i] = (p + 1.0f) / 2.0f;
mountenData[i] = p;
}
drawMounten(600, 100, mountenColor);
}
void BackGround::drawMounten(int line, int varience, Color color)
{
int x = 0;
int diff = size / (mountenData.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 + (mountenData[0] * varience);
for (size_t i = 1; i <= mountenData.size(); i++)
{
// topLeft
rlTexCoord2f(0, 0);
rlVertex2f(x, pervY);
// bottomLeft
rlTexCoord2f(0, 1);
rlVertex2f(x, size);
x += diff;
pervY = line + (mountenData[i] * varience);
// bottomRight
rlTexCoord2f(1, 1);
rlVertex2f(x, size);
// topRight
rlTexCoord2f(1, 0);
rlVertex2f(x, pervY);
}
rlEnd();
rlSetTexture(0);
}