Untitled

 avatar
unknown
plain_text
7 days ago
17 kB
3
Indexable
#pragma once
#include <vector>
#include <random>
#include <cmath>
#include "imgui.h"

namespace Snowflake {

        class SnowflakeSystem {
                public:
                        struct SnowF {
                                        ImVec2 position;
                                                    ImVec2 velocity;
                                                                float size;
                                                                            ImU32 color;
                                                                                        float hue; // Store hue for each snowflake
                        };

                                SnowflakeSystem(int count = 100)
                                            : initialized(false), snowflakeCount(count), generator(std::random_device{}()) {}

                                                    void Init() {
                                                                    snowflakes.clear();
                                                                                snowflakes.reserve(snowflakeCount);

                                                                                            ImVec2 screenSize = ImGui::GetIO().DisplaySize;

                                                                                                        std::uniform_real_distribution<float> xDist(0.0f, screenSize.x);
                                                                                                                    std::uniform_real_distribution<float> yDist(0.0f, screenSize.y);
                                                                                                                                std::uniform_real_distribution<float> speedDist(100.0f, 300.0f);
                                                                                                                                            std::uniform_real_distribution<float> sideMovement(-0.5f, 0.5f);
                                                                                                                                                        std::uniform_real_distribution<float> sizeVariation(0.85f, 1.15f);
                                                                                                                                                                    std::uniform_real_distribution<float> hueDist(0.0f, 1.0f); // Hue between 0 and 1

                                                                                                                                                                                for (int i = 0; i < snowflakeCount; ++i) {
                                                                                                                                                                                                    SnowF flake;
                                                                                                                                                                                                                    flake.position = {xDist(generator), yDist(generator)};
                                                                                                                                                                                                                                    flake.velocity = {sideMovement(generator), speedDist(generator)};
                                                                                                                                                                                                                                                    flake.size = 2.0f * sizeVariation(generator);
                                                                                                                                                                                                                                                                    flake.hue = hueDist(generator); // Randomize initial hue
                                                                                                                                                                                                                                                                                    flake.color = HSVtoRGB(flake.hue, 1.0f, 0.7f); // Convert to RGB with full saturation
                                                                                                                                                                                                                                                                                                    snowflakes.push_back(flake);
                                                                                                                                                                                }

                                                                                                                                                                                            initialized = true;
                                                    }

                                                            void Update(float deltaTime) {
                                                                            ImVec2 screenSize = ImGui::GetIO().DisplaySize;

                                                                                        static float globalHueShift = 0.0f;
                                                                                                    globalHueShift += deltaTime * 0.1f; // Animate global hue shift

                                                                                                                for (auto& flake : snowflakes) {
                                                                                                                                    flake.position.x += flake.velocity.x * deltaTime;
                                                                                                                                                    flake.position.y += flake.velocity.y * deltaTime;

                                                                                                                                                                    flake.hue += globalHueShift; // Adjust hue over time
                                                                                                                                                                                    if (flake.hue > 1.0f) flake.hue -= 1.0f; // Wrap hue back to 0
                                                                                                                                                                                                    flake.color = HSVtoRGB(flake.hue, 1.0f, 0.7f); // Update color

                                                                                                                                                                                                                    // Reset snowflake if it goes off-screen
                                                                                                                                                                                                                                    if (flake.position.y > screenSize.y) {
                                                                                                                                                                                                                                                            flake.position.y = 0.0f;
                                                                                                                                                                                                                                                                                flake.position.x = static_cast<float>(rand() % static_cast<int>(screenSize.x));
                                                                                                                                                                                                                                    }
                                                                                                                }
                                                            }

                                                                    void Render() {
                                                                                    if (!initialized) Init();

                                                                                                float deltaTime = ImGui::GetIO().DeltaTime;
                                                                                                            Update(deltaTime);

                                                                                                                        auto* bgDraw = ImGui::GetBackgroundDrawList();
                                                                                                                                    auto* fgDraw = ImGui::GetWindowDrawList();

                                                                                                                                                for (size_t i = 0; i < snowflakes.size(); ++i) {
                                                                                                                                                                    SnowF& flake = snowflakes[i];

                                                                                                                                                                                    if (i % 2 == 0) {
                                                                                                                                                                                                            bgDraw->AddCircleFilled(flake.position, flake.size, flake.color);
                                                                                                                                                                                    } else {
                                                                                                                                                                                                            fgDraw->AddCircleFilled(flake.position, flake.size, flake.color);
                                                                                                                                                                                    }
                                                                                                                                                }
                                                                    }

                                                                        private:
                                                                                bool initialized;
                                                                                        int snowflakeCount;
                                                                                                std::vector<SnowF> snowflakes;
                                                                                                        std::mt19937 generator; // Random number generator

                                                                                                                ImU32 HSVtoRGB(float h, float s, float v) {
                                                                                                                                int i = static_cast<int>(h * 6.0f);
                                                                                                                                            float f = h * 6.0f - i;
                                                                                                                                                        float p = v * (1.0f - s);
                                                                                                                                                                    float q = v * (1.0f - f * s);
                                                                                                                                                                                float t = v * (1.0f - (1.0f - f) * s);

                                                                                                                                                                                            float r, g, b;
                                                                                                                                                                                                        switch (i % 6) {
                                                                                                                                                                                                                        case 0: r = v, g = t, b = p; break;
                                                                                                                                                                                                                                    case 1: r = q, g = v, b = p; break;
                                                                                                                                                                                                                                                case 2: r = p, g = v, b = t; break;
                                                                                                                                                                                                                                                            case 3: r = p, g = q, b = v; break;
                                                                                                                                                                                                                                                                        case 4: r = t, g = p, b = v; break;
                                                                                                                                                                                                                                                                                    case 5: r = v, g = p, b = q; break;
                                                                                                                                                                                                        }

                                                                                                                                                                                                                    return IM_COL32(static_cast<int>(r * 255), static_cast<int>(g * 255), static_cast<int>(b * 255), static_cast<int>(v * 255));
                                                                                                                }
        };

            inline SnowflakeSystem snowflakeSystem;

                inline void Snowflakes() {
                            snowflakeSystem.Render();
                }
}
                }
                                                                                                                                                                                                        }
                                                                                                                }
                                                                                                                                                                                    }
                                                                                                                                                                                    }
                                                                                                                                                }
                                                                    }
                                                                                                                                                                                                                                    }
                                                                                                                }
                                                            }
                                                                                                                                                                                }
                                                    }
                        }
        }
}
Editor is loading...
Leave a Comment