View Full Version : printing text

02-27-2004, 10:17 PM
When in full screen 320*200 8bit mode. I have double buffering, I want to print characters not directly to the screen but to my buffer then I can copy my buffer to the screen. How can I print characters to my buffer?

Thanx in advance!

02-29-2004, 07:47 AM
Option 1 - using the ROM BIOS
You must create your own font or get the font directly from the ROM BIOS. The problem with the ROM font is that it will be the same hideous size as printf.

After you create your font, then you simply blit the font to the surface or the back buffer like you would any other graphic.

Here are a few ideas on how to create some fonts - I've not done this in awhile so gimme some leeway.

1. First decide on how many pixels each font cell will be.
2. Create either a linear array with the data in it, or create a 2D semi-linear array to hold the data. I'll explain.
3. Blit the data to screen.

Fonts as 2D semi-linear arrays.
Ok we access 320x200 in the form y*320+x. Well you can also access arrays in this form, after all the screen is simply one big array. So let's say you want to be able to have 100 characters in your font. Let's say that each font cell is 20x20 - one big font, but still.

The array would look like the following:

BYTE fontarray[100][400];

Now that's a fairly large array. The 400 part is the 20x20 data. To access it correctly and display it simply do spriteoffsety*20+spriteoffsetx. The first array element corresponds to what character you are on.

Fonts as linear arrays
The second way to do this is to eliminate the first array element and make this one huge linear array since the first element is not being used. Note that you still must do a multiply to access the data. In the first example we are making the compiler do the multiply and in this one, we are doing it.

BYTE fontarray[40000];

Again its still 40000 bytes. To access this, take your font character number (1-100) and multiply it by 400. That is the start of each character. Then for the actual data you simply use the start you just calculate as a base and then add (spriteoffsety*20+spriteoffsetx). Ways to speed this up would be to ensure that your arrays are all powers of 2. Multiplies could then be accomplished by bitshifts instead of MULs.

Using bitwise operations with font data
But there is yet another way to do this which requires that you know a little about bitwise operation. Since all a font really is, is a bunch of 1's and 0's representing which pixels are on and which are off, you could easily represent them as 32-bit values. One 32 bit value would cover 32 pixels. Simply test each bit in the value to see if its a 1 or a 0. If 1, write it to the screen, if 0 don't - if you want transparent cells - if not, write your cell color when you get 0's from the data. Since we know the width based on the 32-bit value we only need the height of each cell to be able to access the array.

The array then becomes

BYTE fontarray[HeightOfCell*NumberOfCharacters];

To access this array, simply select the character number you wish to print out and multiply it by the height of all the cells. This will get you to the start of that particular character. Then simply bit test each value for height iterations.

So your font data will look like this:

Let's say A is character 0

0x7C00000 -> first row of letter A
0x8FFA1250 -> second row of letter A
0x6500FFFF -> first row of letter B

and so on - these values are not correct.

To find out the correct value do this: Plot out your bitmaps on graph paper.
The final values for your letter rows can be derived from the bitmap.
I will show you an 8-bit font with each letter being 8 pixels high at a scale factor of 1.


The values are this for the top row:


or the decimal number 24 and hex number 18h.
So your first row of data will be 0x18.

As you can see, with this method you can store large fonts on disk and in memory and access them rather quickly as well. If you don't want to do the test and set method inside of your game loop, then draw each character and turn them into a sprite - then at run time- simply blit them to the screen.

Fonts as sprite's
The last way to make fonts is to simply make a sprite sheet similar to your other sprite sheets for your game. Then all you do is use your blitting code to draw the font. The only catch is you must know which bitmap to blit based on what you want to print.
This can be easy to do if you make your font bitmap IDs the same as they appear in a standard ASCII code listing. Then derive the ASCII value of your letter and blit FontImage[AsciiLetter] to the screen.

If you need help, let me know.
I'm sure there are a million other ways to do this, but these are some I've experimented with. Blitting a font to the screen as sprite's is faster than printf() in graphics modes.

02-29-2004, 10:19 AM
How may I get font directly from bios rom?

Thanx in advance!

03-01-2004, 09:39 PM
You must create a pointer to the section of memory where the font data is. Look it up on google to find out where the system fonts are.

03-26-2004, 01:28 PM
hi i am running windows xp.when my compuer boots it does not boot with bios and it just has this windows sign with a bar thing at the bottom i am wondering how to boot the computer with bios instead of this windows stuff

03-27-2004, 08:45 AM

The BIOS cannot boot the computer.

The BIOS will attempt to load sector 0 from some device - floppy, hard drive, CD, etc.. The boot sector must be 512 bytes in length.
The sector is loaded at 07C00h. From there the operating system bootstrap will probably relocate itself into memory, jump up there and run it. The bootstrap will then load the operating system kernel and any other relevant code into memory. After that, the system will either load a command prompt interpreter like DOS or it might start a GUI shell like Windows which would then call the OS to load other programs into memory, etc., etc.

You would have to write your own operating system to do this or you would need DOS or Win9X to emulate DOS for you.

All XP has is command which is a very sparse DOS emulator.