Homemade operating System (10) Image Overlay Processing

Source: Internet
Author: User
Tags transparent color

2016.07.12

Reference books: "30 days homemade operating System", "self-write operating system"

qq:992591601 Welcome to Exchange

Image overlay Processing principle is very simple, is to give the image layering, from the lower to the top of the painting, you can achieve the effect of superposition. For example, a screen background + a window + mouse condition.

For example, the following scenario:

There are three Windows programs on the computer desktop, a, B, C. b is above A and C is above B. To do this, you just need to draw a (cover the part of the desktop), Draw B (cover a and desktop parts), and then draw C (cover the B and desktop parts). Then refresh the screen at a certain time.

The concept of a layer is introduced for this purpose. In fact, the essence of the layer is what I said above that passage.

struct SHEET {    char *buf;     int bxsize, Bysize, vx0, Vy0, COL_INV, height, flags;};

The BUF is used to record the address of the content depicted on the layer.

The overall size of the layer: Bxsize, bysize.

The coordinates of the position of the layer on the screen: vx0, Vy0.

COL_INV is a transparent color number.

Height is the level of the layer.

Flags are used to store various settings information about the layer. (Usage status)

Next, for programmers, to achieve the image overlay effect, is actually the logic of the management of the dry layer (management: Add and delete changes) (Picture screen Image: Draw all layers in the order of layers) ~

Data structures for layer management:

struct shtctl {    char *VRAM;     int xsize, Ysize, top;     struct SHEET *sheets[max_sheets];     struct SHEET sheets0[max_sheets];};

#include"c_head.h"/** *author: Nameless *date:2016.07.12 *description:sheet Management **/#defineSheet_use 1/** Initialize layer data structure Shtctl*/structShtctl *shtctl_init (structMemman *memman, unsignedChar*vram,intXsize,intysize) {    structShtctl *ctl; inti; CTL= (structShtctl *) memman_alloc_4k (Memman,sizeof(structshtctl)); if(ctl = =0)     {        Gotoerr; } CTL->vram =VRAM; CTL->xsize =xsize; CTL->ysize =ysize; CTL->top =-1;/*no sheet .*/     for(i =0; i < max_sheets; i++) {ctl->sheets0[i].flags =0;/*mark as not used*/}err:returnctl;}/** Get a layer that is not used*/structSHEET *sheet_alloc (structShtctl *ctl) {    structSHEET *Sht; inti;  for(i =0; i < max_sheets; i++)     {        if(Ctl->sheets0[i].flags = =0) {Sht= &ctl->Sheets0[i]; Sht->flags = Sheet_use;/*mark as in use*/Sht->height =-1;/*Hidden*/            returnSht; }    }    return 0;/*all sheet are in positive use condition*/}/** Set layer size and transparent color*/voidSheet_setbuf (structSHEET *sht, unsignedChar*buf,intXsize,intYsize,intCOL_INV) {Sht->buf =buf; Sht->bxsize =xsize; Sht->bysize =ysize; Sht-&GT;COL_INV =COL_INV; return;}/** Improved refresh function*/voidSheet_refreshsub (structShtctl *ctl,intVx0,intVy0,intVX1,intvy1) {    inth, BX, by, VX, VY, bx0, By0, Bx1, by1; unsignedChar*buf, c, *vram = ctl->VRAM; structSHEET *Sht;  for(h =0; H <= ctl->top; h++) {Sht= ctl->Sheets[h]; BUF= sht->buf; /*use Vx0~vy1 to reverse the 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;}/** Set Floor height*/voidSheet_updown (structShtctl *ctl,structSHEET *sht,intheight) {    intH, old = sht->height;/*height information before storage settings*/    /*correct If the specified height is too high or too low*/    if(Height > ctl->top +1) {Height= Ctl->top +1; }    if(Height <-1) {Height= -1; } sht->height = height;/*Set Height*/    /*The following are mainly for sheets[] rearranging*/    if(Old >height) {        if(Height >=0)        {                 for(h = old; h > height; h--) {ctl-&GT;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-&GT;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); } Else if(Old <height) {            if(Old >=0)         {             for(h = old; h < height; h++) {ctl-&GT;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); }    return;}/** Paint all layers from top to bottom*/voidSheet_refresh (structShtctl *ctl,structSHEET *sht,intBx0,intBy0,intBX1,intby1) {    if(Sht->height >=0) {sheet_refreshsub (ctl, Sht->vx0 + bx0, Sht->vy0 + by0, sht->vx0 + bx1, Sht->vy0 +by1); }    return;}voidSheet_slide (structShtctl *ctl,structSHEET *sht,intVx0,intvy0) {    intold_vx0 = sht->vx0, old_vy0 = sht->vy0; Sht->vx0 =vx0; Sht->vy0 =vy0; if(Sht->height >=0)//Refresh the screen according to the new layer information if it is being displayed{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;}/** Release Layer*/voidSheet_free (structShtctl *ctl,structSHEET *Sht) {    if(Sht->height >=0) {Sheet_updown (CTL, Sht,-1); } sht->flags =0; return;}

The final:

Homemade operating System (10) Image overlay processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.