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 int Font::StringWidth(const char *s) const {
26 for (int i(0); s[i]; ++i) {
36 return (width < col ? col : width) * CharWidth();
39 int Font::StringHeight(const char *s) const {
49 return height * CharHeight();
53 void Font::DrawChar(char c, SDL_Surface *dest, const Vector<int> &position) const {
56 int col(colOffset + (c % 0x10));
57 int row(rowOffset + (c / 0x10));
58 sprite->Draw(dest, position, col, row);
61 void Font::DrawString(const char *s, SDL_Surface *dest, const Vector<int> &positionIn, int maxWidth) const {
64 Vector<int> position(positionIn);
65 Vector<int> lineHead(positionIn);
66 Vector<int> step(CharWidth(), 0);
67 Vector<int> lineBreak(0, CharHeight());
68 for (int i(0), col(0); s[i] && (maxWidth <= 0 || col < maxWidth); ++i) {
70 lineHead += lineBreak;
74 DrawChar(s[i], dest, position);
81 void Font::DrawStringRight(const char *s, SDL_Surface *dest, const Vector<int> &positionIn, int maxWidth) const {
82 // NOTE: this does not handle line breaks
86 while (length < maxWidth && s[length] != '\0') {
89 Vector<int> position(positionIn.X() - length * CharWidth(), positionIn.Y());
91 DrawString(s, dest, position, length);
94 void Font::DrawDigit(int digit, SDL_Surface *dest, const Vector<int> &position) const {
97 DrawChar(digit + 0x30, dest, position);
100 void Font::DrawNumber(int numberIn, SDL_Surface *dest, const Vector<int> &positionIn, int digits) const {
103 int number(numberIn);
104 if (digits > 0 && numberIn >= pow(10.0, digits)) {
105 numberIn = pow(10.0, digits) - 1;
108 Vector<int> position(positionIn);
109 Vector<int> step(sprite->Width(), 0);
113 while (number < pow(10.0, i) && i > 0) {
120 while (m <= number) {
126 DrawDigit((number / m) % 10, dest, position);
131 void Font::DrawNumberRight(int number, SDL_Surface *dest, const Vector<int> &positionIn, int digits) const {
134 Vector<int> position(positionIn.X() - digits * CharWidth(), positionIn.Y());
136 DrawNumber(number, dest, position, digits);
140 void Font::CreateTypeDescription() {
143 TypeDescription &td(TypeDescription::Create(TYPE_ID, "Font"));
145 "Simple font with fixed-width characters using a sprite for rendering.\n"
146 "Characters from strings are mapped as follows:\n"
147 "<pre>sprite column = column offset + (character % 16)\n"
148 "sprite row = row offset + (character / 16)</pre>");
149 td.SetConstructor(&Construct);
150 td.SetSize(sizeof(Font));
152 td.AddField("sprite", FieldDescription(((char *)&f.sprite) - ((char *)&f), Sprite::TYPE_ID).SetReferenced().SetDescription("a sprite where each tile corresponds to a character"));
153 td.AddField("columnoffset", FieldDescription(((char *)&f.colOffset) - ((char *)&f), Interpreter::NUMBER_ID).SetDescription("offset of the column of the first character"));
154 td.AddField("rowoffset", FieldDescription(((char *)&f.rowOffset) - ((char *)&f), Interpreter::NUMBER_ID).SetDescription("offset of the row of the first character"));
157 void Font::Construct(void *data) {