-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
56 lines (46 loc) · 1.05 KB
/
main.c
File metadata and controls
56 lines (46 loc) · 1.05 KB
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
#include <stdio.h>
#include <stdbool.h>
#include <SDL2/SDL.h>
#include "chip.h"
#include "display.h"
int
main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "usage: %s game\n", argv[0]);
return EXIT_FAILURE;
}
char *filename = argv[1];
chip_t *chip = chip_init();
display_t display = display_init(DISPLAY_WIDTH, DISPLAY_HEIGHT);
SDL_Event event;
bool run = true;
load_game(chip, filename);
while (run) {
chip_cycle(chip);
if (chip->draw) {
display_draw(display, chip->display);
chip->draw = false;
}
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
run = false;
break;
case SDL_KEYUP:
case SDL_KEYDOWN:
for (int i = 0; i < NUM_KEYS; i++) {
if (event.key.keysym.scancode == SDL_KEYS[i]) {
chip->keys[KEY_MAP[i]] = (event.type == SDL_KEYDOWN) ? 1 : 0;
break;
}
}
break;
}
}
SDL_Delay(1);
}
free(chip);
display_free(display);
return EXIT_SUCCESS;
}