summaryrefslogtreecommitdiff
path: root/Week1-Pacman/src/main.c
blob: 6ae2fdefa79c490a136dbd7539e2c1ba2107c7dd (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
#include <SDL2/SDL.h>
#include <stdio.h>
#include <stdbool.h>
#include "demo.h"
#include "pacman.h"
#include "map.h"
#include "collision.h"

#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600

const Uint8* keyboard;

int main(int argc, char** argv) {
  (void)argc;
  (void)argv;

  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);
  keyboard =
    SDL_GetKeyboardState(NULL);

  load_pacman_spritesheet(&demo);
  init_pacman(&pacman, &(struct pacman_init) {
      .initial_xpos = 100,
      .initial_ypos = 200,
      .initial_facing = FACING_DOWN
    });

  int then = SDL_GetTicks();

  while(running) {
    while(SDL_PollEvent(&e)) {
      if(e.type == SDL_QUIT) {
        running = false;
      }
    }

    int now = SDL_GetTicks();
    float dt = (float)(now - then) / 1000;
    then = now;

    SDL_RenderClear(demo.ren);
    update_pacman(&pacman, dt, &map);
    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;
}