From 267e6f7d5dea6d4347839dcbfa2d557a8f3ba2b2 Mon Sep 17 00:00:00 2001 From: Pradana AUMARS Date: Tue, 29 Jun 2021 16:50:02 +0200 Subject: [PATCH] Add 'Press any key to begin...' message to the screen * Text font is initialised with each text struct (to be improved) --- src/pong.c | 21 ++++++++++++++------- src/pong.h | 2 +- src/text.c | 23 +++++++++-------------- src/text.h | 4 ++-- 4 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/pong.c b/src/pong.c index afb1c9c..8303564 100644 --- a/src/pong.c +++ b/src/pong.c @@ -57,14 +57,22 @@ Pong_clear(Pong *p) { } int -Pong_render(Pong *p) { +Pong_render(Pong *p, int start) { Pong_clear(p); - const int error = Ball_render(p->ball, p->renderer) + int error = Ball_render(p->ball, p->renderer) + Racket_render(p->racketL, p->renderer) + Racket_render(p->racketR, p->renderer); + + if (!start) { + Text *start_text = Text_init(50, 400, "Regular", 8); + const char *start_text_str = "Press any key to begin..."; + error += Text_render(start_text, p->renderer, start_text_str, 400, 50); + Text_free(start_text); + } if (error > 0) { return error; } + SDL_RenderPresent(p->renderer); return 0; } @@ -104,9 +112,8 @@ void Pong_run(Pong *p) { int start = false; int quit = false; - Pong_render(p); + Pong_render(p, start); while (!start) { - printf("Press any key to start...\r"); fflush(stdout); while( SDL_PollEvent( &(p->e) ) != 0 ) { @@ -114,8 +121,8 @@ Pong_run(Pong *p) { } } putc('\n', stdout); - - while (!quit) { + + while (!quit) { while( SDL_PollEvent( &(p->e) ) != 0 ) { @@ -130,7 +137,7 @@ Pong_run(Pong *p) { Ball_move(p->ball, p); Racket_move(p->racketL); Racket_move(p->racketR); - if (Pong_render(p) > 0) quit = true; + if (Pong_render(p, start) > 0) quit = true; Pong_status(p, quit); } } diff --git a/src/pong.h b/src/pong.h index 14eecb3..1aa3368 100644 --- a/src/pong.h +++ b/src/pong.h @@ -23,7 +23,7 @@ typedef struct Pong { Pong* Pong_init(); void Pong_free(Pong *p); void Pong_clear(Pong *p); -int Pong_render(Pong *p); +int Pong_render(Pong *p, int start); void Ball_move(Ball *b, Pong *p); void Pong_run(Pong *p); void Pong_status(const Pong *p, int quit); diff --git a/src/text.c b/src/text.c index b59ae6a..bbc5b43 100644 --- a/src/text.c +++ b/src/text.c @@ -5,8 +5,7 @@ Text_init(int posX, int posY, const FcChar8 *TEXT_FONT_STYLE, int TEXT_FONT_PTSI Text *t = malloc(sizeof(Text)); *(int *)&t->POSX = posX; *(int *)&t->POSY = posY; - - if (TEXT_FONT == NULL) { + FcConfig *config = FcInitLoadConfigAndFonts(); FcPattern *pat = FcPatternCreate(); @@ -16,7 +15,7 @@ Text_init(int posX, int posY, const FcChar8 *TEXT_FONT_STYLE, int TEXT_FONT_PTSI 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 + if (FcPatternGetString(font, FC_FILE, 0, &(t->TEXT_FONT_FILE)) == FcResultMatch && FcPatternGetString(font, FC_FAMILY, 0, &family) == FcResultMatch && FcPatternGetString(font, FC_STYLE, 0, &style) == FcResultMatch && strcmp(TEXT_FONT_FAMILY, family) == 0 @@ -24,35 +23,31 @@ Text_init(int posX, int posY, const FcChar8 *TEXT_FONT_STYLE, int TEXT_FONT_PTSI break; } } - TEXT_FONT = TTF_OpenFont(TEXT_FONT_FILE, TEXT_FONT_PTSIZE); + t->TEXT_FONT = TTF_OpenFont(t->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()); + if (t->TEXT_FONT == NULL) { + printf("TTF_OpenFont fail with path '%s'. TTF_Error: %s\n", t->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; - } + TTF_CloseFont(t->TEXT_FONT); 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 }; +Text_render(const Text* t, SDL_Renderer *renderer, const char *str, int width, int height) { + SDL_Rect textRect = { t->POSX , t->POSY, width, height }; - SDL_Surface *textSurface = TTF_RenderText_Solid(TEXT_FONT, str, TEXT_FONT_COLOR); + SDL_Surface *textSurface = TTF_RenderText_Solid(t->TEXT_FONT, str, TEXT_FONT_COLOR); if (textSurface == NULL) { printf("Text rendering failed. TTF_Error: %s\n", TTF_GetError()); return 1; diff --git a/src/text.h b/src/text.h index f098942..7a4c40d 100644 --- a/src/text.h +++ b/src/text.h @@ -6,13 +6,13 @@ #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; + TTF_Font *TEXT_FONT; + FcChar8 *TEXT_FONT_FILE; } Text; Text* Text_init(int posX, int posY, const FcChar8 *font_style, int font_ptsize);