-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
144 lines (117 loc) · 4.37 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
#include "Chip8.h"
#include <iostream>
#include <fstream>
#include <SDL.h>
#undef main
void handle_window_event(SDL_Renderer *renderer, const SDL_Event &event);
void set_scale(SDL_Renderer *renderer, int width, int height);
int main(int argc, char** argv) {
if(argc != 2) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Invalid number of arguments, please provide a path to a ROM file");
return -1;
}
std::string file = argv[1];
const int width = 500;
const int height = 500;
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *window = SDL_CreateWindow(
"Chip8 Emulator",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
width,
height,
0
);
SDL_SetWindowResizable(window, SDL_TRUE);
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer);
set_scale(renderer, width, height);
Chip8 chip8(renderer);
chip8.read_rom(file);
SDL_Event event;
SDL_PollEvent(&event);
bool quit = false;
while (!quit) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
quit = true;
} else if (event.type == SDL_KEYDOWN) {
chip8.handle_keydown(event.key.keysym.sym);
SDL_Log("Down %d", event.key.keysym.sym);
} else if (event.type == SDL_KEYUP) {
chip8.handle_keyup(event.key.keysym.sym);
SDL_Log("Up %d", event.key.keysym.sym);
} else if (event.type == SDL_WINDOWEVENT) {
handle_window_event(renderer, event);
}
}
chip8.emulate_cycle();
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
void set_scale(SDL_Renderer *renderer, int width, int height) {
SDL_RenderSetScale(renderer, width / Chip8::width, height / Chip8::height);
}
void handle_window_event(SDL_Renderer *renderer, const SDL_Event &event) {
switch (event.window.event) {
case SDL_WINDOWEVENT_SHOWN:
SDL_Log("Window %d shown", event.window.windowID);
break;
case SDL_WINDOWEVENT_HIDDEN:
SDL_Log("Window %d hidden", event.window.windowID);
break;
case SDL_WINDOWEVENT_EXPOSED:
SDL_Log("Window %d exposed", event.window.windowID);
break;
case SDL_WINDOWEVENT_MOVED:
SDL_Log("Window %d moved to %d,%d",
event.window.windowID, event.window.data1,
event.window.data2);
break;
case SDL_WINDOWEVENT_RESIZED:
SDL_Log("Window %d resized to %dx%d",
event.window.windowID, event.window.data1,
event.window.data2);
set_scale(renderer, event.window.data1, event.window.data2);
break;
case SDL_WINDOWEVENT_SIZE_CHANGED:
SDL_Log("Window %d size changed to %dx%d",
event.window.windowID, event.window.data1,
event.window.data2);
set_scale(renderer, event.window.data1, event.window.data2);
break;
case SDL_WINDOWEVENT_MINIMIZED:
SDL_Log("Window %d minimized", event.window.windowID);
break;
case SDL_WINDOWEVENT_MAXIMIZED:
SDL_Log("Window %d maximized", event.window.windowID);
break;
case SDL_WINDOWEVENT_RESTORED:
SDL_Log("Window %d restored", event.window.windowID);
break;
case SDL_WINDOWEVENT_ENTER:
SDL_Log("Mouse entered window %d",
event.window.windowID);
break;
case SDL_WINDOWEVENT_LEAVE:
SDL_Log("Mouse left window %d", event.window.windowID);
break;
case SDL_WINDOWEVENT_FOCUS_GAINED:
SDL_Log("Window %d gained keyboard focus",
event.window.windowID);
break;
case SDL_WINDOWEVENT_FOCUS_LOST:
SDL_Log("Window %d lost keyboard focus",
event.window.windowID);
break;
case SDL_WINDOWEVENT_CLOSE:
SDL_Log("Window %d closed", event.window.windowID);
break;
default:
std::cerr << "Unknown event " << event.window.event << '\n';
}
}