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

58 lines
1.4 KiB
C
Raw Normal View History

2023-05-03 21:07:59 +02:00
#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;
}
2023-05-19 14:19:31 +02:00
int writeText(GLuint *_textTexId, TTF_Font *font, const char *text,
SDL_Color color) {
2023-05-03 21:07:59 +02:00
glGenTextures(1, _textTexId);
2023-05-08 15:27:50 +02:00
assert(_textTexId);
2023-05-03 21:07:59 +02:00
glBindTexture(GL_TEXTURE_2D, *_textTexId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
2023-05-08 15:31:06 +02:00
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
2023-05-03 21:07:59 +02:00
SDL_Surface *d, *s;
2023-05-04 10:36:15 +02:00
if (!(d = TTF_RenderUTF8_Blended_Wrapped(font, text, color, 0))) {
2023-05-03 21:07:59 +02:00
freeFont(font);
fprintf(stderr, "Erreur TTF : TTF_RenderUTF8_Blended_Wrapped\n");
return 1;
}
2023-05-04 10:36:15 +02:00
if (!(s = SDL_CreateRGBSurface(0, d->w, d->h, 32, R_MASK, G_MASK, B_MASK,
A_MASK))) {
2023-05-03 21:14:21 +02:00
freeFont(font);
fprintf(stderr, "Erreur SDL : %s\n", SDL_GetError());
return 2;
}
2023-05-03 21:07:59 +02:00
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;
}
}