11、制作窗口

显示窗口和绘制鼠标的原理一样
bootpack.c节选

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

bootpack.c中的主函数节选

unsigned int memtotal, count = 0;
struct SHEET *sht_back, *sht_mouse, *sht_win;
unsigned char *buf_back, buf_mouse[256], *buf_win;

sht_win   = sheet_alloc(shtctl);
buf_win   = (unsigned char *) memman_alloc_4k(memman, 160 * 52);
sheet_setbuf(sht_win, buf_win, 160, 52, -1); /* 没有透明色 */
make_window8(buf_win, 160, 52, "counter");

sheet_slide(sht_sht_win, 80, 72);

sheet_updown(sht_back,  0);
sheet_updown(sht_win,   1);
sheet_updown(sht_mouse, 2);

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

这里写图片描述

取消闪烁
问题:显示的内容在闪烁
原因:刷新的时候总是先刷新背景,再刷新窗口
解决:如果只是窗口变了,背景不需要刷新。如果上面有鼠标,但鼠标的图层没有变化,也应该刷新。所以刷新函数应该增加参数h0,h1,表示刷新高度范围在h0~h1的图层,然后修改与刷新相关的函数。

问题:背景闪烁的问题解决了,但鼠标放在上面时,鼠标开始闪烁了。
原因:是因为一会儿描绘,一会儿消除鼠标造成的。
解决:在刷新窗口时避开鼠标所在的地方对VRAM进行写入处理。为此,开辟一块内存,大小和VRAM一样,称之为map,存放那个点是哪个图层的像素,根据map中的信息决定是否要刷新。

bootpack.h节选

struct SHTCTL                 //图层管理
{
    unsigned char *vram;  //VRAM地址
    unsigned char *map;   //图层的地图,用来表示画面上的点是哪个图层的像素
    int xsize, ysize;     //画面大小
    int 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->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;   //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;
}

//类似于sheet_refreshsub,只是用色号代替了图层号
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;

    /* 如果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 = 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;

            /* 使用vx0~vy1, 对bx0~by1进行倒推 限制刷新范围:*/
        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;
}

//从下到上重绘所有图层,最后最上面的内容就留在了画面上
//提高叠加处理速度:只刷新移动前后的部分,而不是整个屏幕,避免每次都对全部VRAM重新填像素
//因为会按照map对VRAM写入,所以不必要从下面一直刷新到最上面一层,所以指定h0, h1.
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, c, *vram = ctl->vram, sid;
    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 = h0; h <= h1; h++) 
    {
        sht = ctl->sheets[h];
        buf = sht->buf;
        sid = sht - ctl->sheets0;  //颜色号码

        /* 使用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; }

        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 (map[vy * ctl->xsize + vx] == sid)  //如果map中存储的是当前层中的像素
                    vram[vy * ctl->xsize + vx] = c;  //重新填充到VRAM中
            }
        }
    }
    return;
}

//接着修改调用sheet_refreshsub的函数


//不直接指定画面中的坐标,而是以缓冲区的坐标来表示
//需要刷新的图层只有1void 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_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;
            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 /* 隐藏 */
        {   
            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_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) /* 比以前高 */
    {   
        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_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;
}

//移动图层
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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值