-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
139 lines (115 loc) · 3.46 KB
/
main.cpp
File metadata and controls
139 lines (115 loc) · 3.46 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
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
#include <iostream>
#include <chrono>
#include <thread>
#include "stdint.h"
#include "SDL2/SDL.h"
#include "chip8.h"
using namespace std;
// Keypad keymap
uint8_t keymap[16] = {
SDLK_x,
SDLK_1,
SDLK_2,
SDLK_3,
SDLK_q,
SDLK_w,
SDLK_e,
SDLK_a,
SDLK_s,
SDLK_d,
SDLK_z,
SDLK_c,
SDLK_4,
SDLK_r,
SDLK_f,
SDLK_v,
};
int main(int argc, char **argv) {
// Command usage
if (argc != 2) {
cout << "Usage: chip8 <ROM file>" << endl;
return 1;
}
Chip8 chip8 = Chip8(); // Initialise Chip8
int w = 1024; // Window width
int h = 512; // Window height
// The window we'll be rendering to
SDL_Window* window = NULL;
// Initialize SDL
if ( SDL_Init(SDL_INIT_EVERYTHING) < 0 ) {
printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
exit(1);
}
// Create window
window = SDL_CreateWindow(
"CHIP-8 Emulator",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
w, h, SDL_WINDOW_SHOWN
);
if (window == NULL){
printf( "Window could not be created! SDL_Error: %s\n",
SDL_GetError() );
exit(2);
}
// Create renderer
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
SDL_RenderSetLogicalSize(renderer, w, h);
// Create texture that stores frame buffer
SDL_Texture* sdlTexture = SDL_CreateTexture(renderer,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
64, 32);
// Temporary pixel buffer
uint32_t pixels[2048];
load:
// Attempt to load ROM
if (!chip8.load(argv[1]))
return 2;
// Emulation loop
while (true) {
chip8.emulate_cycle();
// Process SDL events
SDL_Event e;
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) exit(0);
// Process keydown events
if (e.type == SDL_KEYDOWN) {
if (e.key.keysym.sym == SDLK_ESCAPE)
exit(0);
if (e.key.keysym.sym == SDLK_F1)
goto load; // *gasp*, a goto statement!
// Used to reset/reload ROM
for (int i = 0; i < 16; ++i) {
if (e.key.keysym.sym == keymap[i]) {
chip8.key[i] = 1;
}
}
}
// Process keyup events
if (e.type == SDL_KEYUP) {
for (int i = 0; i < 16; ++i) {
if (e.key.keysym.sym == keymap[i]) {
chip8.key[i] = 0;
}
}
}
}
// If draw occurred, redraw SDL screen
if (chip8.drawFlag) {
chip8.drawFlag = false;
// Store pixels in temporary buffer
for (int i = 0; i < 2048; ++i) {
uint8_t pixel = chip8.gfx[i];
pixels[i] = (0x00FFFFFF * pixel) | 0xFF000000;
}
// Update SDL texture
SDL_UpdateTexture(sdlTexture, NULL, pixels, 64 * sizeof(Uint32));
// Clear screen and render
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, sdlTexture, NULL, NULL);
SDL_RenderPresent(renderer);
}
// Sleep to slow down emulation speed
std::this_thread::sleep_for(std::chrono::microseconds(1200));
}
}