summaryrefslogtreecommitdiff
path: root/Week1-Pacman/src/main.c
diff options
context:
space:
mode:
authorBoredGuy <osome3717@gmail.com>2024-12-14 12:08:02 +0300
committerBoredGuy <osome3717@gmail.com>2024-12-14 12:08:02 +0300
commitfc3032135f4da3662d6b727c70f22049d6e09231 (patch)
tree8e0caba74cc6749c76f211540fe908f6b9a3d476 /Week1-Pacman/src/main.c
parent993f45cec7a08a5b980ef5a08ea7ce19b877b743 (diff)
Update
- Finish animation system - Fix bug where leftward/upward movement was slightly faster than downward/forward movement
Diffstat (limited to 'Week1-Pacman/src/main.c')
-rw-r--r--Week1-Pacman/src/main.c80
1 files changed, 80 insertions, 0 deletions
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 <SDL2/SDL.h>
#include <stdio.h>
+#include <stdbool.h>
+#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;
+
}