2 // mainwin.cpp - Qt-based GUI for Virtual Jaguar: Main Application Window
4 // (C) 2009 Underground Software
6 // JLH = James L. Hammons <jlhamm@acm.org>
9 // --- ---------- -------------------------------------------------------------
10 // JLH 12/23/2009 Created this file
11 // JLH 12/20/2010 Added settings, menus & toolbars
21 // Uncomment this for debugging...
23 //#define DEBUGFOO // Various tool debugging...
24 //#define DEBUGTP // Toolpalette debugging...
33 #include "filepicker.h"
41 // Uncomment this to use built-in BIOS/CD-ROM BIOS
42 // You'll need a copy of jagboot.h & jagcd.h for this to work...!
43 //#define USE_BUILT_IN_BIOS
45 // Uncomment this for an official Virtual Jaguar release
46 //#define VJ_RELEASE_VERSION "2.0.0"
47 #warning !!! FIX !!! Figure out how to use this in GUI.CPP as well!
49 #ifdef USE_BUILT_IN_BIOS
54 // The way BSNES controls things is by setting a timer with a zero
55 // timeout, sleeping if not emulating anything. Seems there has to be a
58 // It has a novel approach to plugging-in/using different video/audio/input
59 // methods, can we do something similar or should we just use the built-in
62 // We're going to try to use the built-in OpenGL support and see how it goes.
63 // We'll make the VJ core modular so that it doesn't matter what GUI is in
64 // use, we can drop it in anywhere and use it as-is.
68 videoWidget = new GLWidget(this);
69 setCentralWidget(videoWidget);
70 setWindowIcon(QIcon(":/res/vj.xpm"));
71 setWindowTitle("Virtual Jaguar v2.0.0");
74 setUnifiedTitleAndToolBarOnMac(true);
76 aboutWin = new AboutWindow(this);
77 filePickWin = new FilePickerWindow(this);
79 videoWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
80 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
84 quitAppAct = new QAction(tr("E&xit"), this);
85 quitAppAct->setShortcuts(QKeySequence::Quit);
86 quitAppAct->setStatusTip(tr("Quit Virtual Jaguar"));
87 connect(quitAppAct, SIGNAL(triggered()), this, SLOT(close()));
89 powerAct = new QAction(QIcon(":/res/power.png"), tr("&Power"), this);
90 powerAct->setStatusTip(tr("Toggle running state"));
91 powerAct->setCheckable(true);
92 connect(powerAct, SIGNAL(triggered()), this, SLOT(ToggleRunState()));
94 zoomActs = new QActionGroup(this);
96 x1Act = new QAction(QIcon(":/res/zoom100.png"), tr("Zoom 100%"), zoomActs);
97 x1Act->setStatusTip(tr("Set window zoom to 100%"));
98 x1Act->setCheckable(true);
99 connect(x1Act, SIGNAL(triggered()), this, SLOT(SetZoom100()));
101 x2Act = new QAction(QIcon(":/res/zoom200.png"), tr("Zoom 200%"), zoomActs);
102 x2Act->setStatusTip(tr("Set window zoom to 200%"));
103 x2Act->setCheckable(true);
104 connect(x2Act, SIGNAL(triggered()), this, SLOT(SetZoom200()));
106 x3Act = new QAction(QIcon(":/res/zoom300.png"), tr("Zoom 300%"), zoomActs);
107 x3Act->setStatusTip(tr("Set window zoom to 300%"));
108 x3Act->setCheckable(true);
109 connect(x3Act, SIGNAL(triggered()), this, SLOT(SetZoom300()));
111 tvTypeActs = new QActionGroup(this);
113 ntscAct = new QAction(QIcon(":/res/generic.png"), tr("NTSC"), tvTypeActs);
114 ntscAct->setStatusTip(tr("Sets Jaguar to NTSC mode"));
115 ntscAct->setCheckable(true);
116 connect(ntscAct, SIGNAL(triggered()), this, SLOT(SetNTSC()));
118 palAct = new QAction(QIcon(":/res/generic.png"), tr("PAL"), tvTypeActs);
119 palAct->setStatusTip(tr("Sets Jaguar to PAL mode"));
120 palAct->setCheckable(true);
121 connect(palAct, SIGNAL(triggered()), this, SLOT(SetPAL()));
123 blurAct = new QAction(QIcon(":/res/generic.png"), tr("Blur"), this);
124 blurAct->setStatusTip(tr("Sets OpenGL rendering to GL_NEAREST"));
125 blurAct->setCheckable(true);
126 connect(blurAct, SIGNAL(triggered()), this, SLOT(ToggleBlur()));
128 aboutAct = new QAction(QIcon(":/res/generic.png"), tr("&About..."), this);
129 aboutAct->setStatusTip(tr("Blatant self-promotion"));
130 connect(aboutAct, SIGNAL(triggered()), this, SLOT(ShowAboutWin()));
132 filePickAct = new QAction(QIcon(":/res/generic.png"), tr("&Insert Cartridge..."), this);
133 filePickAct->setStatusTip(tr("Insert a cartridge into Virtual Jaguar"));
134 connect(filePickAct, SIGNAL(triggered()), this, SLOT(InsertCart()));
136 // Create menus & toolbars
138 fileMenu = menuBar()->addMenu(tr("&File"));
139 fileMenu->addAction(filePickAct);
140 fileMenu->addAction(powerAct);
141 fileMenu->addAction(quitAppAct);
143 helpMenu = menuBar()->addMenu(tr("&Help"));
144 helpMenu->addAction(aboutAct);
146 toolbar = addToolBar(tr("Stuff"));
147 toolbar->addAction(powerAct);
148 toolbar->addSeparator();
149 toolbar->addAction(x1Act);
150 toolbar->addAction(x2Act);
151 toolbar->addAction(x3Act);
152 toolbar->addSeparator();
153 toolbar->addAction(ntscAct);
154 toolbar->addAction(palAct);
155 toolbar->addSeparator();
156 toolbar->addAction(blurAct);
159 statusBar()->showMessage(tr("Ready"));
161 // Set toolbar buttons/menus based on settings read in (sync the UI)...
162 blurAct->setChecked(vjs.glFilter);
163 x1Act->setChecked(zoomLevel == 1);
164 x2Act->setChecked(zoomLevel == 2);
165 x3Act->setChecked(zoomLevel == 3);
166 running = powerAct->isChecked();
167 ntscAct->setChecked(vjs.hardwareTypeNTSC);
168 palAct->setChecked(!vjs.hardwareTypeNTSC);
170 // Do this in case original size isn't correct (mostly for the first-run case)
173 // Set up timer based loop for animation...
174 timer = new QTimer(this);
175 connect(timer, SIGNAL(timeout()), this, SLOT(Timer()));
178 #ifdef VJ_RELEASE_VERSION
179 WriteLog("Virtual Jaguar %s (Last full build was on %s %s)\n", VJ_RELEASE_VERSION, __DATE__, __TIME__);
181 WriteLog("Virtual Jaguar SVN %s (Last full build was on %s %s)\n", __DATE__, __DATE__, __TIME__);
183 WriteLog("Initializing jaguar subsystem...\n");
187 #ifdef USE_BUILT_IN_BIOS
188 WriteLog("VJ: Using built in BIOS/CD BIOS...\n");
189 memcpy(jaguarBootROM, jagBootROM, 0x20000);
190 memcpy(jaguarCDBootROM, jagCDROM, 0x40000);
191 BIOSLoaded = CDBIOSLoaded = true;
193 // What would be nice here would be a way to check if the BIOS was loaded so that we
194 // could disable the pushbutton on the Misc Options menu... !!! FIX !!! [DONE here, but needs to be fixed in GUI as well!]
195 WriteLog("About to attempt to load BIOSes...\n");
197 //This is short-circuiting the file finding thread... ??? WHY ???
198 BIOSLoaded = (JaguarLoadROM(jaguarBootROM, vjs.jagBootPath) == 0x20000 ? true : false);
202 WriteLog("VJ: BIOS is %savailable...\n", (BIOSLoaded ? "" : "not "));
203 CDBIOSLoaded = (JaguarLoadROM(jaguarCDBootROM, vjs.CDBootPath) == 0x40000 ? true : false);
204 WriteLog("VJ: CD BIOS is %savailable...\n", (CDBIOSLoaded ? "" : "not "));
207 SET32(jaguarMainRAM, 0, 0x00200000); // Set top of stack...
210 // JaguarLoadFile("./software/Rayman (World).j64");
211 // JaguarLoadFile("./software/I-War (World).j64");
212 JaguarLoadFile("./software/Alien vs Predator (World).j64");
214 //This is crappy!!! !!! FIX !!!
215 //Is this even needed any more? Hmm. Maybe. Dunno.
216 //Seems like it is... But then again, maybe not. Have to test it to see.
217 WriteLog("GUI: Resetting Jaguar...\n");
221 void MainWin::closeEvent(QCloseEvent * event)
224 event->accept(); // ignore() if can't close for some reason
227 void MainWin::Open(void)
232 // Here's the main emulator loop
234 void MainWin::Timer(void)
240 // Random hash & trash
241 // We try to simulate an untuned tank circuit here... :-)
242 for(uint32_t x=0; x<videoWidget->rasterWidth; x++)
244 for(uint32_t y=0; y<videoWidget->rasterHeight; y++)
246 videoWidget->buffer[(y * videoWidget->textureWidth) + x] = (rand() & 0xFF) << 8 | (rand() & 0xFF) << 16 | (rand() & 0xFF) << 24;// | (rand() & 0xFF);//0x000000FF;
247 // buffer[(y * textureWidth) + x] = x*y;
252 // memcpy(videoWidget->buffer, backbuffer, videoWidget->rasterHeight * videoWidget->rasterWidth);
253 memcpy(videoWidget->buffer, backbuffer, videoWidget->rasterHeight * videoWidget->textureWidth * sizeof(uint32_t));
254 // memcpy(surface->pixels, backbuffer, TOMGetVideoModeWidth() * TOMGetVideoModeHeight() * 4);
257 videoWidget->updateGL();
261 Window * RunEmu(void)
263 // extern uint32 * backbuffer;
264 uint32 * overlayPixels = (uint32 *)sdlemuGetOverlayPixels();
265 memset(overlayPixels, 0x00, 640 * 480 * 4); // Clear out overlay...
267 //This is crappy... !!! FIX !!!
268 // extern bool finished, showGUI;
270 sdlemuDisableOverlay();
272 // uint32 nFrame = 0, nFrameskip = 0;
273 uint32 totalFrames = 0;
275 bool showMessage = true;
276 uint32 showMsgFrames = 120;
277 uint8 transparency = 0xFF;
278 // Pass a message to the "joystick" code to debounce the ESC key...
279 debounceRunKey = true;
282 if (jaguarRomSize == 0x200000)
284 else if (jaguarRomSize == 0x400000)
286 else if (jaguarMainRomCRC32 == 0x687068D5)
288 else if (jaguarMainRomCRC32 == 0x55A0669C)
291 const char * cartTypeName[5] = { "2M Cartridge", "4M Cartridge", "CD BIOS", "CD Dev BIOS", "Homebrew" };
292 uint32 elapsedTicks = SDL_GetTicks(), frameCount = 0, framesPerSecond = 0;
296 // Set up new backbuffer with new pixels and data
299 //WriteLog("Frame #%u...\n", totalFrames);
300 //extern bool doDSPDis;
301 //if (totalFrames == 373)
304 //Problem: Need to do this *only* when the state changes from visible to not...
305 //Also, need to clear out the GUI when not on (when showMessage is active...)
306 if (showGUI || showMessage)
307 sdlemuEnableOverlay();
309 sdlemuDisableOverlay();
311 //Add in a new function for clearing patches of screen (ClearOverlayRect)
313 // Also: Take frame rate into account when calculating fade time...
315 // Some QnD GUI stuff here...
318 FillScreenRectangle(overlayPixels, 8, 1*FONT_HEIGHT, 128, 4*FONT_HEIGHT, 0x00000000);
319 extern uint32 gpu_pc, dsp_pc;
320 DrawString(overlayPixels, 8, 1*FONT_HEIGHT, false, "GPU PC: %08X", gpu_pc);
321 DrawString(overlayPixels, 8, 2*FONT_HEIGHT, false, "DSP PC: %08X", dsp_pc);
322 DrawString(overlayPixels, 8, 4*FONT_HEIGHT, false, "%u FPS", framesPerSecond);
327 DrawString2(overlayPixels, 8, 24*FONT_HEIGHT, 0x007F63FF, transparency, "Running...");
328 DrawString2(overlayPixels, 8, 26*FONT_HEIGHT, 0x001FFF3F, transparency, "%s, run address: %06X", cartTypeName[cartType], jaguarRunAddress);
329 DrawString2(overlayPixels, 8, 27*FONT_HEIGHT, 0x001FFF3F, transparency, "CRC: %08X", jaguarMainRomCRC32);
331 if (showMsgFrames == 0)
335 if (transparency == 0)
338 /*extern bool doGPUDis;
349 if (SDL_GetTicks() - elapsedTicks > 250)
350 elapsedTicks += 250, framesPerSecond = frameCount * 4, frameCount = 0;
353 // Save the background for the GUI...
354 // In this case, we squash the color to monochrome, then force it to blue + green...
355 for(uint32 i=0; i<TOMGetVideoModeWidth() * 256; i++)
357 uint32 pixel = backbuffer[i];
358 uint8 b = (pixel >> 16) & 0xFF, g = (pixel >> 8) & 0xFF, r = pixel & 0xFF;
359 pixel = ((r + g + b) / 3) & 0x00FF;
360 backbuffer[i] = 0xFF000000 | (pixel << 16) | (pixel << 8);
363 sdlemuEnableOverlay();
369 void MainWin::ToggleRunState(void)
376 for(uint32_t x=0; x<videoWidget->rasterWidth; x++)
377 for(uint32_t y=0; y<videoWidget->rasterHeight; y++)
378 videoWidget->buffer[(y * videoWidget->textureWidth) + x] = 0x00000000;
380 // for(uint32_t i=0; i<TOMGetVideoModeWidth() * 256; i++)
381 for(uint32_t i=0; i<videoWidget->textureWidth * 256; i++)
383 uint32_t pixel = backbuffer[i];
384 // uint8_t b = (pixel >> 16) & 0xFF, g = (pixel >> 8) & 0xFF, r = pixel & 0xFF;
385 uint8_t r = (pixel >> 24) & 0xFF, g = (pixel >> 16) & 0xFF, b = (pixel >> 8) & 0xFF;
386 pixel = ((r + g + b) / 3) & 0x00FF;
387 // backbuffer[i] = 0xFF000000 | (pixel << 16) | (pixel << 8);
388 backbuffer[i] = 0x000000FF | (pixel << 16) | (pixel << 8);
391 // memcpy(videoWidget->buffer, backbuffer, videoWidget->rasterHeight * videoWidget->rasterWidth);
392 memcpy(videoWidget->buffer, backbuffer, videoWidget->rasterHeight * videoWidget->textureWidth * sizeof(uint32_t));
395 videoWidget->updateGL();
399 void MainWin::SetZoom100(void)
405 void MainWin::SetZoom200(void)
411 void MainWin::SetZoom300(void)
417 void MainWin::SetNTSC(void)
419 vjs.hardwareTypeNTSC = true;
423 void MainWin::SetPAL(void)
425 vjs.hardwareTypeNTSC = false;
429 void MainWin::ToggleBlur(void)
431 vjs.glFilter = !vjs.glFilter;
434 void MainWin::ShowAboutWin(void)
439 void MainWin::InsertCart(void)
444 void MainWin::ResizeMainWindow(void)
446 videoWidget->setFixedSize(zoomLevel * 320, zoomLevel * (vjs.hardwareTypeNTSC ? 240 : 256));
449 for(int i=0; i<2; i++)
453 QApplication::processEvents();
457 void MainWin::ReadSettings(void)
459 QSettings settings("Underground Software", "Virtual Jaguar");
460 QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
461 QSize size = settings.value("size", QSize(400, 400)).toSize();
465 zoomLevel = settings.value("zoom", 1).toInt();
467 vjs.useJoystick = settings.value("useJoystick", false).toBool();
468 vjs.joyport = settings.value("joyport", 0).toInt();
469 vjs.hardwareTypeNTSC = settings.value("hardwareTypeNTSC", true).toBool();
470 vjs.frameSkip = settings.value("frameSkip", 0).toInt();
471 vjs.useJaguarBIOS = settings.value("useJaguarBIOS", false).toBool();
472 vjs.DSPEnabled = settings.value("DSPEnabled", false).toBool();
473 vjs.usePipelinedDSP = settings.value("usePipelinedDSP", false).toBool();
474 vjs.fullscreen = settings.value("fullscreen", false).toBool();
475 vjs.useOpenGL = settings.value("useOpenGL", true).toBool();
476 vjs.glFilter = settings.value("glFilterType", 0).toInt();
477 vjs.renderType = settings.value("renderType", 0).toInt();
478 strcpy(vjs.jagBootPath, settings.value("JagBootROM", "./bios/jagboot.rom").toString().toAscii().data());
479 strcpy(vjs.CDBootPath, settings.value("CDBootROM", "./bios/jagcd.rom").toString().toAscii().data());
480 strcpy(vjs.EEPROMPath, settings.value("EEPROMs", "./eeproms").toString().toAscii().data());
481 strcpy(vjs.ROMPath, settings.value("ROMs", "./software").toString().toAscii().data());
484 void MainWin::WriteSettings(void)
486 QSettings settings("Underground Software", "Virtual Jaguar");
487 settings.setValue("pos", pos());
488 settings.setValue("size", size());
490 settings.setValue("zoom", zoomLevel);
492 settings.setValue("useJoystick", vjs.useJoystick);
493 settings.setValue("joyport", vjs.joyport);
494 settings.setValue("hardwareTypeNTSC", vjs.hardwareTypeNTSC);
495 settings.setValue("frameSkip", vjs.frameSkip);
496 settings.setValue("useJaguarBIOS", vjs.useJaguarBIOS);
497 settings.setValue("DSPEnabled", vjs.DSPEnabled);
498 settings.setValue("usePipelinedDSP", vjs.usePipelinedDSP);
499 settings.setValue("fullscreen", vjs.fullscreen);
500 settings.setValue("useOpenGL", vjs.useOpenGL);
501 settings.setValue("glFilterType", vjs.glFilter);
502 settings.setValue("renderType", vjs.renderType);
503 settings.setValue("JagBootROM", vjs.jagBootPath);
504 settings.setValue("CDBootROM", vjs.CDBootPath);
505 settings.setValue("EEPROMs", vjs.EEPROMPath);
506 settings.setValue("ROMs", vjs.ROMPath);
509 // Here's how Byuu does it...
510 // I think I have it working now... :-)
512 void Utility::resizeMainWindow()
514 unsigned region = config().video.context->region;
515 unsigned multiplier = config().video.context->multiplier;
516 unsigned width = 256 * multiplier;
517 unsigned height = (region == 0 ? 224 : 239) * multiplier;
519 if(config().video.context->correctAspectRatio)
523 width = (double)width * config().video.ntscAspectRatio + 0.5; //NTSC adjust
527 width = (double)width * config().video.palAspectRatio + 0.5; //PAL adjust
531 if(config().video.isFullscreen == false)
533 //get effective desktop work area region (ignore Windows taskbar, OS X dock, etc.)
534 QRect deskRect = QApplication::desktop()->availableGeometry(mainWindow);
536 //ensure window size will not be larger than viewable desktop area
537 constrainSize(height, width, deskRect.height()); //- frameHeight);
538 constrainSize(width, height, deskRect.width()); //- frameWidth );
540 mainWindow->canvas->setFixedSize(width, height);
545 for(unsigned i = 0; i < 2; i++)
547 unsigned iWidth = width, iHeight = height;
549 constrainSize(iHeight, iWidth, mainWindow->canvasContainer->size().height());
550 constrainSize(iWidth, iHeight, mainWindow->canvasContainer->size().width());
552 //center canvas onscreen; ensure it is not larger than viewable area
553 mainWindow->canvas->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
554 mainWindow->canvas->setFixedSize(iWidth, iHeight);
555 mainWindow->canvas->setMinimumSize(0, 0);
558 QApplication::processEvents();
562 //workaround for Qt/Xlib bug:
563 //if window resize occurs with cursor over it, Qt shows Qt::Size*DiagCursor;
564 //so force it to show Qt::ArrowCursor, as expected
565 mainWindow->setCursor(Qt::ArrowCursor);
566 mainWindow->canvasContainer->setCursor(Qt::ArrowCursor);
567 mainWindow->canvas->setCursor(Qt::ArrowCursor);
569 //workaround for DirectSound(?) bug:
570 //window resizing sometimes breaks audio sync, this call re-initializes it
574 void Utility::setScale(unsigned scale)
576 config().video.context->multiplier = scale;
578 mainWindow->shrink();
579 mainWindow->syncUi();
582 void QbWindow::shrink()
584 if(config().video.isFullscreen == false)
586 for(unsigned i = 0; i < 2; i++)
590 QApplication::processEvents();