-
Notifications
You must be signed in to change notification settings - Fork 2
/
wmu.cpp
251 lines (237 loc) · 7.8 KB
/
wmu.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#define _CRT_SECURE_NO_DEPRECATE
#include "SDL2/include/SDL.h"
#include <string>
#include <string.h>
#include <thread>
#ifdef _WIN32
#include <Windows.h>
#include <WinUser.h>
#include "SDL2/include/SDL_syswm.h"
#endif // _WIN32
#include "commctrl.h"
#include "ppu.h"
#include "cpu.h"
#include "mmu.h"
#include "main.h"
#include "input.h"
#include "commctrl.h"
#undef main
using namespace::std;
SDL_Window* mainWindow; // Main Window
void initWindow(SDL_Window *win, string filename) {
mainWindow = win;
char title[50];
string rom = filename;
if (filename.find_last_of("\\") != string::npos)
rom = filename.substr(filename.find_last_of("\\") + 1);
snprintf(title, sizeof title, "[ q00.nes ][ rom: %s ]", rom.c_str());
SDL_SetWindowTitle(mainWindow, title);
SDL_SysWMinfo wmInfo;
SDL_VERSION(&wmInfo.version);
SDL_GetWindowWMInfo(mainWindow, &wmInfo);
HWND hwnd = wmInfo.info.win.window;
HMENU hMenuBar = CreateMenu();
HMENU hFile = CreateMenu();
HMENU hEdit = CreateMenu();
HMENU hHelp = CreateMenu();
HMENU hConfig = CreateMenu();
HMENU hSound = CreateMenu();
HMENU hPalettes = CreateMenu();
HMENU hDebugger = CreateMenu();
HMENU hSavestates = CreateMenu();
HMENU hVol = CreateMenu();
AppendMenu(hMenuBar, MF_POPUP, (UINT_PTR)hFile, "[ main ]");
AppendMenu(hMenuBar, MF_POPUP, (UINT_PTR)hConfig, "[ config ]");
AppendMenu(hMenuBar, MF_POPUP, (UINT_PTR)hVol, "[ vol ]");
AppendMenu(hMenuBar, MF_POPUP, (UINT_PTR)hSavestates, "[ savestates ]");
AppendMenu(hMenuBar, MF_STRING, 11, "[ ||> un/pause ]");
AppendMenu(hMenuBar, MF_POPUP, (UINT_PTR)hDebugger, "[ debug ]");
AppendMenu(hMenuBar, MF_POPUP, (UINT_PTR)hHelp, "[ help ]");
AppendMenu(hFile, MF_STRING, 9, "» load rom");
AppendMenu(hFile, MF_STRING, 7, "» reset");
AppendMenu(hFile, MF_STRING, 1, "» exit");
AppendMenu(hHelp, MF_STRING, 3, "» about");
AppendMenu(hDebugger, MF_STRING, 4, "» CHR table");
AppendMenu(hDebugger, MF_STRING, 5, "» Nametables");
AppendMenu(hDebugger, MF_STRING, 10, "» OAM tables");
AppendMenu(hSavestates, MF_STRING, 12, "» save state");
AppendMenu(hSavestates, MF_STRING, 13, "» load state");
AppendMenu(hConfig, MF_POPUP, (UINT_PTR)hSound, "[ sound ]");
AppendMenu(hSound, MF_STRING, 14, "» disable/enable");
AppendMenu(hSound, MF_STRING, 15, "» disable/enable SC1");
AppendMenu(hSound, MF_STRING, 16, "» disable/enable SC2");
AppendMenu(hSound, MF_STRING, 17, "» disable/enable SC3");
AppendMenu(hSound, MF_STRING, 18, "» disable/enable SC4");
AppendMenu(hSound, MF_STRING, 23, "» toggle 8-bit remix mode");
AppendMenu(hVol, MF_STRING, 24, "» 10%");
AppendMenu(hVol, MF_STRING, 25, "» 20%");
AppendMenu(hVol, MF_STRING, 26, "» 30%");
AppendMenu(hVol, MF_STRING, 27, "» 40%");
AppendMenu(hVol, MF_STRING, 28, "» 50%");
AppendMenu(hVol, MF_STRING, 29, "» 60%");
AppendMenu(hVol, MF_STRING, 30, "» 70%");
AppendMenu(hVol, MF_STRING, 31, "» 80%");
AppendMenu(hVol, MF_STRING, 32, "» 90%");
AppendMenu(hVol, MF_STRING, 33, "» 100%");
SetMenu(hwnd, hMenuBar);
// Enable WM events for SDL Window
SDL_EventState(SDL_SYSWMEVENT, SDL_ENABLE);
}
void handleWindowEvents(SDL_Event event) {
// poll events from menu
SDL_PollEvent(&event);
switch (event.type)
{
case SDL_SYSWMEVENT:
if (event.syswm.msg->msg.win.msg == WM_COMMAND) {
// Exit
if (LOWORD(event.syswm.msg->msg.win.wParam) == 1) {
exit(0);
}
// About
else if (LOWORD(event.syswm.msg->msg.win.wParam) == 3) {
//showAbout();
}
// CHR Table
else if (LOWORD(event.syswm.msg->msg.win.wParam) == 4) {
drawCHRTable();
}
// Nametables
else if (LOWORD(event.syswm.msg->msg.win.wParam) == 5) {
drawNameTables();
}
// Sprite Map
else if (LOWORD(event.syswm.msg->msg.win.wParam) == 6) {
}
// Reset
else if (LOWORD(event.syswm.msg->msg.win.wParam) == 7) {
resetCPU();
}
// Memory Map
else if (LOWORD(event.syswm.msg->msg.win.wParam) == 8) {
//showMemoryMap();
}
// Load ROM
else if (LOWORD(event.syswm.msg->msg.win.wParam) == 9) {
char f[100];
OPENFILENAME ofn;
ZeroMemory(&f, sizeof(f));
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = NULL; // If you have a window to center over, put its HANDLE here
ofn.lpstrFilter = "NES Roms\0*.nes\0";
ofn.lpstrFile = f;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrTitle = "[ rom selection ]";
ofn.Flags = OFN_DONTADDTORECENT | OFN_FILEMUSTEXIST;
if (GetOpenFileNameA(&ofn)) {
loadROM(f);
}
}
// OAM
else if (LOWORD(event.syswm.msg->msg.win.wParam) == 10) {
drawOAM();
}
// pause / unpause
else if (LOWORD(event.syswm.msg->msg.win.wParam) == 11) {
togglePause();
}
// save state
else if (LOWORD(event.syswm.msg->msg.win.wParam) == 12) {
// TODO STUB
/*printf("saving state\n");
char f[100];
OPENFILENAME ofn;
ZeroMemory(&f, sizeof(f));
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = NULL; // If you have a window to center over, put its HANDLE here
ofn.lpstrFilter = "GameBoy Savestates\0*.gbss\0";
ofn.lpstrFile = f;
ofn.lpstrDefExt = "gbss";
ofn.nMaxFile = MAX_PATH;
ofn.lpstrTitle = "[ rom selection ]";
ofn.Flags = OFN_DONTADDTORECENT;
if (GetSaveFileNameA(&ofn)) {
filename = f;
saveState(f, registers, flags, pc, sp);
}*/
}
// load state
else if (LOWORD(event.syswm.msg->msg.win.wParam) == 13) {
// halt emulation to avoid damaging the data (keep it that way, user must unpause)
/*unpaused = false;
printf("loading savestate\n");
char f[100];
OPENFILENAME ofn;
ZeroMemory(&f, sizeof(f));
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = NULL;
ofn.lpstrFilter = "GameBoy Savestates\0*.gbss\0";
ofn.lpstrFile = f;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrTitle = "[ savestate selection ]";
ofn.Flags = OFN_DONTADDTORECENT | OFN_FILEMUSTEXIST;
if (GetOpenFileNameA(&ofn)) {
filename = f;
loadState(f, registers, flags, pc, sp);
}*/
}
// disable / enable sound
else if (LOWORD(event.syswm.msg->msg.win.wParam) == 14) {
//toggleAudio();
}
// disable / enable SC1
else if (LOWORD(event.syswm.msg->msg.win.wParam) == 15) {
//toggleSC1();
}
// disable / enable SC2
else if (LOWORD(event.syswm.msg->msg.win.wParam) == 16) {
//toggleSC2();
}
// disable / enable SC3
else if (LOWORD(event.syswm.msg->msg.win.wParam) == 17) {
//toggleSC3();
}
// disable / enable SC4
else if (LOWORD(event.syswm.msg->msg.win.wParam) == 18) {
//toggleSC4();
}
// toggle 8bit remix mode
else if (LOWORD(event.syswm.msg->msg.win.wParam) == 23) {
//toggleRemix();
}
// set volume
else if (LOWORD(event.syswm.msg->msg.win.wParam) == 24 ||
LOWORD(event.syswm.msg->msg.win.wParam) == 25 ||
LOWORD(event.syswm.msg->msg.win.wParam) == 26 ||
LOWORD(event.syswm.msg->msg.win.wParam) == 27 ||
LOWORD(event.syswm.msg->msg.win.wParam) == 28 ||
LOWORD(event.syswm.msg->msg.win.wParam) == 29 ||
LOWORD(event.syswm.msg->msg.win.wParam) == 30 ||
LOWORD(event.syswm.msg->msg.win.wParam) == 31 ||
LOWORD(event.syswm.msg->msg.win.wParam) == 32 ||
LOWORD(event.syswm.msg->msg.win.wParam) == 33
) {
//setVolume((float)(LOWORD(event.syswm.msg->msg.win.wParam) - 23) * 0.1);
}
}
// close a window
if (event.syswm.msg->msg.win.msg == WM_CLOSE) {
DestroyWindow(event.syswm.msg->msg.win.hwnd);
PostMessage(event.syswm.msg->msg.win.hwnd, WM_CLOSE, 0, 0);
}
break;
default:
break;
};
uint8_t* keys = (uint8_t*)SDL_GetKeyboardState(NULL);
// pass inputs
setController1(keys);
// pause/unpause
if (keys[SDL_SCANCODE_SPACE]) {
togglePause();
keys[SDL_SCANCODE_SPACE] = 0;
}
}