4 * Created on: Aug 8, 2012
10 #include "../loader/Interpreter.h"
11 #include "../loader/TypeDescription.h"
16 using geometry::Vector;
17 using loader::FieldDescription;
18 using loader::Interpreter;
19 using loader::TypeDescription;
24 void Font::DrawChar(char c, SDL_Surface *dest, const Vector<int> &position) const {
27 int col(colOffset + (c % 0x10));
28 int row(rowOffset + (c / 0x10));
29 sprite->Draw(dest, position, col, row);
32 void Font::DrawString(const char *s, SDL_Surface *dest, const Vector<int> &positionIn, int maxChars) const {
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);
42 void Font::DrawDigit(int digit, SDL_Surface *dest, const Vector<int> &position) const {
45 DrawChar(digit + 0x30, dest, position);
48 void Font::DrawNumber(int numberIn, SDL_Surface *dest, const Vector<int> &positionIn, int digits) const {
52 if (digits > 0 && numberIn >= pow(10.0, digits)) {
53 numberIn = pow(10.0, digits) - 1;
56 Vector<int> position(positionIn);
57 Vector<int> step(sprite->Width(), 0);
61 while (number < pow(10.0, i) && i > 0) {
74 DrawDigit((number / m) % 10, dest, position);
80 void Font::CreateTypeDescription() {
83 TypeDescription &td(TypeDescription::Create(TYPE_ID, "Font"));
85 "Simple font with fixed-width characters using a sprite for rendering.\n"
86 "Characters from strings are mapped as follows:\n"
87 "<pre>sprite column = column offset + (character % 16)\n"
88 "sprite row = row offset + (character / 16)</pre>");
89 td.SetConstructor(&Construct);
90 td.SetSize(sizeof(Font));
92 td.AddField("sprite", FieldDescription(((char *)&f.sprite) - ((char *)&f), Sprite::TYPE_ID).SetReferenced().SetDescription("a sprite where each tile corresponds to a character"));
93 td.AddField("columnoffset", FieldDescription(((char *)&f.colOffset) - ((char *)&f), Interpreter::NUMBER_ID).SetDescription("offset of the column of the first character"));
94 td.AddField("rowoffset", FieldDescription(((char *)&f.rowOffset) - ((char *)&f), Interpreter::NUMBER_ID).SetDescription("offset of the row of the first character"));
97 void Font::Construct(void *data) {