Add nice mountens

This commit is contained in:
Nikola Petrov 2024-03-09 23:00:02 +01:00
parent bb74873557
commit 03f47bcee5
2 changed files with 21 additions and 33 deletions

View File

@ -13,8 +13,7 @@ public:
private: private:
void starts(); void starts();
void moon(); void moon();
void mounten(); void mounten(size_t mountenSegments, float scale, int min, int max, Color color);
void drawMounten(int line, int varience, Color color);
int size = 0; int size = 0;
Texture2D texShapes = {1, 1, 1, 1, 7}; Texture2D texShapes = {1, 1, 1, 1, 7};
@ -27,8 +26,7 @@ private:
Color moonColor = {240, 240, 190, 255}; Color moonColor = {240, 240, 190, 255};
int minSizeOfMoon = 40; int minSizeOfMoon = 40;
int maxSizeOfMoon = 60; int maxSizeOfMoon = 60;
int maxYPosOfMoon = 600;
Color mountenColor = {28, 28, 38, 255}; Color mountenColor = {28, 28, 38, 255};
size_t mountenDataCap = 20;
std::vector<float> mountenData;
}; };

View File

@ -3,13 +3,13 @@
#include "raylib.h" #include "raylib.h"
#include "rlgl.h" #include "rlgl.h"
#include "raymath.h"
#include "external/stb_perlin.h" #include "external/stb_perlin.h"
// Public // Public
void BackGround::init(int size) void BackGround::init(int size)
{ {
this->size = size; this->size = size;
mountenData.assign(mountenDataCap, 0);
} }
void BackGround::newGen() void BackGround::newGen()
@ -17,7 +17,9 @@ void BackGround::newGen()
ClearBackground(backGroundColor); ClearBackground(backGroundColor);
starts(); starts();
moon(); moon();
mounten(); mounten(20, 0.3, 550, 650, mountenColor);
mounten(21, 0.3, 650, 730, ColorLerp(mountenColor, BLACK, 0.1f));
mounten(23, 0.3, 730, 780, ColorLerp(mountenColor, BLACK, 0.3f));
} }
void BackGround::starts() void BackGround::starts()
@ -66,58 +68,46 @@ void BackGround::moon()
DrawCircle(xpos, ypos, r, moonColor); DrawCircle(xpos, ypos, r, moonColor);
} }
void BackGround::mounten() void BackGround::mounten(size_t mountenSegments, float scale, int min, int max, Color color)
{ {
int len = 20; // get random offset in the perlin noise
int offsetX = GetRandomValue(20, 1000); int offsetX = GetRandomValue(20, 1000);
int offsetY = 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 x = 0;
int diff = size / (mountenData.size() - 1); 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); rlSetTexture(texShapes.id);
rlBegin(RL_QUADS); rlBegin(RL_QUADS);
rlNormal3f(0.0f, 0.0f, 1.0f); rlNormal3f(0.0f, 0.0f, 1.0f);
rlColor4ub(color.r, color.g, color.b, color.a); rlColor4ub(color.r, color.g, color.b, color.a);
int pervY = line + (mountenData[0] * varience); for (size_t i = 1; i <= mountenSegments; i++)
for (size_t i = 1; i <= mountenData.size(); i++)
{ {
// topLeft // topLeft
rlTexCoord2f(0, 0); rlTexCoord2f(0, 0);
rlVertex2f(x, pervY); rlVertex2f(x, y);
// bottomLeft // bottomLeft
rlTexCoord2f(0, 1); rlTexCoord2f(0, 1);
rlVertex2f(x, size); 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; x += diff;
pervY = line + (mountenData[i] * varience);
// bottomRight // bottomRight
rlTexCoord2f(1, 1); rlTexCoord2f(1, 1);
rlVertex2f(x, size); rlVertex2f(x, size);
// topRight // topRight
rlTexCoord2f(1, 0); rlTexCoord2f(1, 0);
rlVertex2f(x, pervY); rlVertex2f(x, y);
} }
rlEnd(); rlEnd();
rlSetTexture(0); rlSetTexture(0);