]> Shamusworld >> Repos - virtualjaguar/blob - src/gui/mainwin.cpp
30c3b2a37c1a18b44ad7604eebfdf2576999b48e
[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 "SDL.h"
29 //#include <QtGui>
30 //#include <QtOpenGL>
31 #include "glwidget.h"
32 #include "about.h"
33 #include "settings.h"
34 #include "filepicker.h"
35
36 #include "jaguar.h"
37 #include "video.h"
38 #include "tom.h"
39 #include "log.h"
40 #include "file.h"
41 #include "joystick.h"
42
43 // Uncomment this to use built-in BIOS/CD-ROM BIOS
44 // You'll need a copy of jagboot.h & jagcd.h for this to work...!
45 //#define USE_BUILT_IN_BIOS
46
47 // Uncomment this for an official Virtual Jaguar release
48 //#define VJ_RELEASE_VERSION "2.0.0"
49 #warning !!! FIX !!! Figure out how to use this in GUI.CPP as well!
50
51 #ifdef USE_BUILT_IN_BIOS
52 #include "jagboot.h"
53 #include "jagcd.h"
54 #endif
55
56 // The way BSNES controls things is by setting a timer with a zero
57 // timeout, sleeping if not emulating anything. Seems there has to be a
58 // better way.
59
60 // It has a novel approach to plugging-in/using different video/audio/input
61 // methods, can we do something similar or should we just use the built-in
62 // QOpenGL?
63
64 // We're going to try to use the built-in OpenGL support and see how it goes.
65 // We'll make the VJ core modular so that it doesn't matter what GUI is in
66 // use, we can drop it in anywhere and use it as-is.
67
68 MainWin::MainWin(): running(false), powerButtonOn(false), showUntunedTankCircuit(true),
69         cartridgeLoaded(false)
70 {
71         videoWidget = new GLWidget(this);
72         setCentralWidget(videoWidget);
73         setWindowIcon(QIcon(":/res/vj-icon.png"));
74         setWindowTitle("Virtual Jaguar v2.0.0");
75
76         ReadSettings();
77         setUnifiedTitleAndToolBarOnMac(true);
78
79 #warning "!!! Save/Restore window location for FilePickerWindow !!!"
80         aboutWin = new AboutWindow(this);
81         filePickWin = new FilePickerWindow(this);
82
83     videoWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
84     setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
85
86         // Create actions
87
88         quitAppAct = new QAction(tr("E&xit"), this);
89         quitAppAct->setShortcuts(QKeySequence::Quit);
90         quitAppAct->setStatusTip(tr("Quit Virtual Jaguar"));
91         connect(quitAppAct, SIGNAL(triggered()), this, SLOT(close()));
92
93         powerAct = new QAction(QIcon(":/res/power.png"), tr("&Power"), this);
94         powerAct->setStatusTip(tr("Powers Jaguar on/off"));
95         powerAct->setCheckable(true);
96         powerAct->setChecked(false);
97         powerAct->setDisabled(true);
98         connect(powerAct, SIGNAL(triggered()), this, SLOT(TogglePowerState()));
99
100         pauseAct = new QAction(QIcon(":/res/pause.png"), tr("Pause"), this);
101         pauseAct->setStatusTip(tr("Toggles the running state"));
102         pauseAct->setCheckable(true);
103         pauseAct->setDisabled(true);
104         connect(pauseAct, SIGNAL(triggered()), this, SLOT(ToggleRunState()));
105
106         zoomActs = new QActionGroup(this);
107
108         x1Act = new QAction(QIcon(":/res/zoom100.png"), tr("Zoom 100%"), zoomActs);
109         x1Act->setStatusTip(tr("Set window zoom to 100%"));
110         x1Act->setCheckable(true);
111         connect(x1Act, SIGNAL(triggered()), this, SLOT(SetZoom100()));
112
113         x2Act = new QAction(QIcon(":/res/zoom200.png"), tr("Zoom 200%"), zoomActs);
114         x2Act->setStatusTip(tr("Set window zoom to 200%"));
115         x2Act->setCheckable(true);
116         connect(x2Act, SIGNAL(triggered()), this, SLOT(SetZoom200()));
117
118         x3Act = new QAction(QIcon(":/res/zoom300.png"), tr("Zoom 300%"), zoomActs);
119         x3Act->setStatusTip(tr("Set window zoom to 300%"));
120         x3Act->setCheckable(true);
121         connect(x3Act, SIGNAL(triggered()), this, SLOT(SetZoom300()));
122
123         tvTypeActs = new QActionGroup(this);
124
125         ntscAct = new QAction(QIcon(":/res/generic.png"), tr("NTSC"), tvTypeActs);
126         ntscAct->setStatusTip(tr("Sets Jaguar to NTSC mode"));
127         ntscAct->setCheckable(true);
128         connect(ntscAct, SIGNAL(triggered()), this, SLOT(SetNTSC()));
129
130         palAct = new QAction(QIcon(":/res/generic.png"), tr("PAL"), tvTypeActs);
131         palAct->setStatusTip(tr("Sets Jaguar to PAL mode"));
132         palAct->setCheckable(true);
133         connect(palAct, SIGNAL(triggered()), this, SLOT(SetPAL()));
134
135         blurAct = new QAction(QIcon(":/res/generic.png"), tr("Blur"), this);
136         blurAct->setStatusTip(tr("Sets OpenGL rendering to GL_NEAREST"));
137         blurAct->setCheckable(true);
138         connect(blurAct, SIGNAL(triggered()), this, SLOT(ToggleBlur()));
139
140         aboutAct = new QAction(QIcon(":/res/generic.png"), tr("&About..."), this);
141         aboutAct->setStatusTip(tr("Blatant self-promotion"));
142         connect(aboutAct, SIGNAL(triggered()), this, SLOT(ShowAboutWin()));
143
144 #warning "!!! Set up a decent keyboard shortcut for Insert Cartridge !!!"
145         filePickAct = new QAction(QIcon(":/res/software.png"), tr("&Insert Cartridge..."), this);
146         filePickAct->setStatusTip(tr("Insert a cartridge into Virtual Jaguar"));
147         connect(filePickAct, SIGNAL(triggered()), this, SLOT(InsertCart()));
148
149         // Misc. connections...
150         connect(filePickWin, SIGNAL(RequestLoad(QString)), this, SLOT(LoadSoftware(QString)));
151
152         // Create menus & toolbars
153
154         fileMenu = menuBar()->addMenu(tr("&File"));
155         fileMenu->addAction(filePickAct);
156         fileMenu->addAction(powerAct);
157         fileMenu->addAction(pauseAct);
158         fileMenu->addAction(quitAppAct);
159
160         helpMenu = menuBar()->addMenu(tr("&Help"));
161         helpMenu->addAction(aboutAct);
162
163         toolbar = addToolBar(tr("Stuff"));
164         toolbar->addAction(powerAct);
165         toolbar->addAction(pauseAct);
166         toolbar->addAction(filePickAct);
167         toolbar->addSeparator();
168         toolbar->addAction(x1Act);
169         toolbar->addAction(x2Act);
170         toolbar->addAction(x3Act);
171         toolbar->addSeparator();
172         toolbar->addAction(ntscAct);
173         toolbar->addAction(palAct);
174         toolbar->addSeparator();
175         toolbar->addAction(blurAct);
176
177         //      Create status bar
178         statusBar()->showMessage(tr("Ready"));
179
180         // Set toolbar buttons/menus based on settings read in (sync the UI)...
181         blurAct->setChecked(vjs.glFilter);
182         x1Act->setChecked(zoomLevel == 1);
183         x2Act->setChecked(zoomLevel == 2);
184         x3Act->setChecked(zoomLevel == 3);
185         running = powerAct->isChecked();
186         ntscAct->setChecked(vjs.hardwareTypeNTSC);
187         palAct->setChecked(!vjs.hardwareTypeNTSC);
188
189         // Do this in case original size isn't correct (mostly for the first-run case)
190         ResizeMainWindow();
191
192         // Set up timer based loop for animation...
193         timer = new QTimer(this);
194         connect(timer, SIGNAL(timeout()), this, SLOT(Timer()));
195         timer->start(20);
196
197         // NOTE: Keyboards/joysticks will *not* work until SDL is brought back in, or
198         //       the key handling is improved in Qt...
199         // Wait a minute... it seems they already are... So why no keyboard love?
200
201 #ifdef VJ_RELEASE_VERSION
202         WriteLog("Virtual Jaguar %s (Last full build was on %s %s)\n", VJ_RELEASE_VERSION, __DATE__, __TIME__);
203 #else
204         WriteLog("Virtual Jaguar SVN %s (Last full build was on %s %s)\n", __DATE__, __DATE__, __TIME__);
205 #endif
206         WriteLog("Initializing jaguar subsystem...\n");
207         JaguarInit();
208
209         // Get the BIOS ROM
210 #ifdef USE_BUILT_IN_BIOS
211         WriteLog("VJ: Using built in BIOS/CD BIOS...\n");
212         memcpy(jaguarBootROM, jagBootROM, 0x20000);
213         memcpy(jaguarCDBootROM, jagCDROM, 0x40000);
214         BIOSLoaded = CDBIOSLoaded = true;
215 #else
216 // What would be nice here would be a way to check if the BIOS was loaded so that we
217 // could disable the pushbutton on the Misc Options menu... !!! FIX !!! [DONE here, but needs to be fixed in GUI as well!]
218 WriteLog("About to attempt to load BIOSes...\n");
219 #if 1
220 //This is short-circuiting the file finding thread... ??? WHY ???
221         BIOSLoaded = (JaguarLoadROM(jaguarBootROM, vjs.jagBootPath) == 0x20000 ? true : false);
222 #else
223         BIOSLoaded = false;
224 #endif
225         WriteLog("VJ: BIOS is %savailable...\n", (BIOSLoaded ? "" : "not "));
226         CDBIOSLoaded = (JaguarLoadROM(jaguarCDBootROM, vjs.CDBootPath) == 0x40000 ? true : false);
227         WriteLog("VJ: CD BIOS is %savailable...\n", (CDBIOSLoaded ? "" : "not "));
228 #endif
229
230         SET32(jaguarMainRAM, 0, 0x00200000);                    // Set top of stack...
231
232 //Let's try this...
233 //      JaguarLoadFile("./software/Rayman (World).j64");
234 //      JaguarLoadFile("./software/I-War (World).j64");
235 //      JaguarLoadFile("./software/Alien vs Predator (World).j64");
236 //no    JaguarLoadFile("./software/battlesphere.bin");
237 //      JaguarLoadFile("./software/Battle Sphere Gold (World).j64");
238 //      JaguarLoadFile("./software/Rayman (USA, Europe).zip");
239 //This is crappy!!! !!! FIX !!!
240 //Is this even needed any more? Hmm. Maybe. Dunno.
241 //Seems like it is... But then again, maybe not. Have to test it to see.
242 //WriteLog("GUI: Resetting Jaguar...\n");
243 //      JaguarReset();
244 }
245
246 void MainWin::closeEvent(QCloseEvent * event)
247 {
248         WriteSettings();
249         event->accept(); // ignore() if can't close for some reason
250 }
251
252 void MainWin::keyPressEvent(QKeyEvent * e)
253 {
254         // NOTE: Keys are hardwired...
255 /*
256         vjs.p1KeyBindings[0] = settings.value("p1k_up", SDLK_UP).toInt();
257         vjs.p1KeyBindings[1] = settings.value("p1k_down", SDLK_DOWN).toInt();
258         vjs.p1KeyBindings[2] = settings.value("p1k_left", SDLK_LEFT).toInt();
259         vjs.p1KeyBindings[3] = settings.value("p1k_right", SDLK_RIGHT).toInt();
260         vjs.p1KeyBindings[4] = settings.value("p1k_c", SDLK_z).toInt();
261         vjs.p1KeyBindings[5] = settings.value("p1k_b", SDLK_x).toInt();
262         vjs.p1KeyBindings[6] = settings.value("p1k_a", SDLK_c).toInt();
263         vjs.p1KeyBindings[7] = settings.value("p1k_option", SDLK_QUOTE).toInt();
264         vjs.p1KeyBindings[8] = settings.value("p1k_pause", SDLK_RETURN).toInt();
265         vjs.p1KeyBindings[9] = settings.value("p1k_0", SDLK_KP0).toInt();
266         vjs.p1KeyBindings[10] = settings.value("p1k_1", SDLK_KP1).toInt();
267         vjs.p1KeyBindings[11] = settings.value("p1k_2", SDLK_KP2).toInt();
268         vjs.p1KeyBindings[12] = settings.value("p1k_3", SDLK_KP3).toInt();
269         vjs.p1KeyBindings[13] = settings.value("p1k_4", SDLK_KP4).toInt();
270         vjs.p1KeyBindings[14] = settings.value("p1k_5", SDLK_KP5).toInt();
271         vjs.p1KeyBindings[15] = settings.value("p1k_6", SDLK_KP6).toInt();
272         vjs.p1KeyBindings[16] = settings.value("p1k_7", SDLK_KP7).toInt();
273         vjs.p1KeyBindings[17] = settings.value("p1k_8", SDLK_KP8).toInt();
274         vjs.p1KeyBindings[18] = settings.value("p1k_9", SDLK_KP9).toInt();
275         vjs.p1KeyBindings[19] = settings.value("p1k_pound", SDLK_KP_DIVIDE).toInt();
276         vjs.p1KeyBindings[20] = settings.value("p1k_star", SDLK_KP_MULTIPLY).toInt();
277 */
278         if (e->key() == Qt::Key_Up)
279                 keyBuffer[0] = true;
280         else if (e->key() == Qt::Key_Down)
281                 keyBuffer[1] = true;
282         else if (e->key() == Qt::Key_Left)
283                 keyBuffer[2] = true;
284         else if (e->key() == Qt::Key_Right)
285                 keyBuffer[3] = true;
286         else if (e->key() == Qt::Key_Z)
287                 keyBuffer[4] = true;
288         else if (e->key() == Qt::Key_X)
289                 keyBuffer[5] = true;
290         else if (e->key() == Qt::Key_C)
291                 keyBuffer[6] = true;
292 }
293
294 void MainWin::keyReleaseEvent(QKeyEvent * e)
295 {
296         if (e->key() == Qt::Key_Up)
297                 keyBuffer[0] = false;
298         else if (e->key() == Qt::Key_Down)
299                 keyBuffer[1] = false;
300         else if (e->key() == Qt::Key_Left)
301                 keyBuffer[2] = false;
302         else if (e->key() == Qt::Key_Right)
303                 keyBuffer[3] = false;
304         else if (e->key() == Qt::Key_Z)
305                 keyBuffer[4] = false;
306         else if (e->key() == Qt::Key_X)
307                 keyBuffer[5] = false;
308         else if (e->key() == Qt::Key_C)
309                 keyBuffer[6] = false;
310 }
311
312 void MainWin::Open(void)
313 {
314 }
315
316 //
317 // Here's the main emulator loop
318 //
319 void MainWin::Timer(void)
320 {
321         if (!running)
322                 return;
323
324         if (showUntunedTankCircuit)
325         {
326                 // Random hash & trash
327                 // We try to simulate an untuned tank circuit here... :-)
328                 for(uint32_t x=0; x<videoWidget->rasterWidth; x++)
329                 {
330                         for(uint32_t y=0; y<videoWidget->rasterHeight; y++)
331                         {
332                                 videoWidget->buffer[(y * videoWidget->textureWidth) + x] = (rand() & 0xFF) << 8 | (rand() & 0xFF) << 16 | (rand() & 0xFF) << 24;// | (rand() & 0xFF);//0x000000FF;
333         //                      buffer[(y * textureWidth) + x] = x*y;
334                         }
335                 }
336         }
337         else
338         {
339                 // Otherwise, run the Jaguar simulation
340                 JaguarExecuteNew();
341 //              memcpy(videoWidget->buffer, backbuffer, videoWidget->rasterHeight * videoWidget->rasterWidth);
342                 memcpy(videoWidget->buffer, backbuffer, videoWidget->rasterHeight * videoWidget->textureWidth * sizeof(uint32_t));
343 //              memcpy(surface->pixels, backbuffer, TOMGetVideoModeWidth() * TOMGetVideoModeHeight() * 4);
344         }
345
346         videoWidget->updateGL();
347 }
348
349 #if 0
350 Window * RunEmu(void)
351 {
352 //      extern uint32 * backbuffer;
353         uint32 * overlayPixels = (uint32 *)sdlemuGetOverlayPixels();
354         memset(overlayPixels, 0x00, 640 * 480 * 4);                     // Clear out overlay...
355
356 //This is crappy... !!! FIX !!!
357 //      extern bool finished, showGUI;
358
359         sdlemuDisableOverlay();
360
361 //      uint32 nFrame = 0, nFrameskip = 0;
362         uint32 totalFrames = 0;
363         finished = false;
364         bool showMessage = true;
365         uint32 showMsgFrames = 120;
366         uint8 transparency = 0xFF;
367         // Pass a message to the "joystick" code to debounce the ESC key...
368         debounceRunKey = true;
369
370         uint32 cartType = 4;
371         if (jaguarRomSize == 0x200000)
372                 cartType = 0;
373         else if (jaguarRomSize == 0x400000)
374                 cartType = 1;
375         else if (jaguarMainRomCRC32 == 0x687068D5)
376                 cartType = 2;
377         else if (jaguarMainRomCRC32 == 0x55A0669C)
378                 cartType = 3;
379
380         const char * cartTypeName[5] = { "2M Cartridge", "4M Cartridge", "CD BIOS", "CD Dev BIOS", "Homebrew" };
381         uint32 elapsedTicks = SDL_GetTicks(), frameCount = 0, framesPerSecond = 0;
382
383         while (!finished)
384         {
385                 // Set up new backbuffer with new pixels and data
386                 JaguarExecuteNew();
387                 totalFrames++;
388 //WriteLog("Frame #%u...\n", totalFrames);
389 //extern bool doDSPDis;
390 //if (totalFrames == 373)
391 //      doDSPDis = true;
392
393 //Problem: Need to do this *only* when the state changes from visible to not...
394 //Also, need to clear out the GUI when not on (when showMessage is active...)
395 if (showGUI || showMessage)
396         sdlemuEnableOverlay();
397 else
398         sdlemuDisableOverlay();
399
400 //Add in a new function for clearing patches of screen (ClearOverlayRect)
401
402 // Also: Take frame rate into account when calculating fade time...
403
404                 // Some QnD GUI stuff here...
405                 if (showGUI)
406                 {
407                         FillScreenRectangle(overlayPixels, 8, 1*FONT_HEIGHT, 128, 4*FONT_HEIGHT, 0x00000000);
408                         extern uint32 gpu_pc, dsp_pc;
409                         DrawString(overlayPixels, 8, 1*FONT_HEIGHT, false, "GPU PC: %08X", gpu_pc);
410                         DrawString(overlayPixels, 8, 2*FONT_HEIGHT, false, "DSP PC: %08X", dsp_pc);
411                         DrawString(overlayPixels, 8, 4*FONT_HEIGHT, false, "%u FPS", framesPerSecond);
412                 }
413
414                 if (showMessage)
415                 {
416                         DrawString2(overlayPixels, 8, 24*FONT_HEIGHT, 0x007F63FF, transparency, "Running...");
417                         DrawString2(overlayPixels, 8, 26*FONT_HEIGHT, 0x001FFF3F, transparency, "%s, run address: %06X", cartTypeName[cartType], jaguarRunAddress);
418                         DrawString2(overlayPixels, 8, 27*FONT_HEIGHT, 0x001FFF3F, transparency, "CRC: %08X", jaguarMainRomCRC32);
419
420                         if (showMsgFrames == 0)
421                         {
422                                 transparency--;
423
424                                 if (transparency == 0)
425 {
426                                         showMessage = false;
427 /*extern bool doGPUDis;
428 doGPUDis = true;//*/
429 }
430
431                         }
432                         else
433                                 showMsgFrames--;
434                 }
435
436                 frameCount++;
437
438                 if (SDL_GetTicks() - elapsedTicks > 250)
439                         elapsedTicks += 250, framesPerSecond = frameCount * 4, frameCount = 0;
440         }
441
442         // Save the background for the GUI...
443         // In this case, we squash the color to monochrome, then force it to blue + green...
444         for(uint32 i=0; i<TOMGetVideoModeWidth() * 256; i++)
445         {
446                 uint32 pixel = backbuffer[i];
447                 uint8 b = (pixel >> 16) & 0xFF, g = (pixel >> 8) & 0xFF, r = pixel & 0xFF;
448                 pixel = ((r + g + b) / 3) & 0x00FF;
449                 backbuffer[i] = 0xFF000000 | (pixel << 16) | (pixel << 8);
450         }
451
452         sdlemuEnableOverlay();
453
454         return NULL;
455 }
456 #endif
457
458 void MainWin::TogglePowerState(void)
459 {
460         powerButtonOn = !powerButtonOn;
461
462         if (!powerButtonOn)
463         {
464                 pauseAct->setChecked(false);
465                 pauseAct->setDisabled(true);
466                 showUntunedTankCircuit = true;
467                 running = true;
468         }
469         else
470         {
471                 showUntunedTankCircuit = (cartridgeLoaded ? false : true);
472                 pauseAct->setDisabled(!cartridgeLoaded);
473 //This is crappy!!! !!! FIX !!!
474 //Is this even needed any more? Hmm. Maybe. Dunno.
475 //Seems like it is... But then again, maybe not. Have to test it to see.
476                 WriteLog("GUI: Resetting Jaguar...\n");
477                 JaguarReset();
478                 running = true;
479         }
480 }
481
482 void MainWin::ToggleRunState(void)
483 {
484         running = !running;
485
486         if (!running)
487         {
488 #if 0
489                 for(uint32_t x=0; x<videoWidget->rasterWidth; x++)
490                         for(uint32_t y=0; y<videoWidget->rasterHeight; y++)
491                                 videoWidget->buffer[(y * videoWidget->textureWidth) + x] = 0x00000000;
492 #else
493 //              for(uint32_t i=0; i<TOMGetVideoModeWidth() * 256; i++)
494                 for(uint32_t i=0; i<videoWidget->textureWidth * 256; i++)
495                 {
496                         uint32_t pixel = backbuffer[i];
497 //                      uint8_t b = (pixel >> 16) & 0xFF, g = (pixel >> 8) & 0xFF, r = pixel & 0xFF;
498                         uint8_t r = (pixel >> 24) & 0xFF, g = (pixel >> 16) & 0xFF, b = (pixel >> 8) & 0xFF;
499                         pixel = ((r + g + b) / 3) & 0x00FF;
500 //                      backbuffer[i] = 0xFF000000 | (pixel << 16) | (pixel << 8);
501                         backbuffer[i] = 0x000000FF | (pixel << 16) | (pixel << 8);
502                 }
503
504 //              memcpy(videoWidget->buffer, backbuffer, videoWidget->rasterHeight * videoWidget->rasterWidth);
505                 memcpy(videoWidget->buffer, backbuffer, videoWidget->rasterHeight * videoWidget->textureWidth * sizeof(uint32_t));
506 #endif
507
508                 videoWidget->updateGL();
509         }
510 }
511
512 void MainWin::SetZoom100(void)
513 {
514         zoomLevel = 1;
515         ResizeMainWindow();
516 }
517
518 void MainWin::SetZoom200(void)
519 {
520         zoomLevel = 2;
521         ResizeMainWindow();
522 }
523
524 void MainWin::SetZoom300(void)
525 {
526         zoomLevel = 3;
527         ResizeMainWindow();
528 }
529
530 void MainWin::SetNTSC(void)
531 {
532         vjs.hardwareTypeNTSC = true;
533         ResizeMainWindow();
534 }
535
536 void MainWin::SetPAL(void)
537 {
538         vjs.hardwareTypeNTSC = false;
539         ResizeMainWindow();
540 }
541
542 void MainWin::ToggleBlur(void)
543 {
544         vjs.glFilter = !vjs.glFilter;
545 }
546
547 void MainWin::ShowAboutWin(void)
548 {
549         aboutWin->show();
550 }
551
552 void MainWin::InsertCart(void)
553 {
554         filePickWin->show();
555 }
556
557 void MainWin::LoadSoftware(QString file)
558 {
559         running = false;                                                        //  Prevent bad things(TM) from happening...
560         SET32(jaguarMainRAM, 0, 0x00200000);                    // Set top of stack...
561         cartridgeLoaded = (JaguarLoadFile(file.toAscii().data()) ? true : false);
562
563 #if 0
564         showUntunedTankCircuit = !cartridgeLoaded;
565 //This is crappy!!! !!! FIX !!!
566 //Is this even needed any more? Hmm. Maybe. Dunno.
567 //Seems like it is... But then again, maybe not. Have to test it to see.
568         WriteLog("GUI: Resetting Jaguar...\n");
569         JaguarReset();
570         running = true;
571 #else
572         powerAct->setDisabled(false);
573         powerAct->setChecked(true);
574         powerButtonOn = false;
575         TogglePowerState();
576 #endif
577
578         QString newTitle = QString("Virtual Jaguar 2.0.0 - Now playing: %1")
579                 .arg(filePickWin->GetSelectedPrettyName());
580         setWindowTitle(newTitle);
581 }
582
583 void MainWin::ResizeMainWindow(void)
584 {
585         videoWidget->setFixedSize(zoomLevel * 320, zoomLevel * (vjs.hardwareTypeNTSC ? 240 : 256));
586         show();
587
588         for(int i=0; i<2; i++)
589         {
590                 resize(0, 0);
591                 usleep(2000);
592                 QApplication::processEvents();
593         }
594 }
595
596 void MainWin::ReadSettings(void)
597 {
598         QSettings settings("Underground Software", "Virtual Jaguar");
599         QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
600         QSize size = settings.value("size", QSize(400, 400)).toSize();
601         resize(size);
602         move(pos);
603
604         zoomLevel = settings.value("zoom", 1).toInt();
605
606         vjs.useJoystick      = settings.value("useJoystick", false).toBool();
607         vjs.joyport          = settings.value("joyport", 0).toInt();
608         vjs.hardwareTypeNTSC = settings.value("hardwareTypeNTSC", true).toBool();
609         vjs.frameSkip        = settings.value("frameSkip", 0).toInt();
610         vjs.useJaguarBIOS    = settings.value("useJaguarBIOS", false).toBool();
611         vjs.DSPEnabled       = settings.value("DSPEnabled", false).toBool();
612         vjs.usePipelinedDSP  = settings.value("usePipelinedDSP", false).toBool();
613         vjs.fullscreen       = settings.value("fullscreen", false).toBool();
614         vjs.useOpenGL        = settings.value("useOpenGL", true).toBool();
615         vjs.glFilter         = settings.value("glFilterType", 0).toInt();
616         vjs.renderType       = settings.value("renderType", 0).toInt();
617         strcpy(vjs.jagBootPath, settings.value("JagBootROM", "./bios/[BIOS] Atari Jaguar (USA, Europe).zip").toString().toAscii().data());
618         strcpy(vjs.CDBootPath, settings.value("CDBootROM", "./bios/jagcd.rom").toString().toAscii().data());
619         strcpy(vjs.EEPROMPath, settings.value("EEPROMs", "./eeproms").toString().toAscii().data());
620         strcpy(vjs.ROMPath, settings.value("ROMs", "./software").toString().toAscii().data());
621 WriteLog("MainWin: Paths\n");
622 WriteLog("    jagBootPath = \"%s\"\n", vjs.jagBootPath);
623 WriteLog("    CDBootPath  = \"%s\"\n", vjs.CDBootPath);
624 WriteLog("    EEPROMPath  = \"%s\"\n", vjs.EEPROMPath);
625 WriteLog("    ROMPath     = \"%s\"\n", vjs.ROMPath);
626
627         // Keybindings in order of U, D, L, R, C, B, A, Op, Pa, 0-9, #, *
628         vjs.p1KeyBindings[0] = settings.value("p1k_up", SDLK_UP).toInt();
629         vjs.p1KeyBindings[1] = settings.value("p1k_down", SDLK_DOWN).toInt();
630         vjs.p1KeyBindings[2] = settings.value("p1k_left", SDLK_LEFT).toInt();
631         vjs.p1KeyBindings[3] = settings.value("p1k_right", SDLK_RIGHT).toInt();
632         vjs.p1KeyBindings[4] = settings.value("p1k_c", SDLK_z).toInt();
633         vjs.p1KeyBindings[5] = settings.value("p1k_b", SDLK_x).toInt();
634         vjs.p1KeyBindings[6] = settings.value("p1k_a", SDLK_c).toInt();
635         vjs.p1KeyBindings[7] = settings.value("p1k_option", SDLK_QUOTE).toInt();
636         vjs.p1KeyBindings[8] = settings.value("p1k_pause", SDLK_RETURN).toInt();
637         vjs.p1KeyBindings[9] = settings.value("p1k_0", SDLK_KP0).toInt();
638         vjs.p1KeyBindings[10] = settings.value("p1k_1", SDLK_KP1).toInt();
639         vjs.p1KeyBindings[11] = settings.value("p1k_2", SDLK_KP2).toInt();
640         vjs.p1KeyBindings[12] = settings.value("p1k_3", SDLK_KP3).toInt();
641         vjs.p1KeyBindings[13] = settings.value("p1k_4", SDLK_KP4).toInt();
642         vjs.p1KeyBindings[14] = settings.value("p1k_5", SDLK_KP5).toInt();
643         vjs.p1KeyBindings[15] = settings.value("p1k_6", SDLK_KP6).toInt();
644         vjs.p1KeyBindings[16] = settings.value("p1k_7", SDLK_KP7).toInt();
645         vjs.p1KeyBindings[17] = settings.value("p1k_8", SDLK_KP8).toInt();
646         vjs.p1KeyBindings[18] = settings.value("p1k_9", SDLK_KP9).toInt();
647         vjs.p1KeyBindings[19] = settings.value("p1k_pound", SDLK_KP_DIVIDE).toInt();
648         vjs.p1KeyBindings[20] = settings.value("p1k_star", SDLK_KP_MULTIPLY).toInt();
649
650         vjs.p2KeyBindings[0] = settings.value("p2k_up", SDLK_UP).toInt();
651         vjs.p2KeyBindings[1] = settings.value("p2k_down", SDLK_DOWN).toInt();
652         vjs.p2KeyBindings[2] = settings.value("p2k_left", SDLK_LEFT).toInt();
653         vjs.p2KeyBindings[3] = settings.value("p2k_right", SDLK_RIGHT).toInt();
654         vjs.p2KeyBindings[4] = settings.value("p2k_c", SDLK_z).toInt();
655         vjs.p2KeyBindings[5] = settings.value("p2k_b", SDLK_x).toInt();
656         vjs.p2KeyBindings[6] = settings.value("p2k_a", SDLK_c).toInt();
657         vjs.p2KeyBindings[7] = settings.value("p2k_option", SDLK_QUOTE).toInt();
658         vjs.p2KeyBindings[8] = settings.value("p2k_pause", SDLK_RETURN).toInt();
659         vjs.p2KeyBindings[9] = settings.value("p2k_0", SDLK_KP0).toInt();
660         vjs.p2KeyBindings[10] = settings.value("p2k_1", SDLK_KP1).toInt();
661         vjs.p2KeyBindings[11] = settings.value("p2k_2", SDLK_KP2).toInt();
662         vjs.p2KeyBindings[12] = settings.value("p2k_3", SDLK_KP3).toInt();
663         vjs.p2KeyBindings[13] = settings.value("p2k_4", SDLK_KP4).toInt();
664         vjs.p2KeyBindings[14] = settings.value("p2k_5", SDLK_KP5).toInt();
665         vjs.p2KeyBindings[15] = settings.value("p2k_6", SDLK_KP6).toInt();
666         vjs.p2KeyBindings[16] = settings.value("p2k_7", SDLK_KP7).toInt();
667         vjs.p2KeyBindings[17] = settings.value("p2k_8", SDLK_KP8).toInt();
668         vjs.p2KeyBindings[18] = settings.value("p2k_9", SDLK_KP9).toInt();
669         vjs.p2KeyBindings[19] = settings.value("p2k_pound", SDLK_KP_DIVIDE).toInt();
670         vjs.p2KeyBindings[20] = settings.value("p2k_star", SDLK_KP_MULTIPLY).toInt();
671 }
672
673 void MainWin::WriteSettings(void)
674 {
675         QSettings settings("Underground Software", "Virtual Jaguar");
676         settings.setValue("pos", pos());
677         settings.setValue("size", size());
678
679         settings.setValue("zoom", zoomLevel);
680
681         settings.setValue("useJoystick", vjs.useJoystick);
682         settings.setValue("joyport", vjs.joyport);
683         settings.setValue("hardwareTypeNTSC", vjs.hardwareTypeNTSC);
684         settings.setValue("frameSkip", vjs.frameSkip);
685         settings.setValue("useJaguarBIOS", vjs.useJaguarBIOS);
686         settings.setValue("DSPEnabled", vjs.DSPEnabled);
687         settings.setValue("usePipelinedDSP", vjs.usePipelinedDSP);
688         settings.setValue("fullscreen", vjs.fullscreen);
689         settings.setValue("useOpenGL", vjs.useOpenGL);
690         settings.setValue("glFilterType", vjs.glFilter);
691         settings.setValue("renderType", vjs.renderType);
692         settings.setValue("JagBootROM", vjs.jagBootPath);
693         settings.setValue("CDBootROM", vjs.CDBootPath);
694         settings.setValue("EEPROMs", vjs.EEPROMPath);
695         settings.setValue("ROMs", vjs.ROMPath);
696
697         settings.setValue("p1k_up", vjs.p1KeyBindings[0]);
698         settings.setValue("p1k_down", vjs.p1KeyBindings[1]);
699         settings.setValue("p1k_left", vjs.p1KeyBindings[2]);
700         settings.setValue("p1k_right", vjs.p1KeyBindings[3]);
701         settings.setValue("p1k_c", vjs.p1KeyBindings[4]);
702         settings.setValue("p1k_b", vjs.p1KeyBindings[5]);
703         settings.setValue("p1k_a", vjs.p1KeyBindings[6]);
704         settings.setValue("p1k_option", vjs.p1KeyBindings[7]);
705         settings.setValue("p1k_pause", vjs.p1KeyBindings[8]);
706         settings.setValue("p1k_0", vjs.p1KeyBindings[9]);
707         settings.setValue("p1k_1", vjs.p1KeyBindings[10]);
708         settings.setValue("p1k_2", vjs.p1KeyBindings[11]);
709         settings.setValue("p1k_3", vjs.p1KeyBindings[12]);
710         settings.setValue("p1k_4", vjs.p1KeyBindings[13]);
711         settings.setValue("p1k_5", vjs.p1KeyBindings[14]);
712         settings.setValue("p1k_6", vjs.p1KeyBindings[15]);
713         settings.setValue("p1k_7", vjs.p1KeyBindings[16]);
714         settings.setValue("p1k_8", vjs.p1KeyBindings[17]);
715         settings.setValue("p1k_9", vjs.p1KeyBindings[18]);
716         settings.setValue("p1k_pound", vjs.p1KeyBindings[19]);
717         settings.setValue("p1k_star", vjs.p1KeyBindings[20]);
718
719         settings.setValue("p2k_up", vjs.p2KeyBindings[0]);
720         settings.setValue("p2k_down", vjs.p2KeyBindings[1]);
721         settings.setValue("p2k_left", vjs.p2KeyBindings[2]);
722         settings.setValue("p2k_right", vjs.p2KeyBindings[3]);
723         settings.setValue("p2k_c", vjs.p2KeyBindings[4]);
724         settings.setValue("p2k_b", vjs.p2KeyBindings[5]);
725         settings.setValue("p2k_a", vjs.p2KeyBindings[6]);
726         settings.setValue("p2k_option", vjs.p2KeyBindings[7]);
727         settings.setValue("p2k_pause", vjs.p2KeyBindings[8]);
728         settings.setValue("p2k_0", vjs.p2KeyBindings[9]);
729         settings.setValue("p2k_1", vjs.p2KeyBindings[10]);
730         settings.setValue("p2k_2", vjs.p2KeyBindings[11]);
731         settings.setValue("p2k_3", vjs.p2KeyBindings[12]);
732         settings.setValue("p2k_4", vjs.p2KeyBindings[13]);
733         settings.setValue("p2k_5", vjs.p2KeyBindings[14]);
734         settings.setValue("p2k_6", vjs.p2KeyBindings[15]);
735         settings.setValue("p2k_7", vjs.p2KeyBindings[16]);
736         settings.setValue("p2k_8", vjs.p2KeyBindings[17]);
737         settings.setValue("p2k_9", vjs.p2KeyBindings[18]);
738         settings.setValue("p2k_pound", vjs.p2KeyBindings[19]);
739         settings.setValue("p2k_star", vjs.p2KeyBindings[20]);
740 }
741
742 // Here's how Byuu does it...
743 // I think I have it working now... :-)
744 #if 0
745 void Utility::resizeMainWindow()
746 {
747   unsigned region = config().video.context->region;
748   unsigned multiplier = config().video.context->multiplier;
749   unsigned width = 256 * multiplier;
750   unsigned height = (region == 0 ? 224 : 239) * multiplier;
751
752   if(config().video.context->correctAspectRatio)
753   {
754     if(region == 0)
755         {
756       width = (double)width * config().video.ntscAspectRatio + 0.5;  //NTSC adjust
757     }
758         else
759         {
760       width = (double)width * config().video.palAspectRatio  + 0.5;  //PAL adjust
761     }
762   }
763
764   if(config().video.isFullscreen == false)
765   {
766     //get effective desktop work area region (ignore Windows taskbar, OS X dock, etc.)
767     QRect deskRect = QApplication::desktop()->availableGeometry(mainWindow);
768
769     //ensure window size will not be larger than viewable desktop area
770     constrainSize(height, width, deskRect.height()); //- frameHeight);
771     constrainSize(width, height, deskRect.width());  //- frameWidth );
772
773     mainWindow->canvas->setFixedSize(width, height);
774     mainWindow->show();
775   }
776   else
777   {
778     for(unsigned i = 0; i < 2; i++)
779         {
780       unsigned iWidth = width, iHeight = height;
781
782       constrainSize(iHeight, iWidth, mainWindow->canvasContainer->size().height());
783       constrainSize(iWidth, iHeight, mainWindow->canvasContainer->size().width());
784
785       //center canvas onscreen; ensure it is not larger than viewable area
786       mainWindow->canvas->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
787       mainWindow->canvas->setFixedSize(iWidth, iHeight);
788       mainWindow->canvas->setMinimumSize(0, 0);
789
790       usleep(2000);
791       QApplication::processEvents();
792     }
793   }
794
795   //workaround for Qt/Xlib bug:
796   //if window resize occurs with cursor over it, Qt shows Qt::Size*DiagCursor;
797   //so force it to show Qt::ArrowCursor, as expected
798   mainWindow->setCursor(Qt::ArrowCursor);
799   mainWindow->canvasContainer->setCursor(Qt::ArrowCursor);
800   mainWindow->canvas->setCursor(Qt::ArrowCursor);
801
802   //workaround for DirectSound(?) bug:
803   //window resizing sometimes breaks audio sync, this call re-initializes it
804   updateAvSync();
805 }
806
807 void Utility::setScale(unsigned scale)
808 {
809   config().video.context->multiplier = scale;
810   resizeMainWindow();
811   mainWindow->shrink();
812   mainWindow->syncUi();
813 }
814
815 void QbWindow::shrink()
816 {
817   if(config().video.isFullscreen == false)
818   {
819     for(unsigned i = 0; i < 2; i++)
820         {
821       resize(0, 0);
822       usleep(2000);
823       QApplication::processEvents();
824     }
825   }
826 }
827 #endif