]> git.localhorst.tv Git - l2e.git/blob - src/graphics/Font.cpp
fixed positioning in Font when number is 0
[l2e.git] / src / graphics / Font.cpp
1 /*
2  * Font.cpp
3  *
4  *  Created on: Aug 8, 2012
5  *      Author: holy
6  */
7
8 #include "Font.h"
9
10 #include "../geometry/operators.h"
11 #include "../geometry/Vector.h"
12
13 #include <cmath>
14
15 using geometry::Point;
16 using geometry::Vector;
17 using std::pow;
18
19 namespace graphics {
20
21 void Font::DrawChar(char c, SDL_Surface *dest, Point<int> position) const {
22         if (!HasChar(c)) return;
23         const Mapping &m(map[(unsigned char)c]);
24         sprite->Draw(dest, position, m.col, m.row);
25 }
26
27 void Font::DrawString(const char *s, SDL_Surface *dest, Point<int> positionIn, int maxChars) const {
28         Point<int> position(positionIn);
29         Vector<int> step(CharWidth(), 0);
30         for (int i(0); s[i] && (maxChars <= 0 || i < maxChars); ++i, position += step) {
31                 DrawChar(s[i], dest, position);
32         }
33 }
34
35 void Font::DrawDigit(int digit, SDL_Surface *dest, Point<int> position) const {
36         sprite->Draw(dest, position, digitsCol + digit, digitsRow);
37 }
38
39 void Font::DrawNumber(int numberIn, SDL_Surface *dest, Point<int> positionIn, int digits) const {
40         int number(numberIn);
41         if (digits > 0 && numberIn >= pow(10.0, digits)) {
42                 numberIn = pow(10.0, digits) - 1;
43         }
44
45         Point<int> position(positionIn);
46         Vector<int> step(sprite->Width(), 0);
47
48         if (digits > 0) {
49                 int i(digits - 1);
50                 while (number < pow(10.0, i) && i > 0) {
51                         position += step;
52                         --i;
53                 }
54         }
55
56         int m(10);
57         while (m <= number) {
58                 m *= 10;
59         }
60
61         while (m > 9) {
62                 m /= 10;
63                 DrawDigit((number / m) % 10, dest, position);
64                 position += step;
65         }
66 }
67
68 }