From fc3032135f4da3662d6b727c70f22049d6e09231 Mon Sep 17 00:00:00 2001 From: BoredGuy Date: Sat, 14 Dec 2024 12:08:02 +0300 Subject: Update - Finish animation system - Fix bug where leftward/upward movement was slightly faster than downward/forward movement --- Week1-Pacman/src/main.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) (limited to 'Week1-Pacman/src/main.c') diff --git a/Week1-Pacman/src/main.c b/Week1-Pacman/src/main.c index e1f447f..895c359 100644 --- a/Week1-Pacman/src/main.c +++ b/Week1-Pacman/src/main.c @@ -1,6 +1,86 @@ +#include #include +#include +#include "demo.h" +#include "pacman.h" + +#define WINDOW_WIDTH 800 +#define WINDOW_HEIGHT 600 int main() { + SDL_Init(SDL_INIT_EVERYTHING); + struct demo demo = {0}; + + demo.win = SDL_CreateWindow("Pacman", + SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED, + WINDOW_WIDTH, WINDOW_HEIGHT, + SDL_WINDOW_SHOWN | + SDL_WINDOW_ALLOW_HIGHDPI); + demo.ren = SDL_CreateRenderer(demo.win, + -1, + SDL_RENDERER_PRESENTVSYNC | + SDL_RENDERER_ACCELERATED); + bool running = true; + SDL_Event e; + + struct pacman pacman = {0}; + for(int i = 0; i < 4; i++) + init_animation(&pacman.animations[i], &(struct animation_init){ + .ren = demo.ren, + .spritesheet = "assets/sprites.png", + + .initial_angle = 90.0 * i, + .initial_frame_count = 3, + .initial_frame_times = (float[]){0.10, 0.10, 0.10}, + .initial_frames = (SDL_Rect[]) { + { + .x = 0, + .y = 190, + .w = 120, + .h = 130 + }, + { + .x = 160, + .y = 190, + .w = 120, + .h = 130 + }, + { + .x = 310, + .y = 190, + .w = 130, + .h = 130 + } + } + }); + pacman.facing = FACING_DOWN; + + int then = SDL_GetTicks(); + while(running) { + while(SDL_PollEvent(&e)) { + if(e.type == SDL_QUIT) { + running = false; + } + + handle_pacman_input(&e, &pacman); + } + + int now = SDL_GetTicks(); + float dt = (float)(now - then) / 1000; + then = now; + + SDL_RenderClear(demo.ren); + update_pacman(&pacman, dt); + update_demo(&demo); + draw_pacman(&demo, &pacman); + SDL_RenderPresent(demo.ren); + } + + SDL_DestroyRenderer(demo.ren); + SDL_DestroyWindow(demo.win); + SDL_Quit(); return 0; + } -- cgit v1.2.3