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

来源:互联网 发布:淘宝ab单危险 编辑:程序博客网 时间:2024/05/29 05:01

http://blog.csdn.net/ltbylc/article/details/8309257


1. 显示到屏幕上的图形界面信息要分层,比如最顶层是鼠标,中间是应用程序,最低是桌面。

2. 移动鼠标和窗口应用可以看为是移动图层。

3. 屏幕显示的原理是向显存内写信息,显存的地址在0x000a0000这个已经在前面见过了。

[cpp] view plaincopy
  1. /*一个图层结构体*/  
  2. struct SHEET  
  3. {  
  4.     unsigned char* buf;/*图层内容地址*/  
  5.     int bxsize,bysize;/*图层大小*/  
  6.     int vx0,vy0;/*图层在屏幕上位置*/  
  7.     int col_inv;/*色号(调色板)*/  
  8.     int height;/*图层高度*/  
  9.     int flag;/*设定信息*/  
  10. };  
  11. /*管理多个图层结构体*/  
  12. #define MAX_SHEETS 256  
  13. struct SHTCTL  
  14. {  
  15.     unsigned char *vram;/*VRAM地址*/  
  16.     int xsize,ysize;/*屏幕宽高*/  
  17.     int top;/*最上图层高度*/  
  18.     struct SHEET * sheets[MAX_SHEETS];/*下面的图层信息混乱,所以按照高度进行排序*/  
  19.     struct SHEET sheets0[MAX_SHEETS];/*保存所有图层信息*/  
  20. };  

4. 内存空间的分配应使用前面学过的知识,不能使用static分配,初始化图层管理节点

[cpp] view plaincopy
  1. #define SHEET_USE       1  
  2.   
  3. struct SHTCTL *shtctl_init(struct MEMMAN *memman, unsigned char *vram, int xsize, int ysize)  
  4. {  
  5.     struct SHTCTL *ctl;  
  6.     int i;  
  7.     ctl = (struct SHTCTL *) memman_alloc_4k(memman, sizeof (struct SHTCTL));  
  8.     if (ctl == 0) {  
  9.         goto err;  
  10.     }  
  11.     ctl->vram = vram;  
  12.     ctl->xsize = xsize;  
  13.     ctl->ysize = ysize;  
  14.     ctl->top = -1;   
  15.     for (i = 0; i < MAX_SHEETS; i++) {  
  16.         ctl->sheets0[i].flags = 0;  
  17.     }  
  18. err:  
  19.     return ctl;  
  20. }  

5. 生成未使用的图层

[cpp] view plaincopy
  1. struct SHEET *sheet_alloc(struct SHTCTL *ctl)  
  2. {  
  3.     struct SHEET *sht;  
  4.     int i;  
  5.     for (i = 0; i < MAX_SHEETS; i++) {  
  6.         if (ctl->sheets0[i].flags == 0) {  
  7.             sht = &ctl->sheets0[i];  
  8.             sht->flags = SHEET_USE;   
  9.             sht->height = -1;   
  10.             return sht;  
  11.         }  
  12.     }  
  13.     return 0;     
  14. }  

6. 设置图层

[cpp] view plaincopy
  1. void sheet_setbuf(struct SHEET *sht, unsigned char *buf, int xsize, int ysize, int col_inv)  
  2. {  
  3.     sht->buf = buf;  
  4.     sht->bxsize = xsize;  
  5.     sht->bysize = ysize;  
  6.     sht->col_inv = col_inv;  
  7.     return;  
  8. }  

7. 设定某个图层的高度

[cpp] view plaincopy
  1. void sheet_updown(struct SHTCTL *ctl, struct SHEET *sht, int height)  
  2. {  
  3.     int h, old = sht->height;   
  4.   
  5.     if (height > ctl->top + 1) {  
  6.         height = ctl->top + 1;  
  7.     }  
  8.     if (height < -1) {  
  9.         height = -1;  
  10.     }  
  11.     sht->height = height;  
  12.   
  13.     if (old > height) {    
  14.         if (height >= 0) {  
  15.             for (h = old; h > height; h--) {  
  16.                 ctl->sheets[h] = ctl->sheets[h - 1];  
  17.                 ctl->sheets[h]->height = h;  
  18.             }  
  19.             ctl->sheets[height] = sht;  
  20.         } else {  
  21.             if (ctl->top > old) {  
  22.                 for (h = old; h < ctl->top; h++) {  
  23.                     ctl->sheets[h] = ctl->sheets[h + 1];  
  24.                     ctl->sheets[h]->height = h;  
  25.                 }  
  26.             }  
  27.             ctl->top--;  
  28.         }  
  29.         sheet_refresh(ctl);   
  30.     } else if (old < height) {  
  31.         if (old >= 0) {  
  32.               
  33.             for (h = old; h < height; h++) {  
  34.                 ctl->sheets[h] = ctl->sheets[h + 1];  
  35.                 ctl->sheets[h]->height = h;  
  36.             }  
  37.             ctl->sheets[height] = sht;  
  38.         } else {      
  39.             for (h = ctl->top; h >= height; h--) {  
  40.                 ctl->sheets[h + 1] = ctl->sheets[h];  
  41.                 ctl->sheets[h + 1]->height = h + 1;  
  42.             }  
  43.             ctl->sheets[height] = sht;  
  44.             ctl->top++;   
  45.         }  
  46.         sheet_refresh(ctl);   
  47.     }  
  48.     return;  
  49. }  

8. 刷新各图层,写入显存,这个需要优化

[cpp] view plaincopy
  1. void sheet_refresh(struct SHTCTL *ctl)  
  2. {  
  3.     int h, bx, by, vx, vy;  
  4.     unsigned char *buf, c, *vram = ctl->vram;  
  5.     struct SHEET *sht;  
  6.     for (h = 0; h <= ctl->top; h++) {  
  7.         sht = ctl->sheets[h];  
  8.         buf = sht->buf;  
  9.         for (by = 0; by < sht->bysize; by++) {  
  10.             vy = sht->vy0 + by;  
  11.             for (bx = 0; bx < sht->bxsize; bx++) {  
  12.                 vx = sht->vx0 + bx;  
  13.                 c = buf[by * sht->bxsize + bx];  
  14.                 if (c != sht->col_inv) {  
  15.                     vram[vy * ctl->xsize + vx] = c;  
  16.                 }  
  17.             }  
  18.         }  
  19.     }  
  20.     return;  
  21. }  

9. 移动图层

[cpp] view plaincopy
  1. void sheet_slide(struct SHTCTL *ctl, struct SHEET *sht, int vx0, int vy0)  
  2. {  
  3.     sht->vx0 = vx0;  
  4.     sht->vy0 = vy0;  
  5.     if (sht->height >= 0) {   
  6.         sheet_refresh(ctl);   
  7.     }  
  8.     return;