Pong now uses installed fonts using fontconfig

This commit is contained in:
Pradana AUMARS 2021-06-29 15:07:19 +02:00
parent b9df2a3cf0
commit 9a069cfa84
4 changed files with 36 additions and 6 deletions

4
README
View File

@ -3,4 +3,6 @@ A game of Pong.
Requirements Requirements
=========== ===========
* sdl2 * sdl2
* sdl2-ttf * sdl2-ttf
* fontconfig
* Liberation Mono fonts

View File

@ -10,5 +10,5 @@ pong_SOURCES = \
racket.h \ racket.h \
score.c \ score.c \
score.h score.h
pong_CFLAGS = $(SDL_CFLAGS) `pkg-config --cflags SDL2_ttf` pong_CFLAGS = $(SDL_CFLAGS) `pkg-config --cflags SDL2_ttf fontconfig`
pong_LDFLAGS = $(SDL_LIBS) `pkg-config --libs SDL2_ttf` -lm pong_LDFLAGS = $(SDL_LIBS) `pkg-config --libs SDL2_ttf fontconfig` -lm

Binary file not shown.

View File

@ -1,19 +1,47 @@
#include <fontconfig/fontconfig.h>
#include "score.h" #include "score.h"
static const char font_path[] = "src/assets/LiberationMono-Bold.ttf"; static const FcChar8 *SCORE_FONT_FAMILY = "Liberation Mono";
static const FcChar8 *SCORE_FONT_STYLE = "Bold";
static const int SCORE_FONT_PTSIZE = 16;
Score* Score*
Score_init(int posX, int posY) { Score_init(int posX, int posY) {
FcConfig *config = FcInitLoadConfigAndFonts();
FcPattern *pat = FcPatternCreate();
FcObjectSet* os = FcObjectSetBuild (FC_FAMILY, FC_STYLE, FC_LANG, FC_FILE, (char *) 0);
FcFontSet* fs = FcFontList(config, pat, os);
FcChar8 *file, *style, *family;
for (int i=0; fs && i < fs->nfont; ++i) {
FcPattern* font = fs->fonts[i];
if (FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch
&& FcPatternGetString(font, FC_FAMILY, 0, &family) == FcResultMatch
&& FcPatternGetString(font, FC_STYLE, 0, &style) == FcResultMatch
&& strcmp(SCORE_FONT_FAMILY, family) == 0
&& strcmp(SCORE_FONT_STYLE, style) == 0) {
break;
}
}
Score *s = malloc(sizeof(Score)); Score *s = malloc(sizeof(Score));
*(int *)&s->POSX = posX; *(int *)&s->POSX = posX;
*(int *)&s->POSY = posY; *(int *)&s->POSY = posY;
s->score = 0; s->score = 0;
s->font = TTF_OpenFont(font_path, 16); s->font = TTF_OpenFont(file, SCORE_FONT_PTSIZE);
if (s->font == NULL) { if (s->font == NULL) {
printf("TTF_OpenFont fail with path '%s'. TTF_Error: %s\n", font_path, TTF_GetError()); printf("TTF_OpenFont fail with path '%s'. TTF_Error: %s\n", file, TTF_GetError());
free(s); free(s);
return NULL; return NULL;
} }
FcFontSetDestroy(fs);
FcObjectSetDestroy(os);
FcPatternDestroy(pat);
FcConfigDestroy(config);
s->textColor = (SDL_Color) { 255, 255, 255 }; s->textColor = (SDL_Color) { 255, 255, 255 };
return s; return s;
} }