]> git.localhorst.tv Git - l2e.git/blob - src/graphics/Font.h
added string support in Font
[l2e.git] / src / graphics / Font.h
1 /*
2  * Font.h
3  *
4  *  Created on: Aug 8, 2012
5  *      Author: holy
6  */
7
8 #ifndef GRAPHICS_FONT_H_
9 #define GRAPHICS_FONT_H_
10
11 #include "Sprite.h"
12 #include "../geometry/Point.h"
13
14 #include <map>
15 #include <SDL.h>
16
17 namespace graphics {
18
19 // TODO: maybe fix fonts to use a 8x8 tile sprite for all chars
20 class Font {
21
22 public:
23         explicit Font(const Sprite *sprite, int digitsCol = 0, int digitsRow = 0)
24         : sprite(sprite), digitsCol(digitsCol), digitsRow(digitsRow) {
25                 MapRange('0', '9', digitsCol, digitsRow);
26         }
27
28 public:
29         int CharWidth() const { return sprite->Width(); }
30         int CharHeight() const { return sprite->Height(); }
31         void DrawChar(char c, SDL_Surface *dest, geometry::Point<int> position) const;
32         void DrawString(const char *s, SDL_Surface *dest, geometry::Point<int> position, int maxChars = 0) const;
33         void DrawDigit(int d, SDL_Surface *dest, geometry::Point<int> position) const;
34         void DrawNumber(int n, SDL_Surface *dest, geometry::Point<int> position, int digits = 0) const;
35
36 public:
37         bool HasChar(char c) const { return map[(unsigned char)c].mapped; };
38         void MapChar(char c, int col, int row) { map[(unsigned char)c].mapped = true; map[(unsigned char)c].col = col; map[(unsigned char)c].row = row; };
39         void MapRange(char from, char to, int colStart, int row) {
40                 int col(colStart);
41                 for (unsigned char c(from); c <= to; ++c, ++col) {
42                         MapChar(c, col, row);
43                 }
44         }
45
46 private:
47         struct Mapping {
48                 Mapping() : mapped(false), col(0), row(0) { }
49                 bool mapped;
50                 int col;
51                 int row;
52         };
53         const Sprite *sprite;
54         Mapping map[256];
55         int digitsCol;
56         int digitsRow;
57
58 };
59
60 }
61
62 #endif /* GRAPHICS_FONT_H_ */