This repository has been archived on 2023-05-27. You can view files and clone it, but cannot push or open issues or pull requests.
api8/src/font.c
2023-05-19 14:19:31 +02:00

57 lines
1.4 KiB
C

#include "../includes/font.h"
int initFont(TTF_Font **font, char *filename, int size) {
if (TTF_Init() == -1) {
fprintf(stderr, "Erreur TTF : %s\n", TTF_GetError());
return 1;
}
if (!(*font = TTF_OpenFont(filename, size))) {
fprintf(stderr, "Erreur TTF : %s\n", TTF_GetError());
return 2;
}
return 0;
}
int writeText(GLuint *_textTexId, TTF_Font *font, const char *text,
SDL_Color color) {
glGenTextures(1, _textTexId);
assert(_textTexId);
glBindTexture(GL_TEXTURE_2D, *_textTexId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
SDL_Surface *d, *s;
if (!(d = TTF_RenderUTF8_Blended_Wrapped(font, text, color, 0))) {
freeFont(font);
fprintf(stderr, "Erreur TTF : TTF_RenderUTF8_Blended_Wrapped\n");
return 1;
}
if (!(s = SDL_CreateRGBSurface(0, d->w, d->h, 32, R_MASK, G_MASK, B_MASK,
A_MASK))) {
freeFont(font);
fprintf(stderr, "Erreur SDL : %s\n", SDL_GetError());
return 2;
}
SDL_BlitSurface(d, NULL, s, NULL);
SDL_FreeSurface(d);
glBindTexture(GL_TEXTURE_2D, *_textTexId);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, s->w, s->h, 0, GL_RGBA,
GL_UNSIGNED_BYTE, s->pixels);
SDL_FreeSurface(s);
glBindTexture(GL_TEXTURE_2D, 0);
return 0;
}
void freeFont(TTF_Font *font) {
if (font) {
TTF_CloseFont(font);
font = NULL;
}
}