If this page looks like garbage then you need one of these browsers...Please sign my programmer's page guestbook. |
Rendering text in Mode 13HBefore we wrap up on understanding graphics, we should consider understanding how to draw text characters on the graphics screen. Every graphics card has a method of drawring text onto the graphics screen, mainly this is accomplished by storing a bitmapped image of each of the ASCII characters in memory somewhere. These bitmaps have an exact image of the character and is used to render text onto any graphics screen. The BIOS itself is capable of rendering text onto the graphics screen using this collection of bitmapped images stored by the graphics card. We can take advantage of this and allow the BIOS to render our text for us by using the text functions of Turbo C's conio.h library and by setting a single global variable called directvideo declared by conio.h to zero or false. This forces the text functions of conio.h to use the BIOS to render text. By default, it will do direct video writes (directvideo == 1) but this only works in the text screen, in the graphics screen, you typically only get either garbage or nothing at all. Conio.h functions in actionBelow is a sample program that illustrates how the conio.h functions can be used to render text in screen 13H. These functions will work in any Borland compiler for DOS.
/* Rendering text in the Graphics mode using Conio.H functions */ #include <conio.h> int main(void) { directvideo = 0; /* Use BIOS to render text */ SetVideoMode(VGA256); /* Set screen to mode 13H */ cputs("Hello World, in 13H..."); getch(); SetVideoMode(TEXT_MODE) /* Back to text mode */ return 0; }
Notice that I did not include the function for SetVideoMode. By now I am assuming that you already understand how to change to the graphics mode using SetVideoMode and that you have already defined the function to include in this program. Our main focus now is rendering text. |
Do not use cprintf in the middle of the main game loop, or in the middle of any other time critical code. All the printf functions are extremely slow. |
Now that we know that just by setting 'directvideo' to zero or false, we can use the functions to render our text for us in any graphics mode, not just 13H. However, we have little control over how our text will appear. That is, we cannot tell the functions that we want to background of the text characters to be of a specific color, or that we want the background to be transparent and allow the background of the screen to show around the text. So how can we do these things with the text? Well, using the conio.h functions, we can't. We have to draw the text ourselves by accessing this set of bitmaps stored by the VGA card and write a couple or functions that copy the appropriate characters to the graphics screen. Creating our own text rendering functionsBefore we can render the text ourselves, we need to know where in memory the bitmap images of the characters are stored and the format that they are stored in. Once we have that information, we can then proceed to write these functions. The VGA card stores all of the characters in memory beginning at memory location F000:FA6E. Now let's see if we can understand the format of the characters... The characters are 8x8 bitmaps. Each character occupies only 8 bytes of memory for the vertical resolution. The horizontal resolution is stored in the 8 bits that make up each byte.
With this knowledge, we can write our own text rendering functions. All we need to write are two functions. These functions can be used over and over to render all of our text on the graphics screen in any location and using any color for both the text and the background around it or we can make the background totally transparent. The first function we will write will allow us to render a single character and the second function will allow us to render a string of characters. Below is a listing of both functions. /* Points to all characters in ROM character set */ unsigned char far *charSet=(unsigned char far *)0xF000FA6EL; void Gputch(int x, int y, int c, int fc, int bc) { char far *workChar; /* Points to character rendered */ unsigned char bit; /* Bit mask */ int xOff, yOff; /* Offset from x, y coordinate */ /* Get character to be rendered */ workChar = &charSet[(c & 255) * CharHeight]; for (yOff=0; yOff<CharHeight; yOff++) { if (y + yOff < ScreenHeight) { bit = 0x80; for (xOff=0; xOff<CharWidth; xOff++) { if (x + xOff < ScreenWidth) { if (*workChar & bit) SetPixel(x + xOff, y + yOff, fc); else if (bc >= 0) SetPixel(x + xOff, y + yOff, bc); } bit >>= 1; } } workChar++; } } void Gputs(int x, int y, char *string, int fc, int bc) { int i; /* Call Gputch for each character of the string */ for (i=0; string[i]; i++) Gputch(x + (i << 3), y, string[i], fc, bc); }
Both functions take 5 parameters. The first function, Gputch, works just like the putch function from conio.h. The first two arguments are the screen coordinates, from (0,0) to (319,199) of the upper left corner of the character. The third coordinate is the actual character you want to render. The fourth and fifth parameter is the foreground and background color of the character respectively. These two parameters can be any of the 256 colors of the VGA card (0-255). If the background parameter is less than 0, the background is transparent. The Gputs function uses the same parameter list as the Gputch, except that the third parameter is a string, instead of a character. Send your questions, comments, or ideas here. This page hosted by
Get your own Free Home Page |