-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
192 lines (166 loc) · 5.1 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
#include <iostream>
#include "chip8.h"
#include <SDL2/SDL.h>
#include "cleanup.h"
#include <unordered_map>
#include <chrono>
#include <thread>
// using namespace std;
// Screen attributes
const int Screen_Width = 640;
const int Screen_Height = 480;
Chip8 chip8;
/*
* Log an SDL error with some error message to the output stream of our choice
* @param os The output stream to write the message too
* @param msg The error message to write, format will be msg error: SDL_GetError()
*/
void logSDLError(std::ostream &os, const std::string &msg)
{
os << msg << " error: " << SDL_GetError() << std::endl;
}
void display()
{
chip8.emulateCycle();
if(chip8.drawFlag)
{
// Clear framebuffer
// Update texture
// Processed frame
chip8.drawFlag = false;
}
}
int main (int argc, char **argv)
{
/* Initialize the Chip8 system and load the game into the memory */
if(argc < 2)
{
printf("No ROM given...\n");
return 1;
}
// First we need to start up SDL, and make sure it went ok
if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
logSDLError(std::cout, "SDL_Init");
return 1;
}
// Now create a window with title "Hello World" at 100, 100 on the screen with w:640 h:480 and show it
// Setup our window and renderer
SDL_Window *window = SDL_CreateWindow(argv[1], SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, Screen_Width, Screen_Height, SDL_WINDOW_RESIZABLE);
if (window == nullptr)
{
logSDLError(std::cout, "CreateWindow");
return 1;
}
// Create a renderer that will draw to the window, -1 specifies that we want to load whichever
// video driver supports the flags we're passing
// Flags: SDL_RENDERER_ACCELERATED: We want to use hardware accelerated rendering
// SDL_RENDERER_PRESENTVSYNC: We want the renderer's present function (update screen) to be
// synchronized with the monitor's refresh rate
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (renderer == nullptr)
{
logSDLError(std::cout, "CreateRenderer");
cleanup(window);
SDL_Quit();
return 1;
}
// To use a hardware accelerated texture for rendering we can create one
SDL_Texture *texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, SCREEN_WIDTH, SCREEN_HEIGHT);
if (texture == nullptr)
{
cleanup(texture, renderer, window);
logSDLError(std::cout, "CreateTexture");
SDL_Quit();
return 1;
}
// Create a mapping of SDL keyboard symbols to CHIP-8 keypad codes
std::unordered_map<int,unsigned char> keymap
{
{SDLK_1, 0x1}, {SDLK_2, 0x2}, {SDLK_3, 0x3}, {SDLK_4, 0xC},
{SDLK_q, 0x4}, {SDLK_w, 0x5}, {SDLK_e, 0x6}, {SDLK_r, 0xD},
{SDLK_a, 0x7}, {SDLK_s, 0x8}, {SDLK_d, 0x9}, {SDLK_f, 0xE},
{SDLK_z, 0xA}, {SDLK_x, 0x0}, {SDLK_c, 0xB}, {SDLK_v, 0xF},
{SDLK_ESCAPE, -1}
};
// Temporary pixel buffer for updating texture
uint32_t pixels[2048];
// Load Application
if (chip8.loadApplication(argv[1]) != 0) // initialization included
{
std::cout << "Failed to Load Application" << std::endl;
return 1;
}
// Our event structure
SDL_Event e;
// For tracking if we want to quit
bool quit = false;
// Emulation loop
while (!quit)
{
chip8.emulateCycle();
// Read any SDL events that occured
while (SDL_PollEvent(&e))
{
//If user closes the window
if (e.type == SDL_QUIT)
quit = true;
// If user presses any key
if (e.type == SDL_KEYDOWN)
{
// If user presses ESC we exit
if (e.key.keysym.sym == SDLK_ESCAPE)
quit = true;
else
{
// find if valid is key pressed and set to 1
auto pressed_key = keymap.find(e.key.keysym.sym);
if (pressed_key != keymap.end())
{
printf ("Pressed Key: 0x%X\n", pressed_key->first);
printf ("Mapped to: 0x%X\n", pressed_key->second);
chip8.key[pressed_key->second] = 1;
}
}
}
// If user releases any key
if (e.type == SDL_KEYUP)
{
// find if valid is key pressed and set to 0
auto released_key = keymap.find(e.key.keysym.sym);
if (released_key != keymap.end())
{
printf ("Released Key: 0x%X\n", released_key->first);
printf ("Mapped to: 0x%X\n", released_key->second);
chip8.key[released_key->second] = 0;
}
}
}
// If draw occurred, redraw SDL screen
if(chip8.drawFlag)
{
chip8.drawFlag = false;
// Store pixels in temporary buffer
for (int i = 0; i < SCREEN_SIZE; ++i)
{
unsigned char pixel = chip8.gfx[i];
pixels[i] = (0x00FFFFFF * pixel) | 0xFF000000;
}
// Update SDL texture
SDL_UpdateTexture(texture, NULL, pixels, SCREEN_WIDTH* sizeof(Uint32));
// First clear the renderer
SDL_RenderClear(renderer);
// Draw the texture
SDL_RenderCopy(renderer, texture, NULL, NULL);
// Update the screen
SDL_RenderPresent(renderer);
}
// Sleep to slow down emulation speed
// std::this_thread::sleep_for(std::chrono::microseconds(1200));
SDL_Delay(100/60);
}
// Clean up our objects and quit
cleanup(renderer, window);
SDL_Quit();
return 0;
}