VGA Fonts: Difference between revisions

Jump to navigation Jump to search
no edit summary
[unchecked revision][unchecked revision]
No edit summary
No edit summary
Line 32:
Most modular way. You can use different fonts if you like. Downside you'll need a working filesystem implementation.
=== Get the copy stored in the VGA BIOS ===
It's a standard BIOS call (no need to check it's persistence). If you're still in real mode, it's quite easy to use.
<source lang="asm">
;in: es:di=4k buffer
Line 49:
mov cx, 256*16/4
rep movsd
loop @b
pop ds
</source>
=== Get from VGA RAM directly ===
Maybe you're already in protected mode, so cannot access BIOS functions. In this case you can still get the bitmap by programming VGA registers. Be careful that the VGA always reserves space for 8x32 fonts so you will need to either copy 8k of data or trim off the bottom 16 bytes of each character during the copy:
<source lang="asm">
;in: edi=8k4k buffer
;out: buffer filled with font
;clear even/odd mode
Line 72 ⟶ 73:
;copy charmap
mov esi, 0A0000h
mov cxecx, 256*32/4
;copy 16 bytes to bitmap
rep @@: movsd
movsd
movsd
movsd
;skip another 16 bytes
add esi, 16
loop @b
;restore VGA state to normal operation
mov ax, 0302h
Line 85 ⟶ 93:
out dx, ax
</source>
It worth mentioning that it has to be done '''before''' you switch to aVBE graphics mode, because the VGA fontregisters isare wipedusually fromnot memoryaccessible duringafterwards. transitionThis andmeans especiallyyou inwon't thebe caseable ofto VBEmap wherethe VGA registerscard's arefont usuallymemory notto accessiblescreen afterwardsmemory, and you will read only garbage.
 
== Set VGA fonts ==
If you're still in text mode and want the VGA card to draw different glyphs, or you want to change back from a graphics mode, you can set the VGA font. It's worthless in graphics mode (because characters are displayed by your code there, not by the card), I only wrote this section for completeness. Modifying the font bitmaps in VGA RAM isn't hard if you read carefully what's written so far. I'll left it to you as a homework.
 
=== Set fonts via BIOS ===
Line 97 ⟶ 105:
 
== Displaying a character ==
And finally we came to the point where we can display a character. I'll assume you have a putpixel procedure ready.
Okay, we have a font bitmap, but how to use it? Here's a simple example:
We have to draw 8x16 pixels, one for every bit in the bitmap.
<source lang="c">
//this is the bitmap font you've loaded
Line 110 ⟶ 119:
for(cy=0;cy<16;cy++){
for(cx=0;cx<8;cx++){
putpixel(glyph[cy]&mask[cx]?fgcolor:bgcolor,x+cx,y+cy-12);
}
}
}
</source>
The arguments are straightforward. You may wonder why to subtract 12 from y. It's for the baseline: you specify y coordinate as the bottom of the character, not counting the "piggy tail" in a glyph that goes down (like in "p","g","q" etc.). I other words it's the most bottom row of letter "A" that has a bit set.
It's not so complicated. You supply the function with arguments:
 
<pre>
ItAlthough couldit's bemostly handlyuseful to drawerase the backgroundscreen asunder wellthe glyph, becausein itsome erasecases theit areacould underbe thebad glyph(eg.: Butwriting sometimeson ita couldshiny begradiented annoying,button). soSo here's a slightly modificated version, that's usinguses a transparent background:.
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
Line 135 ⟶ 139:
for(cy=0;cy<16;cy++){
for(cx=0;cx<8;cx++){
if(glyph[cy]&mask[cx]) putpixel(fgcolor,x+cx,y+cy-12);
}
}
}
</source>
As you can see, we have only haveforeground one fgcolorcolor this time., Theand the putpixel call has a condition,: only invoked if the according bit in the bitmap is set. If it's clear, nothing will happen, thus makes the background transparent.
== See Also ==
* [[VGA Hardware]]
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu