xormod/text.c

35 lines
1018 B
C
Raw Normal View History

2024-12-30 18:28:05 +00:00
#include "font.h"
2021-04-10 19:30:51 +00:00
2024-10-05 21:19:26 +00:00
#include <SDL3/SDL.h>
2021-04-10 19:30:51 +00:00
2024-12-30 11:38:15 +00:00
static SDL_Texture *font = NULL;
2021-04-10 19:30:51 +00:00
2024-12-30 11:38:15 +00:00
void draw_text(SDL_Renderer *renderer,
2021-04-10 19:30:51 +00:00
char *text, size_t len, int x, int y, uint8_t r, uint8_t g, uint8_t b) {
2024-12-30 11:38:15 +00:00
if (font == NULL) {
SDL_Surface *s = SDL_LoadBMP_IO(SDL_IOFromConstMem(font_data, font_data_len), true);
SDL_SetSurfaceColorKey(s, true, SDL_MapSurfaceRGB(s, 255, 0, 255));
font = SDL_CreateTextureFromSurface(renderer, s);
SDL_DestroySurface(s);
}
2021-04-10 19:30:51 +00:00
SDL_SetTextureColorMod(font, r, g, b);
2024-10-05 21:19:26 +00:00
SDL_FRect src = { .w = font_w, .h = font_h },
2021-04-10 19:30:51 +00:00
dst = { .x = x, .y = y, .w = font_w, .h = font_h };
for (size_t i = 0; i < len; i++) {
2024-12-30 11:26:44 +00:00
unsigned char c = text[i];
2021-04-10 19:30:51 +00:00
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;
2024-10-05 21:19:26 +00:00
SDL_RenderTexture(renderer, font, &src, &dst);
2021-04-10 19:30:51 +00:00
dst.x += font_w;
}
}