-
Notifications
You must be signed in to change notification settings - Fork 1
/
font.cpp
28 lines (25 loc) · 1013 Bytes
/
font.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include "font.h"
Font::Font(const char* filename, SDL_Renderer* renderer, int glyphWidth, int glyphHeight) {
this->renderer = renderer;
this->glyphWidth = glyphWidth;
this->glyphHeight = glyphHeight;
SDL_Surface* glyphSurface = SDL_LoadBMP(filename);
SDL_SetColorKey(glyphSurface, true, SDL_MapRGB(glyphSurface->format, 255, 255, 255));
glyphs = SDL_CreateTextureFromSurface(renderer, glyphSurface);
}
void Font::Draw(const char* text, int x, int y) {
const int left = x;
for (int index = 0; text[index] != '\0'; ++index) {
unsigned char c = text[index];
if (c == '\n' || c == '\r') {
x = left;
y += glyphHeight;
}
else if (c >= ' ' && c <= '~') {
SDL_Rect source { (c % 16) * glyphWidth, (c / 16) * glyphHeight, glyphWidth, glyphHeight };
SDL_Rect dest { x, y, glyphWidth, glyphHeight };
SDL_RenderCopy(renderer, glyphs, &source, &dest);
x += glyphWidth;
}
}
}