namespace graphics {
void Font::DrawChar(char c, SDL_Surface *dest, Point<int> position) const {
- if (!HasChar(c)) return;
- const Mapping &m(map[(unsigned char)c]);
- sprite->Draw(dest, position, m.col, m.row);
+ int col(colOffset + (c % 0x10));
+ int row(rowOffset + (c / 0x10));
+ sprite->Draw(dest, position, col, row);
}
void Font::DrawString(const char *s, SDL_Surface *dest, Point<int> positionIn, int maxChars) const {
}
void Font::DrawDigit(int digit, SDL_Surface *dest, Point<int> position) const {
- sprite->Draw(dest, position, digitsCol + digit, digitsRow);
+ DrawChar(digit + 0x30, dest, position);
}
void Font::DrawNumber(int numberIn, SDL_Surface *dest, Point<int> positionIn, int digits) const {
#include "Sprite.h"
#include "../geometry/Point.h"
-#include <map>
#include <SDL.h>
namespace graphics {
-// TODO: maybe fix fonts to use a 8x8 tile sprite for all chars
class Font {
public:
- explicit Font(const Sprite *sprite, int digitsCol = 0, int digitsRow = 0)
- : sprite(sprite), digitsCol(digitsCol), digitsRow(digitsRow) {
- MapRange('0', '9', digitsCol, digitsRow);
+ explicit Font(const Sprite *sprite, int colOffset = 0, int rowOffset = 0)
+ : sprite(sprite), colOffset(colOffset), rowOffset(rowOffset) {
+
}
public:
void DrawDigit(int d, SDL_Surface *dest, geometry::Point<int> position) const;
void DrawNumber(int n, SDL_Surface *dest, geometry::Point<int> position, int digits = 0) const;
-public:
- bool HasChar(char c) const { return map[(unsigned char)c].mapped; };
- 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; };
- void MapRange(char from, char to, int colStart, int row) {
- int col(colStart);
- for (unsigned char c(from); c <= to; ++c, ++col) {
- MapChar(c, col, row);
- }
- }
-
private:
- struct Mapping {
- Mapping() : mapped(false), col(0), row(0) { }
- bool mapped;
- int col;
- int row;
- };
const Sprite *sprite;
- Mapping map[256];
- int digitsCol;
- int digitsRow;
+ int colOffset;
+ int rowOffset;
};
SDL_Surface *largeFontImg(IMG_Load("test-data/large-font.png"));
Sprite largeFontSprite(largeFontImg, 16, 32);
- Font largeFont(&largeFontSprite);
- largeFont.MapRange('A', 'M', 0, 1);
- largeFont.MapRange('N', 'Z', 0, 2);
- largeFont.MapRange('a', 'm', 0, 3);
- largeFont.MapRange('n', 'z', 0, 4);
- largeFont.MapChar(':', 10, 0);
- largeFont.MapChar('!', 11, 0);
- largeFont.MapChar('?', 12, 0);
- // TODO: add '.' and '-' characters
+ Font largeFont(&largeFontSprite, 0, -2);
battleRes.titleFont = &largeFont;
SDL_Surface *heroTagImg(IMG_Load("test-data/hero-tag-sprites.png"));
SDL_Surface *numbersImg(IMG_Load("test-data/numbers.png"));
Sprite numbersSprite(numbersImg, 16, 16);
- Font heroTagFont(&numbersSprite);
+ Font heroTagFont(&numbersSprite, 0, -3);
battleRes.heroTagFont = &heroTagFont;
SDL_Surface *tagFramesImg(IMG_Load("test-data/tag-frames.png"));
Frame heroTagFrame(tagFramesImg, 16, 16, 1, 1, 0, 33);
SDL_Surface *normalFontImg(IMG_Load("test-data/normal-font.png"));
Sprite normalFontSprite(normalFontImg, 16, 16);
- Font normalFont(&normalFontSprite);
- normalFont.MapRange('A', 'M', 0, 1);
- normalFont.MapRange('N', 'Z', 0, 2);
- normalFont.MapRange('a', 'm', 0, 3);
- normalFont.MapRange('n', 'z', 0, 4);
- normalFont.MapChar(':', 10, 0);
- normalFont.MapChar('!', 11, 0);
- normalFont.MapChar('?', 12, 0);
- // TODO: add '.' and '-' characters
+ Font normalFont(&normalFontSprite, 0, -2);
battleRes.normalFont = &normalFont;
SDL_Surface *disabledFontImg(IMG_Load("test-data/disabled-font.png"));
Sprite disabledFontSprite(disabledFontImg, 16, 16);
- Font disabledFont(&disabledFontSprite);
- disabledFont.MapRange('A', 'M', 0, 1);
- disabledFont.MapRange('N', 'Z', 0, 2);
- disabledFont.MapRange('a', 'm', 0, 3);
- disabledFont.MapRange('n', 'z', 0, 4);
- disabledFont.MapChar(':', 10, 0);
- disabledFont.MapChar('!', 11, 0);
- disabledFont.MapChar('?', 12, 0);
- // TODO: add '.' and '-' characters
+ Font disabledFont(&disabledFontSprite, 0, -2);
battleRes.disabledFont = &disabledFont;
SDL_Surface *handCursorImg(IMG_Load("test-data/cursor-hand.png"));