-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
193 lines (180 loc) · 4.48 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include "chip8.h"
#include <SDL2/SDL.h>
#include <cstdio>
#include <cstdlib>
const int PIXEL_SIZE = 15;
const int SCREEN_WIDTH = 64*PIXEL_SIZE;
const int SCREEN_HEIGHT = 32*PIXEL_SIZE;
void drawPixels(chip8 *ch, SDL_Renderer *renderer) {
SDL_SetRenderDrawColor(renderer, 30, 30, 46, 255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 166, 227, 161, 255);
for (int i = 0; i < 32; ++i) {
for (int j = 0; j < 64; ++j) {
if (ch->gfx[i * 64 + j]) {
SDL_Rect rect = {j * PIXEL_SIZE, i * PIXEL_SIZE, PIXEL_SIZE,
PIXEL_SIZE};
SDL_RenderFillRect(renderer, &rect);
}
}
}
SDL_RenderPresent(renderer);
}
int main(int argc, char *argv[]) {
chip8 ch = chip8();
if (argc < 2) {
printf("ROM not specified.\n");
return 1;
}
ch.loadApplication(argv[1]);
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
return 1;
}
SDL_Window *window = SDL_CreateWindow(
"CHIP-8 Emulator", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (!window) {
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
SDL_Quit();
return 1;
}
SDL_Renderer *renderer =
SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (!renderer) {
printf("Renderer could not be created! SDL_Error: %s\n", SDL_GetError());
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
bool quit = false;
SDL_Event event;
unsigned int prev_time = SDL_GetTicks();
while (!quit) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_KEYDOWN) {
switch (event.key.keysym.sym) {
case SDLK_1:
ch.key[0x1] = true;
break;
case SDLK_2:
ch.key[0x2] = true;
break;
case SDLK_3:
ch.key[0x3] = true;
break;
case SDLK_4:
ch.key[0xC] = true;
break;
case SDLK_q:
ch.key[0x4] = true;
break;
case SDLK_w:
ch.key[0x5] = true;
break;
case SDLK_e:
ch.key[0x6] = true;
break;
case SDLK_r:
ch.key[0xD] = true;
break;
case SDLK_a:
ch.key[0x7] = true;
break;
case SDLK_s:
ch.key[0x8] = true;
break;
case SDLK_d:
ch.key[0x9] = true;
break;
case SDLK_f:
ch.key[0xE] = true;
break;
case SDLK_z:
ch.key[0xA] = true;
break;
case SDLK_x:
ch.key[0x0] = true;
break;
case SDLK_c:
ch.key[0xB] = true;
break;
case SDLK_v:
ch.key[0xF] = true;
break;
case SDLK_ESCAPE:
exit(1);
break;
}
} else if (event.type == SDL_KEYUP) {
switch (event.key.keysym.sym) {
case SDLK_1:
ch.key[0x1] = false;
break;
case SDLK_2:
ch.key[0x2] = false;
break;
case SDLK_3:
ch.key[0x3] = false;
break;
case SDLK_4:
ch.key[0xC] = false;
break;
case SDLK_q:
ch.key[0x4] = false;
break;
case SDLK_w:
ch.key[0x5] = false;
break;
case SDLK_e:
ch.key[0x6] = false;
break;
case SDLK_r:
ch.key[0xD] = false;
break;
case SDLK_a:
ch.key[0x7] = false;
break;
case SDLK_s:
ch.key[0x8] = false;
break;
case SDLK_d:
ch.key[0x9] = false;
break;
case SDLK_f:
ch.key[0xE] = false;
break;
case SDLK_z:
ch.key[0xA] = false;
break;
case SDLK_x:
ch.key[0x0] = false;
break;
case SDLK_c:
ch.key[0xB] = false;
break;
case SDLK_v:
ch.key[0xF] = false;
break;
}
} else if (event.type == SDL_QUIT) {
quit = true;
}
}
ch.emulateCycle();
if (ch.drawFlag) {
drawPixels(&ch, renderer);
ch.drawFlag = false;
}
unsigned int curr_time = SDL_GetTicks();
unsigned int elapsed_time = curr_time - prev_time;
if (elapsed_time < 4) {
SDL_Delay(4 - elapsed_time);
}
prev_time = curr_time;
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}