-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
119 lines (93 loc) · 3.2 KB
/
Copy pathmain.cpp
File metadata and controls
119 lines (93 loc) · 3.2 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
#include <iostream>
#include <SDL3/SDL.h>
#include "cpu.hpp"
//Key pressed
enum Key
{
K1, K2, K3, K4,
KQ, KW, KE, KR,
KA, KS, KD, KF,
KZ, KX, KC, KV
};
CPU myCPU;
int main()
{
SDL_Init(SDL_INIT_VIDEO);
// Size of each pixel in a 64x32 grid
const int pixel_size = 20;
SDL_Window* window = SDL_CreateWindow("blank", 64 * pixel_size, 32 * pixel_size, SDL_WINDOW_RESIZABLE);
SDL_Renderer* renderer = SDL_CreateRenderer(window, NULL);
SDL_Event e;
bool run = true;
myCPU.initialize();
if(myCPU.load("C:\\C++\\Chip8-Emulator\\Chip-8-Emulator\\Cave.ch8"))
{
return 1;
}
uint32_t last_timer_tick = SDL_GetTicks();
const int cycles_per_second = 700;
const int ms_per_cycle = 1000 / cycles_per_second;
while(run)
{
// Get the cycle to speed
Uint32 start = SDL_GetTicks();
myCPU.emulateCycle();
Uint32 duration = SDL_GetTicks() - start;
if (duration < ms_per_cycle)
SDL_Delay(ms_per_cycle - duration);
// Update timers at 60 Hz
if(SDL_GetTicks() - last_timer_tick >= 16) {
if(myCPU.getDelay() > 0) myCPU.setDelay(myCPU.getDelay()-1);
if(myCPU.getSound() > 0) myCPU.setSound(myCPU.getSound()-1);
last_timer_tick = SDL_GetTicks();
}
while(SDL_PollEvent(&e))
{
if(e.type == SDL_EVENT_QUIT)
{
run = false;
}
}
const bool* kb = SDL_GetKeyboardState(NULL);
myCPU.setKey(Key::K1, kb[SDL_SCANCODE_1]);
myCPU.setKey(Key::K2, kb[SDL_SCANCODE_2]);
myCPU.setKey(Key::K3, kb[SDL_SCANCODE_3]);
myCPU.setKey(Key::K4, kb[SDL_SCANCODE_4]);
myCPU.setKey(Key::KQ, kb[SDL_SCANCODE_Q]);
myCPU.setKey(Key::KW, kb[SDL_SCANCODE_W]);
myCPU.setKey(Key::KE, kb[SDL_SCANCODE_E]);
myCPU.setKey(Key::KR, kb[SDL_SCANCODE_R]);
myCPU.setKey(Key::KA, kb[SDL_SCANCODE_A]);
myCPU.setKey(Key::KS, kb[SDL_SCANCODE_S]);
myCPU.setKey(Key::KD, kb[SDL_SCANCODE_D]);
myCPU.setKey(Key::KF, kb[SDL_SCANCODE_F]);
myCPU.setKey(Key::KZ, kb[SDL_SCANCODE_Z]);
myCPU.setKey(Key::KX, kb[SDL_SCANCODE_X]);
myCPU.setKey(Key::KC, kb[SDL_SCANCODE_C]);
myCPU.setKey(Key::KV, kb[SDL_SCANCODE_V]);
// Draw the emulated graphics
if(myCPU.draw_flag)
{
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
for(int y_line = 0;y_line < 32;y_line++)
{
for(int x_line = 0;x_line < 64;x_line++)
{
SDL_FRect pixel = {x_line * pixel_size, y_line * pixel_size, pixel_size, pixel_size};
if(myCPU.getGfx()[(y_line * 64) + x_line])
{
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderFillRect(renderer, &pixel);
}
}
}
SDL_RenderPresent(renderer);
myCPU.draw_flag = false;
}
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}