-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
463 lines (393 loc) · 16.2 KB
/
Copy pathmain.cpp
File metadata and controls
463 lines (393 loc) · 16.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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#define SDL_MAIN_HANDLED
#include "utilities/settings.hpp"
#include <stdio.h>
#include <stdint.h>
#include <math.h>
#include <memory>
#include <imgui.h>
#include <imgui_internal.h>
#include "third_party/imgui/imgui_impl_sdl2.h"
#include "third_party/imgui/imgui_impl_opengl3.h"
#ifdef TRACY_ENABLE
#define TRACY_OPENGL_AUTO_CALIBRATION
#include <tracy/tracy/TracyOpenGL.hpp>
#endif
#include "utilities/logger.hpp"
#include "emulators/emulators.hpp"
#include "eyecandy/dots.hpp"
#include "gui/filebrowser.hpp"
#include "gui/log.hpp"
#include "utilities/iconfonts/IconsFontAwesome7.h"
#include "utilities/fonts.hpp"
ImFont* VritaFontSmall = nullptr;
#ifdef TRACY_ENABLE
void* operator new(size_t size) {
void* ptr = malloc(size);
TracyAlloc(ptr, size);
return ptr;
}
void operator delete(void* ptr) noexcept {
TracyFree(ptr);
free(ptr);
}
#endif
SDL_Window* appWindow;
SDL_GLContext glContext;
Settings appSettings("app_settings.ini");
std::shared_ptr<Dots> eyeCandy_Dots;
bool SHOW_DOTS = false;
std::shared_ptr<Logger> logger;
std::shared_ptr<Emulators> managerEmulators;
std::string emulatorType = "dmg";
std::string romLoadError;
std::shared_ptr<FileBrowser> guiFileBrowser;
bool guiFileBrowserVisible = false;
std::shared_ptr<Log> guiLog;
bool guiLogVisible = false;
bool guiStyleOptionsVisible = false;
bool guiMetricsVisible = false;
static void showMainMenu() {
if (ImGui::BeginMainMenuBar()) {
if (ImGui::BeginMenu("File")) {
if (ImGui::MenuItem("Show Log"))
guiLogVisible = !guiLogVisible;
ImGui::Separator();
if (ImGui::MenuItem("Exit"))
vritaRunning = true;
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Emulators")) {
if (ImGui::MenuItem("GameBoy", nullptr, managerEmulators->EMULATORS_SHOW_DMG))
managerEmulators->EMULATORS_SHOW_DMG = !managerEmulators->EMULATORS_SHOW_DMG;
if (ImGui::MenuItem("GameBoy Advance", nullptr, managerEmulators->EMULATORS_SHOW_AGB))
managerEmulators->EMULATORS_SHOW_AGB = !managerEmulators->EMULATORS_SHOW_AGB;
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Debuggers")) {
if (ImGui::MenuItem("Memory Editor", nullptr, managerEmulators->debuggersMemoryEditorVisible))
managerEmulators->debuggersMemoryEditorVisible = !managerEmulators->debuggersMemoryEditorVisible;
if (ImGui::MenuItem("Tile Viewer (Tiles)", nullptr, managerEmulators->debuggerTileViewerVisible))
managerEmulators->debuggerTileViewerVisible = !managerEmulators->debuggerTileViewerVisible;
if (ImGui::MenuItem("Tilemap Viewer (BG)", nullptr, managerEmulators->debuggerTilemapViewerVisible))
managerEmulators->debuggerTilemapViewerVisible = !managerEmulators->debuggerTilemapViewerVisible;
if (ImGui::MenuItem("Sprite Viewer (OAM)", nullptr, managerEmulators->debuggerSpriteViewerVisible))
managerEmulators->debuggerSpriteViewerVisible = !managerEmulators->debuggerSpriteViewerVisible;
if (ImGui::MenuItem("Palette Viewer (Palettes)", nullptr, managerEmulators->debuggerPaletteViewerVisible))
managerEmulators->debuggerPaletteViewerVisible = !managerEmulators->debuggerPaletteViewerVisible;
if (ImGui::MenuItem("Debugger", nullptr, managerEmulators->debuggerDebuggerVisible))
managerEmulators->debuggerDebuggerVisible = !managerEmulators->debuggerDebuggerVisible;
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Eyecandy")) {
if (ImGui::MenuItem("Dots", nullptr, SHOW_DOTS))
SHOW_DOTS = !SHOW_DOTS;
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Tools")) {
if (ImGui::MenuItem("Style", nullptr, guiStyleOptionsVisible))
guiStyleOptionsVisible = !guiStyleOptionsVisible;
if (ImGui::MenuItem("Metrics", nullptr, guiMetricsVisible))
guiMetricsVisible = !guiMetricsVisible;
ImGui::EndMenu();
}
ImGui::EndMainMenuBar();
}
}
static void showFileBrowser(const char* type) {
guiFileBrowserVisible = !guiFileBrowserVisible;
emulatorType = type;
}
static void renderGUIComponents() {
if (guiFileBrowserVisible)
guiFileBrowser->render(&guiFileBrowserVisible, emulatorType);
if (guiLogVisible)
guiLog->render(&guiLogVisible);
if (guiStyleOptionsVisible)
ImGui::ShowStyleEditor(&ImGui::GetStyle());
if (guiMetricsVisible)
ImGui::ShowMetricsWindow(&guiMetricsVisible);
}
static void initComponents() {
guiMetricsVisible = appSettings.GetBool("Visibility", "gui_metrics_visible", false);
guiLogVisible = appSettings.GetBool("Visibility", "gui_log_visible", false);
guiLog = std::make_shared<Log>();
logger = std::make_shared<Logger>([] (const char* msg) {
guiLog->addToLog("%s\n", msg);
});
managerEmulators = std::make_shared<Emulators>(*logger);
managerEmulators->init(appSettings);
guiFileBrowser = std::make_shared<FileBrowser>(appSettings);
guiFileBrowser->init(std::bind_front(&Emulators::loadROM, managerEmulators));
}
static void saveAppSettings() {
appSettings.Set("Visibility", "gui_metrics_visible", guiMetricsVisible);
appSettings.Set("Visibility", "gui_log_visible", guiLogVisible);
int width;
int height;
SDL_GetWindowSizeInPixels(appWindow, &width, &height);
appSettings.Set("MainWindow", "width", width);
appSettings.Set("MainWindow", "height", height);
int x;
int y;
SDL_GetWindowPosition(appWindow, &x, &y);
appSettings.Set("MainWindow", "has_position", true);
appSettings.Set("MainWindow", "x", x);
appSettings.Set("MainWindow", "y", y);
appSettings.Save();
}
static void loadFonts() {
ImGuiIO& io = ImGui::GetIO();
float baseFontSize = 13.0f;
ImFontConfig base_config;
base_config.SizePixels = baseFontSize;
io.Fonts->AddFontDefault(&base_config);
static const ImWchar icons_ranges[] = { ICON_MIN_FA, ICON_MAX_16_FA, 0 };
float iconFontSizeNormal = baseFontSize * 1.5f;
ImFontConfig iconsConfigNormal;
iconsConfigNormal.MergeMode = true;
iconsConfigNormal.PixelSnapH = true;
iconsConfigNormal.GlyphMinAdvanceX = iconFontSizeNormal;
iconsConfigNormal.GlyphOffset = ImVec2(0.0f, 4.0f);
io.Fonts->AddFontFromFileTTF("./resources/fonts/fa-regular-400.ttf", iconFontSizeNormal, &iconsConfigNormal, icons_ranges);
io.Fonts->AddFontFromFileTTF("./resources/fonts/fa-solid-900.ttf", iconFontSizeNormal, &iconsConfigNormal, icons_ranges);
float iconFontSizeSmall = baseFontSize;
ImFontConfig smallBaseConfig;
smallBaseConfig.SizePixels = iconFontSizeSmall;
VritaFontSmall = io.Fonts->AddFontDefault(&smallBaseConfig);
ImFontConfig iconsConfigSmall;
iconsConfigSmall.MergeMode = true;
iconsConfigSmall.PixelSnapH = true;
iconsConfigSmall.GlyphMinAdvanceX = iconFontSizeSmall;
iconsConfigSmall.GlyphOffset = ImVec2(0.0f, 2.0f);
io.Fonts->AddFontFromFileTTF("./resources/fonts/fa-regular-400.ttf", iconFontSizeSmall, &iconsConfigSmall, icons_ranges);
io.Fonts->AddFontFromFileTTF("./resources/fonts/fa-solid-900.ttf", iconFontSizeSmall, &iconsConfigSmall, icons_ranges);
}
static bool initBackend() {
bool initialized = true;
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
printf("[VRITA] Error: SDL_Init(): %s\n", SDL_GetError());
return false;
}
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
#ifdef Def_Kuplung_DEBUG_BUILD
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
#endif
SDL_SetHint(SDL_HINT_MAC_CTRL_CLICK_EMULATE_RIGHT_CLICK, "1");
SDL_SetHint(SDL_HINT_VIDEO_HIGHDPI_DISABLED, "0");
#ifdef SDL_HINT_IME_SHOW_UI
SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");
#endif
float main_scale = ImGui_ImplSDL2_GetContentScaleForDisplay(0);
Uint32 window_flags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
int windowWidth = appSettings.GetInt("MainWindow", "width", WINDOW_WIDTH);
int windowHeight = appSettings.GetInt("MainWindow", "height", WINDOW_HEIGHT);
appWindow = SDL_CreateWindow(AppTitle, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, (int)(windowWidth * main_scale), (int)(windowHeight * main_scale), window_flags);
if (appWindow == nullptr) {
printf("[VRITA] Error: Window could not be created! SDL Error: %s\n", SDL_GetError());
initialized = false;
}
else {
glContext = SDL_GL_CreateContext(appWindow);
if (!glContext) {
printf("[VRITA] Error: Unable to create OpenGL context! SDL Error: %s\n", SDL_GetError());
initialized = false;
}
else {
if (SDL_GL_MakeCurrent(appWindow, glContext) != 0) {
printf("[VRITA] Warning: Unable to set current context! SDL Error: %s\n", SDL_GetError());
initialized = false;
}
else {
if (SDL_GL_SetSwapInterval(1) != 0) {
printf("[VRITA] Warning: Unable to set VSync! SDL Error: %s\n", SDL_GetError());
initialized = false;
}
#ifdef _WIN32
const GLenum glewInitCode = glewInit();
if (glewInitCode != GLEW_OK) {
printf("[VRITA] Cannot initialize GLEW.\n");
initialized = false;
}
#endif
#ifdef TRACY_ENABLE
TracyGpuContext;
#endif
printf("[VRITA] GL_VERSION: %s\n", (const char*)glGetString(GL_VERSION));
printf("[VRITA] GL_VENDOR: %s\n", (const char*)glGetString(GL_VENDOR));
printf("[VRITA] GL_RENDERER: %s\n", (const char*)glGetString(GL_RENDERER));
int glMajor = 0;
int glMinor = 0;
int glProfile = 0;
glGetIntegerv(GL_MAJOR_VERSION, &glMajor);
glGetIntegerv(GL_MINOR_VERSION, &glMinor);
glGetIntegerv(GL_CONTEXT_PROFILE_MASK, &glProfile);
printf("[VRITA] GL_MAJOR_VERSION.GL_MINOR_VERSION: %d.%d, CONTEXT_PROFILE_MASK: 0x%X (core=0x1)\n", glMajor, glMinor, glProfile);
}
}
}
return initialized;
}
int main(int argc, char** argv) {
#ifdef TRACY_ENABLE
ZoneScoped;
#endif
if (!initBackend()) {
printf("[VRITA] Error: Backend not initialized.\n");
exit(EXIT_FAILURE);
}
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
(void)io;
io.IniFilename = "gui_options.ini";
ImGui_ImplSDL2_InitForOpenGL(appWindow, glContext);
ImGui_ImplOpenGL3_Init("#version 410 core");
ImGui::StyleColorsDark();
loadFonts();
bool hasSavedPos = appSettings.GetBool("MainWindow", "has_position", false);
if (hasSavedPos) {
int windowX = appSettings.GetInt("MainWindow", "x", 0);
int windowY = appSettings.GetInt("MainWindow", "y", 0);
SDL_SetWindowPosition(appWindow, windowX, windowY);
}
else
SDL_SetWindowPosition(appWindow, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
initComponents();
eyeCandy_Dots = std::make_shared<Dots>();
if (!managerEmulators->createTexture()) {
logger->log("[VRITA] Error: Cannot create emulator texture");
return 1;
}
if (!eyeCandy_Dots->createTexture()) {
logger->log("[VRITA] Error: Cannot create dots texture");
return 1;
}
const ImVec4 clear_color = ImVec4(145.0f / 255.0f, 145.0f / 255.0f, 145.0f / 255.0f, 1.00f);
#ifdef TRACY_ENABLE
logger->log("[VRITA] Tracy profiling enabled.");
#endif
std::vector<std::string> droppedFiles;
bool showDropCountError = false;
while (!vritaRunning) {
#ifdef TRACY_ENABLE
ZoneScoped;
#endif
SDL_Event event;
{
#ifdef TRACY_ENABLE
ZoneScopedN("PollEvents");
#endif
while (SDL_PollEvent(&event)) {
ImGui_ImplSDL2_ProcessEvent(&event);
if (event.type == SDL_QUIT)
vritaRunning = true;
if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(appWindow))
vritaRunning = true;
if (event.type == SDL_KEYDOWN || event.type == SDL_KEYUP)
managerEmulators->handleKey(event.type, event.key.keysym.sym);
if (event.type == SDL_DROPFILE) {
logger->log("[VRITA] File dropped: %s", event.drop.file);
droppedFiles.emplace_back(event.drop.file);
SDL_free(event.drop.file);
}
if (event.type == SDL_DROPCOMPLETE) {
if (droppedFiles.size() == 1)
managerEmulators->loadROM(droppedFiles[0].c_str());
else if (droppedFiles.size() > 1)
showDropCountError = true;
droppedFiles.clear();
}
}
}
if (SDL_GetWindowFlags(appWindow) & SDL_WINDOW_MINIMIZED) {
SDL_Delay(10);
continue;
}
int fbW;
int fbH;
SDL_GetWindowSizeInPixels(appWindow, &fbW, &fbH);
glViewport(0, 0, fbW, fbH);
glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
glClear(GL_COLOR_BUFFER_BIT);
float emulatorTime = (float)SDL_GetTicks() / 1000.0f;
managerEmulators->generateTestPattern(emulatorTime);
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame();
if (showDropCountError) {
ImGui::OpenPopup("Error");
showDropCountError = false;
}
ImVec2 dropErrorCenter = ImGui::GetMainViewport()->GetCenter();
ImGui::SetNextWindowPos(dropErrorCenter, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));
if (ImGui::BeginPopupModal("Error", nullptr, ImGuiWindowFlags_AlwaysAutoResize)) {
ImGui::Text("Please, drop only one file at a time.");
ImGui::Dummy(ImVec2(0.0f, 10.0f));
float okButtonWidth = 120.0f;
ImGui::SetCursorPosX((ImGui::GetWindowSize().x - okButtonWidth) * 0.5f);
if (ImGui::Button("OK", ImVec2(okButtonWidth, 0)))
ImGui::CloseCurrentPopup();
ImGui::EndPopup();
}
showMainMenu();
renderGUIComponents();
{
#ifdef TRACY_ENABLE
ZoneScopedN("Emulators::Run");
#endif
managerEmulators->run(std::bind_front(&showFileBrowser), [] (const char* type) { emulatorType = type; });
}
if (SHOW_DOTS)
eyeCandy_Dots->run();
{
#ifdef TRACY_ENABLE
ZoneScopedN("ImGui::Render");
#endif
ImGui::Render();
}
{
#ifdef TRACY_ENABLE
TracyGpuZone("Framebuffer Upload");
#endif
managerEmulators->uploadFramebufferToTexture();
eyeCandy_Dots->uploadFramebufferToTexture();
}
ImDrawData* draw_data = ImGui::GetDrawData();
{
#ifdef TRACY_ENABLE
TracyGpuZone("ImGui Draw");
#endif
ImGui_ImplOpenGL3_RenderDrawData(draw_data);
}
{
#ifdef TRACY_ENABLE
ZoneScopedN("SwapWindow (vsync wait)");
#endif
SDL_GL_SwapWindow(appWindow);
}
#ifdef TRACY_ENABLE
TracyGpuCollect;
FrameMark;
#endif
}
managerEmulators->release(appSettings);
eyeCandy_Dots->release();
saveAppSettings();
ImGui::SaveIniSettingsToDisk("gui_options.ini");
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplSDL2_Shutdown();
ImGui::DestroyContext();
SDL_GL_DeleteContext(glContext);
SDL_DestroyWindow(appWindow);
SDL_Quit();
return 0;
}