From 2590fad63e8a2013c1a169c77f21931bc19f2f25 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Thu, 29 Nov 2012 15:23:55 +0100 Subject: [PATCH] added string dimensions calculation functions --- src/graphics/Font.cpp | 29 +++++++++++++++++++++++++++++ src/graphics/Font.h | 3 +++ 2 files changed, 32 insertions(+) diff --git a/src/graphics/Font.cpp b/src/graphics/Font.cpp index 94d451a..42772b2 100644 --- a/src/graphics/Font.cpp +++ b/src/graphics/Font.cpp @@ -21,6 +21,35 @@ using std::pow; namespace graphics { +int Font::StringWidth(const char *s) const { + int width(0), col(0); + for (int i(0); s[i]; ++i) { + if (s[i] == '\n') { + if (width < col) { + width = col; + } + col = 0; + } else { + ++col; + } + } + return (width < col ? col : width) * CharWidth(); +} + +int Font::StringHeight(const char *s) const { + if (*s == '\0') { + return 0; + } + int height(1); + for (; *s; ++s) { + if (*s == '\n') { + ++height; + } + } + return height * CharHeight(); +} + + void Font::DrawChar(char c, SDL_Surface *dest, const Vector &position) const { if (!sprite) return; diff --git a/src/graphics/Font.h b/src/graphics/Font.h index 6a8a5c2..a5d7b12 100644 --- a/src/graphics/Font.h +++ b/src/graphics/Font.h @@ -29,6 +29,9 @@ public: public: int CharWidth() const { return sprite->Width(); } int CharHeight() const { return sprite->Height(); } + int StringWidth(const char *) const; + int StringHeight(const char *) const; + void DrawChar(char c, SDL_Surface *dest, const geometry::Vector &position) const; void DrawString(const char *s, SDL_Surface *dest, const geometry::Vector &position, int maxWidth = 0) const; void DrawStringRight(const char *s, SDL_Surface *dest, const geometry::Vector &position, int maxWidth = 0) const; -- 2.39.2