Bubba-3D  0.9.0
Awesome game engine!
FontManager.h
1 /*
2  * This file is part of Bubba-3D.
3  *
4  * Bubba-3D is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Bubba-3D is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with Bubba-3D. If not, see http://www.gnu.org/licenses/.
16  */
17 //
18 // Created by simon on 2016-03-11.
19 //
20 
21 #pragma once
22 
23 #include <unordered_map>
24 #include <GL/glew.h>
25 #include <functional>
26 
27 #include <ft2build.h>
28 #include FT_FREETYPE_H
29 
30 
31 class Font;
32 
36 class FontManager {
37 
38 public:
39 
47  Font* loadAndFetchFont(std::string fontFace, int pixelSize);
48 
56  GLuint* getTex() ;
57 
61  static FontManager* getInstance();
62 
63 protected:
67  virtual void loadFont(std::string fontFace, int pixelSize);
68 
69 private:
70  FontManager();
71  ~FontManager();
72 
77  struct FontDefinition{
78  std::string face;
79  int pixelSize;
80  FontDefinition(std::string face, int pixelSize);
81  FontDefinition(){}
82  bool operator == (FontDefinition fd) const;
83  size_t hash();
84  std::string getFace() const;
85  int getPixelSize() const;
86  };
87 
91  struct FontDefHash
92  {
93  std::size_t operator()(FontDefinition const& fd) const
94  {
95  std::size_t h1 = std::hash<std::string>()(fd.getFace());
96  std::size_t h2 = std::hash<int>()(fd.getPixelSize());
97  return h1 ^ (h2 << 1); // or use boost::hash_combine
98  }
99  };
100 
104  typedef std::unordered_map<FontDefinition,Font*,FontDefHash> fontMap;
105 
106  void iterateGlyphs(FontDefinition def, unsigned int* width, unsigned int* height);
107  void drawGlyphs();
108  void initTexture();
109  GLuint* getTex(bool force) ;
110 
111  fontMap loadedFonts;
112  FT_Library* ft_library;
113  unsigned int atlasWidth = 0, atlasHeight = 0;
114  bool initiated = false;
115 
116 };
Definition: Font.h:27
Font * loadAndFetchFont(std::string fontFace, int pixelSize)
Definition: FontManager.cpp:74
virtual void loadFont(std::string fontFace, int pixelSize)
Definition: FontManager.cpp:84
GLuint * getTex()
Definition: FontManager.cpp:40
static FontManager * getInstance()
Definition: FontManager.cpp:64