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

55 lines
1.4 KiB
C
Raw Normal View History

2023-05-03 21:07:59 +02:00
#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;
}
2023-05-03 21:14:21 +02:00
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;
}
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;
}
}