]> Shamusworld >> Repos - virtualjaguar/blob - src/gui/mainwin.cpp
Initial attempt to get Jaguar core to run in the new GUI.
[virtualjaguar] / src / gui / mainwin.cpp
1 //
2 // mainwin.cpp - Qt-based GUI for Virtual Jaguar: Main Application Window
3 // by James L. Hammons
4 // (C) 2009 Underground Software
5 //
6 // JLH = James L. Hammons <jlhamm@acm.org>
7 //
8 // Who  When        What
9 // ---  ----------  -------------------------------------------------------------
10 // JLH  12/23/2009  Created this file
11 // JLH  12/20/2010  Added settings, menus & toolbars
12 //
13
14 // FIXED:
15 //
16 //
17 // STILL TO BE DONE:
18 //
19 //
20
21 // Uncomment this for debugging...
22 //#define DEBUG
23 //#define DEBUGFOO                      // Various tool debugging...
24 //#define DEBUGTP                               // Toolpalette debugging...
25
26 #include "mainwin.h"
27
28 //#include <QtGui>
29 //#include <QtOpenGL>
30 #include "glwidget.h"
31 #include "about.h"
32 #include "settings.h"
33 #include "filepicker.h"
34
35 #include "jaguar.h"
36 #include "video.h"
37 #include "tom.h"
38 #include "log.h"
39 #include "file.h"
40
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
44
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!
48
49 #ifdef USE_BUILT_IN_BIOS
50 #include "jagboot.h"
51 #include "jagcd.h"
52 #endif
53
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
56 // better way.
57
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
60 // QOpenGL?
61
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.
65
66 MainWin::MainWin()
67 {
68         videoWidget = new GLWidget(this);
69         setCentralWidget(videoWidget);
70         setWindowIcon(QIcon(":/res/vj.xpm"));
71         setWindowTitle("Virtual Jaguar v2.0.0");
72
73         ReadSettings();
74         setUnifiedTitleAndToolBarOnMac(true);
75
76         aboutWin = new AboutWindow(this);
77         filePickWin = new FilePickerWindow(this);
78
79     videoWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
80     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
81
82         // Create actions
83
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()));
88
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()));
93
94         zoomActs = new QActionGroup(this);
95
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()));
100
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()));
105
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()));
110
111         tvTypeActs = new QActionGroup(this);
112
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()));
117
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()));
122
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()));
127
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()));
131
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()));
135
136         // Create menus & toolbars
137
138         fileMenu = menuBar()->addMenu(tr("&File"));
139         fileMenu->addAction(filePickAct);
140         fileMenu->addAction(powerAct);
141         fileMenu->addAction(quitAppAct);
142
143         helpMenu = menuBar()->addMenu(tr("&Help"));
144         helpMenu->addAction(aboutAct);
145
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);
157
158         //      Create status bar
159         statusBar()->showMessage(tr("Ready"));
160
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);
169
170         // Do this in case original size isn't correct (mostly for the first-run case)
171         ResizeMainWindow();
172
173         // Set up timer based loop for animation...
174         timer = new QTimer(this);
175         connect(timer, SIGNAL(timeout()), this, SLOT(Timer()));
176         timer->start(20);
177
178 #ifdef VJ_RELEASE_VERSION
179         WriteLog("Virtual Jaguar %s (Last full build was on %s %s)\n", VJ_RELEASE_VERSION, __DATE__, __TIME__);
180 #else
181         WriteLog("Virtual Jaguar SVN %s (Last full build was on %s %s)\n", __DATE__, __DATE__, __TIME__);
182 #endif
183         WriteLog("Initializing jaguar subsystem...\n");
184         JaguarInit();
185
186         // Get the BIOS ROM
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;
192 #else
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");
196 #if 1
197 //This is short-circuiting the file finding thread... ??? WHY ???
198         BIOSLoaded = (JaguarLoadROM(jaguarBootROM, vjs.jagBootPath) == 0x20000 ? true : false);
199 #else
200         BIOSLoaded = false;
201 #endif
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 "));
205 #endif
206
207         SET32(jaguarMainRAM, 0, 0x00200000);                    // Set top of stack...
208
209 //This is crappy!!! !!! FIX !!!
210 //Is this even needed any more? Hmm. Maybe. Dunno.
211 //Seems like it is... But then again, maybe not. Have to test it to see.
212 WriteLog("GUI: Resetting Jaguar...\n");
213         JaguarReset();
214
215 }
216
217 void MainWin::closeEvent(QCloseEvent * event)
218 {
219         WriteSettings();
220         event->accept(); // ignore() if can't close for some reason
221 }
222
223 void MainWin::Open(void)
224 {
225 }
226
227 //
228 // Here's the main emulator loop
229 //
230 void MainWin::Timer(void)
231 {
232         if (!running)
233                 return;
234
235 #if 0
236         // Random hash & trash
237         // We try to simulate an untuned tank circuit here... :-)
238         for(uint32_t x=0; x<videoWidget->rasterWidth; x++)
239         {
240                 for(uint32_t y=0; y<videoWidget->rasterHeight; y++)
241                 {
242                         videoWidget->buffer[(y * videoWidget->textureWidth) + x] = (rand() & 0xFF) << 8 | (rand() & 0xFF) << 16 | (rand() & 0xFF) << 24;// | (rand() & 0xFF);//0x000000FF;
243 //                      buffer[(y * textureWidth) + x] = x*y;
244                 }
245         }
246 #else
247         JaguarExecuteNew();
248         memcpy(videoWidget->buffer, backbuffer, videoWidget->rasterHeight * videoWidget->rasterWidth);
249 //      memcpy(surface->pixels, backbuffer, TOMGetVideoModeWidth() * TOMGetVideoModeHeight() * 4);
250 #endif
251
252         videoWidget->updateGL();
253 }
254
255 #if 0
256 Window * RunEmu(void)
257 {
258 //      extern uint32 * backbuffer;
259         uint32 * overlayPixels = (uint32 *)sdlemuGetOverlayPixels();
260         memset(overlayPixels, 0x00, 640 * 480 * 4);                     // Clear out overlay...
261
262 //This is crappy... !!! FIX !!!
263 //      extern bool finished, showGUI;
264
265         sdlemuDisableOverlay();
266
267 //      uint32 nFrame = 0, nFrameskip = 0;
268         uint32 totalFrames = 0;
269         finished = false;
270         bool showMessage = true;
271         uint32 showMsgFrames = 120;
272         uint8 transparency = 0xFF;
273         // Pass a message to the "joystick" code to debounce the ESC key...
274         debounceRunKey = true;
275
276         uint32 cartType = 4;
277         if (jaguarRomSize == 0x200000)
278                 cartType = 0;
279         else if (jaguarRomSize == 0x400000)
280                 cartType = 1;
281         else if (jaguarMainRomCRC32 == 0x687068D5)
282                 cartType = 2;
283         else if (jaguarMainRomCRC32 == 0x55A0669C)
284                 cartType = 3;
285
286         const char * cartTypeName[5] = { "2M Cartridge", "4M Cartridge", "CD BIOS", "CD Dev BIOS", "Homebrew" };
287         uint32 elapsedTicks = SDL_GetTicks(), frameCount = 0, framesPerSecond = 0;
288
289         while (!finished)
290         {
291                 // Set up new backbuffer with new pixels and data
292                 JaguarExecuteNew();
293                 totalFrames++;
294 //WriteLog("Frame #%u...\n", totalFrames);
295 //extern bool doDSPDis;
296 //if (totalFrames == 373)
297 //      doDSPDis = true;
298
299 //Problem: Need to do this *only* when the state changes from visible to not...
300 //Also, need to clear out the GUI when not on (when showMessage is active...)
301 if (showGUI || showMessage)
302         sdlemuEnableOverlay();
303 else
304         sdlemuDisableOverlay();
305
306 //Add in a new function for clearing patches of screen (ClearOverlayRect)
307
308 // Also: Take frame rate into account when calculating fade time...
309
310                 // Some QnD GUI stuff here...
311                 if (showGUI)
312                 {
313                         FillScreenRectangle(overlayPixels, 8, 1*FONT_HEIGHT, 128, 4*FONT_HEIGHT, 0x00000000);
314                         extern uint32 gpu_pc, dsp_pc;
315                         DrawString(overlayPixels, 8, 1*FONT_HEIGHT, false, "GPU PC: %08X", gpu_pc);
316                         DrawString(overlayPixels, 8, 2*FONT_HEIGHT, false, "DSP PC: %08X", dsp_pc);
317                         DrawString(overlayPixels, 8, 4*FONT_HEIGHT, false, "%u FPS", framesPerSecond);
318                 }
319
320                 if (showMessage)
321                 {
322                         DrawString2(overlayPixels, 8, 24*FONT_HEIGHT, 0x007F63FF, transparency, "Running...");
323                         DrawString2(overlayPixels, 8, 26*FONT_HEIGHT, 0x001FFF3F, transparency, "%s, run address: %06X", cartTypeName[cartType], jaguarRunAddress);
324                         DrawString2(overlayPixels, 8, 27*FONT_HEIGHT, 0x001FFF3F, transparency, "CRC: %08X", jaguarMainRomCRC32);
325
326                         if (showMsgFrames == 0)
327                         {
328                                 transparency--;
329
330                                 if (transparency == 0)
331 {
332                                         showMessage = false;
333 /*extern bool doGPUDis;
334 doGPUDis = true;//*/
335 }
336
337                         }
338                         else
339                                 showMsgFrames--;
340                 }
341
342                 frameCount++;
343
344                 if (SDL_GetTicks() - elapsedTicks > 250)
345                         elapsedTicks += 250, framesPerSecond = frameCount * 4, frameCount = 0;
346         }
347
348         // Save the background for the GUI...
349         // In this case, we squash the color to monochrome, then force it to blue + green...
350         for(uint32 i=0; i<TOMGetVideoModeWidth() * 256; i++)
351         {
352                 uint32 pixel = backbuffer[i];
353                 uint8 b = (pixel >> 16) & 0xFF, g = (pixel >> 8) & 0xFF, r = pixel & 0xFF;
354                 pixel = ((r + g + b) / 3) & 0x00FF;
355                 backbuffer[i] = 0xFF000000 | (pixel << 16) | (pixel << 8);
356         }
357
358         sdlemuEnableOverlay();
359
360         return NULL;
361 }
362 #endif
363
364 void MainWin::ToggleRunState(void)
365 {
366         running = !running;
367
368         if (!running)
369         {
370 #if 0
371                 for(uint32_t x=0; x<videoWidget->rasterWidth; x++)
372                         for(uint32_t y=0; y<videoWidget->rasterHeight; y++)
373                                 videoWidget->buffer[(y * videoWidget->textureWidth) + x] = 0x00000000;
374 #else
375                 for(uint32_t i=0; i<TOMGetVideoModeWidth() * 256; i++)
376                 {
377                         uint32_t pixel = backbuffer[i];
378                         uint8_t b = (pixel >> 16) & 0xFF, g = (pixel >> 8) & 0xFF, r = pixel & 0xFF;
379                         pixel = ((r + g + b) / 3) & 0x00FF;
380                         backbuffer[i] = 0xFF000000 | (pixel << 16) | (pixel << 8);
381                 }
382
383                 memcpy(videoWidget->buffer, backbuffer, videoWidget->rasterHeight * videoWidget->rasterWidth);
384 #endif
385
386                 videoWidget->updateGL();
387         }
388
389 }
390
391 void MainWin::SetZoom100(void)
392 {
393         zoomLevel = 1;
394         ResizeMainWindow();
395 }
396
397 void MainWin::SetZoom200(void)
398 {
399         zoomLevel = 2;
400         ResizeMainWindow();
401 }
402
403 void MainWin::SetZoom300(void)
404 {
405         zoomLevel = 3;
406         ResizeMainWindow();
407 }
408
409 void MainWin::SetNTSC(void)
410 {
411         vjs.hardwareTypeNTSC = true;
412         ResizeMainWindow();
413 }
414
415 void MainWin::SetPAL(void)
416 {
417         vjs.hardwareTypeNTSC = false;
418         ResizeMainWindow();
419 }
420
421 void MainWin::ToggleBlur(void)
422 {
423         vjs.glFilter = !vjs.glFilter;
424 }
425
426 void MainWin::ShowAboutWin(void)
427 {
428         aboutWin->show();
429 }
430
431 void MainWin::InsertCart(void)
432 {
433         filePickWin->show();
434 }
435
436 void MainWin::ResizeMainWindow(void)
437 {
438         videoWidget->setFixedSize(zoomLevel * 320, zoomLevel * (vjs.hardwareTypeNTSC ? 240 : 256));
439         show();
440
441         for(int i=0; i<2; i++)
442         {
443                 resize(0, 0);
444                 usleep(2000);
445                 QApplication::processEvents();
446         }
447 }
448
449 void MainWin::ReadSettings(void)
450 {
451         QSettings settings("Underground Software", "Virtual Jaguar");
452         QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
453         QSize size = settings.value("size", QSize(400, 400)).toSize();
454         resize(size);
455         move(pos);
456
457         zoomLevel = settings.value("zoom", 1).toInt();
458
459         vjs.useJoystick      = settings.value("useJoystick", false).toBool();
460         vjs.joyport          = settings.value("joyport", 0).toInt();
461         vjs.hardwareTypeNTSC = settings.value("hardwareTypeNTSC", true).toBool();
462         vjs.frameSkip        = settings.value("frameSkip", 0).toInt();
463         vjs.useJaguarBIOS    = settings.value("useJaguarBIOS", false).toBool();
464         vjs.DSPEnabled       = settings.value("DSPEnabled", false).toBool();
465         vjs.usePipelinedDSP  = settings.value("usePipelinedDSP", false).toBool();
466         vjs.fullscreen       = settings.value("fullscreen", false).toBool();
467         vjs.useOpenGL        = settings.value("useOpenGL", true).toBool();
468         vjs.glFilter         = settings.value("glFilterType", 0).toInt();
469         vjs.renderType       = settings.value("renderType", 0).toInt();
470         strcpy(vjs.jagBootPath, settings.value("JagBootROM", "./bios/jagboot.rom").toString().toAscii().data());
471         strcpy(vjs.CDBootPath, settings.value("CDBootROM", "./bios/jagcd.rom").toString().toAscii().data());
472         strcpy(vjs.EEPROMPath, settings.value("EEPROMs", "./eeproms").toString().toAscii().data());
473         strcpy(vjs.ROMPath, settings.value("ROMs", "./software").toString().toAscii().data());
474 }
475
476 void MainWin::WriteSettings(void)
477 {
478         QSettings settings("Underground Software", "Virtual Jaguar");
479         settings.setValue("pos", pos());
480         settings.setValue("size", size());
481
482         settings.setValue("zoom", zoomLevel);
483
484         settings.setValue("useJoystick", vjs.useJoystick);
485         settings.setValue("joyport", vjs.joyport);
486         settings.setValue("hardwareTypeNTSC", vjs.hardwareTypeNTSC);
487         settings.setValue("frameSkip", vjs.frameSkip);
488         settings.setValue("useJaguarBIOS", vjs.useJaguarBIOS);
489         settings.setValue("DSPEnabled", vjs.DSPEnabled);
490         settings.setValue("usePipelinedDSP", vjs.usePipelinedDSP);
491         settings.setValue("fullscreen", vjs.fullscreen);
492         settings.setValue("useOpenGL", vjs.useOpenGL);
493         settings.setValue("glFilterType", vjs.glFilter);
494         settings.setValue("renderType", vjs.renderType);
495         settings.setValue("JagBootROM", vjs.jagBootPath);
496         settings.setValue("CDBootROM", vjs.CDBootPath);
497         settings.setValue("EEPROMs", vjs.EEPROMPath);
498         settings.setValue("ROMs", vjs.ROMPath);
499 }
500
501 // Here's how Byuu does it...
502 // I think I have it working now... :-)
503 #if 0
504 void Utility::resizeMainWindow()
505 {
506   unsigned region = config().video.context->region;
507   unsigned multiplier = config().video.context->multiplier;
508   unsigned width = 256 * multiplier;
509   unsigned height = (region == 0 ? 224 : 239) * multiplier;
510
511   if(config().video.context->correctAspectRatio)
512   {
513     if(region == 0)
514         {
515       width = (double)width * config().video.ntscAspectRatio + 0.5;  //NTSC adjust
516     }
517         else
518         {
519       width = (double)width * config().video.palAspectRatio  + 0.5;  //PAL adjust
520     }
521   }
522
523   if(config().video.isFullscreen == false)
524   {
525     //get effective desktop work area region (ignore Windows taskbar, OS X dock, etc.)
526     QRect deskRect = QApplication::desktop()->availableGeometry(mainWindow);
527
528     //ensure window size will not be larger than viewable desktop area
529     constrainSize(height, width, deskRect.height()); //- frameHeight);
530     constrainSize(width, height, deskRect.width());  //- frameWidth );
531
532     mainWindow->canvas->setFixedSize(width, height);
533     mainWindow->show();
534   }
535   else
536   {
537     for(unsigned i = 0; i < 2; i++)
538         {
539       unsigned iWidth = width, iHeight = height;
540
541       constrainSize(iHeight, iWidth, mainWindow->canvasContainer->size().height());
542       constrainSize(iWidth, iHeight, mainWindow->canvasContainer->size().width());
543
544       //center canvas onscreen; ensure it is not larger than viewable area
545       mainWindow->canvas->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
546       mainWindow->canvas->setFixedSize(iWidth, iHeight);
547       mainWindow->canvas->setMinimumSize(0, 0);
548
549       usleep(2000);
550       QApplication::processEvents();
551     }
552   }
553
554   //workaround for Qt/Xlib bug:
555   //if window resize occurs with cursor over it, Qt shows Qt::Size*DiagCursor;
556   //so force it to show Qt::ArrowCursor, as expected
557   mainWindow->setCursor(Qt::ArrowCursor);
558   mainWindow->canvasContainer->setCursor(Qt::ArrowCursor);
559   mainWindow->canvas->setCursor(Qt::ArrowCursor);
560
561   //workaround for DirectSound(?) bug:
562   //window resizing sometimes breaks audio sync, this call re-initializes it
563   updateAvSync();
564 }
565
566 void Utility::setScale(unsigned scale)
567 {
568   config().video.context->multiplier = scale;
569   resizeMainWindow();
570   mainWindow->shrink();
571   mainWindow->syncUi();
572 }
573
574 void QbWindow::shrink()
575 {
576   if(config().video.isFullscreen == false)
577   {
578     for(unsigned i = 0; i < 2; i++)
579         {
580       resize(0, 0);
581       usleep(2000);
582       QApplication::processEvents();
583     }
584   }
585 }
586 #endif