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