#pragma once

inline const char* vertTile = R"(
    #version 330 core

    layout(location = 0) in vec2 aPos;

    layout(location = 1) in vec3 iTilePos;
    layout(location = 2) in float iSpriteId;

    uniform mat4 prMatrix;
    uniform mat4 camMatrix;
    uniform vec2 spriteCount;
    uniform vec2 spriteScale;

    out vec2 vUV;
    flat out int vSpriteId;

    void main()
    {
        vec3 local = vec3(aPos.x, aPos.y, 0);
        vec2 baseUV = aPos + vec2(0.5);
        vec3 tileSize = vec3(spriteScale.x, spriteScale.y, 1);

        vec3 worldPos = (iTilePos * tileSize) + (local * tileSize);

        gl_Position = prMatrix * camMatrix * vec4(worldPos.x, worldPos.y, worldPos.z, 1.0);

        vUV = baseUV;
        vSpriteId = int(iSpriteId + 0.5);
    }
)";

inline const char* fragTile = R"(
    #version 330 core

    in vec2 vUV;
    flat in int vSpriteId;

    uniform sampler2D atlas;
    uniform vec2 spriteCount;

    out vec4 FragColor;

    void main()
    {
        vec2 tileUVSize = 1.0 / spriteCount;

        int cols = int(spriteCount.x);
        int sx = vSpriteId % cols;
        int sy = vSpriteId / cols;

        vec2 atlasOffset = vec2(float(sx), float(sy)) * tileUVSize;
        vec2 atlasUV = atlasOffset + (vUV * tileUVSize);
        vec4 c = texture(atlas, atlasUV);
        if (c.a < 0.01) discard;

        FragColor = c;
    }
)";
