From: Daniel Karbach Date: Thu, 29 Nov 2012 14:23:55 +0000 (+0100) Subject: added string dimensions calculation functions X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=2590fad63e8a2013c1a169c77f21931bc19f2f25;p=l2e.git added string dimensions calculation functions --- 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;