#pragma once

// sprite_batch — batched textured quad renderer on top of sokol_gfx.
// Single draw call per atlas binding. Issue 0072b runtime gamedev.

#include "sokol_gfx.h"
#include "math2d.h"

namespace fn::gfx {

struct SpriteBatch {
    sg_pipeline pipeline{};
    sg_buffer   vbo{};
    sg_view     current_view{};        // texture view bound this batch
    sg_sampler  sampler{};
    void*       cpu_buffer = nullptr;  // Vertex* on heap
    int         cpu_capacity_quads = 0;
    int         cpu_count_quads = 0;
    float       proj[16]{};            // current view-proj matrix (column-major)
    bool        in_pass = false;
    bool        ok = false;
};

// Create the batcher. Allocates CPU buffer + sokol resources. cpu_capacity_quads
// caps the per-flush quads (auto-flush when reached). Default 4096.
SpriteBatch sprite_batch_create(int cpu_capacity_quads = 4096);
void sprite_batch_destroy(SpriteBatch& b);

// Begin a batch with a column-major 4x4 view-projection.
// Call between sg_begin_pass and sg_end_pass.
void sprite_batch_begin(SpriteBatch& b, const float view_proj_col_major[16]);

// Queue one textured quad. dst is the screen rect. src is the UV rect inside the
// texture, in pixels (0..image_size). tint multiplies the texture sample.
// view must be a sg_view created with sg_make_view({.texture.image = ...}).
void sprite_batch_draw(SpriteBatch& b, sg_view view, int img_w, int img_h,
                       fn::math2d::Rect src, fn::math2d::Rect dst,
                       fn::math2d::Color tint = fn::math2d::Color::white());

// Flush remaining quads.
void sprite_batch_end(SpriteBatch& b);

}  // namespace fn::gfx
