-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
115 lines (94 loc) · 2.23 KB
/
main.cpp
File metadata and controls
115 lines (94 loc) · 2.23 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
#include <iostream>
#include <chrono>
#include <thread>
#include <stdint.h>
#include "SDL2/SDL.h"
#include "chip8.h"
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)
{
if(argc != 2){
std::cout << "usage: chip8 <rom file>" << std::endl;
return 1;
}
Chip8 chip8 = Chip8();
int w = 1024;
int h = 512;
SDL_Window* window = NULL;
//init sdl
if ( SDL_Init(SDL_INIT_EVERYTHING) < 0){
printf("sdl error. %s\n", SDL_GetError());
exit(1);
}
window = SDL_CreateWindow(
"CHIP-8 Emulator",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
w, h, SDL_WINDOW_SHOWN
);
if(window == NULL){
printf("sdl couldn't make window. %s\n", SDL_GetError());
exit(2);
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
SDL_RenderSetLogicalSize(renderer, w, h);
SDL_Texture *sdlTexture = SDL_CreateTexture(renderer,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
64, 32);
uint32_t pixels[2048];
load:
if (!chip8.loadGame(argv[1]))
return 2;
while(true){
chip8.emulateCycle();
SDL_Event e;
while(SDL_PollEvent(&e)){
if(e.type == SDL_QUIT) exit(0);
if(e.type == SDL_KEYDOWN){
if(e.key.keysym.sym == SDLK_ESCAPE)
exit(0);
if(e.key.keysym.sym == SDLK_F1)
goto load;
for(int i=0; i<16; ++i){
if(e.key.keysym.sym == keymap[i])
chip8.key[i] = 1;
}
}
if(e.type == SDL_KEYUP){
for(int i=0; i<16; ++i){
if(e.key.keysym.sym == keymap[i])
chip8.key[i] = 0;
}
}
}
if(chip8.drawFlag){
chip8.drawFlag = false;
for(int i=0; i<2048; ++i){
uint8_t pixel = chip8.gfx[i];
pixels[i] = (0x00FFFFFF * pixel) | 0xFF000000;
}
SDL_UpdateTexture(sdlTexture, NULL, pixels, 64 * sizeof(Uint32));
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, sdlTexture, NULL, NULL);
SDL_RenderPresent(renderer);
}
std::this_thread::sleep_for(std::chrono::microseconds(1200));
}
}