// Starfield.cpp - implementation (copied into src/graphics)
#include "graphics/Starfield.h"
#include <SDL3/SDL.h>
#include <random>

void Starfield::init(int count, int w, int h)
{
    stars.clear();
    stars.reserve(count);
    std::mt19937 rng{std::random_device{}()};
    std::uniform_real_distribution<float> dx(0.f, (float)w), dy(0.f, (float)h), dz(0.2f, 1.f);
    for (int i = 0; i < count; ++i)
        stars.push_back({dx(rng), dy(rng), dz(rng), 15.f + 35.f * dz(rng)});
    lastW = w;
    lastH = h;
}

void Starfield::update(float dt, int w, int h)
{
    if (w != lastW || h != lastH || stars.empty())
        init((w * h) / 8000 + 120, w, h);
    for (auto &s : stars)
    {
        s.y += s.speed * dt;
        if (s.y > h)
        {
            s.y -= h;
            s.x = (float)(rand() % w);
        }
    }
}

void Starfield::draw(SDL_Renderer *r) const
{
    SDL_SetRenderDrawColor(r, 255, 255, 255, 255);
    for (auto &s : stars)
    {
        SDL_FRect fr{s.x, s.y, 1.f * s.z, 1.f * s.z};
        SDL_RenderFillRect(r, &fr);
    }
}
