1 /*
2 
3 *************************************************************************
4 
5 ArmageTron -- Just another Tron Lightcycle Game in 3D.
6 Copyright (C) 2000  Manuel Moos (manuel@moosnet.de)
7 
8 **************************************************************************
9 
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License
12 as published by the Free Software Foundation; either version 2
13 of the License, or (at your option) any later version.
14 
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23 
24 ***************************************************************************
25 
26 */
27 
28 #ifndef ArmageTron_FONT_H
29 #define ArmageTron_FONT_H
30 
31 #include "rSDL.h"
32 
33 #include "defs.h"
34 #include "rTexture.h"
35 #include "tString.h"
36 #include "tColor.h"
37 
38 class rFont:public rFileTexture{
39     int offset;
40     REAL cwidth;
41     REAL cheight;
42     REAL onepixel;
43     rFont *lowerPart;
44 public:
45     rFont(const char *fileName,int Offset=0,REAL CWidth=(1/16.0),
46           REAL CHeight=(1/8.0),REAL onepixel=1/256.0, rFont *lower=NULL);
47     rFont(const char *fileName, rFont *lower);
48     virtual ~rFont();
49 
50 #ifndef DEDICATED
51     // displays c
52     void Render(unsigned char c,REAL left,REAL top,REAL right,REAL bot);
53 #endif
54     static rFont s_defaultFont,s_defaultFontSmall;
55 
56 protected:
57     virtual void ProcessImage(SDL_Surface *);       //!< process the surface before uploading it to GL
58     virtual void OnSelect( bool enforce );
59 };
60 
61 
62 // **********************************************+
63 
64 // maybe make this a child of std::ostream...
65 class rTextField{
66     tString buffer;       // buffer where we store stuff before we print it
67     int  width;          // width in characters
68     int  parIndent;      // number of spaces to insert after automatic newline
69     REAL left,top;       // top left corner of the console
70     REAL cwidth,cheight; // character dimensions
71     rFont *F;             // the font
72     int  x,y,realx;      // current cursor position
73 
74     tColor color_;               //!< current color
75     static tColor defaultColor_; //!< default color
76     static tColor blendColor_;   //!< color all other colors are multiplied with
77 
78     int cursor; // display mode of the cursor; 0: disabled, 1: low, 2: high
79     int cursorPos; // position of the cursor (number of chars to come)
80 
81     REAL cursor_x,cursor_y; // position on the screen
82 
83     void FlushLine(int len,bool newline=true);
84     void FlushLine(bool newline=true);
85 public:
86 #define  rCWIDTH_NORMAL  (16/640.0)
87 #define  rCHEIGHT_NORMAL (32/480.0)
88 
89     rTextField(REAL Left,REAL Top,
90                REAL Cwidth=rCWIDTH_NORMAL,REAL Cheight=rCHEIGHT_NORMAL,
91                rFont *f=&rFont::s_defaultFont);
92 
93     virtual ~rTextField(); // for future extensions (buffered console?)
94 
GetCWidth()95     REAL GetCWidth() const {
96         return cwidth;
97     }
GetCHeight()98     REAL GetCHeight() const {
99         return cheight;
100     }
101 
SetTop(REAL t)102     void SetTop( REAL t ){
103         top = t;
104     }
105 
SetLeft(REAL l)106     void SetLeft( REAL l ){
107         top = l;
108     }
109 
GetTop()110     REAL GetTop() const{
111         return top;
112     }
113 
GetLeft()114     REAL GetLeft() const{
115         return left;
116     }
117 
SetWidth(int w)118     void SetWidth(int w){
119         width=w;
120     }
121 
GetWidth()122     int GetWidth() const {
123         return width;
124     }
125 
SetIndent(int i)126     void SetIndent(int i){
127         parIndent=i;
128     }
129 
GetIndent()130     int GetIndent() const {
131         return parIndent;
132     }
133 
SetCursor(int c,int p)134     void SetCursor(int c,int p){
135         cursor=c;
136         cursorPos=p;
137     }
138 
ResetColor()139     void ResetColor(){
140         FlushLine(false);
141         color_ = defaultColor_;
142     }
143 
144     // rTextField & operator<<(unsigned char c);
145 
146     enum ColorMode {
147         COLOR_IGNORE,   // ignore color codes, printing everything verbatim
148         COLOR_USE,      // normal mode: hide color codes and use them
149         COLOR_SHOW      // use color codes, but print them as well
150     };
151 
152     rTextField & StringOutput(const char *c, ColorMode colorMode = COLOR_USE );
153 
Lines()154     int Lines(){
155         return y;
156     }
157 
158     inline rTextField & SetColor( tColor const & color );	//!< Sets current color
159     inline tColor const & GetColor( void ) const;	//!< Gets current color
160     inline rTextField const & GetColor( tColor & color ) const;	//!< Gets current color
161     static void SetDefaultColor( tColor const & defaultColor );	//!< Sets default color
162     static tColor const & GetDefaultColor( void );	//!< Gets default color
163     static void GetDefaultColor( tColor & defaultColor );	//!< Gets default color
164     static void SetBlendColor( tColor const & blendColor );	//!< Sets color all other colors are multiplied with
165     static tColor const & GetBlendColor( void );	//!< Gets color all other colors are multiplied with
166     static void GetBlendColor( tColor & blendColor );	//!< Gets color all other colors are multiplied with
167 
168 private:
169     inline void WriteChar(unsigned char c); //!< writes a single character as it is, no automatic newline breaking
170 };
171 
172 template<class T> rTextField & operator<<(rTextField &c,const T &x){
173     tColoredString out;
174     out << x;
175     return c.StringOutput(out);
176 }
177 
178 void DisplayText(REAL x,REAL y,REAL w,REAL h,const char *text,int center=0,
179                  int cursor=0,int cursorPos=0, rTextField::ColorMode colorMode = rTextField::COLOR_USE );
180 
181 // *******************************************************************************************
182 // *
183 // *	GetColor
184 // *
185 // *******************************************************************************************
186 //!
187 //!		@return		current color
188 //!
189 // *******************************************************************************************
190 
GetColor(void)191 tColor const & rTextField::GetColor( void ) const
192 {
193     return this->color_;
194 }
195 
196 // *******************************************************************************************
197 // *
198 // *	GetColor
199 // *
200 // *******************************************************************************************
201 //!
202 //!		@param	color	current color to fill
203 //!		@return		A reference to this to allow chaining
204 //!
205 // *******************************************************************************************
206 
GetColor(tColor & color)207 rTextField const & rTextField::GetColor( tColor & color ) const
208 {
209     color = this->color_;
210     return *this;
211 }
212 
213 // *******************************************************************************************
214 // *
215 // *	SetColor
216 // *
217 // *******************************************************************************************
218 //!
219 //!		@param	color	current color to set
220 //!		@return		A reference to this to allow chaining
221 //!
222 // *******************************************************************************************
223 
SetColor(tColor const & color)224 rTextField & rTextField::SetColor( tColor const & color )
225 {
226     this->color_ = color;
227     return *this;
228 }
229 
230 #endif
231 
232