Score is now displayed on the window
* Added Score structure * Added Liberation Mono Bold font in src/assets/ * Racket structure has Score member * General cleanup and improvements * BUG: font_path string is dependent on where the binary is run
This commit is contained in:
parent
4ac1d84ef6
commit
45df3ec812
@ -7,6 +7,8 @@ pong_SOURCES = \
|
|||||||
ball.c \
|
ball.c \
|
||||||
ball.h \
|
ball.h \
|
||||||
racket.c \
|
racket.c \
|
||||||
racket.h
|
racket.h \
|
||||||
|
score.c \
|
||||||
|
score.h
|
||||||
pong_CFLAGS = $(SDL_CFLAGS) `pkg-config --cflags SDL2_ttf`
|
pong_CFLAGS = $(SDL_CFLAGS) `pkg-config --cflags SDL2_ttf`
|
||||||
pong_LDFLAGS = $(SDL_LIBS) `pkg-config --libs SDL2_ttf` -lm
|
pong_LDFLAGS = $(SDL_LIBS) `pkg-config --libs SDL2_ttf` -lm
|
||||||
|
BIN
src/assets/LiberationMono-Bold.ttf
Normal file
BIN
src/assets/LiberationMono-Bold.ttf
Normal file
Binary file not shown.
34
src/pong.c
34
src/pong.c
@ -14,7 +14,18 @@ Pong_init() {
|
|||||||
|
|
||||||
p->ball = Ball_init(p->SCREEN_WIDTH, p->SCREEN_HEIGHT, p->BALL_POSX);
|
p->ball = Ball_init(p->SCREEN_WIDTH, p->SCREEN_HEIGHT, p->BALL_POSX);
|
||||||
p->racketL = Racket_init(p->SCREEN_WIDTH, p->SCREEN_HEIGHT, p->RACKET_POSX, p->SCREEN_HEIGHT/2, SDL_SCANCODE_E, SDL_SCANCODE_D);
|
p->racketL = Racket_init(p->SCREEN_WIDTH, p->SCREEN_HEIGHT, p->RACKET_POSX, p->SCREEN_HEIGHT/2, SDL_SCANCODE_E, SDL_SCANCODE_D);
|
||||||
|
if (p->racketL == NULL) {
|
||||||
|
Ball_free(p->ball);
|
||||||
|
free(p);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
p->racketR = Racket_init(p->SCREEN_WIDTH, p->SCREEN_HEIGHT, p->SCREEN_WIDTH - p->RACKET_POSX, p->SCREEN_HEIGHT/2, SDL_SCANCODE_I, SDL_SCANCODE_K);
|
p->racketR = Racket_init(p->SCREEN_WIDTH, p->SCREEN_HEIGHT, p->SCREEN_WIDTH - p->RACKET_POSX, p->SCREEN_HEIGHT/2, SDL_SCANCODE_I, SDL_SCANCODE_K);
|
||||||
|
if (p->racketR == NULL) {
|
||||||
|
Ball_free(p->ball);
|
||||||
|
Racket_free(p->racketL);
|
||||||
|
free(p);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
p->window = SDL_CreateWindow( "Pong", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, p->SCREEN_WIDTH, p->SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
|
p->window = SDL_CreateWindow( "Pong", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, p->SCREEN_WIDTH, p->SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
|
||||||
if( p->window == NULL )
|
if( p->window == NULL )
|
||||||
@ -23,6 +34,7 @@ Pong_init() {
|
|||||||
Pong_free(p);
|
Pong_free(p);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
p->renderer = SDL_CreateRenderer(p->window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
p->renderer = SDL_CreateRenderer(p->window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
||||||
p->screenSurface = SDL_GetWindowSurface( p->window );
|
p->screenSurface = SDL_GetWindowSurface( p->window );
|
||||||
|
|
||||||
@ -36,6 +48,7 @@ Pong_init() {
|
|||||||
void
|
void
|
||||||
Pong_free(Pong *p) {
|
Pong_free(Pong *p) {
|
||||||
SDL_DestroyRenderer(p->renderer);
|
SDL_DestroyRenderer(p->renderer);
|
||||||
|
SDL_FreeSurface(p->screenSurface);
|
||||||
SDL_DestroyWindow(p->window);
|
SDL_DestroyWindow(p->window);
|
||||||
Ball_free(p->ball);
|
Ball_free(p->ball);
|
||||||
Racket_free(p->racketL);
|
Racket_free(p->racketL);
|
||||||
@ -49,14 +62,17 @@ Pong_clear(Pong *p) {
|
|||||||
SDL_RenderClear( p->renderer );
|
SDL_RenderClear( p->renderer );
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
int
|
||||||
Pong_render(Pong *p) {
|
Pong_render(Pong *p) {
|
||||||
Pong_clear(p);
|
Pong_clear(p);
|
||||||
Ball_render(p->ball, p->renderer);
|
const 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 (error > 0) {
|
||||||
|
return error;
|
||||||
|
}
|
||||||
SDL_RenderPresent(p->renderer);
|
SDL_RenderPresent(p->renderer);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -72,13 +88,13 @@ Ball_move(Ball *b, Pong *p)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (b->posX < 0) {
|
if (b->posX < 0) {
|
||||||
p->racketR->score++;
|
p->racketR->score->score++;
|
||||||
Ball_reset(b, -1);
|
Ball_reset(b, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
else if(b->posX + b->BALL_WIDTH > b->SCREEN_WIDTH)
|
else if(b->posX + b->BALL_WIDTH > b->SCREEN_WIDTH)
|
||||||
{
|
{
|
||||||
p->racketL->score++;
|
p->racketL->score->score++;
|
||||||
Ball_reset(b, 1);
|
Ball_reset(b, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,7 +124,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);
|
||||||
Pong_render(p);
|
if (Pong_render(p) > 0) quit = true;
|
||||||
Pong_score_run(p);
|
Pong_score_run(p);
|
||||||
}
|
}
|
||||||
Pong_score_end(p);
|
Pong_score_end(p);
|
||||||
@ -116,7 +132,7 @@ Pong_run(Pong *p) {
|
|||||||
|
|
||||||
void
|
void
|
||||||
Pong_score(const Pong *p, char c) {
|
Pong_score(const Pong *p, char c) {
|
||||||
printf("SCORE: L %i | R %i", p->racketL->score, p->racketR->score);
|
printf("SCORE: L %i | R %i", p->racketL->score->score, p->racketR->score->score);
|
||||||
putc(c, stdout);
|
putc(c, stdout);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,7 +11,6 @@ typedef struct Pong {
|
|||||||
SDL_Window* window;
|
SDL_Window* window;
|
||||||
SDL_Surface* screenSurface;
|
SDL_Surface* screenSurface;
|
||||||
SDL_Renderer* renderer;
|
SDL_Renderer* renderer;
|
||||||
//SDL_Texture* texture;
|
|
||||||
SDL_Event e;
|
SDL_Event e;
|
||||||
Ball *ball;
|
Ball *ball;
|
||||||
const int BALL_POSX;
|
const int BALL_POSX;
|
||||||
@ -23,7 +22,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);
|
||||||
void Pong_render(Pong *p);
|
int Pong_render(Pong *p);
|
||||||
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_score(const Pong *p, char c);
|
void Pong_score(const Pong *p, char c);
|
||||||
|
10
src/racket.c
10
src/racket.c
@ -12,7 +12,11 @@ Racket_init(int screen_width, int screen_height, int posX, int posY, SDL_Keycode
|
|||||||
*(int *)&r->posX = posX;
|
*(int *)&r->posX = posX;
|
||||||
r->posY = posY;
|
r->posY = posY;
|
||||||
*(int *)&r->vel = 5;
|
*(int *)&r->vel = 5;
|
||||||
r->score = 0;
|
r->score = Score_init(r->posX, 20);
|
||||||
|
if (r->score == NULL) {
|
||||||
|
free(r);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
*(SDL_Keycode *)&r->up = up;
|
*(SDL_Keycode *)&r->up = up;
|
||||||
*(SDL_Keycode *)&r->down = down;
|
*(SDL_Keycode *)&r->down = down;
|
||||||
r->updown = 0;
|
r->updown = 0;
|
||||||
@ -22,6 +26,7 @@ Racket_init(int screen_width, int screen_height, int posX, int posY, SDL_Keycode
|
|||||||
void
|
void
|
||||||
Racket_free(Racket *r)
|
Racket_free(Racket *r)
|
||||||
{
|
{
|
||||||
|
Score_free(r->score);
|
||||||
free(r);
|
free(r);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,6 +38,9 @@ Racket_render(const Racket *r, SDL_Renderer *renderer) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
SDL_RenderFillRect(renderer, &rect);
|
SDL_RenderFillRect(renderer, &rect);
|
||||||
|
if (Score_render(r->score, renderer) > 0) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,6 +3,8 @@
|
|||||||
|
|
||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
|
|
||||||
|
#include "score.h"
|
||||||
|
|
||||||
typedef struct Racket {
|
typedef struct Racket {
|
||||||
const int RACKET_WIDTH;
|
const int RACKET_WIDTH;
|
||||||
const int RACKET_HEIGHT;
|
const int RACKET_HEIGHT;
|
||||||
@ -12,7 +14,7 @@ typedef struct Racket {
|
|||||||
const int posX;
|
const int posX;
|
||||||
int posY;
|
int posY;
|
||||||
const int vel;
|
const int vel;
|
||||||
int score;
|
Score* score;
|
||||||
const SDL_Keycode up;
|
const SDL_Keycode up;
|
||||||
const SDL_Keycode down;
|
const SDL_Keycode down;
|
||||||
int updown;
|
int updown;
|
||||||
|
50
src/score.c
Normal file
50
src/score.c
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
#include "score.h"
|
||||||
|
|
||||||
|
static const char font_path[] = "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, 50, 50 };
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
19
src/score.h
Normal file
19
src/score.h
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#ifndef SCORE_H
|
||||||
|
#define SCORE_H
|
||||||
|
|
||||||
|
#include <SDL.h>
|
||||||
|
#include <SDL_ttf.h>
|
||||||
|
|
||||||
|
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);
|
||||||
|
void Score_free(Score* score);
|
||||||
|
int Score_render(const Score* score, SDL_Renderer *renderer);
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in New Issue
Block a user