Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Font size.

Credits go to miran and Thomas Harte for helping out!
This thread is locked; no one can reply to it. rss feed Print
Font size.
type568
Member #8,381
March 2007
avatar

Probably this question is stupid, but I can't figure it out myself.

From Allegro examples I understood how to use custom fonts, and even merge fonts. But even that in the Allegro example (exfont.c) I had gotten the text in big letters, I didn't understand how to change font size.

Basically I want to write the score(of a pong :p) but in BIG letters..

miran
Member #2,407
June 2002

You can do this in three easy steps:

1. Make a big font.
2. Use the big font from step 1 by loading it into your program and passing a pointer to it to the text output functions.
3. There is no step 3.

--
sig used to be here

type568
Member #8,381
March 2007
avatar

How do i make a big font? Do I have to use any *.dat (or other) file for it?

Thomas Harte
Member #33
April 2000
avatar

If you're willing to use an extra library, I highly recommend Glyph Keeper, which can provide FONTs from TrueType originals at runtime... though it still doesn't do pair kerning. GRRRRRR.

miran
Member #2,407
June 2002

Quote:

How do i make a big font?

Several options. Either use a converter like ttf2pcx to convert a ttf to a bitmap font or use a bitmap editor and draw each individual char. For that you can use a general purpose editor such as photoshop or a specialized program such as my own font editor (go to my profile to find it).

Quote:

Do I have to use any *.dat (or other) file for it?

You can use a *.dat file but you don't have to. Allegro can load *.bmp, *.pcx and *.tga files too.

--
sig used to be here

type568
Member #8,381
March 2007
avatar

So i can't just basically say "font size= 32" n get it of that size? (easiest way u suggest is to make a bitmap with 10 numbers, and build the score myself.. ?)

Thomas Harte
Member #33
April 2000
avatar

Easiest way if you have access to a Windows machine is to download ttf2pcx and put the font size you want to generate into that. If you want to be able to dynamically generate fonts of different sizes in game, use Glyph Keeper.

type568
Member #8,381
March 2007
avatar

Basically thanks..

Andrei Ellman
Member #3,434
April 2003

If you don't mind heavily-pixellated text, you can zoom the characters' bitmaps. To give a Pong-game that retro-feeling, this is probably what you want anyway.

