发布时间:2026-07-21阅读(1)

现在bootpack.c中写了很多memory功能
把他们独立出来
新建:memory.c(具体代码查看10_day\harib07a文件夹)
#include "bootpack.h"#define EFLAGS_AC_BIT0x00040000#define CR0_CACHE_DISABLE0x60000000unsigned int memtest(unsigned int start, unsigned int end){....代码省略}void memman_init(struct MEMMAN *man){....代码省略}unsigned int memman_total(struct MEMMAN *man){....代码省略}unsigned int memman_alloc(struct MEMMAN *man, unsigned int size){....代码省略}int memman_free(struct MEMMAN *man, unsigned int addr, unsigned int size){....代码省略}

内存管理功能有小bug,反复进行内存分配和释放后,很快消耗殆尽
更新:memory.c
....代码省略unsigned int memman_alloc_4k(struct MEMMAN *man, unsigned int size){unsigned int a;size = (size 0xfff) & 0xfffff000;a = memman_alloc(man, size);return a;}int memman_free_4k(struct MEMMAN *man, unsigned int addr, unsigned int size){int i;size = (size 0xfff) & 0xfffff000;i = memman_free(man, addr, size);return i;}
什么是向下舍入(round down)?
什么是向上舍入(round up)?
优化向上舍入
本书中采用的是优化版

叠加的意思是透明图层叠加:鼠标层,窗口层,桌面层,壁纸层
struct SHEET { unsigned char *buf; int bxsize, bysize, vx0, vy0, col_inv, height, flags;}
创建多图层管理结构
#define MAX_SHEETS 256struct SHTCTL { unsigned char *vram; int xsize, ysize, top; struct SHEET *sheets[MAX_SHEETS]; struct SHEET sheets0[MAX_SHEETS];};


新建:sheet.c
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;}
这段程序作用:

更新:sheet.c
#define SHEET_USE 1struct 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都处于正在使用状态*/}
这段代码作用:取得新生成的未使用图层
更新:sheet,c
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;}
设定图层的缓冲区大小和透明色的函数
更新:sheet.c
设定底板高度的函数
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;}
ctl—>sheets[h] —>height = h;
(* (*ctl).sheets[h]).height = h;
struct SHEET *sht2; sht2 = ctl->sheets[h]; sht2 -> height = h;
三段代码同一个意思,缩写而已。
更新:sheet.c
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;}
sheet_refresh函数会从下到上描绘所有的图层。refresh是“刷新”的意思,电视屏幕就是在1秒内完成多帧的描绘才做出动画效果
对于已设定了高度的所有图层而言,要从下往上,将透明以外的所有像素都复制到VRAM中。由于是从下开始复制,所以最后最上面的内容就留在了画面上。
更新:sheet.c
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_slide:不改变图层高度而只上下左右移动图层的函数。slide原意是“滑动”,这里指上下左右移动图层
更新:sheet.c
void sheet_free(struct SHTCTL *ctl, struct SHEET *sht){ if (sht->height >= 0) { sheet_updown(ctl, sht, -1); /* 如果处于显示状态,则先设定为隐藏 */ } sht->flags = 0; /* "未使用"标志 */ return;}
sheet_free是释放已使用图层的内存的函数
sheet.c功能暂时写这些
添加到bootpack.c中
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, "(=, =)", 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, "X", 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 M M]", 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, "(=, =)", 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,用于在其中描绘图形。以前我们指定为binfo —> vram的部分,现在有很多都改成了buf_back。而且每次修改缓冲区之后都要刷新。
测试:cmd,输入make run文件夹:10_day\harib07b

鼠标实现了透明

1.如何提高鼠标反应速度?
鼠标图标16x16=256个像素,只要鼠标稍微移动,程序就会对整个画面进行刷新,屏幕像素=320x200=64 000个像素。这就是反应慢的问题点,所以能否只刷新一小部分呢?
更新:sheet.c
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_refreshsub和sheet_refresh一样,唯一区别是使用vx0~ vy1指定刷新的范围,而我们只追加了一个if语句就实现了这个新功能。
&&运算符是把多个条件关系式连接起来的运算符。当用它连接的所有 条件都满足时,就执行{ }中的程序;只要有一个条件不满足,就不执行
“ ||”也是把多个条件关系式连接起来的运算符,不过由它连接 的各个条件,只要其中一个满足了,就执行{ }中的程序
条件“vx大于等于vx0且小于vx1”可以用数学式vx0 <= vx < vx1来表达, 但在C语言中不能这样写,我们只能写成 vx0 <= vx && vx < vx1。
更新:sheet.c
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;}
这段程序所做的是:
除了鼠标移动,还有文字刷新问题20个文字,8x16=2560个像素,但每次都要刷新整个屏幕,所以这也需要修改
更新:sheet.c
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就可以不考虑图层在画面中的位置了。
更新:sheet.c
void sheet_updown(struct SHTCTL *ctl, struct SHEET *sht, int height){ ....代码自行脑补 sheet_refreshsub(ctl, sht->vx0, sht->vy0, sht->vx0 sht->bxsize, sht->vy0 sht->bysize);}
更新bootpack.c
void HariMain(void){ (中略) sprintf(s, "(=, =)", 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, "(=, =)", 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); } } } }}
改写了sheet_refresh,变更点共有4个。只有每次要往 buf_back中写入信息时,才进行sheet_refresh
提高叠加处理速度2鼠标和文字相交部分也是可以进行优化
sheet.c文件中void sheet_refreshsub()即便只刷新图层的一部分,也要对所有图层的全部 像素执行if语句。而对于刷新范围以外的部分,就算执行if判断语句,最后也不会进行刷新,所以这纯粹就是一种浪费。既然如此,我们最初就应该把for语句的范围限定在刷新范围之内
更新:sheet.c
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; 计算vx0的坐标相当于bx中的哪个位置,然后把它作为bx0。其他的坐标处理方法也一样。
测试:鼠标速度确实提高了
鼠标这里涉及到图层以及刷新,优化方法。需要慢慢细品

洗洗睡了!

Copyright © 2024 有趣生活 All Rights Reserve吉ICP备19000289号-5 TXT地图HTML地图XML地图