3 #include "../loader/Interpreter.h"
4 #include "../loader/TypeDescription.h"
9 using geometry::Vector;
10 using loader::FieldDescription;
11 using loader::Interpreter;
12 using loader::TypeDescription;
17 int Font::StringWidth(const char *s) const {
19 for (int i(0); s[i]; ++i) {
29 return (width < col ? col : width) * CharWidth();
32 int Font::StringHeight(const char *s) const {
42 return height * CharHeight();
46 void Font::DrawChar(char c, SDL_Surface *dest, const Vector<int> &position) const {
49 int col(colOffset + (c % 0x10));
50 int row(rowOffset + (c / 0x10));
51 sprite->Draw(dest, position, col, row);
54 void Font::DrawString(const char *s, SDL_Surface *dest, const Vector<int> &positionIn, int maxWidth) const {
57 Vector<int> position(positionIn);
58 Vector<int> lineHead(positionIn);
59 Vector<int> step(CharWidth(), 0);
60 Vector<int> lineBreak(0, CharHeight());
61 for (int i(0), col(0); s[i] && (maxWidth <= 0 || col < maxWidth); ++i) {
63 lineHead += lineBreak;
67 DrawChar(s[i], dest, position);
74 void Font::DrawStringRight(const char *s, SDL_Surface *dest, const Vector<int> &positionIn, int maxWidth) const {
75 // NOTE: this does not handle line breaks
79 while (length < maxWidth && s[length] != '\0') {
82 Vector<int> position(positionIn.X() - length * CharWidth(), positionIn.Y());
84 DrawString(s, dest, position, length);
87 void Font::DrawDigit(int digit, SDL_Surface *dest, const Vector<int> &position) const {
90 DrawChar(digit + 0x30, dest, position);
93 void Font::DrawNumber(int numberIn, SDL_Surface *dest, const Vector<int> &positionIn, int digits) const {
97 if (digits > 0 && numberIn >= pow(10.0, digits)) {
98 numberIn = pow(10.0, digits) - 1;
101 Vector<int> position(positionIn);
102 Vector<int> step(sprite->Width(), 0);
106 while (number < pow(10.0, i) && i > 0) {
113 while (m <= number) {
119 DrawDigit((number / m) % 10, dest, position);
124 void Font::DrawNumberRight(int number, SDL_Surface *dest, const Vector<int> &positionIn, int digits) const {
127 Vector<int> position(positionIn.X() - digits * CharWidth(), positionIn.Y());
129 DrawNumber(number, dest, position, digits);
133 void Font::CreateTypeDescription() {
136 TypeDescription &td(TypeDescription::Create(TYPE_ID, "Font"));
138 "Simple font with fixed-width characters using a sprite for rendering.\n"
139 "Characters from strings are mapped as follows:\n"
140 "<pre>sprite column = column offset + (character % 16)\n"
141 "sprite row = row offset + (character / 16)</pre>");
142 td.SetConstructor(&Construct);
143 td.SetSize(sizeof(Font));
145 td.AddField("sprite", FieldDescription(((char *)&f.sprite) - ((char *)&f), Sprite::TYPE_ID).SetReferenced().SetDescription("a sprite where each tile corresponds to a character"));
146 td.AddField("columnoffset", FieldDescription(((char *)&f.colOffset) - ((char *)&f), Interpreter::NUMBER_ID).SetDescription("offset of the column of the first character"));
147 td.AddField("rowoffset", FieldDescription(((char *)&f.rowOffset) - ((char *)&f), Interpreter::NUMBER_ID).SetDescription("offset of the row of the first character"));
150 void Font::Construct(void *data) {