diff --git a/includes/animations.h b/includes/animations.h index 2d417ba..7532ec7 100644 --- a/includes/animations.h +++ b/includes/animations.h @@ -1,11 +1,10 @@ #ifndef DEMO_ANIMATION_HPP #define DEMO_ANIMATION_HPP 1 -#include #include -#include #include "audio.h" +#include "font.h" // Dimensions de la fenêtre extern GLuint _dims[]; diff --git a/includes/font.h b/includes/font.h new file mode 100644 index 0000000..6036b4d --- /dev/null +++ b/includes/font.h @@ -0,0 +1,21 @@ +#ifndef DEMO_FONT_HPP +#define DEMO_FONT_HPP 1 + +#include +#include + +/* Charge une police + * Renvoie 1 en cas de problème d'initialisation + * Renvoie 2 en cas de problème de chargement de la police + * Renvoie 0 en cas de succès */ +int initFont(TTF_Font **, char *, int); + +/* Ecris un texte sur une texture + * Renvoie 1 en cas de problème d'écriture sur la surface + * Renvoie 0 en cas de succès */ +int writeText(GLuint *, TTF_Font *, char *, SDL_Color); + +// Libère une police de la mémoire +void freeFont(TTF_Font *); + +#endif diff --git a/src/credits.c b/src/credits.c index 74c1a6c..76d5c77 100644 --- a/src/credits.c +++ b/src/credits.c @@ -30,50 +30,25 @@ static void init(void) { gl4duGenMatrix(GL_FLOAT, "proj"); _quadId = gl4dgGenQuadf(); - // Font - char *text = " CRÉDITS\n\n" - "Concours API8 — 7e édition\n" - "Font: fontesk et fontsquirrel\n" - "Audio: beepbox\n" - "Librairies: GL4D, SDL2 et extensions"; - char *fontName = "fonts/Instrument.ttf"; - - SDL_Color c = {255, 255, 255, 255}; - SDL_Surface *d, *s; + // Charge la police TTF_Font *font = NULL; - - if (TTF_Init() == -1) { - fprintf(stderr, "Erreur TTF : %s\n", TTF_GetError()); - exit(3); + if (initFont(&font, "fonts/Instrument.ttf", 100)) { + exit(1); } - glGenTextures(1, &_textTexId); - glBindTexture(GL_TEXTURE_2D, _textTexId); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - - if (!(font = TTF_OpenFont(fontName, 100))) { - fprintf(stderr, "Erreur TTF : %s\n", TTF_GetError()); - exit(3); + // Ecrit avec la police sur une texture + if (writeText(&_textTexId, font, + " CRÉDITS\n\n" + "Concours API8 — 7e édition\n" + "Font: fontesk et fontsquirrel\n" + "Audio: beepbox\n" + "Librairies: GL4D, SDL2 et extensions", + (SDL_Color){255, 255, 255, 255})) { + exit(1); } - if ((d = TTF_RenderUTF8_Blended_Wrapped(font, text, c, 0)) == NULL) { - TTF_CloseFont(font); - fprintf(stderr, "Erreur TTF : TTF_RenderUTF8_Blended_Wrapped\n"); - exit(3); - } - - 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); - TTF_CloseFont(font); - glBindTexture(GL_TEXTURE_2D, 0); + // Libère la police de la mémoire + freeFont(font); } static void draw(void) { diff --git a/src/font.c b/src/font.c new file mode 100644 index 0000000..aa5333e --- /dev/null +++ b/src/font.c @@ -0,0 +1,50 @@ +#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; + } +}