6 #include "./platforms.h"
8 #define PLAYER_WIDTH 50.0f
9 #define PLAYER_HEIGHT 50.0f
10 #define PLAYER_SPEED 500.0f
11 #define PLAYER_GRAVITY 1500.0f
18 struct player_t *create_player(float x, float y)
20 struct player_t *player = malloc(sizeof(struct player_t));
34 void destroy_player(struct player_t * player)
39 int render_player(const struct player_t * player,
40 SDL_Renderer *renderer)
42 if (SDL_SetRenderDrawColor(renderer, 96, 255, 96, 255) < 0) {
47 rect.x = (int)roundf(player->x);
48 rect.y = (int)roundf(player->y);
49 rect.w = (int)roundf(PLAYER_WIDTH);
50 rect.h = (int)roundf(PLAYER_HEIGHT);
52 if (SDL_RenderFillRect(renderer, &rect) < 0) {
59 void update_player(struct player_t * player,
60 const struct platforms_t *platforms,
63 float d = (float) delta_time / 1000.0f;
65 float dx = player->dx;
66 float dy = player->dy + PLAYER_GRAVITY * d;
68 float x = player->x + dx * d;
69 float y = fmodf(player->y + dy * d, 600.0f);
71 struct rect_t player_object = {
78 /* TODO(#6): Implement collision for the left/right sides */
79 if (platforms_rect_object_collide(platforms, &player_object)) {
80 dy = -player->dy * 0.75f;
81 x = player->x + dx * d;
82 y = fmodf(player->y + dy * d, 600.0f);
91 void player_move_left(struct player_t *player)
93 player->dx = -PLAYER_SPEED;
96 void player_move_right(struct player_t *player)
98 player->dx = PLAYER_SPEED;
101 void player_stop(struct player_t *player)
106 void player_jump(struct player_t *player)
108 player->dy = -500.0f;