Drawing In Protected Mode: Difference between revisions

[unchecked revision][unchecked revision]
Content deleted Content added
Durand (talk | contribs)
m Modified the direct link to the VBE GRUB patches at SMK
Line 119:
 
Pitch and pixel width are usually announced by VESA mode info. Once you know them, you can calculate the place where you plot your pixel as:
<source lang="c">unsigned char *pixel = vram + y*pitch + x*pixelwidth;</source>
 
unsigned char *pixel = vram + y*pitch + x*pixelwidth;
 
=== Color ===
Line 131 ⟶ 130:
 
Finally, in VESA modes, you usually have truecolor or hicolor, and in both of them, you have to give independent red, green and blue values for each pixel. modeinfo will (again) instruct you of how the RGB components are organized in the pixel bits. E.g. you will have <tt>xRRRRRGGGGGBBBBB</tt> for 15-bits mode, meaning that #ff0000 red is there <tt>0x7800</tt>, and #808080 grey is <tt>0x4210</tt> (pickup pencil, draw the bits and see by yourself)
<source lang="c">
 
/* only valid for 800x600x16M */
static void putpixel(unsigned char* screen, int x,int y, int color)
Line 149 ⟶ 148:
screen[where+2]=(color>>16)&255; // RED
}
</source>
 
 
=== Optimizations ===
 
It can be tempting from here to write fill_rect, draw_hline, draw_vline, etc. from calls to putpixel ... don't. Drawing a filled rectangle means you access successive pixels and then advance by "pitch - rect_width" to fill the next line. If you do a "for(y=100;y<200;y++) for(x=100;x<200;x++) putpixel (screen,x,y,RED);" loop, you'll recompute 'where' about 10,000 times. Even if the compiler has done good job to translate y*3200 into adds and shifts instead of multiplication, it's silly to run that so much time while you could do
<source lang="c">
 
static void fillrect(unsigned char *vram, unsigned char r, unsigned char g, unsigned char b, unsigned char w, unsigned
char h) {
Line 169 ⟶ 168:
}
}
</source>
 
That should be enough to get you started coding (or googling for) a decent video library.