You are on page 1of 7

Ham Tutorial :: C++ Font Example

Introduction Day 1 GBA Hardware Day 2 "Hello, World!" Day 3 Input Day 4 Backgrounds Bitmapped Modes Day 5 Sprites Day 6 Backgrounds Tile Modes Day 7 Project 1 Tetris Quiz - Week 1 Day 8 Sprites #2 Animation Day 9 Maps Day 10 Sprites #3 Animation #2 Day 11 Backgrounds Rotation Day 12 Sprites #4 Mosaic Version History

HAM Tutorial :: C++ Font Example Many people who looked at the Fonts Howto told me that they still didn't know how to actually use the fonts in a game or demo so I decided to show you one way to do it. For this project I decided to use Peter Schraut's Background Text class. To use it, you will have to use C++ (or convert his code to C). What You'll Need HAM 2.71 (or newer) Font2Gif 1.2 (or newer) Photoshop, GIMP, etc. Visual HAM (not necessary, but very helpful) Step 1 : Find A Font You Like I searched a couple of my favorite free font web sites 1001FreeFonts.com and Nick's Fonts to find one that I liked. After a little bit of testing I decided to use AnimeAce by BlamBot. Download here. Step 2: Create A GIF Of Your Font Hopefully you've already extracted the TTF file and copied it to Control Panel -> Fonts. If not, do that now. Open font2gif and do the following: Select AnimeAce next to Font name Leave Regular selected for Font Style Select User Width for Width Type 8 px for Size and Width Uncheck AntiAlias Choose Align to Center Change Font Color to yellow (or preferred color) Change Bg Color to black (or preferred alpha blended color) Now, you can type in your characters or if you want to make it really easy on yourself, choose Auto Fill -> Digits, CAPS, All font chars, etc. For this example I chose to auto fill capitals and then added 0-9 to the beginning and , (comma) ' (single quote) and MOST IMORTANTLY a space at the end. If you don't add a space, the Background Text class won't be able to clear the screen.

http://www.aaronrogers.com/ham/Projects/Font_Example/font_example.php (1 of 7) [3/23/2003 1:04:55 PM]

Ham Tutorial :: C++ Font Example

Poll Discussions Graphics FAQ GFX2GBA Readme Translations Downloads Games Projects Credits Links Support

Next click Convert, choose Save as solid GIF-file, click Save. Browse to your gfx directory and save the file. I named it AnimeAce. Click Close to exit font2gif. Step 3 : Convert GIF to BMP Hopefully you should all know how to do this by now. For this example we are going to use 16 color palettes instead of the normal 256 color palettes. In this case, our GIF only has two colors so it shouldn't be a problem. If you make your own fancy font, though, be careful. Photoshop: Click Image -> Mode and make sure Indexed Color is checked Click File -> Save As and choose Bitmap and click Save Make sure the File Format is Windows and the Depth is 8 bit Click OK Step 4 : Create A Background Now you'll need to create a 256x256 pixel image to use as the background (remember that only the top-left 240x160 pixels of it will be shown on the screen, though). You'll want to keep it simple as it can only be 16 colors. For now, you can just download and use the background I created. Step 5 : Use gfx2gba To Create Source Files From Images You'll need to use the following commands to convert your images: gfx2gba -b0 -c16 -t8 -fsrc -M -pAnimeAce.pal AnimeAce.bmp gfx2gba -b1 -c16 -t8 -fsrc -M -pbackground.pal background.bmp -b is used to select the bank for the palette. It can be 0 through 15. The fonts background will use bank 0 and the image background will use bank 1. -c is for the color depth. In this case, it's 16.

http://www.aaronrogers.com/ham/Projects/Font_Example/font_example.php (2 of 7) [3/23/2003 1:04:55 PM]

Ham Tutorial :: C++ Font Example

-M is used because we don't want to optimize the map, otherwise our graphics won't look right. Well, let's go ahead and take a look at the code. // The Main HAM Library #include "mygba.h" #include "bgtext.h" // Graphics Includes // Background #include "gfx/background.map.c" #include "gfx/background.pal.c" #include "gfx/background.raw.c" // Sprites #include "gfx/AnimeAce.pal.c" #include "gfx/AnimeAce.raw.c" // Global Variables CBgText BgText; // Instance of BgText class bool newframe = true; // New frame or not u8 line = 1; // Which line of text is being typed // Character index for use with BgText class const TCharIndex Chars[] = { {'0', 0}, {'1', 1}, {'2', 2}, {'3', 3}, {'4', 4}, {'5', 5}, {'6', 6}, {'7', 7}, {'8', 8}, {'9', 9}, {'A',10}, {'B',11}, {'C',12}, {'D',13}, {'E',14}, {'F',15}, {'G',16}, {'H',17}, {'I',18}, {'J',19}, {'K',20}, {'L',21}, {'M',22}, {'N',23}, {'O',24}, {'P',25}, {'Q',26}, {'R',27}, {'S',28}, {'T',29}, {'U',30}, {'V',31}, {'W',32}, {'X',33}, {'Y',34}, {'Z',35}, {',',36}, {'\'',37}, {' ',38} }; // Set BgText class information const TBgTextInfo BgTextInfo = { 0, // Background we want to use for text output 8, // Tile Width 8, // Tile Height (u8*)AnimeAce_Tiles, // Pointer to tile data sizeof(AnimeAce_Tiles), // Tile data size (u16*)AnimeAce_Palette, // Pointer to palette 0, // Palette Index, -1 = 256 color palette 16, // PaletteSize sizeof(Chars)/2, // Number of chars (TCharIndex*)Chars // Pointer to CharIndex Information }; // Function Prototypes void vblFunc(); // VBL function // Main function int main() {
http://www.aaronrogers.com/ham/Projects/Font_Example/font_example.php (3 of 7) [3/23/2003 1:04:55 PM]

