From: Daniel Karbach Date: Wed, 8 Aug 2012 18:53:08 +0000 (+0200) Subject: added string support in Font X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=2f2dad58f3b9f7c98cf20211929ff31f5ebf5e5f;p=l2e.git added string support in Font --- diff --git a/src/graphics/Font.cpp b/src/graphics/Font.cpp index 0ff3d9d..135775c 100644 --- a/src/graphics/Font.cpp +++ b/src/graphics/Font.cpp @@ -18,6 +18,20 @@ using std::pow; namespace graphics { +void Font::DrawChar(char c, SDL_Surface *dest, Point position) const { + if (!HasChar(c)) return; + const Mapping &m(map[(unsigned char)c]); + sprite->Draw(dest, position, m.col, m.row); +} + +void Font::DrawString(const char *s, SDL_Surface *dest, Point positionIn, int maxChars) const { + Point position(positionIn); + Vector step(CharWidth(), 0); + for (int i(0); s[i] && (maxChars <= 0 || i < maxChars); ++i, position += step) { + DrawChar(s[i], dest, position); + } +} + void Font::DrawDigit(int digit, SDL_Surface *dest, Point position) const { sprite->Draw(dest, position, digitsCol + digit, digitsRow); } diff --git a/src/graphics/Font.h b/src/graphics/Font.h index 0294d25..082e4c0 100644 --- a/src/graphics/Font.h +++ b/src/graphics/Font.h @@ -11,23 +11,47 @@ #include "Sprite.h" #include "../geometry/Point.h" +#include #include namespace graphics { +// TODO: maybe fix fonts to use a 8x8 tile sprite for all chars class Font { public: - explicit Font(const Sprite *sprite, int digitsCol = 0, int digitsRow = 0) : sprite(sprite), digitsCol(digitsCol), digitsRow(digitsRow) { } + explicit Font(const Sprite *sprite, int digitsCol = 0, int digitsRow = 0) + : sprite(sprite), digitsCol(digitsCol), digitsRow(digitsRow) { + MapRange('0', '9', digitsCol, digitsRow); + } public: int CharWidth() const { return sprite->Width(); } int CharHeight() const { return sprite->Height(); } - void DrawDigit(int digit, SDL_Surface *dest, geometry::Point position) const; - void DrawNumber(int number, SDL_Surface *dest, geometry::Point position, int digits = 0) const; + void DrawChar(char c, SDL_Surface *dest, geometry::Point position) const; + void DrawString(const char *s, SDL_Surface *dest, geometry::Point position, int maxChars = 0) const; + void DrawDigit(int d, SDL_Surface *dest, geometry::Point position) const; + void DrawNumber(int n, SDL_Surface *dest, geometry::Point position, int digits = 0) const; + +public: + bool HasChar(char c) const { return map[(unsigned char)c].mapped; }; + void MapChar(char c, int col, int row) { map[(unsigned char)c].mapped = true; map[(unsigned char)c].col = col; map[(unsigned char)c].row = row; }; + void MapRange(char from, char to, int colStart, int row) { + int col(colStart); + for (unsigned char c(from); c <= to; ++c, ++col) { + MapChar(c, col, row); + } + } private: + struct Mapping { + Mapping() : mapped(false), col(0), row(0) { } + bool mapped; + int col; + int row; + }; const Sprite *sprite; + Mapping map[256]; int digitsCol; int digitsRow;