/*
 * gfx_demo16 — exercises the 640×256×16 (mode 0x82) primitives.
 *
 * Stages (press a key to advance):
 *   1. 1-pixel frame via hline/vline16 — verifies edge alignment
 *      (odd-x and odd-(x+len) RMW paths)
 *   2. Nested rectangles in cycled colours
 *   3. Colour bars — 16 vertical stripes, each colour 0..15
 *      (palette readability test)
 *   4. Diagonal star + axis-aligned cross via line16
 */

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

static uint8_t palette[16 * 4];

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

static void build_palette(void)
{
    /* 16-color palette: index 0 = black, 1..7 = primary ramp,
     * 8..15 = mid/light variants.  Order chosen so neighbouring
     * indices contrast in the colour-bars test. */
    static const uint8_t bgr[16][3] = {
        {0x00, 0x00, 0x00},   /* 0 black           */
        {0x00, 0x00, 0xFF},   /* 1 red             */
        {0x00, 0xFF, 0x00},   /* 2 green           */
        {0xFF, 0x00, 0x00},   /* 3 blue            */
        {0x00, 0xFF, 0xFF},   /* 4 yellow          */
        {0xFF, 0x00, 0xFF},   /* 5 magenta         */
        {0xFF, 0xFF, 0x00},   /* 6 cyan            */
        {0xFF, 0xFF, 0xFF},   /* 7 white           */
        {0x40, 0x40, 0x40},   /* 8 dim grey        */
        {0x40, 0x40, 0xFF},   /* 9 light red       */
        {0x40, 0xFF, 0x40},   /* A light green     */
        {0xFF, 0x40, 0x40},   /* B light blue      */
        {0x40, 0xFF, 0xFF},   /* C light yellow    */
        {0xFF, 0x40, 0xFF},   /* D light magenta   */
        {0xFF, 0xFF, 0x40},   /* E light cyan      */
        {0xA0, 0xA0, 0xA0},   /* F grey            */
    };
    for (int i = 0; i < 16; i++) {
        palette[i * 4 + 0] = bgr[i][0];
        palette[i * 4 + 1] = bgr[i][1];
        palette[i * 4 + 2] = bgr[i][2];
        palette[i * 4 + 3] = 0;
    }
}

int main(void)
{
    build_palette();

    uint8_t prev = gfx_init(GFX_MODE_640x256x16, 0);
    gfx_pal_load(0, 0, 16, palette);

    /* --- Stage 1: 1-px frame at edges -------------------------------- */
    gfx_clear16(8);                                         /* dim grey */
    gfx_hline16(0,                  0,                 GFX_WIDTH_16,  7); /* white top */
    gfx_hline16(0,                  GFX_HEIGHT_16 - 1, GFX_WIDTH_16,  7); /* white bottom */
    gfx_vline16(0,                  0,                 GFX_HEIGHT_16, 7);
    gfx_vline16(GFX_WIDTH_16 - 1,   0,                 GFX_HEIGHT_16, 7);
    /* 1-px inset purple frame — exercises odd-x edges. */
    gfx_hline16(1,                  1,                 GFX_WIDTH_16 - 2, 5);
    gfx_hline16(1,                  GFX_HEIGHT_16 - 2, GFX_WIDTH_16 - 2, 5);
    gfx_vline16(1,                  2,                 GFX_HEIGHT_16 - 4, 5);
    gfx_vline16(GFX_WIDTH_16 - 2,   2,                 GFX_HEIGHT_16 - 4, 5);
    wait_key();

    /* --- Stage 2: nested rectangles --------------------------------- */
    gfx_clear16(0);
    for (int i = 0; i < 16; i++)
        gfx_rect16(i * 20, i * 8,
                   GFX_WIDTH_16  - i * 40,
                   GFX_HEIGHT_16 - i * 16,
                   (uint8_t)((i + 1) & 0x0F));
    wait_key();

    /* --- Stage 3: vertical color bars ------------------------------- */
    gfx_clear16(0);
    /* 16 bars × 40 px wide = 640 px exactly. */
    for (int i = 0; i < 16; i++)
        gfx_fill_rect16(i * 40, 0, 40, GFX_HEIGHT_16, (uint8_t)i);
    wait_key();

    /* --- Stage 4: diagonal star + cross ----------------------------- */
    gfx_clear16(0);
    int cx = GFX_WIDTH_16 / 2, cy = GFX_HEIGHT_16 / 2;
    for (int i = 0; i < 16; i++) {
        int ex = i * (GFX_WIDTH_16 - 1) / 15;
        gfx_line16(cx, cy, ex, 0,                  7);
        gfx_line16(cx, cy, ex, GFX_HEIGHT_16 - 1,  3);
    }
    for (int i = 0; i < 16; i++) {
        int ey = i * (GFX_HEIGHT_16 - 1) / 15;
        gfx_line16(cx, cy, 0,                ey,  1);
        gfx_line16(cx, cy, GFX_WIDTH_16 - 1, ey,  2);
    }
    /* axis-aligned cross to verify hline/vline alignment at runtime. */
    gfx_hline16(0, cy, GFX_WIDTH_16,  6);
    gfx_vline16(cx, 0, GFX_HEIGHT_16, 6);
    wait_key();

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