summaryrefslogtreecommitdiff
path: root/Week1-Pacman/src/main.c
blob: 373d795b86005ae40a5a0a5f115cd4086f61eb62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <SDL2/SDL.h>
#include <stdio.h>
#include <stdbool.h>
#include "demo.h"
#include "pacman.h"
#include "map.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};

  map_init(&demo);

  struct map map = {0};
  load_map("assets/Maps/maze.csv", &map);

  for(int i = 0; i < 4; i++)
    init_animation(&pacman.animations[i], &(struct animation_init){
        .ren = demo.ren,
        .spritesheet = "assets/Sprites/sprites.png",

        .initial_angle = 90.0 * i,
        .initial_frame_count = 3,
        .initial_frame_times = (float[]){0.05, 0.05, 0.05},
        .initial_frames = (SDL_Rect[]) {
          {
            .x = 0,
            .y = 285,
            .w = 180,
            .h = 195
          },
          {
            .x = 240,
            .y = 285,
            .w = 180,
            .h = 195
          },
          {
            .x = 465,
            .y = 285,
            .w = 195,
            .h = 195
          }
        }
      });
  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_map(&demo, &map);
    draw_pacman(&demo, &pacman);
    SDL_RenderPresent(demo.ren);
  }

  SDL_DestroyRenderer(demo.ren);
  SDL_DestroyWindow(demo.win);
  SDL_Quit();
  return 0;

}