Fonts are initialised only once
This commit is contained in:
parent
9a069cfa84
commit
2ebcf624de
68
src/score.c
68
src/score.c
@ -4,51 +4,57 @@
|
||||
|
||||
static const FcChar8 *SCORE_FONT_FAMILY = "Liberation Mono";
|
||||
static const FcChar8 *SCORE_FONT_STYLE = "Bold";
|
||||
static TTF_Font *SCORE_FONT = NULL;
|
||||
static FcChar8 *SCORE_FONT_FILE = NULL;
|
||||
static const int SCORE_FONT_PTSIZE = 16;
|
||||
static const SDL_Color SCORE_FONT_COLOR = { 255, 255, 255 };
|
||||
|
||||
Score*
|
||||
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));
|
||||
*(int *)&s->POSX = posX;
|
||||
*(int *)&s->POSY = posY;
|
||||
s->score = 0;
|
||||
s->font = TTF_OpenFont(file, SCORE_FONT_PTSIZE);
|
||||
if (s->font == NULL) {
|
||||
printf("TTF_OpenFont fail with path '%s'. TTF_Error: %s\n", file, TTF_GetError());
|
||||
free(s);
|
||||
return NULL;
|
||||
|
||||
if (SCORE_FONT == NULL) {
|
||||
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 *style, *family;
|
||||
for (int i=0; fs && i < fs->nfont; ++i) {
|
||||
FcPattern* font = fs->fonts[i];
|
||||
if (FcPatternGetString(font, FC_FILE, 0, &SCORE_FONT_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_FONT = TTF_OpenFont(SCORE_FONT_FILE, SCORE_FONT_PTSIZE);
|
||||
FcFontSetDestroy(fs);
|
||||
FcObjectSetDestroy(os);
|
||||
FcPatternDestroy(pat);
|
||||
FcConfigDestroy(config);
|
||||
if (SCORE_FONT == NULL) {
|
||||
printf("TTF_OpenFont fail with path '%s'. TTF_Error: %s\n", SCORE_FONT_FILE, TTF_GetError());
|
||||
free(s);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
FcFontSetDestroy(fs);
|
||||
FcObjectSetDestroy(os);
|
||||
FcPatternDestroy(pat);
|
||||
FcConfigDestroy(config);
|
||||
|
||||
s->textColor = (SDL_Color) { 255, 255, 255 };
|
||||
return s;
|
||||
}
|
||||
|
||||
void
|
||||
Score_free(Score *s) {
|
||||
TTF_CloseFont(s->font);
|
||||
if (SCORE_FONT != NULL) {
|
||||
TTF_CloseFont(SCORE_FONT);
|
||||
SCORE_FONT = NULL;
|
||||
}
|
||||
free(s);
|
||||
}
|
||||
|
||||
@ -58,7 +64,7 @@ Score_render(const Score *s, SDL_Renderer *renderer) {
|
||||
SDL_Rect scoreRect = { s->POSX , s->POSY, SCORE_WIDTH, SCORE_HEIGHT };
|
||||
|
||||
sprintf(score_str, "%i", s->score);
|
||||
SDL_Surface *scoreSurface = TTF_RenderText_Solid(s->font, score_str, s->textColor);
|
||||
SDL_Surface *scoreSurface = TTF_RenderText_Solid(SCORE_FONT, score_str, SCORE_FONT_COLOR);
|
||||
if (scoreSurface == NULL) {
|
||||
printf("Text rendering failed. TTF_Error: %s\n", TTF_GetError());
|
||||
return 1;
|
||||
|
@ -11,8 +11,6 @@ typedef struct Score {
|
||||
const int POSX;
|
||||
const int POSY;
|
||||
int score;
|
||||
TTF_Font *font;
|
||||
SDL_Color textColor;
|
||||
} Score;
|
||||
|
||||
Score* Score_init(int posX, int posY);
|
||||
|
Loading…
Reference in New Issue
Block a user