叠加处理
在画面上进行叠加显示,类似于将绘制了图案的透明图层叠加在一起,最上面的小图层用来描绘鼠标指针。同时通过移动图层实现鼠标指针的移动以及窗口的移动。
sheet.c
#define MAX_SHEETS 256 //图层数最多为256
struct SHEET //透明图层
{
unsigned char *buf; //用来记录图层上所描画的内容的地址
int bxsize, bysize, //图层整体的大小
vx0, vy0, //图层在画面上位置的坐标
col_inv, //透明色号
height, //图层高度
flags; //存放有关图层设定的各种信息
struct SHTCTL *ctl; //所属图层管理
};
struct SHTCTL //图层管理
{
unsigned char *vram; //VRAM地址
int xsize, ysize; //画面大小
int top; //最上层图层的高度
struct SHEET *sheets[MAX_SHEETS]; //按高度排好顺序的图层
struct SHEET sheets0[MAX_SHEETS]; //排序之前的图层
};
#define SHEET_USE 1
//初始化图层
struct SHTCTL *shtctl_init(struct MEMMAN *memman, unsigned char *vram, int xsize, int ysize)
{
struct SHTCTL *ctl;
int i;
ctl = (struct SHTCTL *) memman_alloc_4k(memman, sizeof (struct SHTCTL)); //分配用于记忆图层控制变量的内存克难空间
if (ctl == 0) //分配失败
goto err;
ctl->vram = vram; //VRAM地址
ctl->xsize = xsize; //VRAM大小
ctl->ysize = ysize;
ctl->top = -1; /* 一个SHEET也没有 */
for (i = 0; i < MAX_SHEETS; i++)
{
ctl->sheets0[i].flags = 0; /* 标记为未使用 */
ctl->sheets0[i].ctl = ctl; //记录所属
}
err:
return ctl;
}
//分配图层
struct SHEET *sheet_alloc(struct SHTCTL *ctl)
{
struct SHEET *sht;
int i;
for (i = 0; i < MAX_SHEETS; i++)
{
if (ctl->sheets0[i].flags == 0)
{
sht = &ctl->sheets0[i];
sht->flags = SHEET_USE; /* 标记为正在使用 */
sht->height = -1; /* 表示图层高度还没有设定,处于隐藏状态 */
return sht;
}
}
return 0; /* 所有SHEET都处于正在使用状态 */
}
//设置图层缓冲区和透明色
void sheet_setbuf(struct SHEET *sht, unsigned char *buf, int xsize, int ysize, int col_inv)
{
sht->buf = buf;
sht->bxsize = xsize;
sht->bysize = ysize;
sht->col_inv = col_inv;
return;
}
//从下到上重绘所有图层,最后最上面的内容就留在了画面上
//提高叠加处理速度:只刷新移动前后的部分,而不是整个屏幕,避免每次都对全部VRAM重新填像素
void sheet_refreshsub(struct SHTCTL *ctl, int vx0, int vy0, int vx1, int vy1)
{
int h, bx, by, vx, vy, bx0, by0, bx1, by1;
unsigned char *buf, c, *vram = ctl->vram;
struct SHEET *sht;
/* 如果refresh的范围超出了屏幕则修正 */
if (vx0 < 0) { vx0 = 0; }
if (vy0 < 0) { vy0 = 0; }
if (vx1 > ctl->xsize) { vx1 = ctl->xsize; }
if (vy1 > ctl->ysize) { vy1 = ctl->ysize; }
for (h = 0; h <= ctl->top; h++)
{
sht = ctl->sheets[h];
buf = sht->buf;
/* 使用vx0~vy1, 对bx0~by1进行倒推 限制刷新范围:*/
bx0 = vx0 - sht->vx0;
by0 = vy0 - sht->vy0;
bx1 = vx1 - sht->vx0;
by1 = vy1 - sht->vy0;
//如果refresh的范围超出了图层范围则修正
if (bx0 < 0) { bx0 = 0; }
if (by0 < 0) { by0 = 0; }
if (bx1 > sht->bxsize) { bx1 = sht->bxsize; }
if (by1 > sht->bysize) { by1 = sht->bysize; }
if (bx0 >= ctl->xsize-16)
bx1 = ctl->xsize;
for (by = by0; by < by1; by++)
{
vy = sht->vy0 + by;
for (bx = bx0; bx < bx1; bx++)
{
vx = sht->vx0 + bx;
c = buf[by * sht->bxsize + bx]; //之前图层的像素点
if (c != sht->col_inv) //如果不是背景色
vram[vy * ctl->xsize + vx] = c; //重新填充到VRAM中
}
}
}
return;
}
//不直接指定画面中的坐标,而是以缓冲区的坐标来表示
void sheet_refresh(struct SHEET *sht, int bx0, int by0, int bx1, int by1)
{
if (sht->height >= 0) /* 如果正在显示,则按新图层信息刷新画面 */
sheet_refreshsub(sht->ctl, sht->vx0 + bx0, sht->vy0 + by0, sht->vx0 + bx1, sht->vy0 + by1);
return;
}
//设定图层高度
void sheet_updown(struct SHEET *sht, int height)
{
struct SHTCTL *ctl = sht->ctl; //图层管理
int h, old = sht->height; /* 存储设置前的高度信息 */
/* 如果指定的高度过高或过低,则进行修正 */
if (height > ctl->top + 1)
height = ctl->top + 1;
if (height < -1)
height = -1;
sht->height = height; /* 设定高度 */
/* 下面主要是进行sheets[]的重新排列 */
if (old > height) /* 比以前低 */
{
if (height >= 0)
{
/* 把中间的往上提 */
for (h = old; h > height; h--)
{
ctl->sheets[h] = ctl->sheets[h - 1];
ctl->sheets[h]->height = h;
}
ctl->sheets[height] = sht;
}
else /* 隐藏 */
{
if (ctl->top > old)
{
/* 把上面的降下来 */
for (h = old; h < ctl->top; h++) {
ctl->sheets[h] = ctl->sheets[h + 1];
ctl->sheets[h]->height = h;
}
}
ctl->top--; /* 由于显示中的图层减少了一个,所以最上面的图层高度下降 */
}
/* 按新图层的信息重新绘制画面 */
sheet_refreshsub(ctl, sht->vx0, sht->vy0, sht->vx0 + sht->bxsize, sht->vy0 + sht->bysize);
}
else if (old < height) /* 比以前高 */
{
if (old >= 0)
{
/* 把中间的拉上去 */
for (h = old; h < height; h++)
{
ctl->sheets[h] = ctl->sheets[h + 1];
ctl->sheets[h]->height = h;
}
ctl->sheets[height] = sht;
}
else
{ /* 由隐藏状态转为显示状态 */
/* 将已经在上面的提上来 */
for (h = ctl->top; h >= height; h--)
{
ctl->sheets[h + 1] = ctl->sheets[h];
ctl->sheets[h + 1]->height = h + 1;
}
ctl->sheets[height] = sht;
ctl->top++; /* 由于已显示的图层增加了1个,所以最上面的图层高度增加 */
}
/* 按新图层信息重新绘制画面 */
sheet_refreshsub(ctl, sht->vx0, sht->vy0, sht->vx0 + sht->bxsize, sht->vy0 + sht->bysize);
}
return;
}
//移动图层
void sheet_slide(struct SHEET *sht, int vx0, int vy0)
{
int old_vx0 = sht->vx0, old_vy0 = sht->vy0;
sht->vx0 = vx0; //图层左上角坐标
sht->vy0 = vy0;
if (sht->height >= 0) /* 如果正在显示,则按新图层信息刷新画面 */
{
sheet_refreshsub(sht->ctl, old_vx0, old_vy0, old_vx0 + sht->bxsize, old_vy0 + sht->bysize);
sheet_refreshsub(sht->ctl, vx0, vy0, vx0 + sht->bxsize, vy0 + sht->bysize);
}
return;
}
//释放已用图层
void sheet_free(struct SHEET *sht)
{
if (sht->height >= 0)
sheet_updown(sht, -1); /* 如果处于显示状态,则先设定为隐藏 */
sht->flags = 0; /* “未使用”标志 */
return;
}
bootpack.c节选
struct SHTCTL *shtctl;
struct SHEET *sht_back, *sht_mouse;
unsigned char *buf_back, buf_mouse[256]; //在其中绘制图形
init_palette(); //设置调色板
shtctl = shtctl_init(memman, binfo->vram, binfo->scrnx, binfo->scrny); //图层管理初始化
sht_back = sheet_alloc(shtctl); //显示背景的图层
sht_mouse = sheet_alloc(shtctl); //显示鼠标的图层
buf_back = (unsigned char *) memman_alloc_4k(memman, binfo->scrnx * binfo->scrny); //为显示背景的VRAM分配空间
sheet_setbuf(sht_back, buf_back, binfo->scrnx, binfo->scrny, -1); /* 没有透明色 */
sheet_setbuf(sht_mouse, buf_mouse, 16, 16, 99); //透明色号为99
init_screen8(buf_back, binfo->scrnx, binfo->scrny); //绘制背景
init_mouse_cursor8(buf_mouse, 99); //背景色号为99,绘制鼠标
sheet_slide(sht_back, 0, 0);
mx = (binfo->scrnx - 16) / 2; /* 按显示在画面中央计算坐标 */
my = (binfo->scrny -28 - 16) / 2;
sheet_slide(sht_mouse, mx, my);
sheet_updown(sht_back, 0); //设置图层背景高度为0
sheet_updown(sht_mouse, 1); //设置图层背景高度为1
sprintf(s, "(%3d, %3d)", mx, my);
putfonts8_asc(buf_back, binfo->scrnx, 0, 0, COL8_FFFFFF, s); //显示鼠标坐标
sprintf(s, "memory %dMB free : %dKB",
memtotal / (1024 * 1024), memman_total(memman) / 1024);
putfonts8_asc(buf_back, binfo->scrnx, 0, 32, COL8_FFFFFF, s); //显示内存信息
sheet_refresh(sht_back, 0, 0, binfo->scrnx, 48); //刷新
for (;;)
{
io_cli();
if (fifo8_status(&keyfifo) + fifo8_status(&mousefifo) == 0)
io_stihlt();
else
{
if (fifo8_status(&keyfifo) != 0)
{
i = fifo8_get(&keyfifo);
io_sti();
sprintf(s, "%02X", i);
boxfill8(buf_back, binfo->scrnx, COL8_008484, 0, 16, 15, 31);
putfonts8_asc(buf_back, binfo->scrnx, 0, 16, COL8_FFFFFF, s);
sheet_refresh(sht_back, 0, 16, 16, 32);
}
else if (fifo8_status(&mousefifo) != 0)
{
i = fifo8_get(&mousefifo);
io_sti();
if (mouse_decode(&mdec, i) != 0)
{
/* 数据的3个字节都齐了,显示出来 */
sprintf(s, "[lcr %4d %4d]", mdec.x, mdec.y);
if ((mdec.btn & 0x01) != 0) //如果第一位是1,说明按下左键
s[1] = 'L';
if ((mdec.btn & 0x02) != 0)
s[3] = 'R';
if ((mdec.btn & 0x04) != 0)
s[2] = 'C';
boxfill8(buf_back, binfo->scrnx, COL8_008484, 32, 16, 32 + 15 * 8 - 1, 31);
putfonts8_asc(buf_back, binfo->scrnx, 32, 16, COL8_FFFFFF, s);
sheet_refresh(sht_back, 32, 16, 32 + 15 * 8, 32);
/* 鼠标指针的移动 */
mx += mdec.x;
my += mdec.y;
if (mx < 0)
mx = 0;
if (my < 0)
my = 0;
if (mx > binfo->scrnx - 1) //使鼠标可以移到画面外
mx = binfo->scrnx - 1;
if (my > binfo->scrny - 1)
my = binfo->scrny - 1;
sprintf(s, "(%3d, %3d)", mx, my);
boxfill8(buf_back, binfo->scrnx, COL8_008484, 0, 0, 79, 15); /* 隐藏坐标*/
putfonts8_asc(buf_back, binfo->scrnx, 0, 0, COL8_FFFFFF, s); /* 显示坐标 */
sheet_refresh(sht_back, 0, 0, 80, 16);
sheet_slide(sht_mouse, mx, my);
}
}
}
}
可以发现及时鼠标移动到文字上,也不会把文字擦除,因为他们属于不同的图层。
总结一下叠加处理:
1、图层管理初始化,所有图层高度都为-1。
2、分配图层。
3、设定图层高度。如果图层高度过高或过低,则修正。如果现在图层高度比之前低,如果设为-1,则隐藏,图层数减少;否则将现在位于高度和之前高度中间的图层往上提。如果现在图层高度比之前高,如果之前不是隐藏状态,则把位于图层现在高度与之前高度中间的图层下拉;否则显示,并将它上面的图层往上提,图层数增加。最后要重绘新图层。
4、局部刷新图层。移动图层后,从下往上依次刷新每个图层与移动部分重叠部分。这个是重点。
5、移动图层。只刷新移动前和移动后的部分。
6、不需要的时候释放图层。