move font stuff into a file

This commit is contained in:
Mylloon 2023-05-03 21:07:59 +02:00
parent 243de4a4fb
commit ebdf327f66
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
4 changed files with 86 additions and 41 deletions

View file

@ -1,11 +1,10 @@
#ifndef DEMO_ANIMATION_HPP #ifndef DEMO_ANIMATION_HPP
#define DEMO_ANIMATION_HPP 1 #define DEMO_ANIMATION_HPP 1
#include <GL4D/gl4dh.h>
#include <GL4D/gl4duw_SDL2.h> #include <GL4D/gl4duw_SDL2.h>
#include <SDL_ttf.h>
#include "audio.h" #include "audio.h"
#include "font.h"
// Dimensions de la fenêtre // Dimensions de la fenêtre
extern GLuint _dims[]; extern GLuint _dims[];

21
includes/font.h Normal file
View file

@ -0,0 +1,21 @@
#ifndef DEMO_FONT_HPP
#define DEMO_FONT_HPP 1
#include <GL4D/gl4dh.h>
#include <SDL_ttf.h>
/* 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

View file

@ -30,50 +30,25 @@ static void init(void) {
gl4duGenMatrix(GL_FLOAT, "proj"); gl4duGenMatrix(GL_FLOAT, "proj");
_quadId = gl4dgGenQuadf(); _quadId = gl4dgGenQuadf();
// Font // Charge la police
char *text = " CRÉDITS\n\n" TTF_Font *font = NULL;
if (initFont(&font, "fonts/Instrument.ttf", 100)) {
exit(1);
}
// Ecrit avec la police sur une texture
if (writeText(&_textTexId, font,
" CRÉDITS\n\n"
"Concours API8 — 7e édition\n" "Concours API8 — 7e édition\n"
"Font: fontesk et fontsquirrel\n" "Font: fontesk et fontsquirrel\n"
"Audio: beepbox\n" "Audio: beepbox\n"
"Librairies: GL4D, SDL2 et extensions"; "Librairies: GL4D, SDL2 et extensions",
char *fontName = "fonts/Instrument.ttf"; (SDL_Color){255, 255, 255, 255})) {
exit(1);
SDL_Color c = {255, 255, 255, 255};
SDL_Surface *d, *s;
TTF_Font *font = NULL;
if (TTF_Init() == -1) {
fprintf(stderr, "Erreur TTF : %s\n", TTF_GetError());
exit(3);
} }
glGenTextures(1, &_textTexId); // Libère la police de la mémoire
glBindTexture(GL_TEXTURE_2D, _textTexId); freeFont(font);
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);
}
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);
} }
static void draw(void) { static void draw(void) {

50
src/font.c Normal file
View file

@ -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;
}
}