+ }
+
+ address += FONT_WIDTH;
+ }
+}
+
+//
+// Draw "picture"
+// Uses zero as transparent color
+// Can also use an optional alpha channel
+// Alpha channel is now mandatory! ;-)
+//
+//void DrawTransparentBitmap(int16 * screen, uint32 x, uint32 y, uint16 * bitmap, uint8 * alpha/*=NULL*/)
+/*void DrawTransparentBitmap(uint32 * screen, uint32 x, uint32 y, uint32 * bitmap, uint8 * alpha)
+{
+ uint32 width = bitmap[0], height = bitmap[1];
+ bitmap += 2;
+
+// uint32 pitch = GetSDLScreenPitch() / 2; // Returns pitch in bytes but we need words...
+ uint32 pitch = sdlemuGetOverlayWidthInPixels();//GetSDLScreenWidthInPixels();
+ uint32 address = x + (y * pitch);
+
+ for(uint32 yy=0; yy<height; yy++)
+ {
+ for(uint32 xx=0; xx<width; xx++)
+ {
+ if (alpha == NULL)
+ {
+ if (*bitmap && x + xx < pitch) // NOTE: Still doesn't clip the Y val...
+ *(screen + address + xx + (yy * pitch)) = *bitmap;
+ }
+ else
+ {
+ uint8 trans = *alpha;
+ uint32 color = *bitmap;
+ uint32 existingColor = *(screen + address + xx + (yy * pitch));
+
+ uint8 eRed = existingColor & 0xFF,
+ eGreen = (existingColor >> 8) & 0xFF,
+ eBlue = (existingColor >> 16) & 0xFF,
+
+ nRed = color & 0xFF,
+ nGreen = (color >> 8) & 0xFF,
+ nBlue = (color >> 16) & 0xFF;
+
+ uint8 invTrans = 255 - trans;
+ uint32 bRed = (eRed * trans + nRed * invTrans) / 255;
+ uint32 bGreen = (eGreen * trans + nGreen * invTrans) / 255;
+ uint32 bBlue = (eBlue * trans + nBlue * invTrans) / 255;
+
+ uint32 blendedColor = 0xFF000000 | bRed | (bGreen << 8) | (bBlue << 16);
+
+ *(screen + address + xx + (yy * pitch)) = blendedColor;
+
+ alpha++;
+ }
+
+ bitmap++;
+ }
+ }
+}*/
+void DrawTransparentBitmapDeprecated(uint32 * screen, uint32 x, uint32 y, uint32 * bitmap)
+{
+ uint32 width = bitmap[0], height = bitmap[1];
+ bitmap += 2;
+
+ uint32 pitch = sdlemuGetOverlayWidthInPixels();//GetSDLScreenWidthInPixels();
+ uint32 address = x + (y * pitch);
+
+ for(uint32 yy=0; yy<height; yy++)
+ {
+ for(uint32 xx=0; xx<width; xx++)
+ {
+ uint32 color = *bitmap;
+ uint32 blendedColor = color;
+ uint32 existingColor = *(screen + address + xx + (yy * pitch));
+
+ if (existingColor >> 24 != 0x00) // Pixel needs blending
+ {
+ uint8 trans = color >> 24;
+ uint8 invTrans = trans ^ 0xFF;//255 - trans;
+
+ uint8 eRed = existingColor & 0xFF,
+ eGreen = (existingColor >> 8) & 0xFF,
+ eBlue = (existingColor >> 16) & 0xFF,
+
+ nRed = color & 0xFF,
+ nGreen = (color >> 8) & 0xFF,
+ nBlue = (color >> 16) & 0xFF;
+
+ uint32 bRed = (eRed * invTrans + nRed * trans) / 255;
+ uint32 bGreen = (eGreen * invTrans + nGreen * trans) / 255;
+ uint32 bBlue = (eBlue * invTrans + nBlue * trans) / 255;
+
+ blendedColor = 0xFF000000 | bRed | (bGreen << 8) | (bBlue << 16);
+ }
+
+ *(screen + address + xx + (yy * pitch)) = blendedColor;
+ bitmap++;
+ }
+ }
+}
+
+void DrawTransparentBitmap(uint32 * screen, uint32 x, uint32 y, const void * bitmap)
+{
+ uint32 pitch = sdlemuGetOverlayWidthInPixels();
+ uint32 address = x + (y * pitch);
+ uint32 count = 0;
+
+ for(uint32 yy=0; yy<((Bitmap *)bitmap)->height; yy++)
+ {
+ for(uint32 xx=0; xx<((Bitmap *)bitmap)->width; xx++)
+ {
+ uint32 color = ((uint32 *)((Bitmap *)bitmap)->pixelData)[count];
+ uint32 blendedColor = color;
+ uint32 existingColor = *(screen + address + xx + (yy * pitch));
+
+ if (existingColor >> 24 != 0x00) // Pixel needs blending
+ {
+ uint8 trans = color >> 24;
+ uint8 invTrans = trans ^ 0xFF;
+
+ uint8 eRed = existingColor & 0xFF,
+ eGreen = (existingColor >> 8) & 0xFF,
+ eBlue = (existingColor >> 16) & 0xFF,
+
+ nRed = color & 0xFF,
+ nGreen = (color >> 8) & 0xFF,
+ nBlue = (color >> 16) & 0xFF;
+
+ uint32 bRed = (eRed * invTrans + nRed * trans) / 255;
+ uint32 bGreen = (eGreen * invTrans + nGreen * trans) / 255;
+ uint32 bBlue = (eBlue * invTrans + nBlue * trans) / 255;
+
+// Instead of $FF, should use the alpha from the destination pixel as the final alpha value...
+ blendedColor = 0xFF000000 | bRed | (bGreen << 8) | (bBlue << 16);
+ }
+
+ *(screen + address + xx + (yy * pitch)) = blendedColor;
+ count++;
+ }
+ }
+}
+
+//
+// Draw a bitmap without using blending
+//
+void DrawBitmap(uint32 * screen, uint32 x, uint32 y, const void * bitmap)
+{
+ uint32 pitch = sdlemuGetOverlayWidthInPixels();
+ uint32 address = x + (y * pitch);
+ uint32 count = 0;
+
+ for(uint32 yy=0; yy<((Bitmap *)bitmap)->height; yy++)
+ {
+ for(uint32 xx=0; xx<((Bitmap *)bitmap)->width; xx++)
+ {
+ *(screen + address + xx + (yy * pitch)) = ((uint32 *)((Bitmap *)bitmap)->pixelData)[count];
+ count++;
+ }
+ }
+}
+
+//
+// Clear a portion of the screen
+//
+void ClearScreenRectangle(uint32 * screen, uint32 x, uint32 y, uint32 w, uint32 h)
+{
+ uint32 pitch = sdlemuGetOverlayWidthInPixels();
+ uint32 address = x + (y * pitch);
+
+ for(uint32 yy=0; yy<h; yy++)
+ for(uint32 xx=0; xx<w; xx++)
+ *(screen + address + xx + (yy * pitch)) = 0;
+}
+
+
+//
+// GUI stuff--it's not crunchy, it's GUI! ;-)
+//
+
+void InitGUI(void)
+{
+ SDL_ShowCursor(SDL_DISABLE);
+ SDL_GetMouseState(&mouseX, &mouseY);
+}
+
+void GUIDone(void)
+{
+}
+
+//
+// GUI main loop
+//
+//bool GUIMain(void)
+bool GUIMain(char * filename)
+{
+WriteLog("GUI: Inside GUIMain...\n");
+
+ uint32 pointerBGSave[6 * 8 + 2];
+ pointerBGSave[0] = 6;
+ pointerBGSave[1] = 8;
+
+// Need to set things up so that it loads and runs a file if given on the command line. !!! FIX !!! [DONE]
+ extern uint32 * backbuffer;
+// bool done = false;
+ SDL_Event event;
+ Window * mainWindow = NULL;
+
+ // Set up the GUI classes...
+// Element::SetScreenAndPitch(backbuffer, GetSDLScreenWidthInPixels());
+ Element::SetScreenAndPitch((uint32 *)sdlemuGetOverlayPixels(), sdlemuGetOverlayWidthInPixels());
+ sdlemuEnableOverlay();
+
+ Menu mainMenu;
+ MenuItems mi;
+ mi.title = "Jaguar";
+ mi.item.push_back(NameAction("Load...", LoadROM, SDLK_l));
+ mi.item.push_back(NameAction("Reset", ResetJaguar, SDLK_r));
+ if (CDBIOSLoaded)
+ mi.item.push_back(NameAction("Reset CD", ResetJaguarCD, SDLK_c));
+ mi.item.push_back(NameAction("Run", RunEmu, SDLK_ESCAPE));
+ mi.item.push_back(NameAction(""));
+ mi.item.push_back(NameAction("Quit", Quit, SDLK_q));
+ mainMenu.Add(mi);
+ mi.title = "Settings";
+ mi.item.clear();
+ mi.item.push_back(NameAction("Video..."));
+ mi.item.push_back(NameAction("Audio..."));
+ mi.item.push_back(NameAction("Misc...", MiscOptions, SDLK_m));
+ mainMenu.Add(mi);
+ mi.title = "Info";
+ mi.item.clear();
+ mi.item.push_back(NameAction("About...", About));
+ mainMenu.Add(mi);
+
+ bool showMouse = true;
+
+ // Grab the BG where the mouse will be painted (prime the backstore)
+
+/*
+DISNOWOK
+Bitmap ptr = { 6, 8, 4,
+""//"000011112222333344445555"
+//"000011112222333344445555"
+//"000011112222333344445555"
+//"000011112222333344445555"
+//"000011112222333344445555"
+//"000011112222333344445555"
+//"000011112222333344445555"
+//"000011112222333344445555"
+};//*/
+ uint32 * overlayPixels = (uint32 *)sdlemuGetOverlayPixels();
+ uint32 count = 2;
+
+ for(uint32 y=0; y<pointerBGSave[1]; y++)
+ for(uint32 x=0; x<pointerBGSave[0]; x++)
+ pointerBGSave[count++] = overlayPixels[((mouseY + y) * sdlemuGetOverlayWidthInPixels()) + (mouseX + x)];
+
+ uint32 oldMouseX = mouseX, oldMouseY = mouseY;
+
+//This is crappy!!! !!! FIX !!!
+//Is this even needed any more? Hmm. Maybe. Dunno.
+WriteLog("GUI: Resetting Jaguar...\n");
+ jaguar_reset();
+
+WriteLog("GUI: Clearing BG save...\n");
+ // Set up our background save...
+// memset(background, 0x11, tom_getVideoModeWidth() * 240 * 2);
+//1111 -> 000100 01000 10001 -> 0001 0000 0100 0010 1000 1100 -> 10 42 8C
+ for(uint32 i=0; i<tom_getVideoModeWidth()*240; i++)
+// background[i] = 0xFF8C4210;
+ backbuffer[i] = 0xFF8C4210;
+
+/* uint32 * overlayPix = (uint32 *)sdlemuGetOverlayPixels();
+ for(uint32 i=0; i<sdlemuGetOverlayWidthInPixels()*480; i++)
+ overlayPix[i] = 0x00000000;*/
+
+ // Handle loading file passed in on the command line...! [DONE]
+
+ if (filename)
+ {
+ if (JaguarLoadFile(filename))
+ {
+// event.type = SDL_USEREVENT, event.user.code = MENU_ITEM_CHOSEN;
+// event.user.data1 = (void *)ResetJaguar;
+// SDL_PushEvent(&event);
+ // Make it so that if passed in on the command line, we quit right
+ // away when pressing ESC
+WriteLog("GUI: Bypassing GUI since ROM passed in on command line...\n");
+ ResetJaguar();
+ return true;
+ }
+ else
+ {
+ // Create error dialog...
+ char errText[1024];
+ sprintf(errText, "The file %40s could not be loaded.", filename);
+
+ mainWindow = new Window(8, 16, 304, 160);
+ mainWindow->AddElement(new Text(8, 8, "Error!"));
+ mainWindow->AddElement(new Text(8, 24, errText));
+ }
+ }
+
+WriteLog("GUI: Entering main loop...\n");
+ while (!exitGUI)
+ {
+ if (SDL_PollEvent(&event))
+ {
+ if (event.type == SDL_USEREVENT)
+ {
+ if (event.user.code == WINDOW_CLOSE)
+ {
+ delete mainWindow;
+ mainWindow = NULL;
+ }
+ else if (event.user.code == MENU_ITEM_CHOSEN)
+ {
+ // Confused? Let me enlighten... What we're doing here is casting
+ // data1 as a pointer to a function which returns a Window pointer and
+ // which takes no parameters (the "(Window *(*)(void))" part), then
+ // derefencing it (the "*" in front of that) in order to call the
+ // function that it points to. Clear as mud? Yeah, I hate function
+ // pointers too, but what else are you gonna do?
+ mainWindow = (*(Window *(*)(void))event.user.data1)();
+
+ while (SDL_PollEvent(&event)); // Flush the event queue...
+ event.type = SDL_MOUSEMOTION;
+ int mx, my;
+ SDL_GetMouseState(&mx, &my);
+ event.motion.x = mx, event.motion.y = my;
+ SDL_PushEvent(&event); // & update mouse position...!
+
+ oldMouseX = mouseX, oldMouseY = mouseY;
+ mouseX = mx, mouseY = my; // This prevents "mouse flash"...
+ }
+ }
+ else if (event.type == SDL_ACTIVEEVENT)
+ {