51 lines
1.3 KiB
C
51 lines
1.3 KiB
C
|
#include "../includes/font.h"
|
||
|
#include "SDL_ttf.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, char *text, SDL_Color color) {
|
||
|
glGenTextures(1, _textTexId);
|
||
|
glBindTexture(GL_TEXTURE_2D, *_textTexId);
|
||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||
|
|
||
|
SDL_Surface *d, *s;
|
||
|
if ((d = TTF_RenderUTF8_Blended_Wrapped(font, text, color, 0)) == NULL) {
|
||
|
freeFont(font);
|
||
|
fprintf(stderr, "Erreur TTF : TTF_RenderUTF8_Blended_Wrapped\n");
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
|
assert((s = SDL_CreateRGBSurface(0, d->w, d->h, 32, R_MASK, G_MASK, B_MASK,
|
||
|
A_MASK)));
|
||
|
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;
|
||
|
}
|
||
|
}
|