Gamedev Framework (gf)  0.6.0
A C++11 framework for 2D games
ConsoleFont.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2017 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_CONSOLE_FONT_H
22 #define GF_CONSOLE_FONT_H
23 
24 #include "ArrayRef.h"
25 #include "Path.h"
26 #include "Portability.h"
27 #include "StringRef.h"
28 #include "Texture.h"
29 #include "Vector.h"
30 
31 namespace gf {
32 #ifndef DOXYGEN_SHOULD_SKIP_THIS
33 inline namespace v1 {
34 #endif
35 
36  /**
37  * @ingroup game
38  * @brief A console font format
39  *
40  * @sa gf::ConsoleFont, gf::PredefinedConsoleFontFormat
41  */
43  /**
44  * @brief A transparency method
45  */
46  enum Transparency {
47  Alpha, ///< Given by the alpha channel of the image
48  Grayscale, ///< Given by the level of gray (bitmap font only)
49  ColorKey, ///< Given by a color key
50  };
51 
52  Transparency transparency; ///< The transparency method of the font
53 
54  /**
55  * @brief A layout
56  */
57  enum Layout {
58  InColumn, ///< In column
59  InRow, ///< In row
60  };
61 
62  Layout layout; ///< The layout of the font
63 
64  /**
65  * @brief A mapping
66  */
67  enum Mapping {
68  CodePage437, ///< Code page 437
69  ModifiedCodePage437, ///< Code page 437 modified by libtcod
70  Special, ///< The special libtcod mapping
71  Custom, ///< A user-defined mapping
72  };
73 
74  Mapping mapping; ///< The mapping of the font
75  };
76 
77  /**
78  * @ingroup game
79  * @brief A console font element
80  *
81  * @sa gf::ConsoleFont
82  */
84  char16_t character; ///< The represented character
85  uint8_t index; ///< The index in the font
86  };
87 
88  /**
89  * @ingroup game
90  * @brief Predefined console font formats
91  */
93  /**
94  * @brief The libtcod format
95  */
97 
98  /**
99  * @brief The Dwarf Fortress format
100  */
102  };
103 
104  /**
105  * @ingroup game
106  * @brief A console font
107  *
108  * @sa gf::Console, gf::ConsoleFontFormat, gf::ConsoleFontElement
109  */
111  public:
112  /**
113  * @brief Constructor
114  *
115  * No font is loaded.
116  */
117  ConsoleFont();
118 
119  /**
120  * @brief Destructor
121  */
122  virtual ~ConsoleFont();
123 
124  /**
125  * @name Properties
126  * @{
127  */
128 
129  /**
130  * @brief Get the format of the font
131  */
133  return m_format;
134  }
135 
136  /**
137  * @brief Get the size of the fonts
138  *
139  * @returns The size in characters of the font
140  */
141  Vector2u getSize() const {
142  return m_size;
143  }
144 
145  /**
146  * @brief Get the size of the characters
147  *
148  * @returns The size in pixels of the characters
149  */
150  Vector2u getCharacterSize() const {
151  return m_characterSize;
152  }
153 
154  /**
155  * @}
156  */
157 
158  /**
159  * @name Mapping
160  * @{
161  */
162 
163  /**
164  * @brief Map a character to a position in the font
165  *
166  * @param c A character
167  * @param position The position, in characters, of the character
168  */
169  void mapCode(char16_t c, Vector2u position);
170 
171  /**
172  * @brief Map a range of characters to consecutive positions in the font
173  *
174  * @param c The first character in the range
175  * @param count The number of character in the range
176  * @param position The position, in characters, of the first character
177  */
178  void mapCodeRange(char16_t c, unsigned count, Vector2u position);
179 
180  /**
181  * @brief Map characters from a string to consecutive positions in the font
182  *
183  * @param str The string with the characters
184  * @param position The position, in characters, of the first character of the string
185  */
186  void mapString(StringRef str, Vector2u position);
187 
188  /**
189  * @brief Map an element in the font
190  *
191  * @param element A font element
192  */
193  void mapElement(ConsoleFontElement element);
194 
195  /**
196  * @brief Map some elements in the font
197  *
198  * @param elements The font elements
199  */
200  void mapElements(ArrayRef<ConsoleFontElement> elements);
201 
202  /**
203  * @brief Clear the mapping
204  *
205  * All characters are mapped to the first position in the font.
206  */
207  void clearMapping();
208 
209  /**
210  * @}
211  */
212 
213  /**
214  * @name Texture
215  * @{
216  */
217 
218  /**
219  * @brief Get the sub-texture rectangle
220  *
221  * @param c A character
222  * @returns The corresponding sub-texture rectangle
223  *
224  * @sa getTexture(), getTextureRect()
225  */
226  RectU getSubTexture(char16_t c) const;
227 
228  /**
229  * @brief Get the texture rectangle for a character
230  *
231  * @param c A character
232  * @returns The corresponding texture rectangle
233  *
234  * @sa getTexture(), getSubTexture()
235  */
236  RectF getTextureRect(char16_t c) const;
237 
238  /**
239  * @brief Get the texture of the console font
240  *
241  * @returns The texture of the font
242  */
243  virtual const BareTexture *getTexture() const = 0;
244 
245  /**
246  * @}
247  */
248 
249  protected:
250  /**
251  * @brief Set the format of the font
252  *
253  * This function sets the format of the font and computes the actual size
254  * in characters of the font and the size of the characters.
255  *
256  * @param format The format of the font
257  * @param size The size given by the user, may be @f$ (0, 0) @f$
258  * @param imageSize The size of the source image
259  */
260  bool setFormatAndComputeSizes(ConsoleFontFormat format, Vector2u size, Vector2u imageSize);
261 
262  /**
263  * @brief Print the format of the font
264  *
265  * This function logs the format of the font
266  *
267  * @param filename The filename of the source image
268  */
269  void logFormat(const Path& filename) const;
270 
271  /**
272  * @brief Get the color key position
273  *
274  * This function gives the position of a pixel of the space character in
275  * the font.
276  *
277  * @returns A pixel position where to find the color key
278  */
279  Vector2u getColorKeyPosition() const;
280 
281  private:
282  std::vector<uint8_t> m_mapping;
283  ConsoleFontFormat m_format;
284  Vector2u m_size;
285  Vector2u m_characterSize;
286  };
287 
288  /**
289  * @ingroup game
290  * @brief A bitmap console font
291  *
292  * This class is based on [libtcod](http://roguecentral.org/doryen/libtcod/)
293  * console font handling. It supports bitmap fonts provided by libtcod.
294  *
295  * @sa gf::ConsoleFont
296  */
298  public:
299 
300  virtual const BareTexture *getTexture() const override;
301 
302  /**
303  * @brief Load a console font from a file
304  *
305  * @param filename The name of the file
306  * @param format The format of the font
307  * @param size The size, in characters, of the font
308  * @returns True if the font was correctly loaded
309  */
310  bool loadFromFile(const Path& filename, ConsoleFontFormat format, Vector2u size = { 0u, 0u });
311 
312  private:
313  AlphaTexture m_texture;
314  };
315 
316  /**
317  * @ingroup game
318  * @brief A colored console font
319  *
320  * This class handles colored fonts such as the fonts created for
321  * [Dwarf Fortress](http://dwarffortresswiki.org/Tileset_repository).
322  */
324  public:
325 
326  virtual const BareTexture *getTexture() const override;
327 
328  /**
329  * @brief Load a console font from a file
330  *
331  * @param filename The name of the file
332  * @param format The format of the font
333  * @param size The size, in characters, of the font
334  * @returns True if the font was correctly loaded
335  */
336  bool loadFromFile(const Path& filename, ConsoleFontFormat format, Vector2u size = { 0u, 0u });
337 
338  private:
339  Texture m_texture;
340  };
341 
342 
343 #ifndef DOXYGEN_SHOULD_SKIP_THIS
344 }
345 #endif
346 }
347 
348 #endif // GF_CONSOLE_FONT_H
void mapElement(ConsoleFontElement element)
Map an element in the font.
In row.
Definition: ConsoleFont.h:59
void mapCode(char16_t c, Vector2u position)
Map a character to a position in the font.
Vector2u getCharacterSize() const
Get the size of the characters.
Definition: ConsoleFont.h:150
bool setFormatAndComputeSizes(ConsoleFontFormat format, Vector2u size, Vector2u imageSize)
Set the format of the font.
Given by a color key.
Definition: ConsoleFont.h:49
virtual const BareTexture * getTexture() const override
Get the texture of the console font.
void mapElements(ArrayRef< ConsoleFontElement > elements)
Map some elements in the font.
void clearMapping()
Clear the mapping.
Vector2u getSize() const
Get the size of the fonts.
Definition: ConsoleFont.h:141
RectU getSubTexture(char16_t c) const
Get the sub-texture rectangle.
Transparency transparency
The transparency method of the font.
Definition: ConsoleFont.h:52
A bitmap console font.
Definition: ConsoleFont.h:297
char16_t character
The represented character.
Definition: ConsoleFont.h:84
Layout layout
The layout of the font.
Definition: ConsoleFont.h:62
bool loadFromFile(const Path &filename, ConsoleFontFormat format, Vector2u size={ 0u, 0u })
Load a console font from a file.
The special libtcod mapping.
Definition: ConsoleFont.h:70
void logFormat(const Path &filename) const
Print the format of the font.
virtual ~ConsoleFont()
Destructor.
void mapCodeRange(char16_t c, unsigned count, Vector2u position)
Map a range of characters to consecutive positions in the font.
Mapping
A mapping.
Definition: ConsoleFont.h:67
A texture for colored images.
Definition: Texture.h:339
RectF getTextureRect(char16_t c) const
Get the texture rectangle for a character.
Given by the level of gray (bitmap font only)
Definition: ConsoleFont.h:48
Transparency
A transparency method.
Definition: ConsoleFont.h:46
Layout
A layout.
Definition: ConsoleFont.h:57
A console font element.
Definition: ConsoleFont.h:83
In column.
Definition: ConsoleFont.h:58
The namespace for gf classes.
Definition: Action.h:34
An image that lives in the graphic memory that can be used for drawing.
Definition: Texture.h:67
A colored console font.
Definition: ConsoleFont.h:323
A constant reference to an array and its size.
Definition: ArrayRef.h:42
virtual const BareTexture * getTexture() const override
Get the texture of the console font.
virtual const BareTexture * getTexture() const =0
Get the texture of the console font.
Given by the alpha channel of the image.
Definition: ConsoleFont.h:47
A console font.
Definition: ConsoleFont.h:110
Predefined console font formats.
Definition: ConsoleFont.h:92
void mapString(StringRef str, Vector2u position)
Map characters from a string to consecutive positions in the font.
Code page 437.
Definition: ConsoleFont.h:68
A constant reference to a string and its size.
Definition: StringRef.h:41
A user-defined mapping.
Definition: ConsoleFont.h:71
Mapping mapping
The mapping of the font.
Definition: ConsoleFont.h:74
Vector2u getColorKeyPosition() const
Get the color key position.
bool loadFromFile(const Path &filename, ConsoleFontFormat format, Vector2u size={ 0u, 0u })
Load a console font from a file.
A texture with a single alpha channel.
Definition: Texture.h:474
#define GF_API
Definition: Portability.h:35
Code page 437 modified by libtcod.
Definition: ConsoleFont.h:69
ConsoleFont()
Constructor.
ConsoleFontFormat getFormat() const
Get the format of the font.
Definition: ConsoleFont.h:132
uint8_t index
The index in the font.
Definition: ConsoleFont.h:85
static constexpr ConsoleFontFormat Libtcod
The libtcod format.
Definition: ConsoleFont.h:96
static constexpr ConsoleFontFormat DwarfFortress
The Dwarf Fortress format.
Definition: ConsoleFont.h:101
A console font format.
Definition: ConsoleFont.h:42