Main Page | Alphabetical List | Compound List | File List | Compound Members | File Members

TEXT.C

Go to the documentation of this file.
00001 
00002 //
00003 //      Text.c
00004 //
00005 //      Copyright (c) 1997, Eclipse Entertainment; All rights reserved.
00006 //
00007 //      Text output API.
00008 //
00009 //      History
00010 //      -------
00011 //
00012 //      Peter Siamidis  02/05/98        Created.
00013 //
00015 #pragma warning ( disable : 4514 )
00016 #include <stdlib.h>
00017 #include <string.h>
00018 #include <assert.h>
00019 #include "Text.h"
00020 #include "Errorlog.h"
00021 
00022 #define MAX_SUPPORTED_CHARS     128
00023 
00024 
00026 //      Structs
00028 
00029 // font struct
00030 typedef struct
00031 {
00032         geBitmap                        *Bitmap;
00033         int32                           FontWidth;
00034         int32                           FontHeight;
00035         int32                           AddWidth;
00036         int32                           AddHeight;
00037 } Font_Type;
00038 
00039 
00041 //      Globals
00043 static char                     *Dir = "Bmp\\";                                         // where font decals are found
00044 static char                     *Ext = ".Bmp";                                          // exetension of decal files
00045 static geEngine         *Engine = NULL;                                         // engine to use for output
00046 Font_Type                       Fonts[MAX_FONTS];                                       // font list
00047 int32                           FontLookup[MAX_SUPPORTED_CHARS];        // character lookup table
00048 static char                     FontNames[MAX_FONTS][100] =                     // font names
00049 {
00050         "SFont1",
00051         "SFont2",
00052         "Console\\640x480\\font"
00053 };
00054 
00055 
00056 
00058 //
00059 //      Text_Create()
00060 //
00061 //      Create everything required for text output. The passed engine pointer gets
00062 //      saved for later use.
00063 //
00065 extern geVFile *MainFS;
00066 geBoolean Text_Create(
00067         geEngine        *SaveEngine )   // engine used for output
00068 {
00069 
00070         // locals
00071         char            Filename[64];
00072         int32           i, PosX, PosY;
00073         int32           Width;
00074         geVFile *               File;
00075 
00076         // fail if we have an invalid engine pointer
00077         if ( SaveEngine == NULL )
00078         {
00079                 assert( 0 );
00080                 return GE_FALSE;
00081         }
00082 
00083         // setup font character lookup table
00084         PosX = 0;
00085         PosY = 0;
00086         Width = 17 * 15;
00087         for ( i = 0; i < MAX_SUPPORTED_CHARS; i++ )
00088         {
00089                 FontLookup[i] = ( PosX << 16 ) | PosY;
00090                 PosX += 17;
00091 
00092                 if ( PosX >= Width )
00093                 {
00094                         PosY += 17;
00095                         PosX = 0;
00096                 }
00097         }
00098 
00099         // setup all fonts (fail if we can't)
00100         for ( i = 0; i < MAX_FONTS; i++ )
00101         {
00102                 //geBitmap      *Pal;
00103 
00104                 // build font filename
00105                 strcpy( Filename, Dir );
00106                 strcat( Filename, FontNames[i] );
00107                 strcat( Filename, Ext );
00108 
00109                 // load the decal
00110                 File = geVFile_Open(MainFS, Filename, GE_VFILE_OPEN_READONLY);
00111                 if      (!File)
00112                 {
00113                         geErrorLog_AddString(-1, "Text_Create:  geVFile_Open failed:", Filename);
00114                         return GE_FALSE;
00115                 }
00116                 Fonts[i].Bitmap = geBitmap_CreateFromFile(File);
00117                 geVFile_Close(File);
00118 
00119                 if (!Fonts[i].Bitmap)
00120                 {
00121                         geErrorLog_AddString(-1, "Text_Create:  geBitmap_CreateFromFile failed.", NULL);
00122                         return GE_FALSE;
00123                 }
00124 
00125                 if (!geBitmap_SetColorKey(Fonts[i].Bitmap, GE_TRUE, 255, GE_FALSE))
00126                 {
00127                         geErrorLog_AddString(-1, "Text_Create:  geBitmap_SetColorKey failed.", NULL);
00128                         return GE_FALSE;
00129                         //goto ExitWithError;
00130                 }
00131 
00132                 if (!geEngine_AddBitmap(SaveEngine, Fonts[i].Bitmap))
00133                 {
00134                         geErrorLog_AddString(-1, "Text_Create:  geEngine_AddBitmap failed.", NULL);
00135                         return GE_FALSE;
00136                 }
00137 
00138                 // init font dimensions
00139                 if (i == Font_Small)    // special case. sorry. 
00140                         {
00141                                 Fonts[i].FontWidth  = 8;
00142                                 Fonts[i].FontHeight = 16;
00143                                 Fonts[i].AddHeight  = 16  + 2;
00144                                 Fonts[i].AddWidth   = 8  - 1;
00145                                 continue;
00146                         }
00147                 else
00148                         {
00149                                 Fonts[i].FontWidth = 16;
00150                                 Fonts[i].FontHeight = 16;
00151                                 Fonts[i].AddHeight = 16 + 5;
00152                                 Fonts[i].AddWidth = 13;
00153                         }
00154         }
00155 
00156         // save the engine pointer
00157         Engine = SaveEngine;
00158 
00159         // all done
00160         return GE_TRUE;
00161 
00162 } // Text_Create()
00163 
00164 
00165 
00167 //
00168 //      Text_Destroy()
00169 //
00170 //      Free everything.
00171 //
00172 //      Returns
00173 //              GE_TRUE
00174 //                      Text system has been destroyed.
00175 //              GE_FALSE
00176 //                      Text system has no been completely destroyed.
00177 //
00179 geBoolean Text_Destroy(
00180         void )  // no parameters
00181 {
00182 
00183         // locals
00184         int32           i;
00185         geBoolean       Ret;
00186 
00187         Ret = GE_TRUE;
00188 
00189         // fail if we have an invalid engine pointer
00190         if ( Engine == NULL )
00191         {
00192                 assert( 0 );
00193                 return GE_FALSE;
00194         }
00195 
00196         // zap font lookup table
00197         memset( FontLookup, 0, sizeof( FontLookup ) );
00198 
00199         // zap all font decals
00200         for ( i = 0; i < MAX_FONTS; i++ )
00201         {
00202                 if (!Fonts[i].Bitmap)
00203                         continue;
00204 
00205                 if (!geEngine_RemoveBitmap(Engine, Fonts[i].Bitmap))
00206                         Ret = GE_FALSE;
00207 
00208                 geBitmap_Destroy(&Fonts[i].Bitmap);
00209                 Fonts[i].Bitmap = NULL;
00210 
00211         }
00212 
00213         // zap all font info
00214         memset( Fonts, 0, sizeof( Fonts ) );
00215 
00216         // zap saved engine pointer
00217         Engine = NULL;
00218 
00219         // all done
00220         return Ret;
00221 
00222 } // Text_Create()
00223 
00224 
00225 
00227 //
00228 //      Text_Out()
00229 //
00230 //      Output a text string.
00231 //
00233 geBoolean Text_Out(
00234         char            *Text,          // string to output
00235         FontType        FontNum,        // font to use
00236         int32           x,                      // x location
00237         int32           y,                      // y location
00238         FontStyle       Style )         // output style
00239 {
00240 
00241         // locals
00242         GE_Rect Rect;
00243         int32   Length;
00244         uint8   Val;
00245 
00246         // fail if we have invalid data
00247         if ( ( Engine == NULL ) || ( Text == NULL ) )
00248         {
00249                 assert( 0 );
00250                 return GE_FALSE;
00251         }
00252         if ( ( FontNum < 0 ) || ( FontNum >= MAX_FONTS ) )
00253         {
00254                 assert( 0 );
00255                 return GE_FALSE;
00256         }
00257 
00258         // get length of string (fail if it's invalid)
00259         Length = strlen( Text );
00260         if ( Length <= 0 )
00261         {
00262                 return GE_FALSE;
00263         }
00264 
00265         // always center characters on y axis
00266         y -= Fonts[FontNum].FontHeight / 2;
00267 
00268         // adjust x coordinate to reflect style
00269         switch ( Style )
00270         {
00271 
00272                 case Style_Center:
00273                 {
00274                         x -= ( Length * Fonts[FontNum].AddWidth ) / 2;
00275                         break;
00276                 }
00277 
00278                 case Style_RightJustify:
00279                 {
00280                         x -= ( Length * Fonts[FontNum].AddWidth );
00281                         break;
00282                 }
00283         }
00284         if (FontNum == Font_Small)
00285                 {
00286                         // process all characters
00287                         while ( Length-- > 0 )
00288                         {
00289 
00290                                 // get current character
00291                                 Val = *Text++;
00292 
00293                                 // only print characters in range
00294                                 if ( ( Val >= 0 ) && ( Val < MAX_SUPPORTED_CHARS ) )
00295                                 {
00296                                         // setup character rect
00297                                         Rect.Left = Val * Fonts[FontNum].FontWidth;
00298                                         Rect.Right = Rect.Left + Fonts[FontNum].FontWidth;
00299                                         Rect.Top = 0;
00300                                         Rect.Bottom = Rect.Top + Fonts[FontNum].FontHeight;
00301 
00302                                         // draw character
00303                                         geEngine_DrawBitmap( Engine, Fonts[FontNum].Bitmap, &Rect, x, y );
00304                                 }
00305 
00306                                 // shift to next position
00307                                 x += Fonts[FontNum].AddWidth;
00308                         }
00309         
00310                 }
00311         else
00312                 {
00313                         // process all characters
00314                         while ( Length-- > 0 )
00315                         {
00316 
00317                                 // get current character
00318                                 Val = *Text++;
00319 
00320                                 // only print characters in range
00321                                 if ( ( Val >= 0 ) && ( Val < MAX_SUPPORTED_CHARS ) )
00322                                 {
00323                                 
00324                                         // capitalize it if required
00325                                         if ( Val >= 'a' && Val <= 'z' )
00326                                         {
00327                                                 Val += (uint8)( 'A' - 'a' );
00328                                         }
00329 
00330                                         // setup character rect
00331                                         Rect.Left = ( FontLookup[Val] >> 16 ) + 1;
00332                                         Rect.Right = Rect.Left + 16;
00333                                         Rect.Top = ( FontLookup[Val] & 0xffff ) + 1;
00334                                         Rect.Bottom = Rect.Top + 16;
00335 
00336                                         // draw character
00337                                         geEngine_DrawBitmap( Engine, Fonts[FontNum].Bitmap, &Rect, x, y );
00338                                 }
00339 
00340                                 // shift to next position
00341                                 x += Fonts[FontNum].AddWidth;
00342                         }
00343                 }
00344 
00345         // all done
00346         return GE_TRUE;
00347 
00348 } // Text_Out()
00349 
00350 
00351 
00353 //
00354 //      Text_GetWidth()
00355 //
00356 //      Get the width of a font.
00357 //
00358 //      Returns
00359 //              int32
00360 //                      Font spacing width.
00361 //              -1
00362 //                      Could not get font spacing width.
00363 //
00365 int32 Text_GetWidth(
00366         FontType        Font )  // font whose width we want
00367 {
00368 
00369         // fail if we have invalid data
00370         if ( Fonts[Font].Bitmap == NULL )
00371         {
00372                 assert( 0 );
00373                 return -1;
00374         }
00375 
00376         // return font spacing width
00377         return Fonts[Font].AddWidth;
00378         
00379 } // Text_GetWidth()
00380 
00381 
00382 
00384 //
00385 //      Text_GetHeight()
00386 //
00387 //      Get the height of a font.
00388 //
00389 //      Returns
00390 //              int32
00391 //                      Font spacing height.
00392 //              -1
00393 //                      Could not get font spacing height.
00394 //
00396 int32 Text_GetHeight(
00397         FontType        Font )  // font whose height we want
00398 {
00399 
00400         // fail if we have invalid data
00401         if ( Fonts[Font].Bitmap == NULL )
00402         {
00403                 assert( 0 );
00404                 return -1;
00405         }
00406 
00407         // return font spacing height
00408         return Fonts[Font].AddHeight;
00409         
00410 } // Text_GetWidth()

Generated on Tue Sep 30 12:36:29 2003 for GTestAndEngine by doxygen 1.3.2