]> git.localhorst.tv Git - l2e.git/blobdiff - src/graphics/Font.cpp
dynamic width right aligned rendering with Font
[l2e.git] / src / graphics / Font.cpp
index 59059e07436d1893da0a9fc7dc73612fe5c79a92..069d0325f90ffd124bfd9b9d9eb63ad621d528ac 100644 (file)
@@ -1,16 +1,10 @@
-/*
- * Font.cpp
- *
- *  Created on: Aug 8, 2012
- *      Author: holy
- */
-
 #include "Font.h"
 
 #include "../loader/Interpreter.h"
 #include "../loader/TypeDescription.h"
 
 #include <cmath>
+#include <cstring>
 #include <iostream>
 
 using geometry::Vector;
@@ -21,6 +15,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<int> &position) const {
        if (!sprite) return;
 
@@ -29,22 +52,37 @@ void Font::DrawChar(char c, SDL_Surface *dest, const Vector<int> &position) cons
        sprite->Draw(dest, position, col, row);
 }
 
-void Font::DrawString(const char *s, SDL_Surface *dest, const Vector<int> &positionIn, int maxChars) const {
+void Font::DrawString(const char *s, SDL_Surface *dest, const Vector<int> &positionIn, int maxWidth) const {
        if (!sprite) return;
 
        Vector<int> position(positionIn);
+       Vector<int> lineHead(positionIn);
        Vector<int> step(CharWidth(), 0);
-       for (int i(0); s[i] && (maxChars <= 0 || i < maxChars); ++i, position += step) {
-               DrawChar(s[i], dest, position);
+       Vector<int> lineBreak(0, CharHeight());
+       for (int i(0), col(0); s[i] && (maxWidth <= 0 || col < maxWidth); ++i) {
+               if (s[i] == '\n') {
+                       lineHead += lineBreak;
+                       position = lineHead;
+                       col = 0;
+               } else {
+                       DrawChar(s[i], dest, position);
+                       position += step;
+                       ++col;
+               }
        }
 }
 
-void Font::DrawStringRight(const char *s, SDL_Surface *dest, const Vector<int> &positionIn, int maxChars) const {
+void Font::DrawStringRight(const char *s, SDL_Surface *dest, const Vector<int> &positionIn, int maxWidth) const {
+       // NOTE: this does not handle line breaks
        if (!sprite) return;
 
        int length(0);
-       while (length < maxChars && s[length] != '\0') {
-               ++length;
+       if (maxWidth > 0) {
+               while (length < maxWidth && s[length] != '\0') {
+                       ++length;
+               }
+       } else {
+               length = std::strlen(s);
        }
        Vector<int> position(positionIn.X() - length * CharWidth(), positionIn.Y());
 
@@ -91,7 +129,16 @@ void Font::DrawNumber(int numberIn, SDL_Surface *dest, const Vector<int> &positi
 void Font::DrawNumberRight(int number, SDL_Surface *dest, const Vector<int> &positionIn, int digits) const {
        if (!sprite) return;
 
-       Vector<int> position(positionIn.X() - digits * CharWidth(), positionIn.Y());
+       Vector<int> position(positionIn);
+       if (digits > 0) {
+               position.X() -= digits * CharWidth();
+       } else if (number == 0) {
+               position.X() -= CharWidth();
+       } else {
+               for (int i = number; i > 0; i /= 10) {
+                       position.X() -= CharWidth();
+               }
+       }
 
        DrawNumber(number, dest, position, digits);
 }