Ham Tutorial :: C++ Font Example

// Initialize HAMlib ham_Init(); // Setup the background mode ham_SetBgMode(0); // Initialize the Background Text class BgText.Init(BgTextInfo); // Initialize the (16 color) palette ham_LoadBGPal16((void*)background_Palette,1); // Background palette // Setup the tileset & map for the background ham_bg[1].ti = ham_InitTileSet((void*)background_Tiles, SIZEOF_16BIT(background_Tiles),0,1); ham_bg[1].mi = ham_InitMapSet((void*)background_Map,638,0,0); // Display the background ham_InitBg(1,1,1,0); // Print text using the BgText class BgText.Print(9,2,"HAM TUTORIAL"); BgText.Print(9,4,"FONT EXAMPLE"); BgText.Print(7,6,"MARCH 23RD, 2003"); // Start the VBL interrupt handler ham_StartIntHandler(INT_TYPE_VBL,(void*)vblFunc); // Infinite loop to keep the program running while(1) { // Every new frame if(newframe) { // Type the line of text letter by letter if (line == 1) { // Type the first line if(BgText.Type(4, 14, 10, "CREATED WITH PETER")) { // Draw the next line line = 2; } } else if (line == 2) { // Type the second line if(BgText.Type(4, 16, 10, "SCHRAUT'S BGTEXT CLASS")) { // Draw the next line line = 3; } } else if (line == 3) { // Clear the two rows of text BgText.ClearRow(14); BgText.ClearRow(16); line = 1; } // That's the end of a frame (and VBL) newframe=false; } } return 0; } // End of main()

http://www.aaronrogers.com/ham/Projects/Font_Example/font_example.php (4 of 7) [3/23/2003 1:04:55 PM]

Ham Tutorial :: C++ Font Example

// VBL Function void vblFunc() { // Every VBL is a new frame newframe=true; return; } // End of vblFunc() Code Explanation #include "bgtext.h" Of course you have to include the header for the BgText class. CBgText BgText; // Instance of BgText class Hopefully you know C++. This is just an object of type CBgText. bool newframe = true; // New frame or not This is used to make sure we only update the typing once per VBL. u8 line = 1; // Which line of text is being typed You'll notice in the main while() loop that this used to type the first line, second line or to clear the text. const TCharIndex Chars[] = {...} This character array is very important. Notice that you number each character of your font. The order is important - it should match your font exactly or you won't show the character you'd like to when printing text. Two other things to notice. The single quote must be escaped ('\'') and the space must be included. const TBgTextInfo BgTextInfo = {...} I won't go into much detail here as each element is described in the comments. Notice that the Palette Index is 0 - remember 'bank 0' earlier? Also notice the PaletteSize is 16 because it's a 16 color palette. ham_LoadBGPal16((void*)background_Palette,1); Notice we pass it a 1 because this background uses bank 1. Again, there can be one 256 color palette bank or 16 banks of 16 color palettes. ham_bg[1].ti = ham_InitTileSet(...) Not much to explain here. Just notice that I am using background 1 for this background. ham_bg[1].mi = ham_InitMapSet(...) Someone is going to have to help me with the math here. All I know is that it takes 638 16bit chunks to show the necessary part of the image. Remember that it is actually 256x256 pixels but we only need to see 240x160.

http://www.aaronrogers.com/ham/Projects/Font_Example/font_example.php (5 of 7) [3/23/2003 1:04:55 PM]

Ham Tutorial :: C++ Font Example

ham_InitBg(1,1,1,0); Trivial: background 1, active, less priority than the text background and it's not mosaic. BgText.Print(...) BgText is the name of our object of type CBgText. It has a Print function that takes the X position, Y position, and the data to type. Simple, huh?! One last thing to look at, the while(1) loop. if(newframe) Every new frame there is work to do. if (line == x) Make sense, I hope. if(BgText.Type(...) There are a few things going on here. First of all, it's in an if() statement because Type() returns false if it still has typing to do. Type() is passed the X position, Y position, the delay between typing characters and the text to type. One it's done typing, it returns true. This is how line gets incremented. BgText.ClearRow(...); Easy enough, uses your 'space' character to clear the text from the row passed. newframe=false; Once we are done with the previous stuff, we don't want to do anything until the next frame, or VBL.

Well, that's it. Not too difficult, huh?! Feel free to take a look at BgText.h and BgText.cpp to see what else you can do with this library.

Downloads Entire Project: Font_Example.zip (18 KBs) Executable Only: font_example.gba (110 KBs) Font: animeace.zip (42 KBs) This is the unconverted TTF file. BgText Libary: BackgroundText.zip (43 KBs) Freeware Fonts 1001FreeFonts.com BlamBot.com Nick's Fonts

http://www.aaronrogers.com/ham/Projects/Font_Example/font_example.php (6 of 7) [3/23/2003 1:04:55 PM]

Ham Tutorial :: C++ Font Example

View Demo Now Discuss Fonts Example << HOME >>

http://www.aaronrogers.com/ham/Projects/Font_Example/font_example.php (7 of 7) [3/23/2003 1:04:55 PM]

You might also like