自制操作系统day10叠加处理
day10叠加处理
叠加处理(harib07b)
现在是鼠标的叠加处理,以后还有窗口的叠加处理
涉及图层
最上面小图层是鼠标指针,最下面的一张图层用来存放桌面壁纸。移动图层的方法实现鼠标指针的移动以及窗口的移动。
struct SHEET {
unsigned char *buf;
int bxsize, bysize, vx0, vy0, col_inv, height, flags;
}
- sheet:透明图层
- buf是用来记录图层上所描画内容的地址(buffer的略语)
- 图层的整体大小,用bxsize*bysize表示。vx0和vy0是表示图层在画面上位置的坐标,
- v是VRAM的略语。
- col_inv表示透明色色号它是color(颜色)和invisible(透明)的组合略语。height表示图层高度。
- Flags用于存放有关图层的各种设定信息。
一个图层不能实现叠加处理,多个图层
#define MAX_SHEETS 256
struct SHTCTL {
unsigned char *vram;
int xsize, ysize, top;
struct SHEET *sheets[MAX_SHEETS];
struct SHEET sheets0[MAX_SHEETS];
};
MAX_SHEETS是能够管理的最大图层数,
SHTCTL结构体,其名称来源于sheet control的略语,
变量vram、xsize、ysize代表VRAM的地址和画面的大小
top代表最上面图层的高度。
sheets是记忆地址变量的领域
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;
ctl->xsize = xsize;
ctl->ysize = ysize;
ctl->top = -1; /*一个SHEET没都有 */
for (i = 0; i < MAX_SHEETS; i++) {
ctl->sheets0[i].flags = 0; /* 标记为未使用 */
}
err:
return ctl;
}
- memman_alloc_4k来分配用于记忆图层控制变量的内存空间,
- sizeof(struct SHTCTL)C编译器自动计算该变量所占空间的大小
- 我们给控制变量赋值,给其下的所有图层变量都加上“未使用”标签。
这里ctl->sheets0[i].flags = 0; 只是一个标志变量,简单的数组索引管理
再做一个函数,用于取得新生成的未使用图层。
#define SHEET_USE 1
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都处于正在使用状态*/
}
sheets0[ ]中寻找未使用的图层,如果找到了,就将其标记为“正在使用”,并返回其地址就可以了
高度设为-1,表示图层的高度还没有设置,因而不是显示对象。
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;
}
设定图层的缓冲区大小和透明色的函数
设定底板高度的函数
void sheet_updown(struct SHTCTL *ctl, struct SHEET *sht, int height)
{
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_refresh(ctl); /* 按新图层的信息重新绘制画面 */
} 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_refresh(ctl); /* 按新图层信息重新绘制画面 */
}
return;
}
sheet_refresh函数
这个函数会从下到上描绘所有的图层。refresh是“刷新”的意思。电视屏幕就是在1秒内完成多帧的描绘才做出动画效果的,这个动作就被称为刷新。
void sheet_refresh(struct SHTCTL *ctl)
{
int h, bx, by, vx, vy;
unsigned char *buf, c, *vram = ctl->vram;
struct SHEET *sht;
for (h = 0; h <= ctl->top; h++) {
sht = ctl->sheets[h];
buf = sht->buf;
for (by = 0; by < sht->bysize; by++) {
vy = sht->-vy0 + by;
for (bx = 0; bx < sht->bxsize; bx++) {
vx = sht->vx0 + bx;
c = buf[by * sht->bxsize + bx];
if (c != sht->col_inv) {
vram[vy * ctl->xsize + vx] = c;
}
}
}
}
return;
}
对于已设定了高度的所有图层而言,要从下往上,将透明以外的所有像素都复制到VRAM中。由于是从下开始复制,所以最后最上面的内容就留在了画面上。
不改变图层高度而只上下左右移动图层的函数——sheet_slide。
void sheet_slide(struct SHTCTL *ctl, struct SHEET *sht, int vx0, int vy0)
{
sht->vx0 = vx0;
sht->vy0 = vy0;
if (sht->height >= 0) { /* 如果正在显示*/
sheet_refresh(ctl); /* 按新图层的信息刷新画面 */
}
return;
}
释放已使用图层的内存的函数sheet_free。
void sheet_free(struct SHTCTL *ctl, struct SHEET *sht)
{
if (sht->height >= 0) {
sheet_updown(ctl, sht, -1); /* 如果处于显示状态,则先设定为隐藏 */
}
sht->flags = 0; /* "未使用"标志 */
return;
}
HariMain函数
void HariMain(void)
{
(中略)
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);
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(shtctl, sht_back, 0, 0);
mx = (binfo->scrnx - 16) / 2; /* 按显示在画面中央来计算坐标 */
my = (binfo->scrny - 28 - 16) / 2;
sheet_slide(shtctl, sht_mouse, mx, my);
sheet_updown(shtctl, sht_back, 0);
sheet_updown(shtctl, sht_mouse, 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(shtctl);
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(shtctl);
} 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) {
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);
/* 移动光标 */
mx += mdec.x;
my += mdec.y;
if (mx < 0) {
mx = 0;
}
if (my < 0) {
my = 0;
}
if (mx > binfo->scrnx - 16) {
mx = binfo->scrnx - 16;
}
if (my > binfo->scrny - 16) {
my = binfo->scrny - 16;
}
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_slide(shtctl, sht_mouse, mx, my); /* 包含sheet_refresh含
sheet_refresh */
}
}
}
}
}
2个图层,分别是sht_back和sht_mouse,还准备了2个缓冲区buf_back和、buf_mouse,用于在其中描绘图形。
提高叠加处理速度(1)(harib07c)
提高图层移动:之前鼠标移动是将所有的图层刷新,其实只需要刷新两个鼠标指针的像素
不对,这里好像是刷新所有图层的相关像素。想法:鼠标是顶层的,能不能只刷新这个顶层的像素加载到显存
void sheet_refreshsub(struct SHTCTL *ctl, int vx0, int vy0, int vx1, int vy1)
{
int h, bx, by, vx, vy;
unsigned char *buf, c, *vram = ctl->vram;
struct SHEET *sht;
for (h = 0; h <= ctl->top; h++) {
sht = ctl->sheets[h];
buf = sht->buf;
for (by = 0; by < sht->bysize; by++) {
vy = sht->vy0 + by;
for (bx = 0; bx < sht->bxsize; bx++) {
vx = sht->vx0 + bx;
if (vx0 <= vx && vx < vx1 && vy0 <= vy && vy < vy1) {
c = buf[by * sht->bxsize + bx];
if (c != sht->col_inv) {
vram[vy * ctl->xsize + vx] = c;
}
}
}
}
}
return;
}
这个函数几乎和sheet_refresh一样,唯一的不同点在于它能使用vx0~ vy1指定刷新的范围,而我们只追加了一个if语句就实现了这个新功能
使用这个refreshsub函数来提高sheet_slide的运行速度。
void sheet_slide(struct SHTCTL *ctl, 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(ctl, old_vx0, old_vy0, old_vx0 + sht->bxsize, old_vy0 + sht-
>bysize);
sheet_refreshsub(ctl, vx0, vy0, vx0 + sht->bxsize, vy0 + sht->bysize);
}
return;
}
相应的,图层显示文字也可以只刷新对应像素
void sheet_refresh(struct SHTCTL *ctl, struct SHEET *sht, int bx0, int by0, int bx1, int by1)
{
if (sht->height >= 0) { /* 如果正在显示,则按新图层的信息刷新画面*/
sheet_refreshsub(ctl, sht->vx0 + bx0, sht->vy0 + by0, sht->vx0 + bx1, sht-
>vy0 + by1);
}
return;
}
所谓指定范围,并不是直接指定画面内的坐标,而是以缓冲区内的坐标来表示。这样一来,HariMain就可以不考虑图层在画面中的位置了。
改动了refresh,所以也要相应改造updown。做了改动的只有sheet_refresh(ctl)这部分(有两处),修改后的程序如下:
sheet_refreshsub(ctl, sht->vx0, sht->vy0, sht->vx0 + sht->bxsize, sht->vy0 + sht->bysize);
改写HariMain
void HariMain(void)
{
(中略)
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(shtctl, 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) {
(中略)
sheet_refresh(shtctl, sht_back, 0, 16, 16, 32); /* 这里! */
} else if (fifo8_status(&mousefifo) != 0) {
i = fifo8_get(&mousefifo);
io_sti();
if (mouse_decode(&mdec, i) != 0) {
(中略)
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(shtctl, sht_back, 32, 16, 32 + 15 * 8, 32); /* 这里! */
(中略)
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(shtctl, sht_back, 0, 0, 80, 16); /* 这里! */
sheet_slide(shtctl, sht_mouse, mx, my);
}
}
}
}
}
提高叠加处理速度(2)(harib07d)
之前代码缺陷:即便只刷新图层的一部分,也要对所有图层的全部像素执行if语句,判断“是写入呢,还是不写呢”。
因此:最初就应该把for语句的范围限定在刷新范围之内。
原来:
void sheet_refreshsub(struct SHTCTL *ctl, int vx0, int vy0, int vx1, int vy1)
{
int h, bx, by, vx, vy;
unsigned char *buf, c, *vram = ctl->vram;
struct SHEET *sht;
for (h = 0; h <= ctl->top; h++) {
sht = ctl->sheets[h];
buf = sht->buf;
for (by = 0; by < sht->bysize; by++) {
vy = sht->vy0 + by;
for (bx = 0; bx < sht->bxsize; bx++) {
vx = sht->vx0 + bx;
if (vx0 <= vx && vx < vx1 && vy0 <= vy && vy < vy1) {
c = buf[by * sht->bxsize + bx];
if (c != sht->col_inv) {
vram[vy * ctl->xsize + vx] = c;
}
}
}
}
}
return;
}
改进:
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;
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;
if (bx0 < 0) { bx0 = 0; } /* 说明(1) */
if (by0 < 0) { by0 = 0; }
if (bx1 > sht->bxsize) { bx1 = sht->bxsize; } /* 说明(2) */
if (by1 > sht->bysize) { by1 = sht->bysize; }
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;
}
}
}
}
return;
}
改良的关键在于,bx在for语句中并不是在0到bxsize之间循环,而是在bx0到bx1之间循环(对于by也一样)。而bx0和bx1都是从刷新范围“倒推”求得的。倒推其实就是把公式变形转换了一下,具体如下:
vx = sht->vx0 + bx; → bx = vx - sht->vx0;