1 //********************************************************************************************
2 //*
3 //*    This file is part of Egoboo.
4 //*
5 //*    Egoboo is free software: you can redistribute it and/or modify it
6 //*    under the terms of the GNU General Public License as published by
7 //*    the Free Software Foundation, either version 3 of the License, or
8 //*    (at your option) any later version.
9 //*
10 //*    Egoboo is distributed in the hope that it will be useful, but
11 //*    WITHOUT ANY WARRANTY; without even the implied warranty of
12 //*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 //*    General Public License for more details.
14 //*
15 //*    You should have received a copy of the GNU General Public License
16 //*    along with Egoboo.  If not, see <http://www.gnu.org/licenses/>.
17 //*
18 //********************************************************************************************
19 
20 /// @file font_bmp.c
21 /// @brief bitmapped font stuff
22 /// @details
23 
24 #include "font_bmp.h"
25 
26 #include "texture.h"
27 #include "log.h"
28 
29 #include "egoboo_vfs.h"
30 #include "egoboo_strutil.h"
31 #include "egoboo_fileutil.h"
32 
33 //--------------------------------------------------------------------------------------------
34 //--------------------------------------------------------------------------------------------
35 int       fontoffset;                 // Line up fonts from top of screen
36 SDL_Rect  fontrect[NUMFONT];          // The font rectangles
37 Uint8     fontxspacing[NUMFONT];      // The spacing stuff
38 Uint8     fontyspacing;
39 
40 Uint8     asciitofont[256];           // Conversion table
41 
42 //--------------------------------------------------------------------------------------------
43 //--------------------------------------------------------------------------------------------
font_bmp_init()44 void font_bmp_init()
45 {
46     /// @details BB@> fill in default values
47 
48     Uint16 i, ix, iy, cnt;
49     float dx, dy;
50 
51     // Mark all as unused
52     for ( cnt = 0; cnt < 256; cnt++ )
53     {
54         asciitofont[cnt] = 255;
55     }
56 
57     dx = 256 / NUMFONTX;
58     dy = 256 / NUMFONTY;
59     for ( i = 0; i < NUMFONT; i++ )
60     {
61         ix = i % NUMFONTX;
62         iy = i / NUMFONTX;
63 
64         fontrect[i].x = ix * dx;
65         fontrect[i].w = dx;
66         fontrect[i].y = iy * dy;
67         fontrect[i].h = dy;
68         fontxspacing[i] = 0;
69     }
70     fontyspacing = dy;
71 }
72 
73 //--------------------------------------------------------------------------------------------
font_bmp_load_vfs(const char * szBitmap,const char * szSpacing)74 void font_bmp_load_vfs( const char* szBitmap, const char* szSpacing )
75 {
76     /// @details ZZ@> This function loads the font bitmap and sets up the coordinates
77     ///    of each font on that bitmap...  Bitmap must have 16x6 fonts
78 
79     int cnt, y, xsize, ysize, xdiv, ydiv;
80     int stt_x, stt_y;
81     int xspacing, yspacing;
82     char cTmp;
83     vfs_FILE *fileread;
84 
85     font_bmp_init();
86     if ( INVALID_TX_TEXTURE == TxTexture_load_one_vfs( szBitmap, ( TX_REF )TX_FONT, TRANSCOLOR ) )
87     {
88         log_error( "load_font() - Cannot load file! (\"%s\")\n", szBitmap );
89     }
90 
91     // Get the size of the bitmap
92     xsize = oglx_texture_GetImageWidth( TxTexture_get_ptr(( TX_REF )TX_FONT ) );
93     ysize = oglx_texture_GetImageHeight( TxTexture_get_ptr(( TX_REF )TX_FONT ) );
94     if ( 0 == xsize || 0 == ysize )
95     {
96         log_error( "Bad font size! (%i, %i)\n", xsize, ysize );
97     }
98 
99     // Figure out the general size of each font
100     ydiv = ysize / NUMFONTY;
101     xdiv = xsize / NUMFONTX;
102 
103     // Figure out where each font is and its spacing
104     fileread = vfs_openRead( szSpacing );
105     if ( NULL == fileread )
106     {
107         log_error( "Font spacing not avalible! (%i, %i)\n", xsize, ysize );
108     }
109 
110     y = 0;
111     stt_x = 0;
112     stt_y = 0;
113 
114     // Uniform font height is at the top
115     yspacing = fget_next_int( fileread );
116     fontoffset = yspacing;
117     for ( cnt = 0; cnt < NUMFONT && goto_colon( NULL, fileread, btrue ); cnt++ )
118     {
119         vfs_scanf( fileread, "%c", &cTmp );
120         xspacing = fget_int( fileread );
121         if ( asciitofont[( Uint8 )cTmp] == 255 ) asciitofont[( Uint8 )cTmp] = ( Uint8 ) cnt;
122         if ( stt_x + xspacing + 1 > 255 )
123         {
124             stt_x = 0;
125             stt_y += yspacing;
126         }
127 
128         fontrect[cnt].x = stt_x;
129         fontrect[cnt].w = xspacing;
130         fontrect[cnt].y = stt_y;
131         fontrect[cnt].h = yspacing - 2;
132         fontxspacing[cnt] = xspacing + 1;
133 
134         stt_x += xspacing + 1;
135     }
136     vfs_close( fileread );
137 
138     // Space between lines
139     fontyspacing = ( yspacing >> 1 ) + FONTADD;
140 }
141 
142 //--------------------------------------------------------------------------------------------
font_bmp_length_of_word(const char * szText)143 int font_bmp_length_of_word( const char *szText )
144 {
145     /// @details ZZ@> This function returns the number of pixels the
146     ///    next word will take on screen in the x direction
147 
148     // Count all preceeding spaces
149     int x = 0;
150     int cnt = 0;
151     Uint8 cTmp = szText[cnt];
152 
153     while ( ' ' == cTmp || '~' == cTmp || C_NEW_LINE_CHAR == cTmp )
154     {
155         if ( ' ' == cTmp )
156         {
157             x += fontxspacing[asciitofont[cTmp]];
158         }
159         else if ( '~' == cTmp )
160         {
161             x = ( x & TABAND ) + TABADD;
162         }
163 
164         cnt++;
165         cTmp = szText[cnt];
166     }
167 
168     while ( ' ' != cTmp && '~' != cTmp && C_NEW_LINE_CHAR != cTmp && CSTR_END != cTmp )
169     {
170         x += fontxspacing[asciitofont[cTmp]];
171         cnt++;
172         cTmp = szText[cnt];
173     }
174 
175     return x;
176 }