day11制作窗口(鼠标显示、图层和图层控制器、显示窗口、高速计数器、消除闪烁)
day11制作窗口
鼠标显示问题
if (mx > binfo->scrnx - 16) {
mx = binfo->scrnx - 16;
}
if (my > binfo->scrny - 16) {
my = binfo->scrny - 16;
}
因为这段代码的问题,会完整显示整个鼠标,导致鼠标所指的像素无法覆盖整个屏幕像素
if (mx > binfo->scrnx - 1) {
mx = binfo->scrnx - 1;
}
if (my > binfo->scrny - 1) {
my = binfo->scrny - 1;
}
使用这段代码便可以
这里当鼠标移到最右边,左边就会产生鼠标重影,应该就是书中所阐述的问题
只要图层一跑到画面的外面去就会出问题。
后面的操作也证明确实是这个问题才产生了重影
实现画面外的支持(harib08b)
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++) {
(中略)
}
return;
}
因此修正这个刷新的范围,让他不刷新新画面以外的内容
shtctl的指定省略(harib08c)
之前的sheet_updown函数,每一次都需要自己制定控制器,改善这个问题
其实解决方案就是,实现每个图层和管理这个图层控制器的一个双向绑定,在图层添加一个控制器指针,指向它所属于的控制器,然后在其他函数将这个控制器赋给一个控制器变量(不赋值,直接用也可以),就可以在各个函数的形参不需要人为的添加控制器
首先需要在struct SHEET中加入struct SHTCTL *ctl:
struct SHEET {
unsigned char *buf;
int bxsize, bysize, vx0, vy0, col_inv, height, flags;
struct SHTCTL *ctl;
};
然后对函数shtctl_init也进行追加,仅追加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;
ctl->xsize = xsize;
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;
}
函数sheet_updown也要修改:
void sheet_updown(struct SHEET *sht, int height)
{
238
struct SHTCTL *ctl = sht->ctl;
int h, old = sht->height; /* 将设置前的高度记录下来 */
(中略)
}
这样一来在sheet_updown函数里就可以不指定ctl了。函数变得比之前好用了。
将sheet_refresh、sheet_slide、sheet_free这几个函数全部修改一下:
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_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的HariMain中,把相应的shtctl也删掉:
sheet_slide(shtctl, sht_back, 0, 0); → sheet_slide(sht_back, 0, 0);
sheet_slide(shtctl, sht_mouse, mx, my); → sheet_slide(sht_mouse, mx, my);
sheet_updown(shtctl, sht_back, 0); → sheet_updown(sht_back, 0);
sheet_updown(shtctl, sht_mouse, 1); → sheet_updown(sht_mouse, 1);
sheet_refresh(shtctl, sht_back, 0, 0, binfo->scrnx, 48);
→ sheet_refresh(sht_back, 0, 0, binfo->scrnx, 48);
sheet_refresh(shtctl, sht_back, 0, 16, 16, 32);
→ sheet_refresh(sht_back, 0, 16, 16, 32);
sheet_refresh(shtctl, sht_back, 32, 16, 32 + 15 * 8, 32);
→ sheet_refresh(sht_back, 32, 16, 32 + 15 * 8, 32);
sheet_refresh(shtctl, sht_back, 0, 0, 80, 16);
→ sheet_refresh(sht_back, 0, 0, 80, 16);
sheet_slide(shtctl, sht_mouse, mx, my); → sheet_slide(sht_mouse, mx, my);
显示窗口(harib08d)
准备图层,然后图层缓冲区描绘一个貌似窗口的图
void make_window8(unsigned char *buf, int xsize, int ysize, char *title)
{
static char closebtn[14][16] = {
"OOOOOOOOOOOOOOO@",
"OQQQQQQQQQQQQQ$@",
"OQQQQQQQQQQQQQ$@",
"OQQQ@@QQQQ@@QQ$@",
"OQQQQ@@QQ@@QQQ$@",
"OQQQQQ@@@@QQQQ$@",
"OQQQQQQ@@QQQQQ$@",
"OQQQQQ@@@@QQQQ$@",
"OQQQQ@@QQ@@QQQ$@",
"OQQQ@@QQQQ@@QQ$@",
"OQQQQQQQQQQQQQ$@",
"OQQQQQQQQQQQQQ$@",
"O$$$$$$$$$$$$$$@",
"@@@@@@@@@@@@@@@@"
};
int x, y;
char c;
boxfill8(buf, xsize, COL8_C6C6C6, 0, 0, xsize - 1, 0 );
boxfill8(buf, xsize, COL8_FFFFFF, 1, 1, xsize - 2, 1 );
boxfill8(buf, xsize, COL8_C6C6C6, 0, 0, 0, ysize - 1);
boxfill8(buf, xsize, COL8_FFFFFF, 1, 1, 1, ysize - 2);
boxfill8(buf, xsize, COL8_848484, xsize - 2, 1, xsize - 2, ysize - 2);
boxfill8(buf, xsize, COL8_000000, xsize - 1, 0, xsize - 1, ysize - 1);
boxfill8(buf, xsize, COL8_C6C6C6, 2, 2, xsize - 3, ysize - 3);
boxfill8(buf, xsize, COL8_000084, 3, 3, xsize - 4, 20 );
boxfill8(buf, xsize, COL8_848484, 1, ysize - 2, xsize - 2, ysize - 2);
boxfill8(buf, xsize, COL8_000000, 0, ysize - 1, xsize - 1, ysize - 1);
putfonts8_asc(buf, xsize, 24, 4, COL8_FFFFFF, title);
for (y = 0; y < 14; y++) {
for (x = 0; x < 16; x++) {
c = closebtn[y][x];
if (c == '@') {
c = COL8_000000;
} else if (c == '$') {
c = COL8_848484;
} else if (c == 'Q') {
c = COL8_C6C6C6;
} else {
c = COL8_FFFFFF;
}
buf[(5 + y) * xsize + (xsize - 21 + x)] = c;
}
}
return;
}
只是对graph.c的init_screen8函数稍微进行了改造,而×按钮的功能则是通过修改 init_mouse_cursor8而得到的
void HariMain(void)
{
(中略)
struct SHEET *sht_back, *sht_mouse, *sht_win; /* 这里! */
unsigned char *buf_back, buf_mouse[256], *buf_win; /* 这里! */
(中略)
init_palette();
shtctl = shtctl_init(memman, binfo->vram, binfo->scrnx, binfo->scrny);
sht_back = sheet_alloc(shtctl);
sht_mouse = sheet_alloc(shtctl);
sht_win = sheet_alloc(shtctl); /* 这里! */
buf_back = (unsigned char *) memman_alloc_4k(memman, binfo->scrnx * binfo->scrny);
buf_win = (unsigned char *) memman_alloc_4k(memman, 160 * 68); /* 这里! */
sheet_setbuf(sht_back, buf_back, binfo->scrnx, binfo->scrny, -1); /* 没有透明色 */
sheet_setbuf(sht_mouse, buf_mouse, 16, 16, 99);
sheet_setbuf(sht_win, buf_win, 160, 68, -1); /* 没有透明色 */ /* 这里!*/
init_screen8(buf_back, binfo->scrnx, binfo->scrny);
init_mouse_cursor8(buf_mouse, 99);
make_window8(buf_win, 160, 68, "window"); /* 这里! */
putfonts8_asc(buf_win, 160, 24, 28, COL8_000000, "Welcome to"); /* 这里! */
putfonts8_asc(buf_win, 160, 24, 44, COL8_000000, " Haribote-OS!"); /* 这里! */
sheet_slide(sht_back, 0, 0);
mx = (binfo->scrnx - 16) / 2; /* 为使其处于画面的中央位置,计算坐标 */
my = (binfo->scrny - 28 - 16) / 2;
sheet_slide(sht_mouse, mx, my);
sheet_slide(sht_win, 80, 72); /* 这里! */
sheet_updown(sht_back, 0);
sheet_updown(sht_win, 1); /* 这里! */
sheet_updown(sht_mouse, 2);
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);
(中略)
高速计数器(harib08f)
还把图层的高度设置恢复原样,然后再试着做个动作更丰富的窗口。
做一个能够计数,并将计数结果显示出来的窗口。计数器在英语中是counter,所以我们就将窗口的名称改为counter。
void HariMain(void)
{
struct BOOTINFO *binfo = (struct BOOTINFO *) ADR_BOOTINFO;
char s[40], keybuf[32], mousebuf[128];
int mx, my, i;
unsigned int memtotal, count = 0; /* 这里! */
struct MOUSE_DEC mdec;
struct MEMMAN *memman = (struct MEMMAN *) MEMMAN_ADDR;
struct SHTCTL *shtctl;
struct SHEET *sht_back, *sht_mouse, *sht_win;
unsigned char *buf_back, buf_mouse[256], *buf_win;
(中略)
init_palette();
shtctl = shtctl_init(memman, binfo->vram, binfo->scrnx, binfo->scrny);
sht_back = sheet_alloc(shtctl);
sht_mouse = sheet_alloc(shtctl);
sht_win = sheet_alloc(shtctl);
buf_back = (unsigned char *) memman_alloc_4k(memman, binfo->scrnx * binfo->scrny);
buf_win = (unsigned char *) memman_alloc_4k(memman, 160 * 52); /* 这里! */
sheet_setbuf(sht_back, buf_back, binfo->scrnx, binfo->scrny, -1); /* 没有透明色 */
sheet_setbuf(sht_mouse, buf_mouse, 16, 16, 99);
sheet_setbuf(sht_win, buf_win, 160, 52, -1); /* 没有透明色 */ /* 这里! */
init_screen8(buf_back, binfo->scrnx, binfo->scrny);
init_mouse_cursor8(buf_mouse, 99);
make_window8(buf_win, 160, 52, "counter"); /* 这里! */
sheet_slide(sht_back, 0, 0);
mx = (binfo->scrnx - 16) / 2; /* 为使其处于画面中央位置,计算坐标 */
my = (binfo->scrny - 28 - 16) / 2;
sheet_slide(sht_mouse, mx, my);
sheet_slide(sht_win, 80, 72);
sheet_updown(sht_back, 0);
sheet_updown(sht_win, 1);
sheet_updown(sht_mouse, 2);
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 (;;) {
count++; /* 从这里开始 */
sprintf(s, "%010d", count);
boxfill8(buf_win, 160, COL8_C6C6C6, 40, 28, 119, 43);
putfonts8_asc(buf_win, 160, 40, 28, COL8_000000, s);
sheet_refresh(sht_win, 40, 28, 120, 44); /* 到这里结束 */
io_cli();
if (fifo8_status(&keyfifo) + fifo8_status(&mousefifo) == 0) {
io_sti(); /* 不做HLT */
} else {
(中略)
}
}
}
消除闪烁(1)(harib08g)
在这里就是之前叠加处理速度时想到的一个问题,只刷新相应的图层,当时鼠标是top图层,所以当时想的是只需要处理鼠标图层,但在这里闪烁,就要考虑refresh对象及其以上的图层,因为其以上的图层可能将refresh对象覆盖
sheet_refreshsub函数
void sheet_refreshsub(struct SHTCTL *ctl, int vx0, int vy0, int vx1, int vy1, int h0)
{
(中略)
for (h = h0; h <= ctl->top; h++) {
//之前h是从0开始
(中略)
}
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,
sht->height);
/* ↑这里! */
}
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, 0);
sheet_refreshsub(sht->ctl, vx0, vy0, vx0 + sht->bxsize, vy0 + sht->bysize, sht-
>height);
/* ↑这里! */
}
return;
}
void sheet_updown(struct SHEET *sht, int 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;
/* 这里 */ sheet_refreshsub(ctl, sht->vx0, sht->vy0, sht->vx0 + sht->bxsize, sht-
>vy0 + sht->bysize, height + 1);
} 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, 0);
}
} 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++; /* 显示中的图层增加了一个,故最上面的高度也增加 */
}
sheet_refreshsub(ctl, sht->vx0, sht->vy0, sht->vx0 + sht->bxsize, sht->vy0 + sht-
>bysize, height);
/* ↑这里 */
}
return;
}
消除闪烁(2)(harib08h)
让鼠标不闪烁,因为之前是刷新要刷新的图层及以上,鼠标又是top图层,所以鼠标会闪烁
开辟一块内存,大小和VRAM一样,我们先称之为map(地图)
struct SHTCTL {
unsigned char *vram, *map; /* 这里! */
int xsize, ysize, top;
struct SHEET *sheets[MAX_SHEETS];
struct SHEET sheets0[MAX_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->map = (unsigned char *) memman_alloc_4k(memman, xsize * ysize);
if (ctl->map == 0) {
memman_free_4k(memman, (int) ctl, sizeof (struct SHTCTL));
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; /* 未使用标记 */
ctl->sheets0[i].ctl = ctl; /* 记录所属 */
}
err:
return ctl;
}
这块内存用来表示画面上的点是哪个图层的像素,所以它就相当于是图层的地图。
下面我们来写向map中写入1、2等图层号码的函数:
void sheet_refreshmap(struct SHTCTL *ctl, int vx0, int vy0, int vx1, int vy1, int h0)
{
int h, bx, by, vx, vy, bx0, by0, bx1, by1;
unsigned char *buf, sid, *map = ctl->map;
struct SHEET *sht;
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 = h0; h <= ctl->top; h++) {
sht = ctl->sheets[h];
sid = sht - ctl->sheets0; /* 将进行了减法计算的地址作为图层号码使用 */
buf = sht->buf;
bx0 = vx0 - sht->vx0;
by0 = vy0 - sht->vy0;
bx1 = vx1 - sht->vx0;
by1 = vy1 - sht->vy0;
if (bx0 < 0) { bx0 = 0; }
if (by0 < 0) { by0 = 0; }
if (bx1 > sht->bxsize) { bx1 = sht->bxsize; }
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;
if (buf[by * sht->bxsize + bx] != sht->col_inv) {
map[vy * ctl->xsize + vx] = sid;
}
}
}
}
return;
}
这个函数与以前的refreshsub函数基本一样,只是用色号代替了图层号码而已。主要就是给每一个像素点给id号,之前就是给色号
下面是sheet_refreshsub函数。我们对它进行改写,让它可以使用map:
void sheet_refreshsub(struct SHTCTL *ctl, int vx0, int vy0, int vx1, int vy1, int h0, int h1)
{
int h, bx, by, vx, vy, bx0, by0, bx1, by1;
unsigned char *buf, *vram = ctl->vram, *map = ctl->map, sid;
struct SHEET *sht;
/* 如果refresh的范围超出了画面则修正*/
(中略)
for (h = h0; h <= h1; h++) {
sht = ctl->sheets[h];
buf = sht->buf;
sid = sht - ctl->sheets0;
/* 利用vx0~vy1,对bx0~by1进行倒推 */
(中略)
for (by = by0; by < by1; by++) {
vy = sht->vy0 + by;
for (bx = bx0; bx < bx1; bx++) {
vx = sht->vx0 + bx;
if (map[vy * ctl->xsize + vx] == sid) {
vram[vy * ctl->xsize + vx] = buf[by * sht->bxsize + bx];
}
}
}
}
return;
}
今后程序会对照map内容来向VRAM中写入,所以有时没必要从下面开始一直刷新到最上面一层,因此不仅要能指定h0,也要可以指定h1。
sheet_refreshsub的3个函数:
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,
sht->height, sht->height);
}
return;
}
void sheet_slide(struct SHEET *sht, int vx0, int vy0)
{
struct SHTCTL *ctl = sht->ctl;
int old_vx0 = sht->vx0, old_vy0 = sht->vy0;
sht->vx0 = vx0;
sht->vy0 = vy0;
if (sht->height >= 0) { /* 如果正在显示,则按新图层的信息进行刷新 */
sheet_refreshmap(ctl, old_vx0, old_vy0, old_vx0 + sht->bxsize, old_vy0 + sht-
>bysize, 0);
sheet_refreshmap(ctl, vx0, vy0, vx0 + sht->bxsize, vy0 + sht->bysize, sht->height);
sheet_refreshsub(ctl, old_vx0, old_vy0, old_vx0 + sht->bxsize, old_vy0 + sht-
>bysize, 0,
sht->height - 1);
sheet_refreshsub(ctl, vx0, vy0, vx0 + sht->bxsize, vy0 + sht->bysize, sht->height,
sht->height);
}
return;
}
void sheet_updown(struct SHEET *sht, int height)
{
(中略)
/* 下面主要是对sheets[]进行重新排列 */
if (old > height) { /* 比以前低 */
if (height >= 0) {
/* 中间的图层也提高一层 */
(中略)
sheet_refreshmap(ctl, sht->vx0, sht->vy0, sht->vx0 + sht->bxsize, sht->vy0 +
sht->bysize, height + 1);
sheet_refreshsub(ctl, sht->vx0, sht->vy0, sht->vx0 + sht->bxsize, sht->vy0 +
sht->bysize, height + 1, old);
} else { /* 隐藏 */
(中略)
sheet_refreshmap(ctl, sht->vx0, sht->vy0, sht->vx0 + sht->bxsize, sht-
>vy0 + sht->bysize, 0);
sheet_refreshsub(ctl, sht->vx0, sht->vy0, sht->vx0 + sht->bxsize, sht-
>vy0 + sht->bysize, 0, old - 1);
}
} else if (old < height) { /* 比以前高 */
(中略)
sheet_refreshmap(ctl, sht->vx0, sht->vy0, sht->vx0 + sht->bxsize, sht->vy0 + sht-
>bysize,
height);
sheet_refreshsub(ctl, sht->vx0, sht->vy0, sht->vx0 + sht->bxsize, sht->vy0 + sht-
>bysize,
height, height);
}
return;
}