libbgi: выделить графику BGI в отдельную библиотеку + тест спрайтов
Графика вынесена из libc/ в новую библиотеку libbgi/:
- common/ — mode-agnostic математика и состояние (один исходник,
.rel попадает в оба driver-архива);
- bgi256/ + bgi16/ — mode-specific leaf'ы (raw-плот/чтение/спаны);
- include/ — graphics.h + gfx.h; _bgi.h — внутренний заголовок.
Собираются lib/bgi256.lib (и bgi16.lib в Фазе 2); выбор режима
линковкой через sprinter-cc --gfx 256|16. libc/ теперь без графики.
tests/bgi_img — тест спрайтов getimage/putimage/imagesize (5 операций
COPY/XOR/OR/AND/NOT + XOR-round-trip + self-check imagesize).
Проверен автотестом в MAME.
Примечание: make size-check пока красный (gfx_dbuf/gfx_demo выросли
после реорга) — закрыть по завершении миграции libbgi.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
PROJ_ROOT := $(abspath $(CURDIR)/../..)
|
||||
EXAMPLE := bgi_img
|
||||
EXTRA_FLAGS := --gfx 256
|
||||
include $(PROJ_ROOT)/app.mk
|
||||
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
* bgi_img — тест спрайтов BGI: getimage / putimage / imagesize
|
||||
* (режим 320×256×256, --gfx 256).
|
||||
*
|
||||
* Что проверяем:
|
||||
* 1. imagesize() совпадает с реальным размером буфера (self-check —
|
||||
* при расхождении рисуется красная плашка "SIZE MISMATCH").
|
||||
* 2. getimage() снимает прямоугольник экрана в буфер (заголовок
|
||||
* w,h + w*h байт).
|
||||
* 3. putimage() кладёт образ всеми пятью операциями COPY/XOR/OR/AND/
|
||||
* NOT поверх серого фона — визуально видно разницу операций.
|
||||
* 4. XOR дважды по одному месту восстанавливает фон (классический
|
||||
* признак корректного спрайтового XOR).
|
||||
*
|
||||
* Прогон в MAME визуальный: см. что источник и его COPY-копия
|
||||
* идентичны (в т.ч. красный маркер в левом-верхнем углу — ловит
|
||||
* зеркалирование), а XOR-дважды не оставляет следа на фоне.
|
||||
*/
|
||||
|
||||
#include <graphics.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/* Габариты спрайта-источника. Из них же считаем и рект, и буфер —
|
||||
* так imagesize() и sizeof(buf) обязаны сойтись. */
|
||||
#define SPR_L 16
|
||||
#define SPR_T 28
|
||||
#define SPR_W 40
|
||||
#define SPR_H 32
|
||||
|
||||
/* Буфер образа: 4 байта заголовка (w,h) + по байту на пиксель. */
|
||||
static uint8_t sprite[4 + SPR_W * SPR_H];
|
||||
|
||||
/* Нарисовать узнаваемый ассиметричный образ в (SPR_L,SPR_T). */
|
||||
static void draw_source(void)
|
||||
{
|
||||
int l = SPR_L, t = SPR_T;
|
||||
int r = l + SPR_W - 1, b = t + SPR_H - 1;
|
||||
|
||||
setfillstyle(SOLID_FILL, LIGHTBLUE);
|
||||
bar(l, t, r, b); /* фон образа */
|
||||
|
||||
setcolor(YELLOW);
|
||||
circle(l + SPR_W / 2, t + SPR_H / 2, 10);
|
||||
|
||||
setcolor(WHITE);
|
||||
line(l, t, r, b); /* диагональ */
|
||||
|
||||
/* Красный маркер в левом-верхнем углу — детектор ориентации. */
|
||||
setfillstyle(SOLID_FILL, LIGHTRED);
|
||||
bar(l, t, l + 5, t + 5);
|
||||
|
||||
setcolor(WHITE);
|
||||
rectangle(l - 1, t - 1, r + 1, b + 1); /* рамка вокруг источника */
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
static const int ops[5] = { COPY_PUT, XOR_PUT, OR_PUT, AND_PUT, NOT_PUT };
|
||||
static const char *names[5] = { "COPY", "XOR", "OR", "AND", "NOT" };
|
||||
int i, x;
|
||||
int sl = SPR_L, st = SPR_T;
|
||||
int sr = SPR_L + SPR_W - 1, sb = SPR_T + SPR_H - 1;
|
||||
|
||||
initgraph();
|
||||
cleardevice();
|
||||
|
||||
setcolor(WHITE);
|
||||
settextstyle(DEFAULT_FONT, HORIZ_DIR, 1);
|
||||
outtextxy(80, 6, "getimage/putimage test");
|
||||
|
||||
/* --- источник --- */
|
||||
outtextxy(SPR_L, 18, "src");
|
||||
draw_source();
|
||||
|
||||
/* --- imagesize() self-check --- */
|
||||
if (imagesize(sl, st, sr, sb) != (unsigned)sizeof(sprite)) {
|
||||
setfillstyle(SOLID_FILL, LIGHTRED);
|
||||
bar(80, 90, 240, 100);
|
||||
setcolor(WHITE);
|
||||
outtextxy(84, 92, "SIZE MISMATCH");
|
||||
}
|
||||
|
||||
/* --- снимаем образ --- */
|
||||
getimage(sl, st, sr, sb, sprite);
|
||||
|
||||
/* --- пять операций putimage поверх серого фона --- */
|
||||
for (i = 0; i < 5; i++) {
|
||||
x = 16 + i * 60;
|
||||
setcolor(WHITE);
|
||||
outtextxy(x, 108, names[i]);
|
||||
|
||||
setfillstyle(SOLID_FILL, LIGHTGRAY); /* общий фон под все ops */
|
||||
bar(x, 120, x + SPR_W - 1, 120 + SPR_H - 1);
|
||||
|
||||
putimage(x, 120, sprite, ops[i]);
|
||||
}
|
||||
|
||||
/* --- XOR дважды = восстановление фона --- */
|
||||
setcolor(WHITE);
|
||||
outtextxy(16, 176, "XOR x2 -> restores bg (patch stays clean):");
|
||||
|
||||
setfillstyle(SOLID_FILL, GREEN);
|
||||
bar(16, 190, 16 + SPR_W - 1, 190 + SPR_H - 1);
|
||||
putimage(16, 190, sprite, XOR_PUT); /* наложили */
|
||||
putimage(16, 190, sprite, XOR_PUT); /* сняли */
|
||||
|
||||
/* Рядом — тот же зелёный фон нетронутым, для сравнения. */
|
||||
setfillstyle(SOLID_FILL, GREEN);
|
||||
bar(80, 190, 80 + SPR_W - 1, 190 + SPR_H - 1);
|
||||
setcolor(WHITE);
|
||||
outtextxy(130, 202, "<- these two must match ->");
|
||||
|
||||
for (;;) { }
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
PROJ_ROOT := $(abspath $(CURDIR)/../..)
|
||||
EXAMPLE := gfx_dbuf
|
||||
EXAMPLE := gfx_dbuf
|
||||
EXTRA_FLAGS := --gfx 256
|
||||
include $(PROJ_ROOT)/app.mk
|
||||
|
||||
+40
-42
@@ -1,29 +1,26 @@
|
||||
/*
|
||||
* gfx_dbuf — double-buffering demo.
|
||||
* gfx_dbuf — double-buffering demo (BGI, bgi256.lib).
|
||||
*
|
||||
* Renders a moving rectangle by alternating between page 0 and page 1:
|
||||
* - Each frame: draw to the HIDDEN page, then flip visible to it.
|
||||
* - The page currently being displayed is never written to, so the
|
||||
* user only sees fully-rendered frames (no half-drawn artefacts,
|
||||
* no flicker from the per-frame clear).
|
||||
* Рендерит движущийся прямоугольник, чередуя страницу 0 и 1:
|
||||
* - Каждый кадр: рисуем на СКРЫТОЙ странице, затем делаем её видимой.
|
||||
* - Видимая страница никогда не пишется → пользователь видит только
|
||||
* целые кадры (без артефактов, без мерцания от per-frame clear).
|
||||
*
|
||||
* Indicators help verify the swap is actually happening:
|
||||
* - A small colour swatch in the top-right alternates each frame:
|
||||
* one shade when page 0 is shown, another when page 1. If the
|
||||
* box motion looks smooth and the swatch flickers between the two
|
||||
* shades at the animation rate, the swap is working.
|
||||
* Переключение страниц и vsync — через mode-agnostic gfx_* (gfx.h,
|
||||
* остались в BGI_GFX): gfx_set_draw_page / gfx_set_visible_page /
|
||||
* gfx_get_visible_page / gfx_wait_vsync. Рисование — через BGI
|
||||
* (graphics.h): cleardevice / bar / rectangle / outtextxy. Режим
|
||||
* 0x81 выбирается линковкой: EXTRA_FLAGS=--gfx 256.
|
||||
*
|
||||
* NOTE: each graphics screen has its OWN palette page (screen 0 →
|
||||
* palette 0, screen 1 → palette 1). We load the same palette into
|
||||
* both so the colours look identical regardless of which page is
|
||||
* currently visible. See memory/sprinter_graphics.md.
|
||||
*
|
||||
* Press any key to exit.
|
||||
* Индикатор страницы (маленький swatch сверху-справа) меняет оттенок
|
||||
* каждый кадр — если бокс движется плавно, а swatch мигает между двумя
|
||||
* оттенками, своп работает. Press any key to exit.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <conio.h>
|
||||
#include <gfx.h>
|
||||
#include <graphics.h>
|
||||
#include <gfx.h> /* double-buffering + vsync (mode-agnostic) */
|
||||
#include <stdint.h>
|
||||
|
||||
static uint8_t palette[256 * 4];
|
||||
@@ -48,42 +45,44 @@ static void make_palette(void)
|
||||
|
||||
static void draw_frame(int box_x, int box_y, uint8_t page_indicator)
|
||||
{
|
||||
gfx_clear256(COL_BG);
|
||||
setbkcolor(COL_BG); cleardevice();
|
||||
|
||||
/* Static decoration — identical on both pages so the swap doesn't
|
||||
* flicker the chrome. */
|
||||
gfx_fill_rect256(0, 0, GFX_WIDTH, 8, COL_STRIPE);
|
||||
gfx_fill_rect256(0, GFX_HEIGHT - 8, GFX_WIDTH, 8, COL_STRIPE);
|
||||
setfillstyle(SOLID_FILL, COL_STRIPE);
|
||||
bar(0, 0, GFX_WIDTH - 1, 7);
|
||||
bar(0, GFX_HEIGHT - 8, GFX_WIDTH - 1, GFX_HEIGHT - 1);
|
||||
|
||||
gfx_text256(8, 16, "double-buffering demo", COL_TEXT, COL_BG);
|
||||
gfx_text256(8, GFX_HEIGHT - 24,
|
||||
"press a key to exit", COL_TEXT, COL_BG);
|
||||
setcolor(COL_TEXT);
|
||||
outtextxy(8, 16, "double-buffering demo");
|
||||
outtextxy(8, GFX_HEIGHT - 24, "press a key to exit");
|
||||
|
||||
/* Page indicator — different shade for each page so a flickering
|
||||
* box here proves the swap is happening. */
|
||||
gfx_fill_rect256(GFX_WIDTH - 24, 16, 12, 12,
|
||||
page_indicator ? COL_PAGE1 : COL_PAGE0);
|
||||
/* Page indicator — different shade per page: flicker here proves
|
||||
* the swap is happening. */
|
||||
setfillstyle(SOLID_FILL, page_indicator ? COL_PAGE1 : COL_PAGE0);
|
||||
bar(GFX_WIDTH - 24, 16, GFX_WIDTH - 24 + 11, 16 + 11);
|
||||
|
||||
/* The animated box. */
|
||||
gfx_fill_rect256(box_x, box_y, 40, 30, COL_BOX);
|
||||
gfx_rect256 (box_x, box_y, 40, 30, COL_BLACK);
|
||||
setfillstyle(SOLID_FILL, COL_BOX);
|
||||
bar(box_x, box_y, box_x + 39, box_y + 29);
|
||||
setcolor(COL_BLACK);
|
||||
rectangle(box_x, box_y, box_x + 39, box_y + 29);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
make_palette();
|
||||
uint8_t prev = gfx_init(GFX_MODE_320x256x256, 0);
|
||||
initgraph();
|
||||
|
||||
/* Each graphics screen has its own palette page (0→pal 0, 1→pal 1).
|
||||
* Load the same data into both so the visual swap is seamless. */
|
||||
/* Каждая графическая страница имеет свою палитру (0→pal 0, 1→pal 1).
|
||||
* Грузим одну и ту же в обе — визуальный своп бесшовный. */
|
||||
gfx_pal_load(0, 0, 0, palette);
|
||||
gfx_pal_load(1, 0, 0, palette);
|
||||
|
||||
/* Clear both pages so the very first flip doesn't reveal garbage. */
|
||||
gfx_set_draw_page(0);
|
||||
gfx_clear256(COL_BG);
|
||||
gfx_set_draw_page(1);
|
||||
gfx_clear256(COL_BG);
|
||||
/* Очистить обе страницы, чтобы первый флип не вскрыл мусор. */
|
||||
setbkcolor(COL_BG);
|
||||
gfx_set_draw_page(0); cleardevice();
|
||||
gfx_set_draw_page(1); cleardevice();
|
||||
|
||||
/* Bouncing rect state. */
|
||||
int box_x = 10;
|
||||
@@ -98,9 +97,8 @@ int main(void)
|
||||
|
||||
draw_frame(box_x, box_y, hidden);
|
||||
|
||||
/* Wait for the next frame interrupt before flipping, so the
|
||||
* page swap lands during vertical retrace and the user never
|
||||
* sees a half-drawn frame. */
|
||||
/* Ждать следующий кадр перед флипом — своп падает в vblank,
|
||||
* пользователь не видит полунарисованный кадр. */
|
||||
gfx_wait_vsync();
|
||||
gfx_set_visible_page(hidden);
|
||||
|
||||
@@ -114,7 +112,7 @@ int main(void)
|
||||
}
|
||||
|
||||
(void)getch();
|
||||
gfx_done(prev);
|
||||
closegraph();
|
||||
puts("done");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
PROJ_ROOT := $(abspath $(CURDIR)/../..)
|
||||
EXAMPLE := gfx_demo
|
||||
EXAMPLE := gfx_demo
|
||||
EXTRA_FLAGS := --gfx 256
|
||||
include $(PROJ_ROOT)/app.mk
|
||||
|
||||
+62
-49
@@ -1,15 +1,23 @@
|
||||
/*
|
||||
* gfx_demo — exercises the libc/gfx primitives. Shows:
|
||||
* 1. accelerator-backed gfx_clear256 / gfx_hline256 / gfx_vline256 / gfx_fill_rect256
|
||||
* 2. gfx_rect256 outline, gfx_line256 diagonals (Bresenham via putpixel)
|
||||
* 3. a grey-ramp palette
|
||||
* 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 <gfx.h>
|
||||
#include <graphics.h>
|
||||
#include <gfx.h> /* GFX_WIDTH/HEIGHT, gfx_pal_load (mode-agnostic) */
|
||||
#include <stdint.h>
|
||||
|
||||
static uint8_t palette_data[256 * 4];
|
||||
@@ -32,69 +40,74 @@ int main(void)
|
||||
palette_data[i * 4 + 3] = 0;
|
||||
}
|
||||
|
||||
uint8_t prev = gfx_init(GFX_MODE_320x256x256, 0);
|
||||
initgraph();
|
||||
gfx_pal_load(0, 0, 0, palette_data);
|
||||
|
||||
/* --- Stage 1: orthogonal frame via hline / vline ----------------- */
|
||||
gfx_clear256(0x40);
|
||||
gfx_hline256(1, 1, GFX_WIDTH - 2, 0xFF);
|
||||
gfx_hline256(1, GFX_HEIGHT - 2, GFX_WIDTH - 2, 0xFF);
|
||||
gfx_vline256(1, 2, GFX_HEIGHT - 4, 0xFF);
|
||||
gfx_vline256(GFX_WIDTH - 2, 2, GFX_HEIGHT - 4, 0xFF);
|
||||
/* --- 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, 2, 1, GFX_HEIGHT - 4); /* left */
|
||||
line(GFX_WIDTH - 2, 2, GFX_WIDTH - 2, GFX_HEIGHT - 4); /* right */
|
||||
wait_key();
|
||||
|
||||
/* --- Stage 2: nested rectangles -------------------------------- */
|
||||
gfx_clear256(0x20);
|
||||
for (int i = 0; i < 16; i++)
|
||||
gfx_rect256(i * 10, i * 8,
|
||||
GFX_WIDTH - i * 20,
|
||||
GFX_HEIGHT - i * 16,
|
||||
(uint8_t)(0x40 + i * 8));
|
||||
wait_key();
|
||||
|
||||
/* --- Stage 3: filled-rect colour bars --------------------------- */
|
||||
gfx_clear256(0);
|
||||
/* Tall-narrow rects (w=20, h=128) — heuristic picks vertical orient. */
|
||||
/* --- Stage 2: nested rectangles --------------------------------- */
|
||||
setbkcolor(0x20); cleardevice();
|
||||
for (int i = 0; i < 16; i++) {
|
||||
gfx_fill_rect256(i * 20, 0, 20, 128, (uint8_t)(i * 16));
|
||||
gfx_fill_rect256(i * 20 + 0, 128, 20, 128, (uint8_t)(255 - i * 16));
|
||||
setcolor((uint8_t)(0x40 + i * 8));
|
||||
rectangle(i * 10, i * 8,
|
||||
GFX_WIDTH - i * 20,
|
||||
GFX_HEIGHT - i * 16);
|
||||
}
|
||||
wait_key();
|
||||
|
||||
/* --- Stage 3b: wide-short rects + a grid of small squares -------- */
|
||||
gfx_clear256(0x08);
|
||||
/* Wide-short stripes (w=320, h=16) — heuristic picks horizontal. */
|
||||
for (int i = 0; i < 8; i++)
|
||||
gfx_fill_rect256(0, i * 32, GFX_WIDTH, 16, (uint8_t)(0x40 + i * 24));
|
||||
/* 8×8 small squares grid (w=h, heuristic picks either — same cost). */
|
||||
for (int row = 0; row < 4; row++)
|
||||
for (int col = 0; col < 16; col++)
|
||||
gfx_fill_rect256(col * 20 + 4, 256 - 80 + row * 20, 12, 12,
|
||||
(uint8_t)((row * 16 + col) * 4));
|
||||
/* --- 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 4: diagonal lines via Bresenham ---------------------- */
|
||||
gfx_clear256(0x18);
|
||||
/* "Star" of lines from centre to a circle of endpoints. */
|
||||
/* --- 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;
|
||||
gfx_line256(cx, cy, ex, 0, 0xFF);
|
||||
gfx_line256(cx, cy, ex, GFX_HEIGHT - 1, 0xC0);
|
||||
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;
|
||||
gfx_line256(cx, cy, 0, ey, 0x80);
|
||||
gfx_line256(cx, cy, GFX_WIDTH - 1, ey, 0x40);
|
||||
setcolor(0x80); line(cx, cy, 0, ey);
|
||||
setcolor(0x40); line(cx, cy, GFX_WIDTH - 1, ey);
|
||||
}
|
||||
/* Box outline using gfx_line256. */
|
||||
gfx_line256(0, 0, GFX_WIDTH - 1, 0, 0xFF);
|
||||
gfx_line256(0, GFX_HEIGHT - 1, GFX_WIDTH - 1, GFX_HEIGHT - 1, 0xFF);
|
||||
gfx_line256(0, 0, 0, GFX_HEIGHT - 1, 0xFF);
|
||||
gfx_line256(GFX_WIDTH - 1, 0, GFX_WIDTH - 1, GFX_HEIGHT - 1, 0xFF);
|
||||
/* 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();
|
||||
|
||||
gfx_done(prev);
|
||||
closegraph();
|
||||
puts("done");
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user