From f893c5759888856dd2293485184c3e56999f8012 Mon Sep 17 00:00:00 2001 From: Pradana AUMARS Date: Tue, 29 Jun 2021 16:20:40 +0200 Subject: [PATCH] Compartementalised text rendering of score struct --- src/Makefile.am | 4 ++- src/score.c | 66 +++------------------------------------------ src/score.h | 8 +++--- src/text.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++ src/text.h | 22 +++++++++++++++ 5 files changed, 104 insertions(+), 68 deletions(-) create mode 100644 src/text.c create mode 100644 src/text.h diff --git a/src/Makefile.am b/src/Makefile.am index cda7906..33665aa 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -9,6 +9,8 @@ pong_SOURCES = \ racket.c \ racket.h \ score.c \ - score.h + score.h \ + text.c \ + text.h pong_CFLAGS = $(SDL_CFLAGS) `pkg-config --cflags SDL2_ttf fontconfig` pong_LDFLAGS = $(SDL_LIBS) `pkg-config --libs SDL2_ttf fontconfig` -lm diff --git a/src/score.c b/src/score.c index addc6f3..ac67ae4 100644 --- a/src/score.c +++ b/src/score.c @@ -1,84 +1,24 @@ -#include - #include "score.h" -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) { Score *s = malloc(sizeof(Score)); - *(int *)&s->POSX = posX; - *(int *)&s->POSY = posY; s->score = 0; - - 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; - } - } + s->text = Text_init(posX, posY, SCORE_FONT_STYLE, SCORE_FONT_PTSIZE); return s; } void Score_free(Score *s) { - if (SCORE_FONT != NULL) { - TTF_CloseFont(SCORE_FONT); - SCORE_FONT = NULL; - } + Text_free(s->text); free(s); } int Score_render(const Score *s, SDL_Renderer *renderer) { char score_str[3]; - SDL_Rect scoreRect = { s->POSX , s->POSY, SCORE_WIDTH, SCORE_HEIGHT }; - sprintf(score_str, "%i", s->score); - 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; - } - SDL_Texture *scoreTexture = SDL_CreateTextureFromSurface(renderer, scoreSurface); - if (scoreTexture == NULL) { - printf("Texture rendering failed. SDL_Error: %s\n", SDL_GetError()); - SDL_FreeSurface(scoreSurface); - return 1; - } - SDL_RenderCopy(renderer, scoreTexture, NULL, &scoreRect); - - SDL_FreeSurface(scoreSurface); - SDL_DestroyTexture(scoreTexture); - + Text_render(s->text, renderer, score_str, SCORE_WIDTH, SCORE_HEIGHT); return 0; } diff --git a/src/score.h b/src/score.h index 421c1f1..0c11bee 100644 --- a/src/score.h +++ b/src/score.h @@ -1,16 +1,16 @@ #ifndef SCORE_H #define SCORE_H -#include -#include +#include "text.h" static const int SCORE_WIDTH = 50; static const int SCORE_HEIGHT = 50; +static const FcChar8 *SCORE_FONT_STYLE = "Bold"; +static const int SCORE_FONT_PTSIZE = 16; typedef struct Score { - const int POSX; - const int POSY; int score; + Text *text; } Score; Score* Score_init(int posX, int posY); diff --git a/src/text.c b/src/text.c new file mode 100644 index 0000000..b59ae6a --- /dev/null +++ b/src/text.c @@ -0,0 +1,72 @@ +#include "text.h" + +Text* +Text_init(int posX, int posY, const FcChar8 *TEXT_FONT_STYLE, int TEXT_FONT_PTSIZE) { + Text *t = malloc(sizeof(Text)); + *(int *)&t->POSX = posX; + *(int *)&t->POSY = posY; + + if (TEXT_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, &TEXT_FONT_FILE) == FcResultMatch + && FcPatternGetString(font, FC_FAMILY, 0, &family) == FcResultMatch + && FcPatternGetString(font, FC_STYLE, 0, &style) == FcResultMatch + && strcmp(TEXT_FONT_FAMILY, family) == 0 + && strcmp(TEXT_FONT_STYLE, style) == 0) { + break; + } + } + TEXT_FONT = TTF_OpenFont(TEXT_FONT_FILE, TEXT_FONT_PTSIZE); + FcFontSetDestroy(fs); + FcObjectSetDestroy(os); + FcPatternDestroy(pat); + FcConfigDestroy(config); + if (TEXT_FONT == NULL) { + printf("TTF_OpenFont fail with path '%s'. TTF_Error: %s\n", TEXT_FONT_FILE, TTF_GetError()); + free(t); + return NULL; + } + } + + return t; +} + +void +Text_free(Text *t) { + if (TEXT_FONT != NULL) { + TTF_CloseFont(TEXT_FONT); + TEXT_FONT = NULL; + } + free(t); +} + +int +Text_render(const Text* text, SDL_Renderer *renderer, const char *str, int width, int height) { + SDL_Rect textRect = { text->POSX , text->POSY, width, height }; + + SDL_Surface *textSurface = TTF_RenderText_Solid(TEXT_FONT, str, TEXT_FONT_COLOR); + if (textSurface == NULL) { + printf("Text rendering failed. TTF_Error: %s\n", TTF_GetError()); + return 1; + } + SDL_Texture *textTexture = SDL_CreateTextureFromSurface(renderer, textSurface); + if (textTexture == NULL) { + printf("Texture rendering failed. SDL_Error: %s\n", SDL_GetError()); + SDL_FreeSurface(textSurface); + return 1; + } + SDL_RenderCopy(renderer, textTexture, NULL, &textRect); + + SDL_FreeSurface(textSurface); + SDL_DestroyTexture(textTexture); + + return 0; +} diff --git a/src/text.h b/src/text.h new file mode 100644 index 0000000..f098942 --- /dev/null +++ b/src/text.h @@ -0,0 +1,22 @@ +#ifndef TEXT_H +#define TEXT_H + +#include +#include +#include + +static const FcChar8 *TEXT_FONT_FAMILY = "Liberation Mono"; +static TTF_Font *TEXT_FONT = NULL; +static FcChar8 *TEXT_FONT_FILE = NULL; +static const SDL_Color TEXT_FONT_COLOR = { 255, 255, 255 }; + +typedef struct Text { + const int POSX; + const int POSY; +} Text; + +Text* Text_init(int posX, int posY, const FcChar8 *font_style, int font_ptsize); +void Text_free(Text* text); +int Text_render(const Text* text, SDL_Renderer *renderer, const char *str, int width, int height); + +#endif