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.
66 MainWin::MainWin(): running(false), powerButtonOn(false), showUntunedTankCircuit(true),
67 cartridgeLoaded(false)
69 videoWidget = new GLWidget(this);
70 setCentralWidget(videoWidget);
71 setWindowIcon(QIcon(":/res/vj-icon.png"));
72 setWindowTitle("Virtual Jaguar v2.0.0");
75 setUnifiedTitleAndToolBarOnMac(true);
77 #warning "!!! Save/Restore window location for FilePickerWindow !!!"
78 aboutWin = new AboutWindow(this);
79 filePickWin = new FilePickerWindow(this);
81 videoWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
82 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
86 quitAppAct = new QAction(tr("E&xit"), this);
87 quitAppAct->setShortcuts(QKeySequence::Quit);
88 quitAppAct->setStatusTip(tr("Quit Virtual Jaguar"));
89 connect(quitAppAct, SIGNAL(triggered()), this, SLOT(close()));
91 powerAct = new QAction(QIcon(":/res/power.png"), tr("&Power"), this);
92 powerAct->setStatusTip(tr("Powers Jaguar on/off"));
93 powerAct->setCheckable(true);
94 powerAct->setChecked(false);
95 powerAct->setDisabled(true);
96 connect(powerAct, SIGNAL(triggered()), this, SLOT(TogglePowerState()));
98 pauseAct = new QAction(QIcon(":/res/pause.png"), tr("Pause"), this);
99 pauseAct->setStatusTip(tr("Toggles the running state"));
100 pauseAct->setCheckable(true);
101 pauseAct->setDisabled(true);
102 connect(pauseAct, SIGNAL(triggered()), this, SLOT(ToggleRunState()));
104 zoomActs = new QActionGroup(this);
106 x1Act = new QAction(QIcon(":/res/zoom100.png"), tr("Zoom 100%"), zoomActs);
107 x1Act->setStatusTip(tr("Set window zoom to 100%"));
108 x1Act->setCheckable(true);
109 connect(x1Act, SIGNAL(triggered()), this, SLOT(SetZoom100()));
111 x2Act = new QAction(QIcon(":/res/zoom200.png"), tr("Zoom 200%"), zoomActs);
112 x2Act->setStatusTip(tr("Set window zoom to 200%"));
113 x2Act->setCheckable(true);
114 connect(x2Act, SIGNAL(triggered()), this, SLOT(SetZoom200()));
116 x3Act = new QAction(QIcon(":/res/zoom300.png"), tr("Zoom 300%"), zoomActs);
117 x3Act->setStatusTip(tr("Set window zoom to 300%"));
118 x3Act->setCheckable(true);
119 connect(x3Act, SIGNAL(triggered()), this, SLOT(SetZoom300()));
121 tvTypeActs = new QActionGroup(this);
123 ntscAct = new QAction(QIcon(":/res/generic.png"), tr("NTSC"), tvTypeActs);
124 ntscAct->setStatusTip(tr("Sets Jaguar to NTSC mode"));
125 ntscAct->setCheckable(true);
126 connect(ntscAct, SIGNAL(triggered()), this, SLOT(SetNTSC()));
128 palAct = new QAction(QIcon(":/res/generic.png"), tr("PAL"), tvTypeActs);
129 palAct->setStatusTip(tr("Sets Jaguar to PAL mode"));
130 palAct->setCheckable(true);
131 connect(palAct, SIGNAL(triggered()), this, SLOT(SetPAL()));
133 blurAct = new QAction(QIcon(":/res/generic.png"), tr("Blur"), this);
134 blurAct->setStatusTip(tr("Sets OpenGL rendering to GL_NEAREST"));
135 blurAct->setCheckable(true);
136 connect(blurAct, SIGNAL(triggered()), this, SLOT(ToggleBlur()));
138 aboutAct = new QAction(QIcon(":/res/generic.png"), tr("&About..."), this);
139 aboutAct->setStatusTip(tr("Blatant self-promotion"));
140 connect(aboutAct, SIGNAL(triggered()), this, SLOT(ShowAboutWin()));
142 #warning "!!! Set up a decent keyboard shortcut for Insert Cartridge !!!"
143 filePickAct = new QAction(QIcon(":/res/software.png"), tr("&Insert Cartridge..."), this);
144 filePickAct->setStatusTip(tr("Insert a cartridge into Virtual Jaguar"));
145 connect(filePickAct, SIGNAL(triggered()), this, SLOT(InsertCart()));
147 // Misc. connections...
148 connect(filePickWin, SIGNAL(RequestLoad(QString)), this, SLOT(LoadSoftware(QString)));
150 // Create menus & toolbars
152 fileMenu = menuBar()->addMenu(tr("&File"));
153 fileMenu->addAction(filePickAct);
154 fileMenu->addAction(powerAct);
155 fileMenu->addAction(pauseAct);
156 fileMenu->addAction(quitAppAct);
158 helpMenu = menuBar()->addMenu(tr("&Help"));
159 helpMenu->addAction(aboutAct);
161 toolbar = addToolBar(tr("Stuff"));
162 toolbar->addAction(powerAct);
163 toolbar->addAction(pauseAct);
164 toolbar->addAction(filePickAct);
165 toolbar->addSeparator();
166 toolbar->addAction(x1Act);
167 toolbar->addAction(x2Act);
168 toolbar->addAction(x3Act);
169 toolbar->addSeparator();
170 toolbar->addAction(ntscAct);
171 toolbar->addAction(palAct);
172 toolbar->addSeparator();
173 toolbar->addAction(blurAct);
176 statusBar()->showMessage(tr("Ready"));
178 // Set toolbar buttons/menus based on settings read in (sync the UI)...
179 blurAct->setChecked(vjs.glFilter);
180 x1Act->setChecked(zoomLevel == 1);
181 x2Act->setChecked(zoomLevel == 2);
182 x3Act->setChecked(zoomLevel == 3);
183 running = powerAct->isChecked();
184 ntscAct->setChecked(vjs.hardwareTypeNTSC);
185 palAct->setChecked(!vjs.hardwareTypeNTSC);
187 // Do this in case original size isn't correct (mostly for the first-run case)
190 // Set up timer based loop for animation...
191 timer = new QTimer(this);
192 connect(timer, SIGNAL(timeout()), this, SLOT(Timer()));
195 // NOTE: Keyboards/joysticks will *not* work until SDL is brought back in, or
196 // the key handling is improved in Qt...
197 // Wait a minute... it seems they already are... So why no keyboard love?
199 #ifdef VJ_RELEASE_VERSION
200 WriteLog("Virtual Jaguar %s (Last full build was on %s %s)\n", VJ_RELEASE_VERSION, __DATE__, __TIME__);
202 WriteLog("Virtual Jaguar SVN %s (Last full build was on %s %s)\n", __DATE__, __DATE__, __TIME__);
204 WriteLog("Initializing jaguar subsystem...\n");
208 #ifdef USE_BUILT_IN_BIOS
209 WriteLog("VJ: Using built in BIOS/CD BIOS...\n");
210 memcpy(jaguarBootROM, jagBootROM, 0x20000);
211 memcpy(jaguarCDBootROM, jagCDROM, 0x40000);
212 BIOSLoaded = CDBIOSLoaded = true;
214 // What would be nice here would be a way to check if the BIOS was loaded so that we
215 // could disable the pushbutton on the Misc Options menu... !!! FIX !!! [DONE here, but needs to be fixed in GUI as well!]
216 WriteLog("About to attempt to load BIOSes...\n");
218 //This is short-circuiting the file finding thread... ??? WHY ???
219 BIOSLoaded = (JaguarLoadROM(jaguarBootROM, vjs.jagBootPath) == 0x20000 ? true : false);
223 WriteLog("VJ: BIOS is %savailable...\n", (BIOSLoaded ? "" : "not "));
224 CDBIOSLoaded = (JaguarLoadROM(jaguarCDBootROM, vjs.CDBootPath) == 0x40000 ? true : false);
225 WriteLog("VJ: CD BIOS is %savailable...\n", (CDBIOSLoaded ? "" : "not "));
228 SET32(jaguarMainRAM, 0, 0x00200000); // Set top of stack...
231 // JaguarLoadFile("./software/Rayman (World).j64");
232 // JaguarLoadFile("./software/I-War (World).j64");
233 // JaguarLoadFile("./software/Alien vs Predator (World).j64");
234 //no JaguarLoadFile("./software/battlesphere.bin");
235 // JaguarLoadFile("./software/Battle Sphere Gold (World).j64");
236 // JaguarLoadFile("./software/Rayman (USA, Europe).zip");
237 //This is crappy!!! !!! FIX !!!
238 //Is this even needed any more? Hmm. Maybe. Dunno.
239 //Seems like it is... But then again, maybe not. Have to test it to see.
240 //WriteLog("GUI: Resetting Jaguar...\n");
244 void MainWin::closeEvent(QCloseEvent * event)
247 event->accept(); // ignore() if can't close for some reason
250 void MainWin::Open(void)
255 // Here's the main emulator loop
257 void MainWin::Timer(void)
262 if (showUntunedTankCircuit)
264 // Random hash & trash
265 // We try to simulate an untuned tank circuit here... :-)
266 for(uint32_t x=0; x<videoWidget->rasterWidth; x++)
268 for(uint32_t y=0; y<videoWidget->rasterHeight; y++)
270 videoWidget->buffer[(y * videoWidget->textureWidth) + x] = (rand() & 0xFF) << 8 | (rand() & 0xFF) << 16 | (rand() & 0xFF) << 24;// | (rand() & 0xFF);//0x000000FF;
271 // buffer[(y * textureWidth) + x] = x*y;
277 // Otherwise, run the Jaguar simulation
279 // memcpy(videoWidget->buffer, backbuffer, videoWidget->rasterHeight * videoWidget->rasterWidth);
280 memcpy(videoWidget->buffer, backbuffer, videoWidget->rasterHeight * videoWidget->textureWidth * sizeof(uint32_t));
281 // memcpy(surface->pixels, backbuffer, TOMGetVideoModeWidth() * TOMGetVideoModeHeight() * 4);
284 videoWidget->updateGL();
288 Window * RunEmu(void)
290 // extern uint32 * backbuffer;
291 uint32 * overlayPixels = (uint32 *)sdlemuGetOverlayPixels();
292 memset(overlayPixels, 0x00, 640 * 480 * 4); // Clear out overlay...
294 //This is crappy... !!! FIX !!!
295 // extern bool finished, showGUI;
297 sdlemuDisableOverlay();
299 // uint32 nFrame = 0, nFrameskip = 0;
300 uint32 totalFrames = 0;
302 bool showMessage = true;
303 uint32 showMsgFrames = 120;
304 uint8 transparency = 0xFF;
305 // Pass a message to the "joystick" code to debounce the ESC key...
306 debounceRunKey = true;
309 if (jaguarRomSize == 0x200000)
311 else if (jaguarRomSize == 0x400000)
313 else if (jaguarMainRomCRC32 == 0x687068D5)
315 else if (jaguarMainRomCRC32 == 0x55A0669C)
318 const char * cartTypeName[5] = { "2M Cartridge", "4M Cartridge", "CD BIOS", "CD Dev BIOS", "Homebrew" };
319 uint32 elapsedTicks = SDL_GetTicks(), frameCount = 0, framesPerSecond = 0;
323 // Set up new backbuffer with new pixels and data
326 //WriteLog("Frame #%u...\n", totalFrames);
327 //extern bool doDSPDis;
328 //if (totalFrames == 373)
331 //Problem: Need to do this *only* when the state changes from visible to not...
332 //Also, need to clear out the GUI when not on (when showMessage is active...)
333 if (showGUI || showMessage)
334 sdlemuEnableOverlay();
336 sdlemuDisableOverlay();
338 //Add in a new function for clearing patches of screen (ClearOverlayRect)
340 // Also: Take frame rate into account when calculating fade time...
342 // Some QnD GUI stuff here...
345 FillScreenRectangle(overlayPixels, 8, 1*FONT_HEIGHT, 128, 4*FONT_HEIGHT, 0x00000000);
346 extern uint32 gpu_pc, dsp_pc;
347 DrawString(overlayPixels, 8, 1*FONT_HEIGHT, false, "GPU PC: %08X", gpu_pc);
348 DrawString(overlayPixels, 8, 2*FONT_HEIGHT, false, "DSP PC: %08X", dsp_pc);
349 DrawString(overlayPixels, 8, 4*FONT_HEIGHT, false, "%u FPS", framesPerSecond);
354 DrawString2(overlayPixels, 8, 24*FONT_HEIGHT, 0x007F63FF, transparency, "Running...");
355 DrawString2(overlayPixels, 8, 26*FONT_HEIGHT, 0x001FFF3F, transparency, "%s, run address: %06X", cartTypeName[cartType], jaguarRunAddress);
356 DrawString2(overlayPixels, 8, 27*FONT_HEIGHT, 0x001FFF3F, transparency, "CRC: %08X", jaguarMainRomCRC32);
358 if (showMsgFrames == 0)
362 if (transparency == 0)
365 /*extern bool doGPUDis;
376 if (SDL_GetTicks() - elapsedTicks > 250)
377 elapsedTicks += 250, framesPerSecond = frameCount * 4, frameCount = 0;
380 // Save the background for the GUI...
381 // In this case, we squash the color to monochrome, then force it to blue + green...
382 for(uint32 i=0; i<TOMGetVideoModeWidth() * 256; i++)
384 uint32 pixel = backbuffer[i];
385 uint8 b = (pixel >> 16) & 0xFF, g = (pixel >> 8) & 0xFF, r = pixel & 0xFF;
386 pixel = ((r + g + b) / 3) & 0x00FF;
387 backbuffer[i] = 0xFF000000 | (pixel << 16) | (pixel << 8);
390 sdlemuEnableOverlay();
396 void MainWin::TogglePowerState(void)
398 powerButtonOn = !powerButtonOn;
402 pauseAct->setChecked(false);
403 pauseAct->setDisabled(true);
404 showUntunedTankCircuit = true;
409 showUntunedTankCircuit = (cartridgeLoaded ? false : true);
410 pauseAct->setDisabled(!cartridgeLoaded);
411 //This is crappy!!! !!! FIX !!!
412 //Is this even needed any more? Hmm. Maybe. Dunno.
413 //Seems like it is... But then again, maybe not. Have to test it to see.
414 WriteLog("GUI: Resetting Jaguar...\n");
420 void MainWin::ToggleRunState(void)
427 for(uint32_t x=0; x<videoWidget->rasterWidth; x++)
428 for(uint32_t y=0; y<videoWidget->rasterHeight; y++)
429 videoWidget->buffer[(y * videoWidget->textureWidth) + x] = 0x00000000;
431 // for(uint32_t i=0; i<TOMGetVideoModeWidth() * 256; i++)
432 for(uint32_t i=0; i<videoWidget->textureWidth * 256; i++)
434 uint32_t pixel = backbuffer[i];
435 // uint8_t b = (pixel >> 16) & 0xFF, g = (pixel >> 8) & 0xFF, r = pixel & 0xFF;
436 uint8_t r = (pixel >> 24) & 0xFF, g = (pixel >> 16) & 0xFF, b = (pixel >> 8) & 0xFF;
437 pixel = ((r + g + b) / 3) & 0x00FF;
438 // backbuffer[i] = 0xFF000000 | (pixel << 16) | (pixel << 8);
439 backbuffer[i] = 0x000000FF | (pixel << 16) | (pixel << 8);
442 // memcpy(videoWidget->buffer, backbuffer, videoWidget->rasterHeight * videoWidget->rasterWidth);
443 memcpy(videoWidget->buffer, backbuffer, videoWidget->rasterHeight * videoWidget->textureWidth * sizeof(uint32_t));
446 videoWidget->updateGL();
450 void MainWin::SetZoom100(void)
456 void MainWin::SetZoom200(void)
462 void MainWin::SetZoom300(void)
468 void MainWin::SetNTSC(void)
470 vjs.hardwareTypeNTSC = true;
474 void MainWin::SetPAL(void)
476 vjs.hardwareTypeNTSC = false;
480 void MainWin::ToggleBlur(void)
482 vjs.glFilter = !vjs.glFilter;
485 void MainWin::ShowAboutWin(void)
490 void MainWin::InsertCart(void)
495 void MainWin::LoadSoftware(QString file)
497 running = false; // Prevent bad things(TM) from happening...
498 SET32(jaguarMainRAM, 0, 0x00200000); // Set top of stack...
499 cartridgeLoaded = (JaguarLoadFile(file.toAscii().data()) ? true : false);
502 showUntunedTankCircuit = !cartridgeLoaded;
503 //This is crappy!!! !!! FIX !!!
504 //Is this even needed any more? Hmm. Maybe. Dunno.
505 //Seems like it is... But then again, maybe not. Have to test it to see.
506 WriteLog("GUI: Resetting Jaguar...\n");
510 powerAct->setDisabled(false);
511 powerAct->setChecked(true);
512 powerButtonOn = false;
516 QString newTitle = QString("Virtual Jaguar 2.0.0 - Now playing: %1")
517 .arg(filePickWin->GetSelectedPrettyName());
518 setWindowTitle(newTitle);
521 void MainWin::ResizeMainWindow(void)
523 videoWidget->setFixedSize(zoomLevel * 320, zoomLevel * (vjs.hardwareTypeNTSC ? 240 : 256));
526 for(int i=0; i<2; i++)
530 QApplication::processEvents();
534 void MainWin::ReadSettings(void)
536 QSettings settings("Underground Software", "Virtual Jaguar");
537 QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
538 QSize size = settings.value("size", QSize(400, 400)).toSize();
542 zoomLevel = settings.value("zoom", 1).toInt();
544 vjs.useJoystick = settings.value("useJoystick", false).toBool();
545 vjs.joyport = settings.value("joyport", 0).toInt();
546 vjs.hardwareTypeNTSC = settings.value("hardwareTypeNTSC", true).toBool();
547 vjs.frameSkip = settings.value("frameSkip", 0).toInt();
548 vjs.useJaguarBIOS = settings.value("useJaguarBIOS", false).toBool();
549 vjs.DSPEnabled = settings.value("DSPEnabled", false).toBool();
550 vjs.usePipelinedDSP = settings.value("usePipelinedDSP", false).toBool();
551 vjs.fullscreen = settings.value("fullscreen", false).toBool();
552 vjs.useOpenGL = settings.value("useOpenGL", true).toBool();
553 vjs.glFilter = settings.value("glFilterType", 0).toInt();
554 vjs.renderType = settings.value("renderType", 0).toInt();
555 strcpy(vjs.jagBootPath, settings.value("JagBootROM", "./bios/[BIOS] Atari Jaguar (USA, Europe).zip").toString().toAscii().data());
556 strcpy(vjs.CDBootPath, settings.value("CDBootROM", "./bios/jagcd.rom").toString().toAscii().data());
557 strcpy(vjs.EEPROMPath, settings.value("EEPROMs", "./eeproms").toString().toAscii().data());
558 strcpy(vjs.ROMPath, settings.value("ROMs", "./software").toString().toAscii().data());
559 WriteLog("MainWin: Paths\n");
560 WriteLog(" jagBootPath = \"%s\"\n", vjs.jagBootPath);
561 WriteLog(" CDBootPath = \"%s\"\n", vjs.CDBootPath);
562 WriteLog(" EEPROMPath = \"%s\"\n", vjs.EEPROMPath);
563 WriteLog(" ROMPath = \"%s\"\n", vjs.ROMPath);
566 void MainWin::WriteSettings(void)
568 QSettings settings("Underground Software", "Virtual Jaguar");
569 settings.setValue("pos", pos());
570 settings.setValue("size", size());
572 settings.setValue("zoom", zoomLevel);
574 settings.setValue("useJoystick", vjs.useJoystick);
575 settings.setValue("joyport", vjs.joyport);
576 settings.setValue("hardwareTypeNTSC", vjs.hardwareTypeNTSC);
577 settings.setValue("frameSkip", vjs.frameSkip);
578 settings.setValue("useJaguarBIOS", vjs.useJaguarBIOS);
579 settings.setValue("DSPEnabled", vjs.DSPEnabled);
580 settings.setValue("usePipelinedDSP", vjs.usePipelinedDSP);
581 settings.setValue("fullscreen", vjs.fullscreen);
582 settings.setValue("useOpenGL", vjs.useOpenGL);
583 settings.setValue("glFilterType", vjs.glFilter);
584 settings.setValue("renderType", vjs.renderType);
585 settings.setValue("JagBootROM", vjs.jagBootPath);
586 settings.setValue("CDBootROM", vjs.CDBootPath);
587 settings.setValue("EEPROMs", vjs.EEPROMPath);
588 settings.setValue("ROMs", vjs.ROMPath);
591 // Here's how Byuu does it...
592 // I think I have it working now... :-)
594 void Utility::resizeMainWindow()
596 unsigned region = config().video.context->region;
597 unsigned multiplier = config().video.context->multiplier;
598 unsigned width = 256 * multiplier;
599 unsigned height = (region == 0 ? 224 : 239) * multiplier;
601 if(config().video.context->correctAspectRatio)
605 width = (double)width * config().video.ntscAspectRatio + 0.5; //NTSC adjust
609 width = (double)width * config().video.palAspectRatio + 0.5; //PAL adjust
613 if(config().video.isFullscreen == false)
615 //get effective desktop work area region (ignore Windows taskbar, OS X dock, etc.)
616 QRect deskRect = QApplication::desktop()->availableGeometry(mainWindow);
618 //ensure window size will not be larger than viewable desktop area
619 constrainSize(height, width, deskRect.height()); //- frameHeight);
620 constrainSize(width, height, deskRect.width()); //- frameWidth );
622 mainWindow->canvas->setFixedSize(width, height);
627 for(unsigned i = 0; i < 2; i++)
629 unsigned iWidth = width, iHeight = height;
631 constrainSize(iHeight, iWidth, mainWindow->canvasContainer->size().height());
632 constrainSize(iWidth, iHeight, mainWindow->canvasContainer->size().width());
634 //center canvas onscreen; ensure it is not larger than viewable area
635 mainWindow->canvas->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
636 mainWindow->canvas->setFixedSize(iWidth, iHeight);
637 mainWindow->canvas->setMinimumSize(0, 0);
640 QApplication::processEvents();
644 //workaround for Qt/Xlib bug:
645 //if window resize occurs with cursor over it, Qt shows Qt::Size*DiagCursor;
646 //so force it to show Qt::ArrowCursor, as expected
647 mainWindow->setCursor(Qt::ArrowCursor);
648 mainWindow->canvasContainer->setCursor(Qt::ArrowCursor);
649 mainWindow->canvas->setCursor(Qt::ArrowCursor);
651 //workaround for DirectSound(?) bug:
652 //window resizing sometimes breaks audio sync, this call re-initializes it
656 void Utility::setScale(unsigned scale)
658 config().video.context->multiplier = scale;
660 mainWindow->shrink();
661 mainWindow->syncUi();
664 void QbWindow::shrink()
666 if(config().video.isFullscreen == false)
668 for(unsigned i = 0; i < 2; i++)
672 QApplication::processEvents();