From: Daniel Karbach Date: Sat, 11 Aug 2012 17:58:56 +0000 (+0200) Subject: forced fonts to use the data's charset X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=c182086fbc039ec2b943b4d109597ccc481b7ba4;p=l2e.git forced fonts to use the data's charset --- diff --git a/src/graphics/Font.cpp b/src/graphics/Font.cpp index d3a3de0..3e46249 100644 --- a/src/graphics/Font.cpp +++ b/src/graphics/Font.cpp @@ -19,9 +19,9 @@ 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); + int col(colOffset + (c % 0x10)); + int row(rowOffset + (c / 0x10)); + sprite->Draw(dest, position, col, row); } void Font::DrawString(const char *s, SDL_Surface *dest, Point positionIn, int maxChars) const { @@ -33,7 +33,7 @@ void Font::DrawString(const char *s, SDL_Surface *dest, Point positionIn, i } void Font::DrawDigit(int digit, SDL_Surface *dest, Point position) const { - sprite->Draw(dest, position, digitsCol + digit, digitsRow); + DrawChar(digit + 0x30, dest, position); } void Font::DrawNumber(int numberIn, SDL_Surface *dest, Point positionIn, int digits) const { diff --git a/src/graphics/Font.h b/src/graphics/Font.h index 082e4c0..70f451c 100644 --- a/src/graphics/Font.h +++ b/src/graphics/Font.h @@ -11,18 +11,16 @@ #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) { - MapRange('0', '9', digitsCol, digitsRow); + explicit Font(const Sprite *sprite, int colOffset = 0, int rowOffset = 0) + : sprite(sprite), colOffset(colOffset), rowOffset(rowOffset) { + } public: @@ -33,27 +31,10 @@ public: 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; + int colOffset; + int rowOffset; }; diff --git a/src/main.cpp b/src/main.cpp index 4b4ca58..b4be5be 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -155,15 +155,7 @@ int main(int argc, char **argv) { SDL_Surface *largeFontImg(IMG_Load("test-data/large-font.png")); Sprite largeFontSprite(largeFontImg, 16, 32); - Font largeFont(&largeFontSprite); - largeFont.MapRange('A', 'M', 0, 1); - largeFont.MapRange('N', 'Z', 0, 2); - largeFont.MapRange('a', 'm', 0, 3); - largeFont.MapRange('n', 'z', 0, 4); - largeFont.MapChar(':', 10, 0); - largeFont.MapChar('!', 11, 0); - largeFont.MapChar('?', 12, 0); - // TODO: add '.' and '-' characters + Font largeFont(&largeFontSprite, 0, -2); battleRes.titleFont = &largeFont; SDL_Surface *heroTagImg(IMG_Load("test-data/hero-tag-sprites.png")); @@ -182,7 +174,7 @@ int main(int argc, char **argv) { SDL_Surface *numbersImg(IMG_Load("test-data/numbers.png")); Sprite numbersSprite(numbersImg, 16, 16); - Font heroTagFont(&numbersSprite); + Font heroTagFont(&numbersSprite, 0, -3); battleRes.heroTagFont = &heroTagFont; SDL_Surface *tagFramesImg(IMG_Load("test-data/tag-frames.png")); Frame heroTagFrame(tagFramesImg, 16, 16, 1, 1, 0, 33); @@ -210,28 +202,12 @@ int main(int argc, char **argv) { SDL_Surface *normalFontImg(IMG_Load("test-data/normal-font.png")); Sprite normalFontSprite(normalFontImg, 16, 16); - Font normalFont(&normalFontSprite); - normalFont.MapRange('A', 'M', 0, 1); - normalFont.MapRange('N', 'Z', 0, 2); - normalFont.MapRange('a', 'm', 0, 3); - normalFont.MapRange('n', 'z', 0, 4); - normalFont.MapChar(':', 10, 0); - normalFont.MapChar('!', 11, 0); - normalFont.MapChar('?', 12, 0); - // TODO: add '.' and '-' characters + Font normalFont(&normalFontSprite, 0, -2); battleRes.normalFont = &normalFont; SDL_Surface *disabledFontImg(IMG_Load("test-data/disabled-font.png")); Sprite disabledFontSprite(disabledFontImg, 16, 16); - Font disabledFont(&disabledFontSprite); - disabledFont.MapRange('A', 'M', 0, 1); - disabledFont.MapRange('N', 'Z', 0, 2); - disabledFont.MapRange('a', 'm', 0, 3); - disabledFont.MapRange('n', 'z', 0, 4); - disabledFont.MapChar(':', 10, 0); - disabledFont.MapChar('!', 11, 0); - disabledFont.MapChar('?', 12, 0); - // TODO: add '.' and '-' characters + Font disabledFont(&disabledFontSprite, 0, -2); battleRes.disabledFont = &disabledFont; SDL_Surface *handCursorImg(IMG_Load("test-data/cursor-hand.png")); diff --git a/test-data/large-font.png b/test-data/large-font.png index db06aee..c00fe6f 100644 Binary files a/test-data/large-font.png and b/test-data/large-font.png differ diff --git a/test-data/normal-font.png b/test-data/normal-font.png index b6a286e..8a1532b 100644 Binary files a/test-data/normal-font.png and b/test-data/normal-font.png differ