Gamedev Framework (gf)  0.19.0
A C++17 framework for 2D games
ConsoleFont.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2021 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 
32 namespace gf {
33 #ifndef DOXYGEN_SHOULD_SKIP_THIS
34 inline namespace v1 {
35 #endif
36 
43  struct GF_GRAPHICS_API ConsoleFontFormat {
47  enum Transparency {
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:
118  ConsoleFont();
119 
127  ConsoleFont(const Image& image, ConsoleFontFormat format, Vector2i size);
128 
132  virtual ~ConsoleFont();
133 
143  return m_format;
144  }
145 
151  Vector2i getSize() const {
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 
203  void mapElement(ConsoleFontElement element);
204 
210  void mapElements(Span<const ConsoleFontElement> elements);
211 
217  void clearMapping();
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 
277  Vector2i getColorKeyPosition() const;
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
In row.
Definition: ConsoleFont.h:60
Given by a color key.
Definition: ConsoleFont.h:50
Transparency transparency
The transparency method of the font.
Definition: ConsoleFont.h:53
A span.
Definition: Span.h:36
A bitmap console font.
Definition: ConsoleFont.h:295
char16_t character
The represented character.
Definition: ConsoleFont.h:85
Layout layout
The layout of the font.
Definition: ConsoleFont.h:63
The special libtcod mapping.
Definition: ConsoleFont.h:71
Mapping
A mapping.
Definition: ConsoleFont.h:68
A texture for colored images.
Definition: Texture.h:313
std::filesystem::path Path
A path in the filesystem.
Definition: Path.h:40
Given by the level of gray (bitmap font only)
Definition: ConsoleFont.h:49
Transparency
A transparency method.
Definition: ConsoleFont.h:47
Layout
A layout.
Definition: ConsoleFont.h:58
A console font element.
Definition: ConsoleFont.h:84
In column.
Definition: ConsoleFont.h:59
Class for loading, manipulating and saving images.
Definition: Image.h:80
The namespace for gf classes.
Definition: Action.h:35
An image that lives in the graphic memory that can be used for drawing.
Definition: Texture.h:79
A colored console font.
Definition: ConsoleFont.h:328
Given by the alpha channel of the image.
Definition: ConsoleFont.h:48
A console font.
Definition: ConsoleFont.h:111
Vector2i getCharacterSize() const
Get the size of the characters.
Definition: ConsoleFont.h:160
Predefined console font formats.
Definition: ConsoleFont.h:93
Code page 437.
Definition: ConsoleFont.h:69
A user-defined mapping.
Definition: ConsoleFont.h:72
Mapping mapping
The mapping of the font.
Definition: ConsoleFont.h:75
Vector2i getSize() const
Get the size of the fonts.
Definition: ConsoleFont.h:151
A texture with a single alpha channel.
Definition: Texture.h:395
Code page 437 modified by libtcod.
Definition: ConsoleFont.h:70
ConsoleFontFormat getFormat() const
Get the format of the font.
Definition: ConsoleFont.h:142
uint8_t index
The index in the font.
Definition: ConsoleFont.h:86
A console font format.
Definition: ConsoleFont.h:43