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