]> git.localhorst.tv Git - l2e.git/blob - src/graphics/Font.cpp
Merge branch 'master' into menus
[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 "../loader/Interpreter.h"
11 #include "../loader/TypeDescription.h"
12
13 #include <cmath>
14 #include <iostream>
15
16 using geometry::Vector;
17 using loader::FieldDescription;
18 using loader::Interpreter;
19 using loader::TypeDescription;
20 using std::pow;
21
22 namespace graphics {
23
24 void Font::DrawChar(char c, SDL_Surface *dest, const Vector<int> &position) const {
25         if (!sprite) return;
26
27         int col(colOffset + (c % 0x10));
28         int row(rowOffset + (c / 0x10));
29         sprite->Draw(dest, position, col, row);
30 }
31
32 void Font::DrawString(const char *s, SDL_Surface *dest, const Vector<int> &positionIn, int maxChars) const {
33         if (!sprite) return;
34
35         Vector<int> position(positionIn);
36         Vector<int> step(CharWidth(), 0);
37         for (int i(0); s[i] && (maxChars <= 0 || i < maxChars); ++i, position += step) {
38                 DrawChar(s[i], dest, position);
39         }
40 }
41
42 void Font::DrawStringRight(const char *s, SDL_Surface *dest, const Vector<int> &positionIn, int maxChars) const {
43         if (!sprite) return;
44
45         int length(0);
46         while (length < maxChars && s[length] != '\0') {
47                 ++length;
48         }
49         Vector<int> position(positionIn.X() - length * CharWidth(), positionIn.Y());
50
51         DrawString(s, dest, position, length);
52 }
53
54 void Font::DrawDigit(int digit, SDL_Surface *dest, const Vector<int> &position) const {
55         if (!sprite) return;
56
57         DrawChar(digit + 0x30, dest, position);
58 }
59
60 void Font::DrawNumber(int numberIn, SDL_Surface *dest, const Vector<int> &positionIn, int digits) const {
61         if (!sprite) return;
62
63         int number(numberIn);
64         if (digits > 0 && numberIn >= pow(10.0, digits)) {
65                 numberIn = pow(10.0, digits) - 1;
66         }
67
68         Vector<int> position(positionIn);
69         Vector<int> step(sprite->Width(), 0);
70
71         if (digits > 0) {
72                 int i(digits - 1);
73                 while (number < pow(10.0, i) && i > 0) {
74                         position += step;
75                         --i;
76                 }
77         }
78
79         int m(10);
80         while (m <= number) {
81                 m *= 10;
82         }
83
84         while (m > 9) {
85                 m /= 10;
86                 DrawDigit((number / m) % 10, dest, position);
87                 position += step;
88         }
89 }
90
91 void Font::DrawNumberRight(int number, SDL_Surface *dest, const Vector<int> &positionIn, int digits) const {
92         if (!sprite) return;
93
94         Vector<int> position(positionIn.X() - digits * CharWidth(), positionIn.Y());
95
96         DrawNumber(number, dest, position, digits);
97 }
98
99
100 void Font::CreateTypeDescription() {
101         Font f;
102
103         TypeDescription &td(TypeDescription::Create(TYPE_ID, "Font"));
104         td.SetDescription(
105                         "Simple font with fixed-width characters using a sprite for rendering.\n"
106                         "Characters from strings are mapped as follows:\n"
107                         "<pre>sprite column = column offset + (character % 16)\n"
108                         "sprite row    = row    offset + (character / 16)</pre>");
109         td.SetConstructor(&Construct);
110         td.SetSize(sizeof(Font));
111
112         td.AddField("sprite", FieldDescription(((char *)&f.sprite) - ((char *)&f), Sprite::TYPE_ID).SetReferenced().SetDescription("a sprite where each tile corresponds to a character"));
113         td.AddField("columnoffset", FieldDescription(((char *)&f.colOffset) - ((char *)&f), Interpreter::NUMBER_ID).SetDescription("offset of the column of the first character"));
114         td.AddField("rowoffset", FieldDescription(((char *)&f.rowOffset) - ((char *)&f), Interpreter::NUMBER_ID).SetDescription("offset of the row of the first character"));
115 }
116
117 void Font::Construct(void *data) {
118         new (data) Font;
119 }
120
121 }