51 lines
1.3 KiB
C
51 lines
1.3 KiB
C
#include "score.h"
|
|
|
|
static const char font_path[] = "src/assets/LiberationMono-Bold.ttf";
|
|
|
|
Score*
|
|
Score_init(int posX, int posY) {
|
|
Score *s = malloc(sizeof(Score));
|
|
*(int *)&s->POSX = posX;
|
|
*(int *)&s->POSY = posY;
|
|
s->score = 0;
|
|
s->font = TTF_OpenFont(font_path, 16);
|
|
if (s->font == NULL) {
|
|
printf("TTF_OpenFont fail with path '%s'. TTF_Error: %s\n", font_path, TTF_GetError());
|
|
free(s);
|
|
return NULL;
|
|
}
|
|
s->textColor = (SDL_Color) { 255, 255, 255 };
|
|
return s;
|
|
}
|
|
|
|
void
|
|
Score_free(Score *s) {
|
|
TTF_CloseFont(s->font);
|
|
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(s->font, score_str, s->textColor);
|
|
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);
|
|
|
|
return 0;
|
|
}
|