diff --git a/Makefile b/Makefile index 8163ae1..760a355 100644 --- a/Makefile +++ b/Makefile @@ -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 \ No newline at end of file + +countLine: + cloc --exclude-dir=raylib --exclude-lang=JSON,make,Markdown . \ No newline at end of file diff --git a/inc/BackGround.hpp b/inc/BackGround.hpp new file mode 100644 index 0000000..5b8a494 --- /dev/null +++ b/inc/BackGround.hpp @@ -0,0 +1,22 @@ +#include +#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 &data, int line, int varience, Color color); + + size_t numOfStarts = 150; + Color starColor = WHITE; + int size = 0; + Texture2D texShapes = {1, 1, 1, 1, 7}; +}; diff --git a/inc/Canvas.hpp b/inc/Canvas.hpp index 7087ef9..7c8357d 100644 --- a/inc/Canvas.hpp +++ b/inc/Canvas.hpp @@ -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}; }; diff --git a/src/BackGround.cpp b/src/BackGround.cpp new file mode 100644 index 0000000..08acb87 --- /dev/null +++ b/src/BackGround.cpp @@ -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 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 &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); +} \ No newline at end of file diff --git a/src/Canvas.cpp b/src/Canvas.cpp index 7fcda7e..b687a09 100644 --- a/src/Canvas.cpp +++ b/src/Canvas.cpp @@ -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();