59 lines
1.4 KiB
C
59 lines
1.4 KiB
C
#include "racket.h"
|
|
|
|
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->SCREEN_WIDTH = screen_width;
|
|
*(int *)&r->SCREEN_HEIGHT = screen_height;
|
|
*(int *)&r->posX = posX;
|
|
r->posY = posY;
|
|
r->score = Score_init(r->posX - SCORE_WIDTH/2 + RACKET_WIDTH/2, RACKET_WIDTH);
|
|
if (r->score == NULL) {
|
|
free(r);
|
|
return NULL;
|
|
}
|
|
*(SDL_Keycode *)&r->up = up;
|
|
*(SDL_Keycode *)&r->down = down;
|
|
r->updown = 0;
|
|
return r;
|
|
}
|
|
|
|
void
|
|
Racket_free(Racket *r)
|
|
{
|
|
Score_free(r->score);
|
|
free(r);
|
|
}
|
|
|
|
int
|
|
Racket_render(const Racket *r, SDL_Renderer *renderer) {
|
|
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;
|
|
}
|
|
SDL_RenderFillRect(renderer, &rect);
|
|
if (Score_render(r->score, renderer) > 0) {
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void
|
|
Racket_handle_event(Racket *r, const Uint8 *keystate) {
|
|
if (keystate[r->up]) r->updown = -1;
|
|
else if (keystate[r->down]) r->updown = 1;
|
|
else r->updown = 0;
|
|
}
|
|
|
|
void
|
|
Racket_move(Racket *r) {
|
|
r->posY += r->updown * RACKET_VELOCITY;
|
|
if ((r->posY < 0) || (r->posY + RACKET_HEIGHT > r->SCREEN_HEIGHT))
|
|
{
|
|
r->posY -= r->updown * RACKET_VELOCITY;
|
|
}
|
|
r->updown = 0;
|
|
}
|