libc: BGI Фаза 2d-1 — getimage/putimage/imagesize (спрайты)
Растровые образы: imagesize (4 байта заголовка w,h + w*h пикселей), getimage (захват прямоугольника), putimage с COPY/XOR/OR/AND/NOT_PUT. Блит идёт raw в одной W3-скобке — добавлен _gfx_getpixel256_raw в gfx + leaf _bgi_read_raw в drv256. Проверено в MAME (tests/bgitest): захват спрайта, 3 COPY-копии, XOR/OR/COPY поверх фона. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -6,7 +6,7 @@ banked 1056
|
||||
bankedbg 1067
|
||||
banklocl 4832
|
||||
banktest 3767
|
||||
bgitest 6414
|
||||
bgitest 5482
|
||||
bios_text 4470
|
||||
cat 915
|
||||
cblstream 6277
|
||||
|
||||
|
@@ -65,6 +65,9 @@ extern uint8_t _bgi_fill_color;
|
||||
/* Raw horizontal span (без своей W3-скобки; клип делает сам gfx-raw). */
|
||||
void _bgi_hspan_raw(int x, int y, int len, uint8_t color);
|
||||
|
||||
/* Raw чтение пикселя (без своей W3-скобки) — для getimage/putimage. */
|
||||
unsigned _bgi_read_raw(int x, int y);
|
||||
|
||||
/* Залить строку [x0..x1] на y ТЕКУЩИМ стилем заливки (паттерн+цвет),
|
||||
* с клипом по экрану. Вызывать между _bgi_begin/_bgi_end. */
|
||||
void _bgi_fill_span(int x0, int x1, int y);
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
/*
|
||||
* _bgi_read_raw (drv256) — чтение пикселя без своей W3-скобки
|
||||
* (между _bgi_begin/_bgi_end). Для getimage/putimage.
|
||||
*/
|
||||
#include "../_bgi.h"
|
||||
#include "../../gfx/_gfx.h"
|
||||
|
||||
unsigned _bgi_read_raw(int x, int y)
|
||||
{
|
||||
return _gfx_getpixel256_raw(x, y);
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* getimage — сохранить прямоугольник экрана в буфер bitmap.
|
||||
* Формат: uint16 width, uint16 height, затем w*h байт (по строкам).
|
||||
* Чтение raw в одной W3-скобке.
|
||||
*/
|
||||
#include "_bgi.h"
|
||||
|
||||
void getimage(int left, int top, int right, int bottom, void *bitmap)
|
||||
{
|
||||
uint8_t *p = (uint8_t *)bitmap;
|
||||
int w = right - left + 1;
|
||||
int h = bottom - top + 1;
|
||||
int x, y;
|
||||
|
||||
if (w <= 0 || h <= 0) return;
|
||||
|
||||
*p++ = (uint8_t)w;
|
||||
*p++ = (uint8_t)(w >> 8);
|
||||
*p++ = (uint8_t)h;
|
||||
*p++ = (uint8_t)(h >> 8);
|
||||
|
||||
_bgi_begin();
|
||||
for (y = 0; y < h; y++)
|
||||
for (x = 0; x < w; x++)
|
||||
*p++ = (uint8_t)_bgi_read_raw(left + x, top + y);
|
||||
_bgi_end();
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* imagesize — размер буфера под образ (left,top)-(right,bottom) в байтах:
|
||||
* 4 (заголовок w,h) + w*h (по байту на пиксель в режиме 256).
|
||||
*
|
||||
* Результат 16-бит unsigned (как в Turbo-C): образы ≥64 КБ не влезают.
|
||||
* Произведение w*h считаем в unsigned — для валидных (<64 КБ) образов
|
||||
* умещается точно; без 32-бит.
|
||||
*/
|
||||
#include "_bgi.h"
|
||||
|
||||
unsigned imagesize(int left, int top, int right, int bottom)
|
||||
{
|
||||
int w = right - left + 1;
|
||||
int h = bottom - top + 1;
|
||||
if (w <= 0 || h <= 0) return 0;
|
||||
return (unsigned)4 + (unsigned)w * (unsigned)h;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* putimage — вывести образ bitmap левым-верхним углом в (left,top)
|
||||
* операцией op. Заголовок буфера: uint16 width, uint16 height.
|
||||
*
|
||||
* COPY — пишем src; XOR/OR/AND — комбинируем с экраном (нужно raw-
|
||||
* чтение); NOT — пишем ~src. Всё в одной W3-скобке (raw-плот/чтение).
|
||||
*/
|
||||
#include "_bgi.h"
|
||||
|
||||
void putimage(int left, int top, const void *bitmap, int op)
|
||||
{
|
||||
const uint8_t *p = (const uint8_t *)bitmap;
|
||||
int w, h, x, y;
|
||||
uint8_t src, dst;
|
||||
|
||||
w = p[0] | (p[1] << 8);
|
||||
h = p[2] | (p[3] << 8);
|
||||
p += 4;
|
||||
if (w <= 0 || h <= 0) return;
|
||||
|
||||
_bgi_begin();
|
||||
for (y = 0; y < h; y++) {
|
||||
for (x = 0; x < w; x++) {
|
||||
src = *p++;
|
||||
switch (op) {
|
||||
case XOR_PUT: dst = (uint8_t)_bgi_read_raw(left + x, top + y) ^ src; break;
|
||||
case OR_PUT: dst = (uint8_t)_bgi_read_raw(left + x, top + y) | src; break;
|
||||
case AND_PUT: dst = (uint8_t)_bgi_read_raw(left + x, top + y) & src; break;
|
||||
case NOT_PUT: dst = (uint8_t)~src; break;
|
||||
case COPY_PUT:
|
||||
default: dst = src; break;
|
||||
}
|
||||
_bgi_plot_raw(left + x, top + y, dst);
|
||||
}
|
||||
}
|
||||
_bgi_end();
|
||||
}
|
||||
+5
-4
@@ -41,10 +41,11 @@ void _gfx_w3_video_end(void);
|
||||
* "Raw" = W3-naive: вызывающий обязан обернуть последовательность в
|
||||
* одну пару _gfx_w3_video_begin/_gfx_w3_video_end. */
|
||||
|
||||
void _gfx_putpixel256_raw(int x, int y, uint8_t color);
|
||||
void _gfx_hline256_raw (int x, int y, int len, uint8_t color);
|
||||
void _gfx_vline256_raw (int x, int y, int len, uint8_t color);
|
||||
void _gfx_clear256_raw (uint8_t color);
|
||||
void _gfx_putpixel256_raw(int x, int y, uint8_t color);
|
||||
uint8_t _gfx_getpixel256_raw(int x, int y);
|
||||
void _gfx_hline256_raw (int x, int y, int len, uint8_t color);
|
||||
void _gfx_vline256_raw (int x, int y, int len, uint8_t color);
|
||||
void _gfx_clear256_raw (uint8_t color);
|
||||
|
||||
/* Скретч акселератора (_gfx_acc256.c) — общий для hline/vline/clear;
|
||||
* однопоточно, IRQ выключены между begin/end. */
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* _gfx_getpixel256_raw — прочитать пиксель (x,y) БЕЗ своей W3-скобки
|
||||
* (вызывающий уже сделал _gfx_w3_video_begin). Для блиттинга image:
|
||||
* getimage/putimage читают тысячи пикселей в одной скобке.
|
||||
* За пределами экрана возвращает 0.
|
||||
*/
|
||||
|
||||
#include "_gfx.h"
|
||||
|
||||
static uint8_t _gfx_gpr_y;
|
||||
static uint16_t _gfx_gpr_addr;
|
||||
static uint8_t _gfx_gpr_color;
|
||||
|
||||
uint8_t _gfx_getpixel256_raw(int x, int y)
|
||||
{
|
||||
if ((unsigned)x >= GFX_WIDTH || (unsigned)y >= GFX_HEIGHT) return 0;
|
||||
_gfx_gpr_y = (uint8_t)y;
|
||||
_gfx_gpr_addr = (uint16_t)(_gfx_addr_base + (unsigned)x);
|
||||
__asm
|
||||
ld a, (__gfx_gpr_y)
|
||||
out (#0x89), a
|
||||
ld hl, (__gfx_gpr_addr)
|
||||
ld a, (hl)
|
||||
ld (__gfx_gpr_color), a
|
||||
__endasm;
|
||||
return _gfx_gpr_color;
|
||||
}
|
||||
@@ -133,6 +133,23 @@ void pieslice(int x, int y, int stangle, int endangle, int radius);
|
||||
void sector(int x, int y, int stangle, int endangle,
|
||||
int xradius, int yradius);
|
||||
|
||||
/* ---- Растровые образы (спрайты) ---------------------------------- *
|
||||
* Формат буфера: 2×uint16 (ширина, высота в пикселях) + пиксели
|
||||
* построчно (1 байт/пиксель в режиме 256). Буфер выделяет вызывающий
|
||||
* размером imagesize(). */
|
||||
enum { COPY_PUT = 0, XOR_PUT, OR_PUT, AND_PUT, NOT_PUT };
|
||||
|
||||
/* Байт под образ прямоугольника (left,top)-(right,bottom) включительно.
|
||||
* ВНИМАНИЕ: результат — 16-бит unsigned; образы ≥64 КБ не поддержаны. */
|
||||
unsigned imagesize(int left, int top, int right, int bottom);
|
||||
|
||||
/* Сохранить прямоугольник экрана в bitmap (размер = imagesize()). */
|
||||
void getimage(int left, int top, int right, int bottom, void *bitmap);
|
||||
|
||||
/* Вывести образ левым-верхним углом в (left,top) операцией op
|
||||
* (COPY/XOR/OR/AND/NOT_PUT). */
|
||||
void putimage(int left, int top, const void *bitmap, int op);
|
||||
|
||||
/* ---- Текст ------------------------------------------------------- *
|
||||
* Шрифт 8×8 (системный). Рисуется текущим цветом на фоновом. */
|
||||
|
||||
|
||||
+29
-21
@@ -1,10 +1,12 @@
|
||||
/*
|
||||
* bgitest — демонстрация BGI-слоя graphics.h (режим 320×256×256).
|
||||
* Фаза 2c: floodfill + pieslice/sector.
|
||||
* Фаза 2d-1: getimage/putimage/imagesize (спрайты, COPY/XOR/OR).
|
||||
*/
|
||||
|
||||
#include <graphics.h>
|
||||
|
||||
static unsigned char buf[2048];
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int mx, my;
|
||||
@@ -17,31 +19,37 @@ int main(void)
|
||||
setcolor(WHITE);
|
||||
rectangle(0, 0, mx, my);
|
||||
setcolor(YELLOW);
|
||||
outtextxy(96, 6, "BGI flood + pie");
|
||||
outtextxy(88, 6, "BGI image / sprite");
|
||||
|
||||
/* Окружность-контур, затем floodfill её нутра. */
|
||||
setcolor(YELLOW);
|
||||
circle(66, 90, 46);
|
||||
setfillstyle(SOLID_FILL, LIGHTRED);
|
||||
floodfill(66, 90, YELLOW);
|
||||
setcolor(WHITE);
|
||||
outtextxy(38, 142, "floodfill");
|
||||
|
||||
/* Круговая диаграмма из трёх pieslice. */
|
||||
setfillstyle(SOLID_FILL, LIGHTGREEN);
|
||||
pieslice(210, 86, 0, 90, 52);
|
||||
/* Исходный спрайт 40×30 в левом верхнем углу. */
|
||||
setfillstyle(SOLID_FILL, LIGHTBLUE);
|
||||
pieslice(210, 86, 90, 200, 52);
|
||||
setfillstyle(SOLID_FILL, LIGHTMAGENTA);
|
||||
pieslice(210, 86, 200, 360, 52);
|
||||
bar(12, 28, 51, 57);
|
||||
setcolor(YELLOW);
|
||||
rectangle(12, 28, 51, 57);
|
||||
setcolor(LIGHTRED);
|
||||
line(12, 28, 51, 57);
|
||||
setfillstyle(SOLID_FILL, LIGHTGREEN);
|
||||
fillellipse(31, 42, 8, 8);
|
||||
setcolor(WHITE);
|
||||
outtextxy(184, 146, "pieslice");
|
||||
outtextxy(12, 60, "src");
|
||||
|
||||
/* Эллиптический сектор со штриховкой. */
|
||||
setfillstyle(HATCH_FILL, LIGHTCYAN);
|
||||
sector(120, 210, 20, 160, 90, 34);
|
||||
/* Захват и раскладка копий (COPY_PUT). */
|
||||
getimage(12, 28, 51, 57, buf);
|
||||
putimage(80, 28, buf, COPY_PUT);
|
||||
putimage(130, 28, buf, COPY_PUT);
|
||||
putimage(180, 28, buf, COPY_PUT);
|
||||
setcolor(WHITE);
|
||||
outtextxy(96, 236, "sector");
|
||||
outtextxy(80, 60, "copy x3");
|
||||
|
||||
/* XOR и OR поверх серого фона. */
|
||||
setfillstyle(SOLID_FILL, DARKGRAY);
|
||||
bar(20, 110, 240, 170);
|
||||
putimage(30, 122, buf, XOR_PUT);
|
||||
outtextxy(30, 176, "xor");
|
||||
putimage(110, 122, buf, OR_PUT);
|
||||
outtextxy(110, 176, "or");
|
||||
putimage(190, 122, buf, COPY_PUT);
|
||||
outtextxy(190, 176, "copy");
|
||||
|
||||
for (;;) { }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user