您的位置:首页 > 其它

《30天自制操作系统》读书笔记Day10

2013-08-06 00:40 309 查看
1.叠加处理

为了让以后操作系统各个窗口正常显示,这里采用叠加显示的概念。最终显示的画面由不同的图层叠加显示。

这里图层的信息使用结构体表示:

#define MAX_SHEETS		256
//SHEET结构体储存图层相关信息
struct SHEET {
unsigned char *buf;	//记录图层上所描画内容的地址
int bxsize, bysize, vx0, vy0, col_inv, height, flags;
/*
* bxsize*bysize图层大小
* vx0,vy0图层位置坐标
* col_inv透明色色号
* height图层高度
* flags图层的标记信息
*/
};
//SHTCTL结构体管理图层
struct SHTCTL {
unsigned char *vram;	//VRAM地址
int xsize, ysize, top;	//xsize,ysize画面大小,top最上面图层的高度
struct SHEET *sheets[MAX_SHEETS];	//按高度排序的图层信息
struct SHEET sheets0[MAX_SHEETS];	//图层信息
};


接下来创建sheet.c文件添加相关的函数:

/*
* sheet.c
* 叠加显示相关函数
*/

#include "bootpack.h"

#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;
ctl->xsize = xsize;
ctl->ysize = ysize;
ctl->top = -1; /* 一个SHEEL都没有 */
for (i = 0; i < MAX_SHEETS; i++) {
ctl->sheets0[i].flags = 0; /* 标记为未使用 */
}
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;	/* 所有的图层都处于正在使用的状态,即分配失败 */
}

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++; /* 图层总高度加一 */
}
sheet_refresh(ctl); /* 重新绘制 */
}
return;
}

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;
}

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;
}

void sheet_free(struct SHTCTL *ctl, struct SHEET *sht)
/* 删除图层 */
{
if (sht->height >= 0) {
sheet_updown(ctl, sht, -1); /* 先把图层隐藏 */
}
sht->flags = 0; /* 设定图层未使用 */
return;
}


最后在bootpack.c中修改HariMain函数即可:

/*
*bootpack.c
*其他处理
*/
#include "bootpack.h"
#include <stdio.h>

void HariMain(void)
{

struct BOOTINFO *binfo = (struct BOOTINFO *) ADR_BOOTINFO;
char s[40], mcursor[256], keybuf[32], mousebuf[128];
int mx, my, i;
unsigned int memtotal;
struct MOUSE_DEC mdec;
struct MEMMAN *memman = (struct MEMMAN *) MEMMAN_ADDR;
struct SHTCTL *shtctl;
struct SHEET *sht_back, *sht_mouse;
unsigned char *buf_back, buf_mouse[256];

init_gdtidt();
init_pic();
io_sti();
fifo8_init(&keyfifo, 32 , keybuf);
fifo8_init(&mousefifo, 128 , mousebuf);
io_out8(PIC0_IMR, 0xf9); /* PIC1以外全部禁止(11111001) */
io_out8(PIC1_IMR, 0xef);

init_keyboard();
enable_mouse(&mdec);
memtotal = memtest(0x00400000, 0xbfffffff);
memman_init(memman);
memman_free(memman, 0x00001000, 0x0009e000); /* 0x00001000 - 0x0009efff */
memman_free(memman, 0x00400000, memtotal - 0x00400000);

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) {

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 */
}
}
}
}
}


make run 可以正常运行。

2.叠加处理加速

前面鼠标移动会刷新所有的像素,因此会很慢,而且画面可能会闪烁,因此需要对叠加处理进行优化。

将变化的部分刷新即可,而不是刷新全部。

在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_slide中改为调用sheet_refreshsub:

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;
}


接着修改sheet_refresh:

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;
}


把sheet_updown中的:

sheet_refresh(ctl);


修改为

sheet_refreshsub(ctl, sht->vx0, sht->vy0, sht->vx0 + sht->bxsize, sht->vy0 + sht->bysize);


最后在HariMain中继续把sheet_refresh改为sheet_refreshsub并添加几处sheet_refreshsub即可

不过在sheet_refreshsub中,即使不写入像素也有较多的判断语句,需要优化:

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; }
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;
c = buf[by * sht->bxsize + bx];
if (c != sht->col_inv) {
vram[vy * ctl->xsize + vx] = c;
}
}
}
}
return;
}


这样就完成了优化工作。

3.鼠标显示问题

运行系统发现鼠标在向右和向下移动不能移出界面,与Windows系统不相同,不怎么适应,因此修改一下,修改HariMain函数中:

if(ma > 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;


但是发现鼠标在右侧超出界面后左侧的显示会出现bug,这是sheet_regreshsub的问题:

...
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++) {
...
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: