#include #include #include #include "font.h" extern SDL_Renderer *renderer; static SDL_Texture *font_tex = NULL; int font_init(const char *path) { if ((font_tex = IMG_LoadTexture(renderer, path)) == NULL) return 1; return 0; } void font_deinit(void) { if (font_tex != NULL) { SDL_DestroyTexture(font_tex); font_tex = NULL; } } void font_draw(int x, int y, const char *s) { for (; *s != '\0'; s++) { const char c = toupper(*s); const int ix = c % 16 * FONT_W; const int iy = c / 16 * FONT_H; const SDL_Rect src = { ix, iy, FONT_W, FONT_H }; const SDL_Rect dest = { x, y, FONT_W, FONT_H }; if (SDL_RenderCopy(renderer, font_tex, &src, &dest)) fprintf(stderr, "%s\n", SDL_GetError()); x += FONT_W; } } void font_set_color(int r, int g, int b, int a) { if (SDL_SetTextureColorMod(font_tex, r, g, b)) fprintf(stderr, "%s\n", SDL_GetError()); if (SDL_SetTextureAlphaMod(font_tex, a)) fprintf(stderr, "%s\n", SDL_GetError()); }