Add 'Press any key to begin...' message to the screen

* Text font is initialised with each text struct (to be improved)
This commit is contained in:
Pradana AUMARS 2021-06-29 16:50:02 +02:00
parent 54ecb1f828
commit 267e6f7d5d
4 changed files with 26 additions and 24 deletions

View File

@ -57,14 +57,22 @@ Pong_clear(Pong *p) {
} }
int int
Pong_render(Pong *p) { Pong_render(Pong *p, int start) {
Pong_clear(p); 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->racketL, p->renderer)
+ Racket_render(p->racketR, 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) { if (error > 0) {
return error; return error;
} }
SDL_RenderPresent(p->renderer); SDL_RenderPresent(p->renderer);
return 0; return 0;
} }
@ -104,9 +112,8 @@ void
Pong_run(Pong *p) { Pong_run(Pong *p) {
int start = false; int start = false;
int quit = false; int quit = false;
Pong_render(p); Pong_render(p, start);
while (!start) { while (!start) {
printf("Press any key to start...\r");
fflush(stdout); fflush(stdout);
while( SDL_PollEvent( &(p->e) ) != 0 ) while( SDL_PollEvent( &(p->e) ) != 0 )
{ {
@ -130,7 +137,7 @@ Pong_run(Pong *p) {
Ball_move(p->ball, p); Ball_move(p->ball, p);
Racket_move(p->racketL); Racket_move(p->racketL);
Racket_move(p->racketR); Racket_move(p->racketR);
if (Pong_render(p) > 0) quit = true; if (Pong_render(p, start) > 0) quit = true;
Pong_status(p, quit); Pong_status(p, quit);
} }
} }

View File

@ -23,7 +23,7 @@ typedef struct Pong {
Pong* Pong_init(); Pong* Pong_init();
void Pong_free(Pong *p); void Pong_free(Pong *p);
void Pong_clear(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 Ball_move(Ball *b, Pong *p);
void Pong_run(Pong *p); void Pong_run(Pong *p);
void Pong_status(const Pong *p, int quit); void Pong_status(const Pong *p, int quit);

View File

@ -6,7 +6,6 @@ Text_init(int posX, int posY, const FcChar8 *TEXT_FONT_STYLE, int TEXT_FONT_PTSI
*(int *)&t->POSX = posX; *(int *)&t->POSX = posX;
*(int *)&t->POSY = posY; *(int *)&t->POSY = posY;
if (TEXT_FONT == NULL) {
FcConfig *config = FcInitLoadConfigAndFonts(); FcConfig *config = FcInitLoadConfigAndFonts();
FcPattern *pat = FcPatternCreate(); 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; FcChar8 *style, *family;
for (int i=0; fs && i < fs->nfont; ++i) { for (int i=0; fs && i < fs->nfont; ++i) {
FcPattern* font = fs->fonts[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_FAMILY, 0, &family) == FcResultMatch
&& FcPatternGetString(font, FC_STYLE, 0, &style) == FcResultMatch && FcPatternGetString(font, FC_STYLE, 0, &style) == FcResultMatch
&& strcmp(TEXT_FONT_FAMILY, family) == 0 && 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; 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); FcFontSetDestroy(fs);
FcObjectSetDestroy(os); FcObjectSetDestroy(os);
FcPatternDestroy(pat); FcPatternDestroy(pat);
FcConfigDestroy(config); FcConfigDestroy(config);
if (TEXT_FONT == NULL) { if (t->TEXT_FONT == NULL) {
printf("TTF_OpenFont fail with path '%s'. TTF_Error: %s\n", TEXT_FONT_FILE, TTF_GetError()); printf("TTF_OpenFont fail with path '%s'. TTF_Error: %s\n", t->TEXT_FONT_FILE, TTF_GetError());
free(t); free(t);
return NULL; return NULL;
} }
}
return t; return t;
} }
void void
Text_free(Text *t) { Text_free(Text *t) {
if (TEXT_FONT != NULL) { TTF_CloseFont(t->TEXT_FONT);
TTF_CloseFont(TEXT_FONT);
TEXT_FONT = NULL;
}
free(t); free(t);
} }
int int
Text_render(const Text* text, SDL_Renderer *renderer, const char *str, int width, int height) { Text_render(const Text* t, SDL_Renderer *renderer, const char *str, int width, int height) {
SDL_Rect textRect = { text->POSX , text->POSY, width, 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) { if (textSurface == NULL) {
printf("Text rendering failed. TTF_Error: %s\n", TTF_GetError()); printf("Text rendering failed. TTF_Error: %s\n", TTF_GetError());
return 1; return 1;

View File

@ -6,13 +6,13 @@
#include <fontconfig/fontconfig.h> #include <fontconfig/fontconfig.h>
static const FcChar8 *TEXT_FONT_FAMILY = "Liberation Mono"; 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 }; static const SDL_Color TEXT_FONT_COLOR = { 255, 255, 255 };
typedef struct Text { typedef struct Text {
const int POSX; const int POSX;
const int POSY; const int POSY;
TTF_Font *TEXT_FONT;
FcChar8 *TEXT_FONT_FILE;
} Text; } Text;
Text* Text_init(int posX, int posY, const FcChar8 *font_style, int font_ptsize); Text* Text_init(int posX, int posY, const FcChar8 *font_style, int font_ptsize);