-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
156 lines (122 loc) · 4.4 KB
/
main.cpp
File metadata and controls
156 lines (122 loc) · 4.4 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <iostream>
#include <chrono>
#include <ctime>
#include "Chip8.h"
#include <SDL2/SDL.h>
#include <stdio.h>
#include <algorithm>
#include <thread>
const int SCREEN_WIDTH = 64;
const int SCREEN_HEIGHT = 32;
int SCALE = 16;
std::string filepath;
// Keypad keymap
const 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_y,
SDLK_c,
SDLK_4, SDLK_r, SDLK_f, SDLK_v,
};
//The window we'll be rendering to
SDL_Window *gWindow = nullptr;
SDL_Renderer *gRenderer = nullptr;
bool init();
void close();
bool HandleEvents(SDL_Event *e, std::vector<bool> *key_pressed);
void UpdateGfx(std::vector<uint32_t> *gfx, std::vector<std::vector<bool>> emulator_gfx);
int main(int argc, char const *argv[]) {
if(argc==1) {
printf("\nYou need to specify the file path of the ROM u wish to emulate");
return -1;
} else if (argc > 2) {
SCALE = std::stoi(argv[2]);
}
filepath = argv[1];
std::chrono::time_point<std::chrono::system_clock> beforeCycle;
long elapsed_millis = 0;
if (!init()) printf("Failed to initialize!\n");
else {
bool quit = false;
//Event handler
SDL_Event e;
std::vector<uint32_t> gfx(SCREEN_WIDTH * SCREEN_HEIGHT);
SDL_Texture *display_texture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC,
SCREEN_WIDTH, SCREEN_HEIGHT);
Emulator::Chip8 chip8(filepath);
//While application is running
while (!quit) {
beforeCycle = std::chrono::system_clock::now();
//Handle Input
quit = HandleEvents(&e, &chip8.key_pressed);
//Handle Logic
chip8.EmulateCycle();
//Handle Graphics
UpdateGfx(&gfx, chip8.GetGfx());
SDL_UpdateTexture(display_texture, nullptr, &gfx[0], SCREEN_WIDTH * sizeof(uint32_t));
SDL_RenderClear(gRenderer);
SDL_RenderCopy(gRenderer, display_texture, nullptr, nullptr);
SDL_RenderPresent(gRenderer);
SDL_UpdateWindowSurface(gWindow);
elapsed_millis = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now()-beforeCycle).count();
std::this_thread::sleep_for(std::chrono::microseconds(1000-elapsed_millis*1000));
}
SDL_DestroyTexture(display_texture);
}
close();
return 0;
}
bool init() {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
return false;
} else {
gWindow = SDL_CreateWindow("My Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH*SCALE,
SCREEN_HEIGHT*SCALE, SDL_WINDOW_SHOWN);
if (gWindow == nullptr) {
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
return false;
} else {
gRenderer = SDL_CreateRenderer(gWindow, -1, 0);
SDL_RenderSetScale(gRenderer, SCALE, SCALE);
}
}
return true;
}
bool HandleEvents(SDL_Event *e, std::vector<bool> *key_pressed) {
//Handle events on queue
while (SDL_PollEvent(e) != 0) {
//Exit if quit event is triggered
if (e->type == SDL_QUIT) {
return true;
}
if (e->type == SDL_KEYDOWN) {
for (int i = 0; i < 16; ++i) {
if (e->key.keysym.sym == keymap[i]) {
key_pressed->at(i) = true;
}
}
}
if (e->type == SDL_KEYUP) {
for (int i = 0; i < 16; ++i) {
if (e->key.keysym.sym == keymap[i]) {
key_pressed->at(i) = false;
}
}
}
}
return false;
}
void UpdateGfx(std::vector<uint32_t> *gfx, std::vector<std::vector<bool>> emulator_gfx) {
for (int i = 0; i < SCREEN_HEIGHT; ++i) {
for (int j = 0; j < SCREEN_WIDTH; ++j) {
uint32_t pixel = emulator_gfx[i][j];
gfx->at(i * SCREEN_WIDTH + j) =
(0x00FFFFFF * pixel) | 0xFF000000; //To convert the emulator gfx to a uint we can use
}
}
}
void close() {
SDL_DestroyWindow(gWindow);
gWindow = nullptr;
SDL_DestroyRenderer(gRenderer);
gRenderer = nullptr;
SDL_Quit();
}