1 // This file belongs to the "MiniCore" game engine.
2 // Copyright (C) 2012 Jussi Lind <jussi.lind@iki.fi>
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
8 //
9 // This program 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 General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 // MA  02110-1301, USA.
18 //
19 
20 #ifndef MCTEXTUREFONT_HH
21 #define MCTEXTUREFONT_HH
22 
23 #include "mcglshaderprogram.hh"
24 #include "mctextureglyph.hh"
25 
26 #include <memory>
27 #include <unordered_map>
28 #include <vector>
29 
30 class MCSurface;
31 
32 //! Textured monospace font.
33 class MCTextureFont
34 {
35 public:
36     /*! Constructor.
37      * \param surface Reference to the surface object containing
38      * all the monospace glyphs. */
39     MCTextureFont(std::shared_ptr<MCSurface> surface);
40 
41     /*! Add a mapping from given glyph to given MCTextureGlyph.
42      *  MCTextureGlyph includes e.g. uv-coordinates. */
43     void addGlyphMapping(wchar_t glyph, MCTextureGlyph textureGlyph);
44 
45     /*! Return an MCTextureGlyph for the given glyph. Default containing
46      *  the whole texture is returned if not found. */
47     MCTextureGlyph & glyph(wchar_t glyph);
48 
49     /*! Set a fallback glyph for the given glyph. Must be call AFTER
50      *  all other glyphs are added. */
51     void setGlyphFallback(wchar_t glyph, wchar_t fallback);
52 
53     //! Return the associated surface.
54     std::shared_ptr<MCSurface> surface() const;
55 
56     //! Set the shader program to be used.
57     void setShaderProgram(MCGLShaderProgramPtr program);
58 
59     //! Set the shader program to be used for 2d shadows.
60     void setShadowShaderProgram(MCGLShaderProgramPtr program);
61 
62     void setDensities(float xDensity = 1.0f, float yDensity = 1.0f);
63 
64     float xDensity() const;
65 
66     float yDensity() const;
67 
68 private:
69     MCTextureGlyph m_default;
70 
71     using GlyphHash = std::unordered_map<wchar_t, MCTextureGlyph>;
72     GlyphHash m_glyphs;
73 
74     using GlyphLookUp = std::vector<MCTextureGlyph>;
75     GlyphLookUp m_glyphLookUp;
76 
77     float m_xDensity;
78 
79     float m_yDensity;
80 
81     std::shared_ptr<MCSurface> m_surface;
82 };
83 
84 #endif // MCTEXTUREFONT_HH
85