xormod/font.c

34 lines
1.0 KiB
C

#include "EGA8x8.h"
#include <SDL.h>
SDL_Texture *load_font(SDL_Renderer *renderer) {
SDL_Surface *s = SDL_LoadBMP_RW(SDL_RWFromConstMem(font_data, font_data_len), 1);
SDL_SetColorKey(s, SDL_TRUE, SDL_MapRGB(s->format, 255, 0, 255));
SDL_Texture *font_texture = SDL_CreateTextureFromSurface(renderer, s);
SDL_FreeSurface(s);
return font_texture;
}
void draw_text(SDL_Renderer *renderer, SDL_Texture *font,
char *text, size_t len, int x, int y, uint8_t r, uint8_t g, uint8_t b) {
if (font == NULL) return;
SDL_SetTextureColorMod(font, r, g, b);
SDL_Rect src = { .w = font_w, .h = font_h },
dst = { .x = x, .y = y, .w = font_w, .h = font_h };
for (size_t i = 0; i < len; i++) {
char c = text[i];
if (c == 0) break;
if (c == '\n') {
dst.x = x;
dst.y += font_h;
continue;
}
src.x = (c & 0xf) * font_w;
src.y = (c >> 4) * font_h;
SDL_RenderCopy(renderer, font, &src, &dst);
dst.x += font_w;
}
}