#include "canvas/BackGround.hpp" #include "Math.hpp" #include "raylib.h" #include "rlgl.h" #include "raymath.h" #include "external/stb_perlin.h" // Public void BackGround::init(int size) { this->size = size; } void BackGround::newGen() { ClearBackground(backGroundColor); starts(); moon(); mounten(20, 0.3, (int)(mounten1 * size), (int)(mounten2 * size), backMountenColor); mounten(21, 0.3, (int)(mounten2 * size), (int)(mounten3 * size), ColorLerp(backMountenColor, frontMountenColor, colorRatio1)); mounten(23, 0.3, (int)(mounten3 * size), (int)(mounten4 * size), ColorLerp(backMountenColor, frontMountenColor, colorRatio2)); } void BackGround::starts() { rlSetTexture(texShapes.id); rlBegin(RL_TRIANGLES); rlNormal3f(0.0f, 0.0f, 1.0f); bool star = true; 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); if (star) { // topLeft rlVertex2f(x, y); // bottomLeft rlVertex2f(x, y + weight); // topRight rlVertex2f(x + weight, y); } else { // bottomRight rlVertex2f(x + weight, y + weight); // topRight rlVertex2f(x + weight, y); // bottomLeft rlVertex2f(x, y + weight); } star = !star; } rlEnd(); rlSetTexture(0); } void BackGround::moon() { int xpos = GetRandomValue(100, size - 100); int ypos = GetRandomValue(100, (int)(maxYPosOfMoon * size)); int r = GetRandomValue((int)(minSizeOfMoon * size), (int)(maxSizeOfMoon * size)); int ring1 = (float)r * bigRingRatio; int ring2 = (float)r * smallRingRatio; DrawCircle(xpos, ypos, r + ring1, ColorLerp(backGroundColor, moonColor, bigRingBlend)); DrawCircle(xpos, ypos, r + ring2, ColorLerp(backGroundColor, moonColor, smallRingBlend)); DrawCircle(xpos, ypos, r, moonColor); } void BackGround::mounten(size_t mountenSegments, float scale, int min, int max, Color color) { // get random offset in the perlin noise int offsetX = GetRandomValue(20, 1000); int offsetY = GetRandomValue(20, 1000); int x = 0; 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); rlBegin(RL_TRIANGLES); rlNormal3f(0.0f, 0.0f, 1.0f); rlColor4ub(color.r, color.g, color.b, color.a); for (size_t i = 1; i <= mountenSegments; i++) { // topLeft rlVertex2f(x, y); // bottomLeft 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; // topRight rlVertex2f(x, y); // bottomRight rlVertex2f(x, size); // topRight rlVertex2f(x, y); rlVertex2f(x - diff, size); } rlEnd(); rlSetTexture(0); }