/*
 * gfx_demo — exercises the BGI primitives (bgi256.lib).  Shows:
 *   1. cleardevice / line (orthogonal frame via horizontal/vertical lines)
 *   2. rectangle outlines, line diagonals (Bresenham)
 *   3. bar filled-rect colour bars (accelerator-backed)
 *   4. a grey-ramp palette
 *
 * Migrated с низкоуровневого gfx.h drawing-API на BGI (graphics.h).
 * Режим 0x81 (320×256×256) выбирается линковкой: EXTRA_FLAGS=--gfx 256
 * (см. Makefile).  Палитура грузится через gfx_pal_load (mode-agnostic,
 * остался в gfx.h) — BGI initgraph уже загрузил EGA-палитру, мы её
 * подменяем серым клином.
 *
 * Press a key to advance through each stage.
 */

#include <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <gfx.h>          /* GFX_WIDTH/HEIGHT, gfx_pal_load (mode-agnostic) */
#include <stdint.h>


extern void gfx_rectfill(int x, int y, int w, int h, uint8_t color);

static uint8_t palette_data[256 * 4];

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

int main(void)
{
    /* Grey ramp: (B=i, G=i, R=i, 0). */
    for (int i = 0; i < 256; i++) {
        palette_data[i * 4 + 0] = (uint8_t)i;
        palette_data[i * 4 + 1] = (uint8_t)i;
        palette_data[i * 4 + 2] = (uint8_t)i;
        palette_data[i * 4 + 3] = 0;
    }

    initgraph();
    gfx_pal_load(0, 0, 0, palette_data);

    /* --- Stage 1: orthogonal frame via horizontal/vertical lines ------ */
    setbkcolor(0x40); cleardevice();
    setcolor(0xFF);
    line(1,             1,             GFX_WIDTH  - 2, 1);              /* top    */
    line(1,             GFX_HEIGHT - 2, GFX_WIDTH - 2, GFX_HEIGHT - 2); /* bottom */
    line(1,             1,             1,             GFX_HEIGHT - 2);  /* left   */
    line(GFX_WIDTH - 2, 1,             GFX_WIDTH - 2, GFX_HEIGHT - 2);  /* right  */

//    gfx_rectfill(20, 20, 40, 3, 120);
//    gfx_rectfill(120, 20, 3, 40, 120);
    wait_key();

    /* --- Stage 2: nested rectangles --------------------------------- */
    setbkcolor(0x20); cleardevice();
    for (int i = 0; i < 16; i++) {
        setcolor((uint8_t)(0x40 + i * 8));
        rectangle(i * 10, i * 8,
                  GFX_WIDTH  - i * 10,
                  GFX_HEIGHT - i * 8);
    }
    wait_key();


    /* --- Stage 3: filled-rect colour bars (bar = залитый прямоугольник) */
    setbkcolor(0); cleardevice();
    for (int i = 0; i < 16; i++) {
        setfillstyle(SOLID_FILL, (uint8_t)(i * 16));
        bar(i * 20,       0,   i * 20 + 19, 127);
        setfillstyle(SOLID_FILL, (uint8_t)(255 - i * 16));
        bar(i * 20,     128,   i * 20 + 19, 255);
    }
    wait_key();

    /* --- Stage 3b: wide-short stripes + a grid of small squares ------ */
    setbkcolor(0x08); cleardevice();
    for (int i = 0; i < 8; i++) {
        setfillstyle(SOLID_FILL, (uint8_t)(0x40 + i * 24));
        bar(0, i * 32, GFX_WIDTH - 1, i * 32 + 15);
    }
    for (int row = 0; row < 4; row++)
        for (int col = 0; col < 16; col++) {
            setfillstyle(SOLID_FILL, (uint8_t)((row * 16 + col) * 4));
            bar(col * 20 + 4, 256 - 80 + row * 20,
               col * 20 + 4 + 11, 256 - 80 + row * 20 + 11);
        }
    wait_key();

    /* --- Stage 4: diagonal lines via Bresenham ----------------------- */
    setbkcolor(0x18); cleardevice();
    int cx = GFX_WIDTH / 2, cy = GFX_HEIGHT / 2;
    for (int i = 0; i < 16; i++) {
        int ex = i * (GFX_WIDTH - 1) / 15;
        setcolor(0xFF); line(cx, cy, ex, 0);
        setcolor(0xC0); line(cx, cy, ex, GFX_HEIGHT - 1);
    }
    for (int i = 0; i < 16; i++) {
        int ey = i * (GFX_HEIGHT - 1) / 15;
        setcolor(0x80); line(cx, cy, 0,             ey);
        setcolor(0x40); line(cx, cy, GFX_WIDTH - 1, ey);
    }
    /* Box outline. */
    setcolor(0xFF);
    line(0,             0,             GFX_WIDTH - 1, 0);
    line(0,             GFX_HEIGHT - 1, GFX_WIDTH - 1, GFX_HEIGHT - 1);
    line(0,             0,             0,             GFX_HEIGHT - 1);
    line(GFX_WIDTH - 1, 0,             GFX_WIDTH - 1, GFX_HEIGHT - 1);
    wait_key();

    closegraph();
    puts("done");
    return 0;
}
