Add BackGround

add stary night
This commit is contained in:
Nikola Petrov 2024-03-09 17:18:17 +01:00
parent ad8296dd7d
commit e6f102266c
5 changed files with 144 additions and 3 deletions

View File

@ -11,8 +11,9 @@ setup:
if [ ! -d "raylib" ]; then \
git clone --depth 1 --branch "5.0" git@github.com:raysan5/raylib.git; \
fi
$(shell mkdir -p obj)
if [ ! -d "obj" ]; then \
mkdir -p obj; \
fi
obj/rcore.o: raylib/src/rcore.c
gcc -c $< -o $@ $(RAYOPT) $(RAYFLAGS) $(RAYINCLUDE)
@ -62,5 +63,10 @@ run: main
clean:
rm -rf obj
rm -f main example
cleanAll: clean
rm -rf raylib
rm main example
countLine:
cloc --exclude-dir=raylib --exclude-lang=JSON,make,Markdown .

22
inc/BackGround.hpp Normal file
View File

@ -0,0 +1,22 @@
#include <vector>
#include "raylib.h"
class BackGround
{
public:
BackGround() = default;
~BackGround() = default;
void init(int size);
void newGen();
private:
void starts();
void mounten();
void drawMounten(std::vector<float> &data, int line, int varience, Color color);
size_t numOfStarts = 150;
Color starColor = WHITE;
int size = 0;
Texture2D texShapes = {1, 1, 1, 1, 7};
};

View File

@ -1,4 +1,5 @@
#include "raylib.h"
#include "BackGround.hpp"
#include "Tree.hpp"
class Canvas
@ -13,6 +14,7 @@ public:
void deinit();
private:
BackGround backGround;
Tree tree;
RenderTexture2D target = {0};
};

109
src/BackGround.cpp Normal file
View File

@ -0,0 +1,109 @@
#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);
}

View File

@ -2,6 +2,7 @@
void Canvas::init(int size)
{
backGround.init(size);
tree.init(size);
target = LoadRenderTexture(size, size);
}
@ -11,6 +12,7 @@ void Canvas::newGen()
BeginTextureMode(target);
ClearBackground(WHITE);
backGround.newGen();
tree.newGen();
EndTextureMode();