From deb843a25ac6bb7bf553007ee8210e6005279d83 Mon Sep 17 00:00:00 2001 From: fredrikr79 Date: Sun, 2 Feb 2025 17:11:48 +0100 Subject: [PATCH] feat: movement with time delta demo --- src/main.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/main.c b/src/main.c index ce63109..f1167fb 100644 --- a/src/main.c +++ b/src/main.c @@ -2,6 +2,8 @@ #include #include +#define FPS 60 + static SDL_Window *window = NULL; static SDL_Renderer *renderer = NULL; @@ -10,6 +12,8 @@ static int height = 480; static int grid_width = 32; static int grid_height = 24; +static Uint64 last; + enum TILES { EMPTY, SNAKE, FRUIT }; typedef enum DIRECTION { LEFT, RIGHT, UP, DOWN } DIRECTION; @@ -17,6 +21,7 @@ typedef enum DIRECTION { LEFT, RIGHT, UP, DOWN } DIRECTION; typedef struct State { int *grid; DIRECTION player_face; + SDL_FRect *r; } State; void log_game_grid(int *game_grid) { @@ -63,11 +68,20 @@ SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) { set_grid_at(game_grid, 4, 12, FRUIT); set_grid_at(game_grid, 18, 5, FRUIT); + SDL_FRect *rect2 = SDL_malloc(sizeof(SDL_FRect)); + rect2->y = 240; + rect2->x = 0; + rect2->w = 100; + rect2->h = 100; + State *state = SDL_malloc(sizeof(State)); state->grid = game_grid; state->player_face = UP; + state->r = rect2; *appstate = state; + last = SDL_GetTicksNS(); + return SDL_APP_CONTINUE; } @@ -83,6 +97,8 @@ SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) { SDL_AppResult SDL_AppIterate(void *appstate) { State *stateptr = appstate; int *game_grid = stateptr->grid; + Uint64 delta = SDL_GetTicksNS() - last; + double duration = (double)delta / (FPS * 1000); SDL_SetRenderDrawColorFloat(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE_FLOAT); SDL_RenderClear(renderer); @@ -112,8 +128,16 @@ SDL_AppResult SDL_AppIterate(void *appstate) { } } + SDL_FRect *rect2 = stateptr->r; + rect2->x += 5 * (double)delta / 1000000; + SDL_Log("%f %f %f %f", rect2->x, rect2->y, rect2->w, rect2->h); + SDL_SetRenderDrawColorFloat(renderer, 0, 255, 0, SDL_ALPHA_OPAQUE_FLOAT); + SDL_RenderFillRect(renderer, rect2); + SDL_RenderPresent(renderer); + last = SDL_GetTicksNS(); + return SDL_APP_CONTINUE; }