summaryrefslogtreecommitdiff
path: root/font.c
blob: 8ae956721353a4cf53c55b63fa7f75df79e8a46f (plain)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <SDL2/SDL_error.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_image.h>
#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());
}