34 #include "nomlib/graphics/BitmapFont.hpp"
40 NOM_LOG_TRACE ( NOM );
44 this->text_alignment = TextAlignment::MiddleLeft;
45 this->rendering = RenderStyle::Solid;
49 this->
coords.setPosition ( 0, 0 );
51 for (
unsigned int idx = 0; idx < 256; idx++ )
59 NOM_LOG_TRACE ( NOM );
64 return IFont::SharedPtr (
new BitmapFont ( *
this ), priv::Free_BitmapFont );
76 for ( uint32 char_pos = 0; char_pos < this->
text_buffer.length(); char_pos++ )
83 #if defined ( NOM_DEBUG_SDL_BITMAP_FONT )
84 NOM_DUMP_VAR ( char_pos );
86 NOM_DUMP_VAR ( text_width );
96 uint8 ascii_char =
static_cast<uchar
> ( this->
text_buffer [ char_pos ] );
97 text_width += this->
chars [ ascii_char ].width + 1;
100 #if defined ( NOM_DEBUG_SDL_BITMAP_FONT )
101 NOM_DUMP_VAR ( char_pos );
103 NOM_DUMP_VAR ( this->
chars [ ascii_char ].width + 1 );
104 NOM_DUMP_VAR ( text_width );
115 int32 text_height = 0;
117 for ( uint32 char_pos = 0; char_pos < this->
text_buffer.length(); char_pos++ )
124 #if defined ( NOM_DEBUG_SDL_BITMAP_FONT )
125 NOM_DUMP_VAR ( char_pos );
127 NOM_DUMP_VAR ( text_height );
133 uint8 ascii_char =
static_cast<uchar
> ( this->
text_buffer [ char_pos ] );
135 text_height = this->
chars [ ascii_char ].height;
138 #if defined ( NOM_DEBUG_SDL_BITMAP_FONT )
139 NOM_DUMP_VAR ( char_pos );
141 NOM_DUMP_VAR ( this->
chars [ ascii_char ].height );
142 NOM_DUMP_VAR ( text_height );
151 const Color& BitmapFont::getColor (
void )
const
156 const Coords& BitmapFont::getPosition (
void )
const
166 void BitmapFont::setPosition (
const Coords& coords )
191 IFont::TextAlignment BitmapFont::getTextJustification (
void )
const
193 return this->text_alignment;
212 case FontStyle::Regular:
213 case FontStyle::Bold:
214 case FontStyle::Italic:
215 case FontStyle::Underlined:
220 case FontStyle::Faded:
224 if ( this->
bitmap_font.set_alpha ( options ) == true )
248 uint32 tile_width = 0;
249 uint32 tile_height = 0;
252 uint32 currentChar = 0;
253 uint32 background_color = 0;
259 NOM_LOG_ERR ( NOM,
"Could not lock Texture." );
272 for ( int32 rows = 0; rows < this->sheet_width; rows++ )
274 for ( int32 cols = 0; cols < this->sheet_height; cols++ )
277 this->
chars[ currentChar ].setPosition ( tile_width * cols, tile_height * rows );
278 this->
chars[ currentChar ].setSize ( tile_width, tile_height );
281 for ( uint32 pCol = 0; pCol < tile_width; pCol++ )
284 for ( uint32 pRow = 0; pRow < tile_height; pRow++ )
287 int pX = ( tile_width * cols ) + pCol;
288 int pY = ( tile_height * rows ) + pRow;
294 this->
chars[ currentChar ].x = pX;
304 for ( int32 pCol_w = tile_width - 1; pCol_w >= 0; pCol_w-- )
307 for ( uint32 pRow_w = 0; pRow_w < tile_height; pRow_w++ )
310 uint32 pX = ( tile_width * cols ) + pCol_w;
311 uint32 pY = ( tile_height * rows ) + pRow_w;
317 this->
chars[ currentChar ].width = ( pX - this->
chars[ currentChar ].x ) + 1;
321 pRow_w = tile_height;
327 for ( uint32 pRow = 0; pRow < tile_height; pRow++ )
330 for ( uint32 pCol = 0; pCol < tile_width; pCol++ )
333 uint32 pX = ( tile_width * cols ) + pCol;
334 uint32 pY = ( tile_height * rows ) + pRow;
354 if ( currentChar ==
'A' )
357 for ( int32 pRow = tile_height - 1; pRow >= 0; pRow-- )
360 for ( uint32 pCol = 0; pCol < tile_width; pCol++ )
363 unsigned int pX = ( tile_width * cols ) + pCol;
364 unsigned int pY = ( tile_height * rows ) + pRow;
387 this->
spacing = tile_width / 2;
393 for ( uint32 t = 0; t < 256; t++ )
395 this->
chars[ t ].y += top;
396 this->
chars[ t ].height -= top;
400 #if defined ( NOM_DEBUG_SDL_BITMAP_FONT )
401 for ( uint32 pos = 0; pos < 256; pos++ )
403 NOM_DUMP_VAR ( pos );
404 NOM_DUMP_VAR ( static_cast<uchar> ( pos ) );
405 NOM_DUMP_VAR ( this->
chars [ pos ].x );
406 NOM_DUMP_VAR ( this->
chars [ pos ].y );
407 NOM_DUMP_VAR ( this->
chars [ pos ].width );
408 NOM_DUMP_VAR ( this->
chars [ pos ].height );
421 NOM_LOG_ERR ( NOM,
"Could not load bitmap font image file: " + filename );
431 if ( this->
rebuild() ==
false )
433 NOM_LOG_ERR ( NOM,
"Could not rebuild bitmap font metrics" );
442 this->text_alignment = alignment;
445 const Coords BitmapFont::findGlyph (
const std::string& glyph )
448 std::istringstream i ( glyph );
452 return this->
chars[ascii];
464 int32 x_offset = this->
coords.x;
465 int32 y_offset = this->
coords.y;
467 switch ( this->text_alignment )
470 case TextAlignment::MiddleLeft:
472 x_offset = this->
coords.x;
473 y_offset = this->
coords.y;
477 case TextAlignment::MiddleCenter:
480 y_offset = ( this->
coords.height / 2 ) + this->
coords.y - ( this->getFontHeight() / 2 );
484 case TextAlignment::MiddleRight:
494 for ( uint32 show = 0; show < this->
text_buffer.length(); show++ )
507 x_offset = this->
coords.x;
517 uint8 ascii =
static_cast<uchar
>( this->
text_buffer[show] );
524 x_offset += ( this->
chars[ascii].width ) + 1;
534 NOM_LOG_ERR ( NOM,
"Video surface is invalid." );
540 NOM_LOG_ERR ( NOM,
"Failed to resize the video surface." );
544 if ( this->
rebuild() ==
false )
546 NOM_LOG_ERR ( NOM,
"Could not rebuild bitmap font metrics" );
uint32 getNewline(void) const
~BitmapFont(void)
Default destructor.
std::string text_buffer
holds contents of text as a string
BitmapFont(void)
Default constructor.
uint32 getSpacing(void) const
void set_position(const Point2i &pos)
uint32 pixel(int32 x, int32 y)
const Color & colorkey(void) const
void setTextJustification(IFont::TextAlignment alignment)
bool set_colorkey(const Color &colorkey)
static const Color null
Convenience object that will always contain a value of -1.
Color color
Not implemented (yet)
enum IFont::FontStyle text_style
Current text effect set.
void setText(const std::string &text)
Set a new text string for drawing; defaults to \0.
Coords chars[256]
individual chars positioning offsets within bitmap_font
void draw(SDL_RENDERER::RawPtr target) const
bool load(const std::string &filename, uint32 flags, bool use_cache=false)
void setColor(const Color &color)
Not implemented.
Texture bitmap_font
pointer reference holding our bitmap font image sheet
const std::string & getText(void) const
Obtains set text string buffer; defaults to \0.
IFont::SharedPtr clone(void) const
Return a std::shared_ptr copy of this instance.
Coords coords
Positioning coordinates.
FontStyle
Text effect styling.
SDL_RENDERER::RawPtr renderer(void) const
Get a raw pointer to the SDL_Renderer in use.
int32 getFontHeight(void) const
bool valid(void) const
Is this object initialized – not nullptr?
void draw(RenderTarget target) const
Coordinate values (x, y, z, width, height ) wrapper.
bool resize(enum ResizeAlgorithm scaling_algorithm)
void setSpacing(uint32 spaces)
uint32 newline
height (in pixels) to offset when newline carriage char is encountered
void setNewline(uint32_t newline)
Set new text character spacing height offsets in pixels.
int32 getFontWidth(void) const
bool load(const std::string &filename, const Color &colorkey, bool use_cache=false)
void setFontStyle(int32 style, uint8 options=150)
Utility class container for RGBA colors.
bool resize(enum ResizeAlgorithm scaling_algorithm)