-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
134 lines (117 loc) · 3.63 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
#define SDL_MAIN_HANDLED
#include <iostream>
#include "SDL.h"
#include "chip8.h"
chip8 chip8;
SDL_Event event;
void setupGraphics();
void drawGraphics();
void handleInput();
[[noreturn]] int main() {
// Setup render system
setupGraphics();
// Initialize the chip8 system and load the game into the memory
chip8.init();
chip8.loadGame("../roms/test_opcode.ch8");
// Emulation loop
for (;;) {
// Emulate one cycle
chip8.emulateCycle();
// Handle input
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
exit(0);
}
handleInput();
}
// If the draw flag is set, update the screen
if (chip8.drawFlag)
drawGraphics();
// Tick the timers
chip8.tick();
}
}
void drawGraphics() {
// Clear the screen
SDL_SetRenderDrawColor(chip8.renderer, 0, 0, 0, 255);
SDL_RenderClear(chip8.renderer);
// Draw the screen
for (int i = 0; i < 64 * 32; i++) {
if (chip8.gfx[i] == 0) {
SDL_SetRenderDrawColor(chip8.renderer, 0, 0, 0, 255); // Draw black
} else {
SDL_SetRenderDrawColor(chip8.renderer, 255, 255, 255, 255); // Draw white
}
SDL_Rect rect = {i % 64 * 10, i / 64 * 10, 10, 10}; // Create a rectangle
SDL_RenderFillRect(chip8.renderer, &rect); // Draw the rectangle
}
SDL_RenderPresent(chip8.renderer); // Update the screen
}
void setupGraphics() {
SDL_SetMainReady();
// Initialize SDL
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
exit(1);
}
// Create window
SDL_CreateWindowAndRenderer(640, 320, SDL_WINDOW_OPENGL, &chip8.window, &chip8.renderer);
SDL_RenderClear(chip8.renderer);
SDL_RenderPresent(chip8.renderer);
}
void handleInput() {
switch (event.key.keysym.sym) {
case SDLK_1:
chip8.key[0x1] = event.type == SDL_KEYDOWN ? 1 : 0;
break;
case SDLK_2:
chip8.key[0x2] = event.type == SDL_KEYDOWN ? 1 : 0;
break;
case SDLK_3:
chip8.key[0x3] = event.type == SDL_KEYDOWN ? 1 : 0;
break;
case SDLK_4:
chip8.key[0xC] = event.type == SDL_KEYDOWN ? 1 : 0;
break;
case SDLK_q:
chip8.key[0x4] = event.type == SDL_KEYDOWN ? 1 : 0;
break;
case SDLK_w:
chip8.key[0x5] = event.type == SDL_KEYDOWN ? 1 : 0;
break;
case SDLK_e:
chip8.key[0x6] = event.type == SDL_KEYDOWN ? 1 : 0;
break;
case SDLK_r:
chip8.key[0xD] = event.type == SDL_KEYDOWN ? 1 : 0;
break;
case SDLK_a:
chip8.key[0x7] = event.type == SDL_KEYDOWN ? 1 : 0;
break;
case SDLK_s:
chip8.key[0x8] = event.type == SDL_KEYDOWN ? 1 : 0;
break;
case SDLK_d:
chip8.key[0x9] = event.type == SDL_KEYDOWN ? 1 : 0;
break;
case SDLK_f:
chip8.key[0xE] = event.type == SDL_KEYDOWN ? 1 : 0;
break;
case SDLK_y:
chip8.key[0xA] = event.type == SDL_KEYDOWN ? 1 : 0;
break;
case SDLK_x:
chip8.key[0x0] = event.type == SDL_KEYDOWN ? 1 : 0;
break;
case SDLK_c:
chip8.key[0xB] = event.type == SDL_KEYDOWN ? 1 : 0;
break;
case SDLK_v:
chip8.key[0xF] = event.type == SDL_KEYDOWN ? 1 : 0;
break;
case SDLK_ESCAPE:
exit(0);
default:
break;
}
}