VGA Fonts: Difference between revisions
Jump to navigation
Jump to search
[unchecked revision] | [unchecked revision] |
Content deleted Content added
Fixing some bugs and misinformed errors, general clean up |
No edit summary |
||
Line 96: | Line 96: | ||
Hint: use the same code as above, but swap source and destination for "rep movsd". |
Hint: use the same code as above, but swap source and destination for "rep movsd". |
||
== Displaying a character == |
|||
Okay, we have a font bitmap, but how to use it? Here's a simple example: |
|||
<source lang="c"> |
|||
//this is the bitmap font you've loaded |
|||
unsigned char *font; |
|||
void drawchar(unsigned char c, int x, int y, int fgcolor, int bgcolor) |
|||
{ |
|||
int cx,cy; |
|||
int mask[8]={1,2,4,8,16,32,64,128}; |
|||
unsigned char *gylph=font+(int)c*8*16; |
|||
for(cy=0;cy<16;cy++){ |
|||
for(cx=0;cx<8;cx++){ |
|||
putpixel(glyph[cy]&mask[cx]?fgcolor:bgcolor,x+cx,y+cy); |
|||
} |
|||
} |
|||
} |
|||
</source> |
|||
It's not so complicated. You supply the function with arguments: |
|||
<pre> |
|||
c - the character's ASCII code you want to display |
|||
x,y - where to draw |
|||
fgcolor - foreground color |
|||
bgcolor - background color |
|||
</pre> |
|||
It could be handly to draw the background as well, because it erase the area under the glyph. But sometimes it could be annoying, so here's a slightly modificated version that's using transparent background: |
|||
<source lang="c"> |
|||
//this is the bitmap font you've loaded |
|||
unsigned char *font; |
|||
void drawchar_transparent(unsigned char c, int x, int y, int fgcolor) |
|||
{ |
|||
int cx,cy; |
|||
int mask[8]={1,2,4,8,16,32,64,128}; |
|||
unsigned char *gylph=font+(int)c*8*16; |
|||
for(cy=0;cy<16;cy++){ |
|||
for(cx=0;cx<8;cx++){ |
|||
if(glyph[cy]&mask[cx]) putpixel(fgcolor,x+cx,y+cy); |
|||
} |
|||
} |
|||
} |
|||
</source> |
|||
As you can see, we only have one fgcolor this time. The putpixel call has a condition, only invoked if the bit in the bitmap is set. If it's clear, nothing will happen, thus makes the background transparent. |
|||
== See Also == |
== See Also == |
||
* [[VGA Hardware]] |
* [[VGA Hardware]] |