00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
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
00028
00029
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
00043 static char *Dir = "Bmp\\";
00044 static char *Ext = ".Bmp";
00045 static geEngine *Engine = NULL;
00046 Font_Type Fonts[MAX_FONTS];
00047 int32 FontLookup[MAX_SUPPORTED_CHARS];
00048 static char FontNames[MAX_FONTS][100] =
00049 {
00050 "SFont1",
00051 "SFont2",
00052 "Console\\640x480\\font"
00053 };
00054
00055
00056
00058
00059
00060
00061
00062
00063
00065 extern geVFile *MainFS;
00066 geBoolean Text_Create(
00067 geEngine *SaveEngine )
00068 {
00069
00070
00071 char Filename[64];
00072 int32 i, PosX, PosY;
00073 int32 Width;
00074 geVFile * File;
00075
00076
00077 if ( SaveEngine == NULL )
00078 {
00079 assert( 0 );
00080 return GE_FALSE;
00081 }
00082
00083
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
00100 for ( i = 0; i < MAX_FONTS; i++ )
00101 {
00102
00103
00104
00105 strcpy( Filename, Dir );
00106 strcat( Filename, FontNames[i] );
00107 strcat( Filename, Ext );
00108
00109
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
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
00139 if (i == Font_Small)
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
00157 Engine = SaveEngine;
00158
00159
00160 return GE_TRUE;
00161
00162 }
00163
00164
00165
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00179 geBoolean Text_Destroy(
00180 void )
00181 {
00182
00183
00184 int32 i;
00185 geBoolean Ret;
00186
00187 Ret = GE_TRUE;
00188
00189
00190 if ( Engine == NULL )
00191 {
00192 assert( 0 );
00193 return GE_FALSE;
00194 }
00195
00196
00197 memset( FontLookup, 0, sizeof( FontLookup ) );
00198
00199
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
00214 memset( Fonts, 0, sizeof( Fonts ) );
00215
00216
00217 Engine = NULL;
00218
00219
00220 return Ret;
00221
00222 }
00223
00224
00225
00227
00228
00229
00230
00231
00233 geBoolean Text_Out(
00234 char *Text,
00235 FontType FontNum,
00236 int32 x,
00237 int32 y,
00238 FontStyle Style )
00239 {
00240
00241
00242 GE_Rect Rect;
00243 int32 Length;
00244 uint8 Val;
00245
00246
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
00259 Length = strlen( Text );
00260 if ( Length <= 0 )
00261 {
00262 return GE_FALSE;
00263 }
00264
00265
00266 y -= Fonts[FontNum].FontHeight / 2;
00267
00268
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
00287 while ( Length-- > 0 )
00288 {
00289
00290
00291 Val = *Text++;
00292
00293
00294 if ( ( Val >= 0 ) && ( Val < MAX_SUPPORTED_CHARS ) )
00295 {
00296
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
00303 geEngine_DrawBitmap( Engine, Fonts[FontNum].Bitmap, &Rect, x, y );
00304 }
00305
00306
00307 x += Fonts[FontNum].AddWidth;
00308 }
00309
00310 }
00311 else
00312 {
00313
00314 while ( Length-- > 0 )
00315 {
00316
00317
00318 Val = *Text++;
00319
00320
00321 if ( ( Val >= 0 ) && ( Val < MAX_SUPPORTED_CHARS ) )
00322 {
00323
00324
00325 if ( Val >= 'a' && Val <= 'z' )
00326 {
00327 Val += (uint8)( 'A' - 'a' );
00328 }
00329
00330
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
00337 geEngine_DrawBitmap( Engine, Fonts[FontNum].Bitmap, &Rect, x, y );
00338 }
00339
00340
00341 x += Fonts[FontNum].AddWidth;
00342 }
00343 }
00344
00345
00346 return GE_TRUE;
00347
00348 }
00349
00350
00351
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00365 int32 Text_GetWidth(
00366 FontType Font )
00367 {
00368
00369
00370 if ( Fonts[Font].Bitmap == NULL )
00371 {
00372 assert( 0 );
00373 return -1;
00374 }
00375
00376
00377 return Fonts[Font].AddWidth;
00378
00379 }
00380
00381
00382
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00396 int32 Text_GetHeight(
00397 FontType Font )
00398 {
00399
00400
00401 if ( Fonts[Font].Bitmap == NULL )
00402 {
00403 assert( 0 );
00404 return -1;
00405 }
00406
00407
00408 return Fonts[Font].AddHeight;
00409
00410 }