4 * Created on: Aug 8, 2012
10 #include "../loader/TypeDescription.h"
15 using geometry::Vector;
16 using loader::FieldDescription;
17 using loader::TypeDescription;
22 void Font::DrawChar(char c, SDL_Surface *dest, const Vector<int> &position) const {
25 int col(colOffset + (c % 0x10));
26 int row(rowOffset + (c / 0x10));
27 sprite->Draw(dest, position, col, row);
30 void Font::DrawString(const char *s, SDL_Surface *dest, const Vector<int> &positionIn, int maxChars) const {
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);
40 void Font::DrawDigit(int digit, SDL_Surface *dest, const Vector<int> &position) const {
43 DrawChar(digit + 0x30, dest, position);
46 void Font::DrawNumber(int numberIn, SDL_Surface *dest, const Vector<int> &positionIn, int digits) const {
50 if (digits > 0 && numberIn >= pow(10.0, digits)) {
51 numberIn = pow(10.0, digits) - 1;
54 Vector<int> position(positionIn);
55 Vector<int> step(sprite->Width(), 0);
59 while (number < pow(10.0, i) && i > 0) {
72 DrawDigit((number / m) % 10, dest, position);
78 void Font::CreateTypeDescription() {
81 int numberId(TypeDescription::GetTypeId("Number"));
82 int spriteId(TypeDescription::GetTypeId("Sprite"));
84 TypeDescription &td(TypeDescription::CreateOrGet("Font"));
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));
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"));
98 void Font::Construct(void *data) {