_________ __                 __
        /   _____//  |_____________ _/  |______     ____  __ __  ______
        \_____  \\   __\_  __ \__  \\   __\__  \   / ___\|  |  \/  ___/
        /        \|  |  |  | \// __ \|  |  / __ \_/ /_/  >  |  /\___ \
       /_______  /|__|  |__|  (____  /__| (____  /\___  /|____//____  >
               \/                  \/          \//_____/            \/
    ______________________                           ______________________
                          T H E   W A R   B E G I N S
                   Stratagus - A free fantasy real time strategy game engine

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
font.h
Go to the documentation of this file.
1 // _________ __ __
2 // / _____// |_____________ _/ |______ ____ __ __ ______
3 // \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/
4 // / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ |
5 // /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ >
6 // \/ \/ \//_____/ \/
7 // ______________________ ______________________
8 // T H E W A R B E G I N S
9 // Stratagus - A free fantasy real time strategy game engine
10 //
12 //
13 // (c) Copyright 1998-2005 by Lutz Sammer and Jimmy Salmon
14 //
15 // This program is free software; you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation; only version 2 of the License.
18 //
19 // This program is distributed in the hope that it will be useful,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 // GNU General Public License for more details.
23 //
24 // You should have received a copy of the GNU General Public License
25 // along with this program; if not, write to the Free Software
26 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 // 02111-1307, USA.
28 //
29 
30 #ifndef __FONT_H__
31 #define __FONT_H__
32 
34 
35 /*----------------------------------------------------------------------------
36 -- Documentation
37 ----------------------------------------------------------------------------*/
38 
58 /*----------------------------------------------------------------------------
59 -- Includes
60 ----------------------------------------------------------------------------*/
61 
62 #include <string>
63 #include "color.h"
64 #include "guichan/font.h"
65 
66 /*----------------------------------------------------------------------------
67 -- Declarations
68 ----------------------------------------------------------------------------*/
69 class CGraphic;
70 class CFontColor;
71 
73 class CFont : public gcn::Font
74 {
75 private:
76  explicit CFont(const std::string &ident) :
77  Ident(ident),
78  CharWidth(NULL),
79  G(NULL)
80  {}
81 
82 public:
83  virtual ~CFont();
84 
85  static CFont *New(const std::string &ident, CGraphic *g);
86  static CFont *Get(const std::string &ident);
87 
88  int Height() const;
89  int Width(const std::string &text) const;
90  int Width(const int number) const;
91  bool IsLoaded() const;
92 
93  virtual int getHeight() const { return Height(); }
94  virtual int getWidth(const std::string &text) const { return Width(text); }
95  virtual void drawString(gcn::Graphics *graphics, const std::string &text, int x, int y, bool is_normal = true);
96 
97  void Load();
98  void Reload() const;
99 #if defined(USE_OPENGL) || defined(USE_GLES)
100  void FreeOpenGL();
101 #endif
102  void Clean();
103 
104  CGraphic *GetFontColorGraphic(const CFontColor &fontColor) const;
105 
106  template<bool CLIP>
107  unsigned int DrawChar(CGraphic &g, int utf8, int x, int y, const CFontColor &fc) const;
108 
109  void DynamicLoad() const;
110 
111 private:
112 #if defined(USE_OPENGL) || defined(USE_GLES)
113  void MakeFontColorTextures() const;
114 #endif
115  void MeasureWidths();
116 
117 private:
118  std::string Ident;
119  char *CharWidth;
120  CGraphic *G;
121 };
122 
123 #define MaxFontColors 9
124 
127 {
128 public:
129  explicit CFontColor(const std::string &ident);
130  ~CFontColor();
131 
132  static CFontColor *New(const std::string &ident);
133  static CFontColor *Get(const std::string &ident);
134 
135  std::string Ident;
137 };
138 
139 /*----------------------------------------------------------------------------
140 -- Definitions
141 ----------------------------------------------------------------------------*/
142 
146 #define FontRed "red"
147 #define FontGreen "green"
148 #define FontYellow "yellow"
149 #define FontWhite "white"
150 #define FontGrey "grey"
151 
152 /*----------------------------------------------------------------------------
153 -- Functions
154 ----------------------------------------------------------------------------*/
155 
160 extern CFont &GetSmallFont();
161 extern CFont &GetGameFont();
162 extern bool IsGameFontReady();
163 
164 
166 extern void SetDefaultTextColors(const std::string &normal, const std::string &reverse);
168 extern void GetDefaultTextColors(std::string &normalp, std::string &reversep);
170 extern std::string GetLineFont(unsigned int line, const std::string &s, unsigned int maxlen, const CFont *font);
171 
173 extern int GetHotKey(const std::string &text);
174 
176 extern void LoadFonts();
177 
178 #if defined(USE_OPENGL) || defined(USE_GLES)
179 extern void FreeOpenGLFonts();
182 extern void ReloadFonts();
183 #endif
184 
186 extern void CleanFonts();
187 
188 class CLabel
189 {
190 public:
191  CLabel(const CFont &f, const std::string &nc, const std::string &rc): font(&f)
192  {
193  normal = CFontColor::Get(nc);
194  reverse = CFontColor::Get(rc);
195  }
196  explicit CLabel(const CFont &f);
197 
198  int Height() const { return font->Height(); }
199 
200  void SetFont(const CFont &f) { font = &f; }
201 
202  void SetNormalColor(const std::string &nc) { normal = CFontColor::Get(nc); }
203 
205  int Draw(int x, int y, const char *const text) const;
206  int Draw(int x, int y, const std::string &text) const;
207  int Draw(int x, int y, int number) const;
209  int DrawClip(int x, int y, const char *const text) const;
210  int DrawClip(int x, int y, const std::string &text, bool is_normal = true) const;
211  int DrawClip(int x, int y, int number) const;
213  int DrawReverse(int x, int y, const char *const text) const;
214  int DrawReverse(int x, int y, const std::string &text) const;
215  int DrawReverse(int x, int y, int number) const ;
217  int DrawReverseClip(int x, int y, const char *const text) const;
218  int DrawReverseClip(int x, int y, const std::string &text) const;
219  int DrawReverseClip(int x, int y, int number) const;
220 
221  int DrawCentered(int x, int y, const std::string &text) const;
222  int DrawReverseCentered(int x, int y, const std::string &text) const;
223 private:
224  template <const bool CLIP>
225  int DoDrawText(int x, int y, const char *const text,
226  const size_t len, const CFontColor *fc) const;
227 private:
228  const CFontColor *normal;
229  const CFontColor *reverse;
230  const CFont *font;
231 };
232 
234 
235 #endif // !__FONT_H__
CLabel(const CFont &f, const std::string &nc, const std::string &rc)
Definition: font.h:191
static CFont * Get(const std::string &ident)
Definition: font.cpp:998
void FreeOpenGLFonts()
Free OpenGL fonts.
Definition: font.cpp:925
void ReloadFonts()
Reload OpenGL fonts.
Definition: font.cpp:960
void DynamicLoad() const
Definition: font.cpp:886
int DrawReverseClip(int x, int y, const char *const text) const
Draw reverse text/number clipped.
Definition: font.cpp:663
int DrawCentered(int x, int y, const std::string &text) const
Definition: font.cpp:680
A platform independent color.
Definition: color.h:39
static CFont * New(const std::string &ident, CGraphic *g)
Definition: font.cpp:977
void FreeOpenGL()
Definition: font.cpp:911
static CFontColor * Get(const std::string &ident)
Definition: font.cpp:1046
Definition: graphics.h:97
virtual int getWidth(const std::string &text) const
Definition: font.h:94
void CleanFonts()
Cleanup the font module.
Definition: font.cpp:1079
int DrawReverse(int x, int y, const char *const text) const
Draw reverse text/number unclipped.
Definition: font.cpp:645
std::string GetLineFont(unsigned int line, const std::string &s, unsigned int maxlen, const CFont *font)
Return the 'line' line of the string 's'.
Definition: font.cpp:775
int Height() const
Definition: font.h:198
Definition: font.h:69
#define MaxFontColors
Definition: font.h:123
void GetDefaultTextColors(std::string &normalp, std::string &reversep)
Get the default text colors for normal and reverse text.
Definition: font.cpp:175
Definition: video.h:64
void Load()
Definition: font.cpp:867
int DrawClip(int x, int y, const char *const text) const
Draw text/number clipped.
Definition: font.cpp:619
unsigned int DrawChar(CGraphic &g, int utf8, int x, int y, const CFontColor &fc) const
Definition: font.cpp:441
static CFontColor * New(const std::string &ident)
Definition: font.cpp:1029
Font definition.
Definition: font.h:73
void Reload() const
Definition: font.cpp:935
int Draw(int x, int y, const char *const text) const
Draw text/number unclipped.
Definition: font.cpp:601
void SetFont(const CFont &f)
Definition: font.h:200
bool IsGameFontReady()
Normal font used in game.
Definition: font.cpp:87
int Width(const std::string &text) const
Definition: font.cpp:352
CGraphic * GetFontColorGraphic(const CFontColor &fontColor) const
Definition: font.cpp:462
Font color definition.
Definition: font.h:126
CFont & GetGameFont()
Small font used in stats.
Definition: font.cpp:92
~CFontColor()
Definition: font.cpp:1018
std::string Ident
Definition: font.h:135
int DrawReverseCentered(int x, int y, const std::string &text) const
Definition: font.cpp:687
bool IsLoaded() const
Definition: font.cpp:290
int Height() const
Definition: font.cpp:284
void Clean()
Definition: font.cpp:1055
CFontColor(const std::string &ident)
Definition: font.cpp:1013
Definition: font.h:188
CFont & GetSmallFont()
Definition: font.cpp:78
void SetNormalColor(const std::string &nc)
Definition: font.h:202
virtual int getHeight() const
Definition: font.h:93
void SetDefaultTextColors(const std::string &normal, const std::string &reverse)
true when GameFont is provided
Definition: font.cpp:161
int GetHotKey(const std::string &text)
Get the hot key from a string.
Definition: font.cpp:395
void LoadFonts()
Load and initialize the fonts.
Definition: font.cpp:898
virtual ~CFont()
Definition: font.cpp:409
virtual void drawString(gcn::Graphics *graphics, const std::string &text, int x, int y, bool is_normal=true)
Definition: font.cpp:106
CColor Colors[MaxFontColors]
Definition: font.h:136
(C) Copyright 1998-2012 by The Stratagus Project under the GNU General Public License.
All trademarks and copyrights on this page are owned by their respective owners.