Below is some code (complete with Doxygen-style comments) for doing just that (and as a bonus, I've also included some code for drawing outlined text). Note that the code currently assumes that the longest character in the font is not longer than the font-height). Also, note that as well as [tt]#includ[/tt]'ing [tt]<allegro.h>[/tt], you must also [tt]#include <allegro/internal/aintern.h>[/tt] in the file that contains this code.

#SelectExpand
1 2#include <allegro.h> 3#include <allegro/internal/aintern.h> /* Used to get the FONT_VTABLE */ 4 5 6// For now, make do with these values. 7#define AERVAL_YOOPY 0 8#define AERVAL_AIEEE -1 9 10 11static BITMAP *g_bmpTextOutZoomedUnzoomedBitmap; /*!< Temporary bitmap used by chTextOutZoomed() */ 12static FONT *g_fpZoomedTextFont; /*!< Font used by chTextOutZoomed() */ 13 14 15 16/****************************************************************************/ 17/*! 18 19 \brief Sets the font for use in the chTextOutZoomed() function. 20 21 This also prepares the chTextOutZoomed() for use if it has not been prepared already. 22 23 \remarks This re-sizes it's temp-bitmap 24 25 \todo Make the calculation of the length of longest char work properly. 26 27 \sa chTextOutZoomedTini 28 \sa chTextOutZoomed 29 30 \param fpFont The font to use. 31 32 \retval AERVAL_YOOPY | The font was successfully set. 33 \retval AERVAL_AIEEE | The temp-bitmap to be used by the font for chTextOutZoomed() could not be setup. 34 35 */ 36 37AERETVAL 38chSetFontForTextOutZoomedBitmap(FONT *fpFont) 39{ 40 int nXFontSize = text_height(fpFont); // !!! TODO: This should be the pixel-length of the longest character in the font!!!!! 41 int nYFontSize = text_height(fpFont); 42 43 /* Getridof the previous one */ 44 if(g_bmpTextOutZoomedUnzoomedBitmap) 45 { 46 destroy_bitmap(g_bmpTextOutZoomedUnzoomedBitmap); 47 } 48 49 50 51 g_bmpTextOutZoomedUnzoomedBitmap = create_bitmap(nXFontSize,nYFontSize); /* Should be the size of the largest character */ 52 53 if(!g_bmpTextOutZoomedUnzoomedBitmap) 54 { 55 return AERVAL_AIEEE; 56 } 57 58 59 /* Remember the font to use */ 60 g_fpZoomedTextFont = fpFont; 61 62 63 return AERVAL_YOOPY; 64} 65 66 67 68/****************************************************************************/ 69/*! 70 71 \brief Un-initialises the chTextOutZoomed() function. 72 73 The temporary bitmap for rendering the font is destroyed. 74 75 \sa chSetFontForTextOutZoomedBitmap 76 \sa chTextOutZoomed 77 78 */ 79 80void 81chTextOutZoomedTini() 82{ 83 if(g_bmpTextOutZoomedUnzoomedBitmap) 84 { 85 destroy_bitmap(g_bmpTextOutZoomedUnzoomedBitmap); 86 g_bmpTextOutZoomedUnzoomedBitmap = NULL; 87 } 88} 89 90 91 92 93/****************************************************************************/ 94/*! 95 96 \brief Draws the contents of the temporary bitmap \c g_bmpTextOutZoomedUnzoomedBitmap zoomed to the destination transparently. 97 98 Renders the contents of g_bmpTextOutZoomedUnzoomedBitmap zoomed to the destination transparently. 99 100 \pre chSetFontForTextOutZoomedBitmap() must be called before this function can be used 101 102 \warning This does not check for character going off screen-edge 103 104 \note The height of the unzoomed bitmap is determined by the height of g_bmpTextOutZoomedUnzoomedBitmap - hence no need to pass in the unzoomed height. 105 106 \sa chRenderCharZoomed() 107 \sa _chRenderCharZoomedFromTmpBitmapOpaque() 108 109 \sa chSetFontForTextOutZoomedBitmap 110 111 \param bmpDest Destination bitmap 112 \param nX X position on destination of zoomed character. 113 \param nY Y position on destination of zoomed character. 114 \param nXZoom Ammount to zoom text by in the X direction (between 1 and 0 means reduce instead of zoom) 115 \param nYZoom Ammount to zoom text by in the Y direction (between 1 and 0 means reduce instead of zoom) 116 \param nCharLength Length of the unzoomed character. 117 \param nCharLengthZoomed Length of the zoomed character. 118 \param nCharHeightZoomed Height of the zoomed character. 119 120 \return int The horizontal length of the zoomed character in pixels. 121 122 */ 123 124static INLINE void 125_chRenderCharZoomedFromTmpBitmapTransparent(BITMAP *bmpDest, int nX, int nY, int nCharLength, int nCharLengthZoomed, int nCharHeightZoomed) 126{ 127 masked_stretch_blit(g_bmpTextOutZoomedUnzoomedBitmap, bmpDest, 0,0, nCharLength,g_bmpTextOutZoomedUnzoomedBitmap->h, 128 nX,nY, nCharLengthZoomed,nCharHeightZoomed); 129} 130 131 132 133/****************************************************************************/ 134/*! 135 136 \brief Draws the contents of the temporary bitmap \c g_bmpTextOutZoomedUnzoomedBitmap zoomed to the destination opaquely. 137 138 Renders the contents of g_bmpTextOutZoomedUnzoomedBitmap zoomed to the destination transparently. 139 140 \pre chSetFontForTextOutZoomedBitmap() must be called before this function can be used 141 142 \warning This does not check for character going off screen-edge 143 144 \note The height of the unzoomed bitmap is determined by the height of g_bmpTextOutZoomedUnzoomedBitmap - hence no need to pass in the unzoomed height. 145 146 \sa chRenderCharZoomed() 147 \sa _chRenderCharZoomedFromTmpBitmapTransparent() 148 149 \sa chSetFontForTextOutZoomedBitmap 150 151 \param bmpDest Destination bitmap 152 \param nX X position on destination of zoomed character. 153 \param nY Y position on destination of zoomed character. 154 \param nXZoom Ammount to zoom text by in the X direction (between 1 and 0 means reduce instead of zoom) 155 \param nYZoom Ammount to zoom text by in the Y direction (between 1 and 0 means reduce instead of zoom) 156 \param nCharLength Length of the unzoomed character. 157 \param nCharLengthZoomed Length of the zoomed character. 158 \param nCharHeightZoomed Height of the zoomed character. 159 160 \return int The horizontal length of the zoomed character in pixels. 161 162 */ 163 164static INLINE void 165_chRenderCharZoomedFromTmpBitmapOpaque(BITMAP *bmpDest, int nX, int nY, int nCharLength, int nCharLengthZoomed, int nCharHeightZoomed) 166{ 167 stretch_blit(g_bmpTextOutZoomedUnzoomedBitmap, bmpDest, 0,0, nCharLength,g_bmpTextOutZoomedUnzoomedBitmap->h, 168 nX,nY, nCharLengthZoomed,nCharHeightZoomed); 169} 170 171 172 173/****************************************************************************/ 174/*! 175 176 \brief Draws a zoomed UNICODE character 177 178 Renders the character onto the bitmap at position x, y, using the current font (set in chSetFontForTextOutZoomedBitmap). 179 180 \pre chSetFontForTextOutZoomedBitmap() must be called before this function can be used 181 182 \warning This does not check for character going off screen-edge 183 184 185 \sa chTextoutZoomed() 186 187 \sa chSetFontForTextOutZoomedBitmap 188 189 \param bmpDest Destination bitmap 190 \param nChar The UNICODE character to print 191 \param nX X position of text 192 \param nY Y position of text 193 \param nXZoom Ammount to zoom text by in the X direction (between 1 and 0 means reduce instead of zoom) 194 \param nYZoom Ammount to zoom text by in the Y direction (between 1 and 0 means reduce instead of zoom) 195 \param nColor Text-colour - If the color is -1 and a color font is in use, it will be drawn using the colors from the original font bitmap. 196 \param nBGColour Text-background-colour 197 198 \return int The horizontal length of the zoomed character in pixels. 199 200 */ 201 202int 203chRenderCharZoomed(BITMAP *bmpDest, int nChar, int nX, int nY, fixed nXZoom, fixed nYZoom, int nColor, int nBGColour) 204{ 205 int nCharLength, nCharLengthZoomed; 206 207 208 ASSERT(g_bmpTextOutZoomedUnzoomedBitmap && g_fpZoomedTextFont); // must be setup 209 210 211 /* Work out the length of the zoomed character */ 212 nCharLength = g_fpZoomedTextFont->vtable->char_length(g_fpZoomedTextFont, nChar); 213 nCharLengthZoomed = (nCharLength * nXZoom)>>16; // ?: Use fixtoi() instead? 214 215 if(!uisspace(nChar)) /* Skip whitespaces */ 216 { 217 int nCharHeightZoomed = (g_bmpTextOutZoomedUnzoomedBitmap->h * nYZoom)>>16; // ?: Use fixtoi() instead? 218 219 /* Render character onto a temporary bitmap (we are using a temporary bitmap because render_char() does not do zoomed renderings) */ 220 if(nBGColour==-1) 221 { 222 clear_bitmap(g_bmpTextOutZoomedUnzoomedBitmap); /* Transparent text-drawing code means we must clear out the mess we made */ 223 } 224 g_fpZoomedTextFont->vtable->render_char(g_fpZoomedTextFont, nChar, nColor, nBGColour, g_bmpTextOutZoomedUnzoomedBitmap, 0, 0); 225 226 /* Stretch the temporary bitmap to the destination */ 227 if(nBGColour==-1) 228 { 229 _chRenderCharZoomedFromTmpBitmapTransparent(bmpDest, nX, nY, nCharLength, nCharLengthZoomed, nCharHeightZoomed); 230 } 231 else 232 { 233 _chRenderCharZoomedFromTmpBitmapOpaque(bmpDest, nX, nY, nCharLength, nCharLengthZoomed, nCharHeightZoomed); 234 } 235 236 } 237 else 238 { 239 if(nBGColour!=-1) 240 { 241 /* If using a solid background, then whitespaces must still be drawn. */ 242 rectfill(bmpDest, nX, nY, nX+nCharLengthZoomed-1, nY+((g_bmpTextOutZoomedUnzoomedBitmap->h * nYZoom)>>16)-1, nBGColour); 243 } 244 } 245 246 return nCharLengthZoomed; 247} 248 249 250 251 252/****************************************************************************/ 253/*! 254 255 \brief Draws a zoomed UNICODE character with an outline 256 257 Renders the character onto the bitmap at position x, y, using the current 258 font (set in chSetFontForTextOutZoomedBitmap) with an outline. 259 260 \note This does not do text with an opaque background 261 262 \pre chSetFontForTextOutZoomedBitmap() must be called before this function can be used. 263 264 \warning This does not check for character going off screen-edge 265 266 267 \sa chTextoutZoomedOutline() 268 269 \sa chSetFontForTextOutZoomedBitmap 270 271 \param bmpDest Destination bitmap 272 \param nChar The UNICODE character to print outlined 273 \param nX X position of text 274 \param nY Y position of text 275 \param nXZoom Ammount to zoom text by in the X direction (between 1 and 0 means reduce instead of zoom) 276 \param nYZoom Ammount to zoom text by in the Y direction (between 1 and 0 means reduce instead of zoom) 277 \param nColor Text-colour - If the color is -1 and a color font is in use, it will be drawn using the colors from the original font bitmap. 278 \param nOutlineColour Colour to render text-outline in (-1 means render the text-outline transparently(!)) 279 280 \return int The horizontal length of the zoomed character in pixels. 281 282 */ 283 284int 285chRenderCharZoomedOutline(BITMAP *bmpDest, int nChar, int nX, int nY, fixed nXZoom, fixed nYZoom, int nColor, int nOutlineColour) 286{ 287 int nCharLength, nCharLengthZoomed; 288 289 290 ASSERT(g_bmpTextOutZoomedUnzoomedBitmap && g_fpZoomedTextFont); // must be setup 291 292 293 /* Work out the length of the zoomed character */ 294 nCharLength = g_fpZoomedTextFont->vtable->char_length(g_fpZoomedTextFont, nChar); 295 nCharLengthZoomed = (nCharLength * nXZoom)>>16; // ?: Use fixtoi() instead? 296 297 298 if(!uisspace(nChar)) /* Skip whitespaces */ 299 { 300 /* Outline */ 301 302 int nCharHeightZoomed = (g_bmpTextOutZoomedUnzoomedBitmap->h * nYZoom)>>16; // ?: Use fixtoi() instead? 303 304 /* Render character onto a temporary bitmap (we are using a temporary bitmap because render_char() does not do zoomed renderings) */ 305 clear_bitmap(g_bmpTextOutZoomedUnzoomedBitmap); /* Transparent text-drawing code means we must clear out the mess we made */ 306 g_fpZoomedTextFont->vtable->render_char(g_fpZoomedTextFont, nChar, nOutlineColour, -1, g_bmpTextOutZoomedUnzoomedBitmap, 0, 0); 307 308 _chRenderCharZoomedFromTmpBitmapTransparent(bmpDest, nX, nY+1, nCharLength, nCharLengthZoomed, nCharHeightZoomed); 309 _chRenderCharZoomedFromTmpBitmapTransparent(bmpDest, nX+2, nY+1, nCharLength, nCharLengthZoomed, nCharHeightZoomed); 310 _chRenderCharZoomedFromTmpBitmapTransparent(bmpDest, nX+1, nY, nCharLength, nCharLengthZoomed, nCharHeightZoomed); 311 _chRenderCharZoomedFromTmpBitmapTransparent(bmpDest, nX+1, nY+2, nCharLength, nCharLengthZoomed, nCharHeightZoomed); 312 313 _chRenderCharZoomedFromTmpBitmapTransparent(bmpDest, nX, nY, nCharLength, nCharLengthZoomed, nCharHeightZoomed); 314 _chRenderCharZoomedFromTmpBitmapTransparent(bmpDest, nX+2, nY, nCharLength, nCharLengthZoomed, nCharHeightZoomed); 315 _chRenderCharZoomedFromTmpBitmapTransparent(bmpDest, nX, nY+2, nCharLength, nCharLengthZoomed, nCharHeightZoomed); 316 _chRenderCharZoomedFromTmpBitmapTransparent(bmpDest, nX+2, nY+2, nCharLength, nCharLengthZoomed, nCharHeightZoomed); 317 318 319 320 /* Main text */ 321 322 /* Stretch the temporary bitmap to the destination */ 323 // Could optimise further by using a temp bitmap for the zoomed character, but then, the bitmap size would depend on the zoom-paramaters. 324 325 /* Redraw the char in the new colour */ 326 g_fpZoomedTextFont->vtable->render_char(g_fpZoomedTextFont, nChar, nColor, -1, g_bmpTextOutZoomedUnzoomedBitmap, 0, 0); 327 328 _chRenderCharZoomedFromTmpBitmapTransparent(bmpDest, nX+1, nY+1, nCharLength, nCharLengthZoomed, nCharHeightZoomed); 329 } 330 331 return nCharLengthZoomed; 332} 333 334 335 336 337 338 339 340/****************************************************************************/ 341/*! 342 343 \brief Draws some zoomed text 344 345 Writes the string onto the bitmap at position x, y, using the current font (set in chSetFontForTextOutZoomedBitmap). 346 347 \remarks 348 Similar to Allegro's textout(), but this zooms the font (can also reduce for values of n[X|Y]Zoom < 1) 349 350 \attention chSetFontForTextOutZoomedBitmap() must be called before this function can be used 351 352 \warning This does not check for text going off screen-edge 353 354 355 \sa chRenderCharZoomed() 356 357 \sa chSetFontForTextOutZoomedBitmap 358 359 \param bmpDest Destination bitmap 360 \param szStr The string to print 361 \param nX X position of text 362 \param nY Y position of text 363 \param nXZoom Ammount to zoom text by in the X direction (between 1 and 0 means reduce instead of zoom) 364 \param nYZoom Ammount to zoom text by in the Y direction (between 1 and 0 means reduce instead of zoom) 365 \param nColor Text-colour - If the color is -1 and a color font is in use, it will be drawn using the colors from the original font bitmap. 366 \param nBGColour Text-background-colour 367 368 */ 369 370void 371chTextoutZoomed(BITMAP *bmpDest, AL_CONST char *szStr, int nX, int nY, fixed nXZoom, fixed nYZoom, int nColor, int nBGColour) 372{ 373 int nChar; /* Current UNICODE char to print */ 374 375 int nTextXPosition = nX; /* X position of current char in the destination bitmap. This is advanced by the zoomed character length each time a character is printed */ 376 377 378 ASSERT(g_bmpTextOutZoomedUnzoomedBitmap && g_fpZoomedTextFont); // must be setup 379 380 381 /* Print all characters in the string */ 382 while( (nChar = ugetxc(&szStr)) ) 383 { 384 nTextXPosition += chRenderCharZoomed(bmpDest, nChar, nTextXPosition, nY, nXZoom, nYZoom, nColor, nBGColour); 385 } 386 387} 388 389 390 391/****************************************************************************/ 392/*! 393 394 \brief Draws some zoomed text with an outline 395 396 Writes the string onto the bitmap at position x, y, using the current 397 font (set in chSetFontForTextOutZoomedBitmap) with an outline. 398 399 \note This does not do text with an opaque background 400 401 \remarks 402 Similar to Allegro's textout(), but this zooms the font (can also reduce for values of n[X|Y]Zoom < 1) and adds an outline 403 404 \attention chSetFontForTextOutZoomedBitmap() must be called before this function can be used 405 406 \warning This does not check for text going off screen-edge 407 408 409 \sa chRenderCharZoomed() 410 411 \sa chSetFontForTextOutZoomedBitmap 412 413 \param bmpDest Destination bitmap 414 \param szStr The string to print 415 \param nX X position of text 416 \param nY Y position of text 417 \param nXZoom Ammount to zoom text by in the X direction (between 1 and 0 means reduce instead of zoom) 418 \param nYZoom Ammount to zoom text by in the Y direction (between 1 and 0 means reduce instead of zoom) 419 \param nColor Text-colour - If the color is -1 and a color font is in use, it will be drawn using the colors from the original font bitmap. 420 \param nOutlineColour Colour to render text-outline in (-1 means render the text-outline transparently(!)) 421 422 */ 423 424void 425chTextoutZoomedOutline(BITMAP *bmpDest, AL_CONST char *szStr, int nX, int nY, fixed nXZoom, fixed nYZoom, int nColor, int nOutlineColour) 426{ 427 int nChar; /* Current UNICODE char to print */ 428 429 int nTextXPosition = nX; /* X position of current char in the destination bitmap. This is advanced by the zoomed character length each time a character is printed */ 430 431 432 ASSERT(g_bmpTextOutZoomedUnzoomedBitmap && g_fpZoomedTextFont); // must be setup 433 434 435 /* Print all characters in the string */ 436 while( (nChar = ugetxc(&szStr)) ) 437 { 438 nTextXPosition += chRenderCharZoomedOutline(bmpDest, nChar, nTextXPosition, nY, nXZoom, nYZoom, nColor, nOutlineColour); 439 } 440}

AE.

--
Don't let the illegitimates turn you into carbon.

Go to: