-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
137 lines (111 loc) · 3.09 KB
/
main.cpp
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream>
#include <fstream>
#include "chip8.h"
#include <SDL.h>
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 320;
chip8 emu;
SDL_Window *window = nullptr;
SDL_Renderer *renderer = nullptr;
bool quit = false;
SDL_Keycode inputMap[] = {SDLK_1, SDLK_2, SDLK_3, SDLK_4,
SDLK_q, SDLK_w, SDLK_e, SDLK_r,
SDLK_a, SDLK_s, SDLK_d, SDLK_f,
SDLK_z, SDLK_x, SDLK_c, SDLK_v};
void initializeGraphics();
void loadGame(char *string);
void draw();
void getInput();
int main(int argc, char *args[]) {
initializeGraphics();
emu.initialize();
loadGame(args[1]);
while (!quit) {
while (emu.isRunning) {
getInput();
emu.emulateCycle();
if (emu.drawFlag) {
draw();
}
SDL_Delay(1);
}
}
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
void getInput() {
unsigned char input[16];
for (int i = 0; i < 16; i++) {
input[i] = 0;
}
SDL_Event e;
while (SDL_PollEvent(&e) != 0) {
if (e.type == SDL_QUIT) {
emu.isRunning = false;
quit = true;
} else if (e.type == SDL_KEYDOWN) {
printf("Key pressed: %d\n", e.key.keysym.sym);
for (int i = 0; i < 16; i++) {
if (inputMap[i] == e.key.keysym.sym) {
input[i] = 1;
}
}
}
}
for (int i = 0; i < 16; i++) {
if (input[i] == 1) {
emu.s.key[i] = 1;
}
}
}
void draw() {
//clear screen
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
//draw rectangles
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 64; j++) {
unsigned char pix = emu.s.gfx[64 * i + j];
if (pix > 0) {
SDL_Rect rect;
rect.x = j * 10;
rect.y = i * 10;
rect.w = 10;
rect.h = 10;
SDL_RenderFillRect(renderer, &rect);
}
}
}
SDL_RenderPresent(renderer);
}
void loadGame(char *file) {
std::ifstream in;
in.open(file, std::ios::in | std::ios::binary);
if (in.is_open()) {
in.seekg(0, in.end);
int size = in.tellg();
in.seekg(0, in.beg);
char *buff = (char *) malloc(sizeof(char) * size);
in.read(buff, size);
in.close();
emu.loadGame((unsigned char *) buff, size);
} else {
perror("Failed to open file.\n");
exit(1);
}
}
void initializeGraphics() {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL could not initialize.\n");
exit(1);
}
window = SDL_CreateWindow("CHIP-8 Emulator", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH,
SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == nullptr) {
printf("SDL could not create window.\n");
exit(1);
}
renderer = SDL_CreateRenderer(window, -1, 0);
}