hx3d  1
2D/3D Simple Game Framework
font.cpp
1 /*
2  Font.
3  Copyright (C) 2015 Denis BOURGE
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
21 #include "hx3d/graphics/font.hpp"
22 
23 #include "hx3d/graphics/shader.hpp"
24 
25 #include "hx3d/core/core.hpp"
26 
27 #include "hx3d/utils/assets.hpp"
28 #include "hx3d/utils/file.hpp"
29 #include "hx3d/utils/log.hpp"
30 
31 #include <utility>
32 
33 namespace hx3d {
34 namespace graphics {
35 
36 Font::Font(std::string fontPath, int characterSize):
37  shader(Core::Assets()->get<Shader>("text")) {
38  file = File::loadBinaryFile(fontPath);
39 
40  defaultSize = characterSize;
41  createFontSize(characterSize);
42 }
43 
44 Font::~Font() {
45  // Cleanup fonts and atlases
46 }
47 
48 void Font::createFontSize(int size) {
49  if (data.find(size) == data.end()) {
50  texture_atlas_t* atlas = texture_atlas_new(512, 512, 1);
51  texture_font_t* font = texture_font_new_from_memory(atlas, size, file->getData(), file->getSize());
52 
53  Data d;
54  d.atlas = atlas;
55  d.font = font;
56 
57  data.emplace(std::make_pair(size, d));
58  }
59 }
60 
62  if (data.find(size) == data.end()) {
63  createFontSize(size);
64  }
65 
66  return data[size];
67 }
68 
69 unsigned int Font::getDefaultSize() const {
70  return defaultSize;
71 }
72 
74  return shader;
75 }
76 
77 } /* graphics */
78 } /* hx3d */
unsigned int getDefaultSize() const
Get the default font size.
Definition: font.cpp:69
Data & getFontData(int size)
Get internal font data from a size.
Definition: font.cpp:61
hx3d framework namespace
Definition: audio.hpp:26
Internal font data.
Definition: font.hpp:45
texture_atlas_t * atlas
Internal atlas.
Definition: font.hpp:49
Centralized framework management.
Definition: core.hpp:50
const Ptr< Shader > & getShader()
Get the font shader.
Definition: font.cpp:73
Shader definition class.
Definition: shader.hpp:42
Font(std::string fontPath, int characterSize)
Build a font from a path and a character size.
Definition: font.cpp:36
static Ptr< File > loadBinaryFile(std::string path)
Load a binary file from a path.
Definition: file.cpp:62
std::shared_ptr< T > Ptr
Quick-typing shared ptr.
Definition: ptr.hpp:34
void createFontSize(int size)
Generate a font in cache.
Definition: font.cpp:48