GameKit  0.0.1a
C++ gamedev tools
Font.cpp
Go to the documentation of this file.
1 /*
2  * =====================================================================================
3  *
4  * Filename: Font.cpp
5  *
6  * Description:
7  *
8  * Created: 30/12/2018 19:00:23
9  *
10  * Author: Quentin Bazin, <quent42340@gmail.com>
11  *
12  * =====================================================================================
13  */
14 #include "gk/graphics/Font.hpp"
15 #include "gk/core/Exception.hpp"
16 
17 namespace gk {
18 
19 void Font::loadFont(int ptsize) const {
20  TTF_FontPtr font{TTF_OpenFont(m_filename.c_str(), ptsize), TTF_CloseFont};
21  if (font) {
22  TTF_SetFontKerning(font.get(), m_kerning);
23  TTF_SetFontHinting(font.get(), m_hinting);
24  TTF_SetFontOutline(font.get(), m_outline);
25 
26  m_fonts.emplace(ptsize, std::move(font));
27 
28  // DEBUG((void*)this, "- Loaded new font:", m_filename, ptsize);
29  }
30  else
31  throw EXCEPTION("Failed to load font '" + m_filename + "':", TTF_GetError());
32 }
33 
34 TTF_Font *Font::getFont(int ptsize) const {
35  auto it = m_fonts.find(ptsize);
36  if (it == m_fonts.end())
37  loadFont(ptsize);
38 
39  return m_fonts.at(ptsize).get();
40 }
41 
42 void Font::update() {
43  for (auto &it : m_fonts) {
44  TTF_SetFontKerning(it.second.get(), m_kerning);
45  TTF_SetFontHinting(it.second.get(), m_hinting);
46  TTF_SetFontOutline(it.second.get(), m_outline);
47  }
48 }
49 
50 } // namespace gk
51 
void update()
Update kerning, hinting and outline of all font sizes.
Definition: Font.cpp:42
void loadFont(int ptsize) const
Load a new font size into this font.
Definition: Font.cpp:19
std::unordered_map< int, TTF_FontPtr > m_fonts
SDL font container.
Definition: Font.hpp:114
#define EXCEPTION(args...)
Definition: Exception.hpp:22
std::string m_filename
Font filename.
Definition: Font.hpp:108
int m_hinting
Hinting value.
Definition: Font.hpp:111
int m_kerning
Kerning value.
Definition: Font.hpp:110
TTF_Font * getFont(int ptsize) const
Get the SDL font for a specific font size.
Definition: Font.cpp:34
std::unique_ptr< TTF_Font, decltype(&TTF_CloseFont)> TTF_FontPtr
Definition: Font.hpp:103
int m_outline
Outline value.
Definition: Font.hpp:112