]> git.localhorst.tv Git - l2e.git/blob - src/graphics/Font.cpp
merged Point into Vector
[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 <cmath>
11
12 using geometry::Vector;
13 using std::pow;
14
15 namespace graphics {
16
17 void Font::DrawChar(char c, SDL_Surface *dest, const Vector<int> &position) const {
18         int col(colOffset + (c % 0x10));
19         int row(rowOffset + (c / 0x10));
20         sprite->Draw(dest, position, col, row);
21 }
22
23 void Font::DrawString(const char *s, SDL_Surface *dest, const Vector<int> &positionIn, int maxChars) const {
24         Vector<int> position(positionIn);
25         Vector<int> step(CharWidth(), 0);
26         for (int i(0); s[i] && (maxChars <= 0 || i < maxChars); ++i, position += step) {
27                 DrawChar(s[i], dest, position);
28         }
29 }
30
31 void Font::DrawDigit(int digit, SDL_Surface *dest, const Vector<int> &position) const {
32         DrawChar(digit + 0x30, dest, position);
33 }
34
35 void Font::DrawNumber(int numberIn, SDL_Surface *dest, const Vector<int> &positionIn, int digits) const {
36         int number(numberIn);
37         if (digits > 0 && numberIn >= pow(10.0, digits)) {
38                 numberIn = pow(10.0, digits) - 1;
39         }
40
41         Vector<int> position(positionIn);
42         Vector<int> step(sprite->Width(), 0);
43
44         if (digits > 0) {
45                 int i(digits - 1);
46                 while (number < pow(10.0, i) && i > 0) {
47                         position += step;
48                         --i;
49                 }
50         }
51
52         int m(10);
53         while (m <= number) {
54                 m *= 10;
55         }
56
57         while (m > 9) {
58                 m /= 10;
59                 DrawDigit((number / m) % 10, dest, position);
60                 position += step;
61         }
62 }
63
64 }