/*
 * gfx_text_demo — bitmap-font text rendering test.
 *
 * Stages (press a key to advance):
 *   1. 320×256×256 mode — text in multiple colors using BIOS default font.
 *   2. 640×256×16  mode — same text with the 16-color palette.
 *      Verifies the same font renders correctly in both pixel formats.
 */

#include <stdio.h>
#include <conio.h>
#include <gfx.h>
#include <stdint.h>

static uint8_t pal256[256 * 4];
static uint8_t pal16[16 * 4];

static void wait_key(void)
{
    __asm
        ld    c, #0x30
        rst   #0x10
    __endasm;
}

int main(void)
{
    /* Grey ramp for 256-color mode. */
    for (int i = 0; i < 256; i++) {
        pal256[i * 4 + 0] = (uint8_t)i;
        pal256[i * 4 + 1] = (uint8_t)i;
        pal256[i * 4 + 2] = (uint8_t)i;
        pal256[i * 4 + 3] = 0;
    }
    /* Simple 16-color palette: 0=black, 1=red, 2=green, ..., 7=white,
     * 8..15 = dimmed variants. */
    static const uint8_t bgr16[16][3] = {
        {0x00,0x00,0x00}, {0x00,0x00,0xFF}, {0x00,0xFF,0x00}, {0xFF,0x00,0x00},
        {0x00,0xFF,0xFF}, {0xFF,0x00,0xFF}, {0xFF,0xFF,0x00}, {0xFF,0xFF,0xFF},
        {0x40,0x40,0x40}, {0x40,0x40,0xFF}, {0x40,0xFF,0x40}, {0xFF,0x40,0x40},
        {0x40,0xFF,0xFF}, {0xFF,0x40,0xFF}, {0xFF,0xFF,0x40}, {0xA0,0xA0,0xA0},
    };
    for (int i = 0; i < 16; i++) {
        pal16[i*4+0] = bgr16[i][0];
        pal16[i*4+1] = bgr16[i][1];
        pal16[i*4+2] = bgr16[i][2];
        pal16[i*4+3] = 0;
    }

    /* --- Stage 1: text in 320×256×256 ----------------------------- */
    uint8_t prev = gfx_init(GFX_MODE_320x256x256, 0);
    gfx_pal_load(0, 0, 0, pal256);
    gfx_clear256(0x20);

    gfx_text256(8,   8,  "Hello from Sprinter!", 0xFF, 0x20);
    gfx_text256(8,  24,  "320x256x256 mode",     0xC0, 0x20);
    gfx_text256(8,  40,  "Bitmap font from BIOS WIN_GET_ZG",  0x80, 0x20);

    /* Color showcase — same string in 8 different greys. */
    for (int i = 0; i < 8; i++) {
        char buf[16] = "Color ramp test";
        gfx_text256(8, 64 + i * 12, buf, (uint8_t)(0x40 + i * 24), 0x20);
    }

    /* Adjacent FG/BG variants. */
    gfx_text256(8, 176, "FG=white BG=red",   0xFF, 0x30);
    gfx_text256(8, 192, "FG=black BG=white", 0x00, 0xFF);
    gfx_text256(8, 208, "Negative space:",   0xFF, 0x20);

    /* Large block to test grid-aligned text. */
    for (int row = 0; row < 3; row++)
        for (int col = 0; col < 8; col++) {
            char ch[2] = { (char)('A' + row * 8 + col), 0 };
            gfx_text256(176 + col * 16, 176 + row * 16, ch, 0xFF, 0x20);
        }
    wait_key();

    /* --- Stage 2: text in 640×256×16 ------------------------------ */
    gfx_done(prev);
    prev = gfx_init(GFX_MODE_640x256x16, 0);
    gfx_pal_load(0, 0, 16, pal16);
    gfx_clear16(0);

    gfx_text16(8,   8,  "Hello from Sprinter!",            7, 0);
    gfx_text16(8,  24,  "640x256x16 mode (16 colours)",    6, 0);
    gfx_text16(8,  40,  "Same font, packed two pixels/byte", 4, 0);

    /* Color demo: each line in a different palette index. */
    for (int i = 1; i < 16; i++) {
        char buf[20];
        /* Simple manual itoa to avoid stdio bloat. */
        buf[0] = 'C'; buf[1] = 'o'; buf[2] = 'l'; buf[3] = 'o'; buf[4] = 'r';
        buf[5] = ' '; buf[6] = (char)('0' + (i / 10));
        buf[7] = (char)('0' + (i % 10));
        buf[8] = 0;
        gfx_text16(8, 64 + (i - 1) * 12, buf, (uint8_t)i, 0);
    }

    /* Show on right side — paragraph in white-on-black, then inverted. */
    gfx_text16(280, 64,  "ABCDEFGHIJKLMNOP", 7, 0);
    gfx_text16(280, 76,  "QRSTUVWXYZ", 7, 0);
    gfx_text16(280, 92,  "abcdefghijklmnop", 7, 0);
    gfx_text16(280, 104, "qrstuvwxyz", 7, 0);
    gfx_text16(280, 120, "0123456789", 7, 0);
    gfx_text16(280, 132, "!@#$%^&*()_+-=", 7, 0);
    gfx_text16(280, 148, "Inverted text", 0, 7);
    gfx_text16(280, 164, "FG=red BG=cyan", 1, 6);
    wait_key();

    gfx_done(prev);
    puts("done");
    return 0;
}
