]> git.localhorst.tv Git - l2e.git/blobdiff - src/graphics/Font.cpp
added simple font implementation
[l2e.git] / src / graphics / Font.cpp
diff --git a/src/graphics/Font.cpp b/src/graphics/Font.cpp
new file mode 100644 (file)
index 0000000..0ff3d9d
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * Font.cpp
+ *
+ *  Created on: Aug 8, 2012
+ *      Author: holy
+ */
+
+#include "Font.h"
+
+#include "../geometry/operators.h"
+#include "../geometry/Vector.h"
+
+#include <cmath>
+
+using geometry::Point;
+using geometry::Vector;
+using std::pow;
+
+namespace graphics {
+
+void Font::DrawDigit(int digit, SDL_Surface *dest, Point<int> position) const {
+       sprite->Draw(dest, position, digitsCol + digit, digitsRow);
+}
+
+void Font::DrawNumber(int numberIn, SDL_Surface *dest, Point<int> positionIn, int digits) const {
+       int number(numberIn);
+       if (digits > 0 && numberIn >= pow(10.0, digits)) {
+               numberIn = pow(10.0, digits) - 1;
+       }
+
+       Point<int> position(positionIn);
+       Vector<int> step(sprite->Width(), 0);
+
+       if (digits > 0) {
+               int i(digits - 1);
+               while (number < pow(10.0, i)) {
+                       position += step;
+                       --i;
+               }
+       }
+
+       int m(10);
+       while (m <= number) {
+               m *= 10;
+       }
+
+       while (m > 9) {
+               m /= 10;
+               DrawDigit((number / m) % 10, dest, position);
+               position += step;
+       }
+}
+
+}