diff --git a/src/ball.c b/src/ball.c index 7708558..c9e2367 100644 --- a/src/ball.c +++ b/src/ball.c @@ -9,11 +9,6 @@ Ball_init(int screen_width, int screen_height, int posX_start) { srand(time(NULL)); Ball *b = malloc(sizeof(Ball)); - *(int *)&b->BALL_WIDTH = 20; - *(int *)&b->BALL_HEIGHT = 20; - *(int *)&b->BALL_VELOCITY = 7; - *(int *)&b->BALL_ANGLE_MIN_OFFSET = 10; - *(int *)&b->BALL_ANGLE_MAX_OFFSET = 60; *(int *)&b->BALL_POSX_START = posX_start; *(int *)&b->SCREEN_WIDTH = screen_width; *(int *)&b->SCREEN_HEIGHT = screen_height; @@ -26,8 +21,8 @@ Ball_reset(Ball *b, int direction) { // 120 deg angle with norm horizontal vector int angle; do { - angle = rand() % b->BALL_ANGLE_MAX_OFFSET*2 + b->BALL_ANGLE_MAX_OFFSET/2 - 90; - } while (-b->BALL_ANGLE_MIN_OFFSET <= angle && angle <= b->BALL_ANGLE_MIN_OFFSET); + angle = rand() % BALL_ANGLE_MAX_OFFSET*2 + BALL_ANGLE_MAX_OFFSET/2 - 90; + } while (-BALL_ANGLE_MIN_OFFSET <= angle && angle <= BALL_ANGLE_MIN_OFFSET); if (direction < 0) { b->posX = b->BALL_POSX_START; } @@ -35,8 +30,8 @@ Ball_reset(Ball *b, int direction) { b->posX = b->SCREEN_WIDTH - b->BALL_POSX_START; } b->posY = b->SCREEN_HEIGHT/2; - b->velX = -direction * b->BALL_VELOCITY * cos(angle * M_PI / 180); - b->velY = b->BALL_VELOCITY * sin(angle * M_PI / 180); + b->velX = -direction * BALL_VELOCITY * cos(angle * M_PI / 180); + b->velY = BALL_VELOCITY * sin(angle * M_PI / 180); } void @@ -48,14 +43,14 @@ Ball_free(Ball *b) int Ball_collision(const Ball *b, const Racket *r) { const int leftB = b->posX; - const int rightB = leftB + b->BALL_WIDTH; + const int rightB = leftB + BALL_WIDTH; const int upB = b->posY; - const int downB = upB + b->BALL_HEIGHT; + const int downB = upB + BALL_HEIGHT; const int leftR = r->posX; - const int rightR = leftR + r->RACKET_WIDTH; + const int rightR = leftR + RACKET_WIDTH; const int upR = r->posY; - const int downR = upR + r->RACKET_HEIGHT; + const int downR = upR + RACKET_HEIGHT; if(downB <= upR || upB >= downR @@ -69,7 +64,7 @@ Ball_collision(const Ball *b, const Racket *r) { int Ball_render(const Ball *b, SDL_Renderer *renderer) { - SDL_Rect rect = { b->posX, b->posY, b->BALL_WIDTH, b->BALL_HEIGHT }; + SDL_Rect rect = { b->posX, b->posY, BALL_WIDTH, BALL_HEIGHT }; if (SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255) < 0) { printf("SDL_SetRenderDrawColor failed! SDL_Error: %s\n", SDL_GetError()); return 1; diff --git a/src/ball.h b/src/ball.h index bb2dc5e..d8577bc 100644 --- a/src/ball.h +++ b/src/ball.h @@ -6,15 +6,16 @@ #include "racket.h" +static const int BALL_WIDTH = 20; +static const int BALL_HEIGHT = 20; +static const int BALL_VELOCITY = 7; +static const int BALL_ANGLE_MIN_OFFSET = 10; +static const int BALL_ANGLE_MAX_OFFSET = 60; + typedef struct Ball { - const int BALL_WIDTH; - const int BALL_HEIGHT; - const int BALL_VELOCITY; - const int BALL_POSX_START; - const int BALL_ANGLE_MIN_OFFSET; - const int BALL_ANGLE_MAX_OFFSET; const int SCREEN_WIDTH; const int SCREEN_HEIGHT; + const int BALL_POSX_START; int posX; int posY; int velX; diff --git a/src/pong.c b/src/pong.c index 1a4068f..968c755 100644 --- a/src/pong.c +++ b/src/pong.c @@ -6,20 +6,14 @@ Pong* Pong_init() { Pong *p = malloc(sizeof(Pong)); - // Cast away the const - *(int *)&p->SCREEN_WIDTH = 640; - *(int *)&p->SCREEN_HEIGHT = 480; - *(int *)&p->BALL_POSX = 100; - *(int *)&p->RACKET_POSX = 50; - - 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->ball = Ball_init(PONG_SCREEN_WIDTH, PONG_SCREEN_HEIGHT, PONG_BALL_POSX); + p->racketL = Racket_init(PONG_SCREEN_WIDTH, PONG_SCREEN_HEIGHT, PONG_RACKET_POSX, PONG_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(PONG_SCREEN_WIDTH, PONG_SCREEN_HEIGHT, PONG_SCREEN_WIDTH - PONG_RACKET_POSX - RACKET_WIDTH, PONG_SCREEN_HEIGHT/2, SDL_SCANCODE_I, SDL_SCANCODE_K); if (p->racketR == NULL) { Ball_free(p->ball); Racket_free(p->racketL); @@ -27,7 +21,7 @@ Pong_init() { 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, PONG_SCREEN_WIDTH, PONG_SCREEN_HEIGHT, SDL_WINDOW_SHOWN ); if( p->window == NULL ) { printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() ); @@ -92,14 +86,14 @@ Ball_move(Ball *b, Pong *p) Ball_reset(b, -1); } - else if(b->posX + b->BALL_WIDTH > b->SCREEN_WIDTH) + else if(b->posX + BALL_WIDTH > b->SCREEN_WIDTH) { p->racketL->score->score++; Ball_reset(b, 1); } b->posY += b->velY; - if ((b->posY < 0) || (b->posY + b->BALL_HEIGHT > b->SCREEN_HEIGHT)) + if ((b->posY < 0) || (b->posY + BALL_HEIGHT > b->SCREEN_HEIGHT)) { b->posY -= b->velY; b->velY = -b->velY; diff --git a/src/pong.h b/src/pong.h index fc6e949..14eecb3 100644 --- a/src/pong.h +++ b/src/pong.h @@ -5,17 +5,18 @@ #include "ball.h" +static const int PONG_SCREEN_WIDTH = 640; +static const int PONG_SCREEN_HEIGHT = 480; +static const int PONG_BALL_POSX = 100; +static const int PONG_RACKET_POSX = 50; + typedef struct Pong { - const int SCREEN_WIDTH; - const int SCREEN_HEIGHT; SDL_Window* window; SDL_Surface* screenSurface; SDL_Renderer* renderer; SDL_Event e; Ball *ball; - const int BALL_POSX; Racket *racketL; - const int RACKET_POSX; Racket *racketR; } Pong; diff --git a/src/racket.c b/src/racket.c index 620979b..ccfb981 100644 --- a/src/racket.c +++ b/src/racket.c @@ -4,15 +4,11 @@ Racket* Racket_init(int screen_width, int screen_height, int posX, int posY, SDL_Keycode up, SDL_Keycode down) { Racket *r = malloc(sizeof(Racket)); - *(int *)&r->RACKET_WIDTH = 20; - *(int *)&r->RACKET_HEIGHT = 100; - *(int *)&r->RACKET_VELOCITY = 10; *(int *)&r->SCREEN_WIDTH = screen_width; *(int *)&r->SCREEN_HEIGHT = screen_height; *(int *)&r->posX = posX; r->posY = posY; - *(int *)&r->vel = 5; - r->score = Score_init(r->posX, 20); + r->score = Score_init(r->posX, RACKET_WIDTH); if (r->score == NULL) { free(r); return NULL; @@ -32,7 +28,7 @@ Racket_free(Racket *r) int Racket_render(const Racket *r, SDL_Renderer *renderer) { - SDL_Rect rect = { r->posX, r->posY, r->RACKET_WIDTH, r->RACKET_HEIGHT }; + SDL_Rect rect = { r->posX, r->posY, RACKET_WIDTH, RACKET_HEIGHT }; if (SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255) < 0) { printf("SDL_SetRenderDrawColor failed! SDL_Error: %s\n", SDL_GetError()); return 1; @@ -53,10 +49,10 @@ Racket_handle_event(Racket *r, const Uint8 *keystate) { void Racket_move(Racket *r) { - r->posY += r->updown * r->vel; - if ((r->posY < 0) || (r->posY + r->RACKET_HEIGHT > r->SCREEN_HEIGHT)) + r->posY += r->updown * RACKET_VELOCITY; + if ((r->posY < 0) || (r->posY + RACKET_HEIGHT > r->SCREEN_HEIGHT)) { - r->posY -= r->updown * r->vel; + r->posY -= r->updown * RACKET_VELOCITY; } r->updown = 0; } diff --git a/src/racket.h b/src/racket.h index ea516d9..fe16b84 100644 --- a/src/racket.h +++ b/src/racket.h @@ -5,18 +5,18 @@ #include "score.h" +static const int RACKET_WIDTH = 20; +static const int RACKET_HEIGHT = 100; +static const int RACKET_VELOCITY = 5; + typedef struct Racket { - const int RACKET_WIDTH; - const int RACKET_HEIGHT; - const int RACKET_VELOCITY; const int SCREEN_WIDTH; const int SCREEN_HEIGHT; const int posX; - int posY; - const int vel; - Score* score; const SDL_Keycode up; const SDL_Keycode down; + int posY; + Score* score; int updown; } Racket; diff --git a/src/score.c b/src/score.c index 278629b..4f9caf4 100644 --- a/src/score.c +++ b/src/score.c @@ -27,7 +27,7 @@ Score_free(Score *s) { int Score_render(const Score *s, SDL_Renderer *renderer) { char score_str[3]; - SDL_Rect scoreRect = { s->POSX , s->POSY, 50, 50 }; + 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); diff --git a/src/score.h b/src/score.h index e7a372b..2711633 100644 --- a/src/score.h +++ b/src/score.h @@ -4,6 +4,9 @@ #include #include +static const int SCORE_WIDTH = 50; +static const int SCORE_HEIGHT = 50; + typedef struct Score { const int POSX; const int POSY;