BitmapFont.hpp
1 /******************************************************************************
2 
3  nomlib - C++11 cross-platform game engine
4 
5 Copyright (c) 2013, Jeffrey Carpenter <jeffrey.carp@gmail.com>
6 All rights reserved.
7 
8 Portions Copyright (c) 2004-2013 Lazy Foo' Productions [1]
9 All rights reserved.
10 
11 Redistribution and use in source and binary forms, with or without
12 modification, are permitted provided that the following conditions are met:
13 
14 1. Redistributions of source code must retain the above copyright notice, this
15  list of conditions and the following disclaimer.
16 2. Redistributions in binary form must reproduce the above copyright notice,
17  this list of conditions and the following disclaimer in the documentation
18  and/or other materials provided with the distribution.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
24 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 1. http://lazyfoo.net/SDL_tutorials/lesson30/index.php
32 
33 ******************************************************************************/
34 #ifndef NOMLIB_SDL2_BITMAP_FONT_HEADERS
35 #define NOMLIB_SDL2_BITMAP_FONT_HEADERS
36 
37 #include <iostream>
38 #include <string>
39 #include <sstream>
40 
41 #include "nomlib/config.hpp"
42 #include "nomlib/graphics/IFont.hpp"
43 #include "nomlib/math/Coords.hpp"
44 #include "nomlib/math/Rect-inl.hpp"
45 #include "nomlib/math/Color.hpp"
46 #include "nomlib/graphics/Texture.hpp"
47 #include "nomlib/SDL_helpers.hpp"
48 
49 //#define NOM_DEBUG_SDL_BITMAP_FONT
50 
51 namespace nom {
52 
53 class BitmapFont: public IFont
54 {
55  public:
57  BitmapFont ( void );
58 
60  ~BitmapFont ( void );
61 
63  IFont::SharedPtr clone ( void ) const;
64 
66  const std::string& getText ( void ) const;
67 
69  void setText ( const std::string& text );
70 
78  int32 getFontWidth ( void ) const;
79 
86  int32 getFontHeight ( void ) const;
87 
88  const Color& getColor ( void ) const;
89  const Coords& getPosition ( void ) const;
90 
93  uint32 getSpacing ( void ) const;
94 
99  void setSpacing ( uint32 spaces );
100 
103  uint32 getNewline ( void ) const;
104 
105  IFont::TextAlignment getTextJustification ( void ) const;
106 
108  void setNewline ( uint32_t newline );
109 
110  IFont::FontStyle getFontStyle ( void ) const;
111  void setFontStyle ( int32 style, uint8 options = 150 );
112 
114  //RenderStyle getRenderingStyle ( void ) const;
115 
117  //void setRenderingStyle ( enum RenderStyle );
118 
119  void setColor ( const Color& color );
120  void setPosition ( const Coords& coords );
121 
125  void setTextJustification ( IFont::TextAlignment alignment );
126 
127  const Coords findGlyph ( const std::string& glyph );
128 
132  bool load (
133  const std::string& filename, const Color& colorkey,
134  bool use_cache = false
135  );
136 
137  void update ( void );
138 
141  void draw ( RenderTarget target ) const;
142 
147  bool resize ( enum ResizeAlgorithm scaling_algorithm );
148 
149  private:
152  bool rebuild ( void );
153 
154  int32 sheet_width;
155  int32 sheet_height;
156 
159 
161  Coords chars[256];
162 
165 
167  uint32 newline;
168 
174  uint32 spacing;
175 
177  std::string text_buffer;
178 
181 
184 
185  enum IFont::TextAlignment text_alignment;
186 
187  enum IFont::RenderStyle rendering;
188 };
189 
190  namespace priv {
191 
194 void Free_BitmapFont ( BitmapFont* ptr );
195 
196 
197  } // namespace priv
198 } // namespace nom
199 
200 #endif // include guard defined
uint32 getNewline(void) const
Definition: BitmapFont.cpp:186
~BitmapFont(void)
Default destructor.
Definition: BitmapFont.cpp:57
std::string text_buffer
holds contents of text as a string
Definition: BitmapFont.hpp:177
BitmapFont(void)
Default constructor.
Definition: BitmapFont.cpp:38
uint32 getSpacing(void) const
Definition: BitmapFont.cpp:176
bool rebuild(void)
Definition: BitmapFont.cpp:246
void setTextJustification(IFont::TextAlignment alignment)
Definition: BitmapFont.cpp:440
Color color
Not implemented (yet)
Definition: BitmapFont.hpp:183
RenderStyle
Text rendering qualities.
Definition: IFont.hpp:71
enum IFont::FontStyle text_style
Current text effect set.
Definition: BitmapFont.hpp:180
void setText(const std::string &text)
Set a new text string for drawing; defaults to \0.
Definition: BitmapFont.cpp:171
Coords chars[256]
individual chars positioning offsets within bitmap_font
Definition: BitmapFont.hpp:161
void setColor(const Color &color)
Not implemented.
Definition: BitmapFont.cpp:161
Texture bitmap_font
pointer reference holding our bitmap font image sheet
Definition: BitmapFont.hpp:158
const std::string & getText(void) const
Obtains set text string buffer; defaults to \0.
Definition: BitmapFont.cpp:67
IFont::SharedPtr clone(void) const
Return a std::shared_ptr copy of this instance.
Definition: BitmapFont.cpp:62
Coords coords
Positioning coordinates.
Definition: BitmapFont.hpp:164
FontStyle
Text effect styling.
Definition: IFont.hpp:48
void update(void)
Definition: BitmapFont.cpp:455
int32 getFontHeight(void) const
Definition: BitmapFont.cpp:113
void draw(RenderTarget target) const
Definition: BitmapFont.cpp:460
Coordinate values (x, y, z, width, height ) wrapper.
Definition: Coords.hpp:44
bool resize(enum ResizeAlgorithm scaling_algorithm)
Definition: BitmapFont.cpp:530
void setSpacing(uint32 spaces)
Definition: BitmapFont.cpp:181
uint32 newline
height (in pixels) to offset when newline carriage char is encountered
Definition: BitmapFont.hpp:167
void setNewline(uint32_t newline)
Set new text character spacing height offsets in pixels.
Definition: BitmapFont.cpp:196
int32 getFontWidth(void) const
Definition: BitmapFont.cpp:72
bool load(const std::string &filename, const Color &colorkey, bool use_cache=false)
Definition: BitmapFont.cpp:415
void setFontStyle(int32 style, uint8 options=150)
Definition: BitmapFont.cpp:206
Utility class container for RGBA colors.
Definition: Color.hpp:43