Gamedev Framework (gf) 1.2.0
A C++17 framework for 2D games
ConsoleFont.h
1/*
2 * Gamedev Framework (gf)
3 * Copyright (C) 2016-2022 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 <string_view>
25
26#include "GraphicsApi.h"
27#include "Path.h"
28#include "Span.h"
29#include "Texture.h"
30#include "Vector.h"
31
32namespace gf {
33#ifndef DOXYGEN_SHOULD_SKIP_THIS
34inline namespace v1 {
35#endif
36
43 struct GF_GRAPHICS_API ConsoleFontFormat {
51 };
52
54
58 enum Layout {
61 };
62
64
68 enum Mapping {
73 };
74
76 };
77
84 struct GF_GRAPHICS_API ConsoleFontElement {
85 char16_t character;
86 uint8_t index;
87 };
88
93 struct GF_GRAPHICS_API PredefinedConsoleFontFormat {
98
103 };
104
111 class GF_GRAPHICS_API ConsoleFont {
112 public:
119
127 ConsoleFont(const Image& image, ConsoleFontFormat format, Vector2i size);
128
132 virtual ~ConsoleFont();
133
143 return m_format;
144 }
145
152 return m_size;
153 }
154
161 return m_characterSize;
162 }
163
179 void mapCode(char16_t c, Vector2i position);
180
188 void mapCodeRange(char16_t c, int count, Vector2i position);
189
196 void mapString(std::string_view str, Vector2i position);
197
204
211
218
236 RectI getSubTexture(char16_t c) const;
237
246 RectF getTextureRect(char16_t c) const;
247
253 virtual const BareTexture *getTexture() const = 0;
254
259 protected:
267 void logFormat(const Path& filename) const;
268
278
279 private:
280 std::vector<uint8_t> m_mapping;
281 ConsoleFontFormat m_format;
282 Vector2i m_size;
283 Vector2i m_characterSize;
284 };
285
295 class GF_GRAPHICS_API BitmapConsoleFont : public ConsoleFont {
296 public:
304 BitmapConsoleFont(const Image& image, ConsoleFontFormat format, Vector2i size = { 0u, 0u });
305
313 BitmapConsoleFont(const Path& filename, ConsoleFontFormat format, Vector2i size = { 0u, 0u });
314
315 const BareTexture *getTexture() const override;
316
317 private:
318 AlphaTexture m_texture;
319 };
320
328 class GF_GRAPHICS_API ColoredConsoleFont : public ConsoleFont {
329 public:
337 ColoredConsoleFont(const Image& image, ConsoleFontFormat format, Vector2i size = { 0u, 0u });
338
346 ColoredConsoleFont(const Path& filename, ConsoleFontFormat format, Vector2i size = { 0u, 0u });
347
348 const BareTexture *getTexture() const override;
349
350 private:
351 Texture m_texture;
352 };
353
354
355#ifndef DOXYGEN_SHOULD_SKIP_THIS
356}
357#endif
358}
359
360#endif // GF_CONSOLE_FONT_H
A texture with a single alpha channel.
Definition: Texture.h:427
An image that lives in the graphic memory that can be used for drawing.
Definition: Texture.h:79
A bitmap console font.
Definition: ConsoleFont.h:295
BitmapConsoleFont(const Path &filename, ConsoleFontFormat format, Vector2i size={ 0u, 0u })
Load a console font from a file.
const BareTexture * getTexture() const override
Get the texture of the console font.
BitmapConsoleFont(const Image &image, ConsoleFontFormat format, Vector2i size={ 0u, 0u })
Load a console font from an image.
A colored console font.
Definition: ConsoleFont.h:328
ColoredConsoleFont(const Path &filename, ConsoleFontFormat format, Vector2i size={ 0u, 0u })
Load a console font from a file.
ColoredConsoleFont(const Image &image, ConsoleFontFormat format, Vector2i size={ 0u, 0u })
Load a console font from an image.
const BareTexture * getTexture() const override
Get the texture of the console font.
A console font.
Definition: ConsoleFont.h:111
void mapElement(ConsoleFontElement element)
Map an element in the font.
ConsoleFontFormat getFormat() const
Get the format of the font.
Definition: ConsoleFont.h:142
void clearMapping()
Clear the mapping.
void mapCodeRange(char16_t c, int count, Vector2i position)
Map a range of characters to consecutive positions in the font.
void logFormat(const Path &filename) const
Print the format of the font.
Vector2i getColorKeyPosition() const
Get the color key position.
Vector2i getCharacterSize() const
Get the size of the characters.
Definition: ConsoleFont.h:160
ConsoleFont(const Image &image, ConsoleFontFormat format, Vector2i size)
Constructor.
void mapElements(Span< const ConsoleFontElement > elements)
Map some elements in the font.
void mapString(std::string_view str, Vector2i position)
Map characters from a string to consecutive positions in the font.
RectI getSubTexture(char16_t c) const
Get the sub-texture rectangle.
RectF getTextureRect(char16_t c) const
Get the texture rectangle for a character.
virtual const BareTexture * getTexture() const =0
Get the texture of the console font.
virtual ~ConsoleFont()
Destructor.
void mapCode(char16_t c, Vector2i position)
Map a character to a position in the font.
Vector2i getSize() const
Get the size of the fonts.
Definition: ConsoleFont.h:151
ConsoleFont()
Constructor.
Class for loading, manipulating and saving images.
Definition: Image.h:81
A span.
Definition: Span.h:414
A texture for colored images.
Definition: Texture.h:313
std::filesystem::path Path
A path in the filesystem.
Definition: Path.h:40
The namespace for gf classes.
A console font element.
Definition: ConsoleFont.h:84
uint8_t index
The index in the font.
Definition: ConsoleFont.h:86
char16_t character
The represented character.
Definition: ConsoleFont.h:85
A console font format.
Definition: ConsoleFont.h:43
Layout
A layout.
Definition: ConsoleFont.h:58
@ InRow
In row.
Definition: ConsoleFont.h:60
@ InColumn
In column.
Definition: ConsoleFont.h:59
Mapping mapping
The mapping of the font.
Definition: ConsoleFont.h:75
Transparency transparency
The transparency method of the font.
Definition: ConsoleFont.h:53
Layout layout
The layout of the font.
Definition: ConsoleFont.h:63
Mapping
A mapping.
Definition: ConsoleFont.h:68
@ Custom
A user-defined mapping.
Definition: ConsoleFont.h:72
@ ModifiedCodePage437
Code page 437 modified by libtcod.
Definition: ConsoleFont.h:70
@ Special
The special libtcod mapping.
Definition: ConsoleFont.h:71
@ CodePage437
Code page 437.
Definition: ConsoleFont.h:69
Transparency
A transparency method.
Definition: ConsoleFont.h:47
@ ColorKey
Given by a color key.
Definition: ConsoleFont.h:50
@ Alpha
Given by the alpha channel of the image.
Definition: ConsoleFont.h:48
@ Grayscale
Given by the level of gray (bitmap font only)
Definition: ConsoleFont.h:49
Predefined console font formats.
Definition: ConsoleFont.h:93