2021-06-27 16:41:52 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include "pong.h"
|
|
|
|
|
|
|
|
Pong*
|
|
|
|
Pong_init() {
|
|
|
|
Pong *p = malloc(sizeof(Pong));
|
|
|
|
|
2021-06-29 01:25:08 +02:00
|
|
|
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);
|
2021-06-28 21:37:34 +02:00
|
|
|
if (p->racketL == NULL) {
|
|
|
|
Ball_free(p->ball);
|
|
|
|
free(p);
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-06-29 01:25:08 +02:00
|
|
|
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);
|
2021-06-28 21:37:34 +02:00
|
|
|
if (p->racketR == NULL) {
|
|
|
|
Ball_free(p->ball);
|
|
|
|
Racket_free(p->racketL);
|
|
|
|
free(p);
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-06-27 16:41:52 +02:00
|
|
|
|
2021-06-29 01:25:08 +02:00
|
|
|
p->window = SDL_CreateWindow( "Pong", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, PONG_SCREEN_WIDTH, PONG_SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
|
2021-06-27 16:41:52 +02:00
|
|
|
if( p->window == NULL )
|
|
|
|
{
|
|
|
|
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
|
|
|
|
Pong_free(p);
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-06-28 21:37:34 +02:00
|
|
|
|
2021-06-27 16:41:52 +02:00
|
|
|
p->renderer = SDL_CreateRenderer(p->window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
|
|
|
|
p->screenSurface = SDL_GetWindowSurface( p->window );
|
|
|
|
|
|
|
|
SDL_FillRect( p->screenSurface, NULL, SDL_MapRGB( p->screenSurface->format, 0, 0, 0 ) );
|
|
|
|
|
|
|
|
SDL_UpdateWindowSurface( p->window );
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pong_free(Pong *p) {
|
|
|
|
SDL_DestroyRenderer(p->renderer);
|
2021-06-28 21:37:34 +02:00
|
|
|
SDL_FreeSurface(p->screenSurface);
|
2021-06-27 16:41:52 +02:00
|
|
|
SDL_DestroyWindow(p->window);
|
|
|
|
Ball_free(p->ball);
|
|
|
|
Racket_free(p->racketL);
|
|
|
|
Racket_free(p->racketR);
|
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pong_clear(Pong *p) {
|
|
|
|
SDL_SetRenderDrawColor(p->renderer, 0, 0, 0, 255 );
|
|
|
|
SDL_RenderClear( p->renderer );
|
|
|
|
}
|
|
|
|
|
2021-06-28 21:37:34 +02:00
|
|
|
int
|
2021-06-27 16:41:52 +02:00
|
|
|
Pong_render(Pong *p) {
|
|
|
|
Pong_clear(p);
|
2021-06-28 21:37:34 +02:00
|
|
|
const int error = Ball_render(p->ball, p->renderer)
|
|
|
|
+ Racket_render(p->racketL, p->renderer)
|
|
|
|
+ Racket_render(p->racketR, p->renderer);
|
|
|
|
if (error > 0) {
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
SDL_RenderPresent(p->renderer);
|
|
|
|
return 0;
|
2021-06-27 16:41:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
Ball_move(Ball *b, Pong *p)
|
|
|
|
{
|
|
|
|
b->posX += b->velX;
|
|
|
|
|
|
|
|
if (Ball_collision(b, p->racketL) || Ball_collision(b, p->racketR)) {
|
|
|
|
b->posX -= b->velX;
|
|
|
|
b->velX = -b->velX;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (b->posX < 0) {
|
2021-06-28 21:37:34 +02:00
|
|
|
p->racketR->score->score++;
|
2021-06-27 16:41:52 +02:00
|
|
|
Ball_reset(b, -1);
|
|
|
|
}
|
|
|
|
|
2021-06-29 01:25:08 +02:00
|
|
|
else if(b->posX + BALL_WIDTH > b->SCREEN_WIDTH)
|
2021-06-27 16:41:52 +02:00
|
|
|
{
|
2021-06-28 21:37:34 +02:00
|
|
|
p->racketL->score->score++;
|
2021-06-27 16:41:52 +02:00
|
|
|
Ball_reset(b, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
b->posY += b->velY;
|
2021-06-29 01:25:08 +02:00
|
|
|
if ((b->posY < 0) || (b->posY + BALL_HEIGHT > b->SCREEN_HEIGHT))
|
2021-06-27 16:41:52 +02:00
|
|
|
{
|
|
|
|
b->posY -= b->velY;
|
|
|
|
b->velY = -b->velY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Pong_run(Pong *p) {
|
|
|
|
int quit = false;
|
|
|
|
while (!quit) {
|
|
|
|
|
|
|
|
while( SDL_PollEvent( &(p->e) ) != 0 )
|
|
|
|
{
|
|
|
|
if( p->e.type == SDL_QUIT) quit = true;
|
|
|
|
}
|
|
|
|
const Uint8* currentKeyStates = SDL_GetKeyboardState( NULL );
|
|
|
|
if (currentKeyStates[SDL_SCANCODE_ESCAPE]) quit = true;
|
|
|
|
Racket_handle_event(p->racketL, currentKeyStates);
|
|
|
|
Racket_handle_event(p->racketR, currentKeyStates);
|
|
|
|
|
|
|
|
|
|
|
|
Ball_move(p->ball, p);
|
|
|
|
Racket_move(p->racketL);
|
|
|
|
Racket_move(p->racketR);
|
2021-06-28 21:37:34 +02:00
|
|
|
if (Pong_render(p) > 0) quit = true;
|
2021-06-28 22:37:49 +02:00
|
|
|
Pong_status(p, quit);
|
2021-06-27 16:41:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-06-28 22:37:49 +02:00
|
|
|
Pong_status(const Pong *p, int quit) {
|
2021-06-28 22:36:04 +02:00
|
|
|
printf("[STATUS] SCORE: L %i | R %i", p->racketL->score->score, p->racketR->score->score);
|
2021-06-28 22:37:49 +02:00
|
|
|
if (quit) {
|
|
|
|
putc('\n', stdout);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
putc('\r', stdout);
|
|
|
|
}
|
2021-06-27 16:41:52 +02:00
|
|
|
}
|