-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
86 lines (66 loc) · 1.66 KB
/
main.c
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
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <SDL2/SDL.h>
#include "chip8.h"
#define SCREEN_WIDTH 64 * 8
#define SCREEN_HEIGHT 32 * 8
#define REFRESH_RATE 700
int main(int argc, char *argv[])
{
if (argc == 1)
{
printf("Usage: %s <rom-path>\n", argv[0]);
return -1;
}
initialise_chip8(&chip8);
load_rom(argv[1], &chip8);
// Seed random values
srand(time(NULL));
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
printf("There has been an error initialising SDL.\n%s\n", SDL_GetError());
return -1;
}
app.window = SDL_CreateWindow(
"CHIP-8",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH,
SCREEN_HEIGHT,
0
);
if (!app.window)
{
printf("There has been an error creating the window.\n%s\n", SDL_GetError());
return -1;
}
app.renderer = SDL_CreateRenderer(app.window, -1, SDL_RENDERER_SOFTWARE);
if (!app.renderer)
{
printf("There has been an error creating the renderer.\n%s\n", SDL_GetError());
return -1;
}
// Set initial render draw color
SDL_SetRenderDrawColor(app.renderer, 0, 0, 0, 255);
SDL_Event e;
int quit = 0;
while (!quit)
{
while (SDL_PollEvent(&e) != 0)
{
if (e.type == SDL_QUIT)
{
quit = 1;
}
}
update(&chip8);
SDL_RenderPresent(app.renderer);
// Enforce FPS
SDL_Delay(1000/REFRESH_RATE);
}
SDL_DestroyWindow(app.window);
SDL_Quit();
return 0;
}