+//
+// Draw text at the given x/y coordinates, using FG/BG colors.
+//
+void DrawStringOpaque(int16 * screen, uint32 x, uint32 y, uint16 color1, uint16 color2, const char * text, ...)
+{
+ char string[4096];
+ va_list arg;
+
+ va_start(arg, text);
+ vsprintf(string, text, arg);
+ va_end(arg);
+
+ uint32 pitch = GetSDLScreenPitch() / 2; // Returns pitch in bytes but we need words...
+ uint32 length = strlen(string), address = x + (y * pitch);
+
+ for(uint32 i=0; i<length; i++)
+ {
+ uint32 fontAddr = (uint32)string[i] * 64;
+
+ for(uint32 yy=0; yy<8; yy++)
+ {
+ for(uint32 xx=0; xx<8; xx++)
+ {
+ *(screen + address + xx + (yy * pitch)) = (font1[fontAddr] ? color1 : color2);
+ fontAddr++;
+ }
+ }
+
+ address += 8;
+ }
+}
+
+//
+// Draw text at the given x/y coordinates. Can invert text as well.
+//
+void DrawStringTrans(int16 * screen, uint32 x, uint32 y, uint16 color, uint8 trans, const char * text, ...)
+{
+ char string[4096];
+ va_list arg;
+
+ va_start(arg, text);
+ vsprintf(string, text, arg);
+ va_end(arg);
+
+ uint32 pitch = GetSDLScreenPitch() / 2; // Returns pitch in bytes but we need words...
+ uint32 length = strlen(string), address = x + (y * pitch);
+
+ for(uint32 i=0; i<length; i++)
+ {
+ uint32 fontAddr = (uint32)string[i] * 64;
+
+ for(uint32 yy=0; yy<8; yy++)
+ {
+ for(uint32 xx=0; xx<8; xx++)
+ {
+ if (font1[fontAddr])
+ {
+ uint16 existingColor = *(screen + address + xx + (yy * pitch));
+
+ uint8 eRed = (existingColor >> 10) & 0x1F,
+ eGreen = (existingColor >> 5) & 0x1F,
+ eBlue = existingColor & 0x1F,
+//This could be done ahead of time, instead of on each pixel...
+ nRed = (color >> 10) & 0x1F,
+ nGreen = (color >> 5) & 0x1F,
+ nBlue = color & 0x1F;
+
+//This could be sped up by using a table of 5 + 5 + 5 bits (32 levels transparency -> 32768 entries)
+//Here we've modified it to have 33 levels of transparency (could have any # we want!)
+//because dividing by 32 is faster than dividing by 31!
+ uint8 invTrans = 32 - trans;
+ uint16 bRed = (eRed * trans + nRed * invTrans) / 32;
+ uint16 bGreen = (eGreen * trans + nGreen * invTrans) / 32;
+ uint16 bBlue = (eBlue * trans + nBlue * invTrans) / 32;
+
+ uint16 blendedColor = (bRed << 10) | (bGreen << 5) | bBlue;
+
+ *(screen + address + xx + (yy * pitch)) = blendedColor;
+ }
+
+ fontAddr++;
+ }
+ }
+
+ address += 8;
+ }
+}
+
+//
+// GUI Main loop
+//
+enum { GUI_TOP_MENU, GUI_MENU_CLICKED, GUI_WINDOW };
+bool GUIMain(void)
+{
+ extern int16 * backbuffer;
+ bool done = false;
+ SDL_Event event;
+ int32 menuChosen = -1;
+ uint32 menuXPos = 0;
+ int32 menuItemChosen = -1;
+ uint32 GUIState = GUI_TOP_MENU;
+
+ Element::SetScreenAndPitch(backbuffer, GetSDLScreenPitch() / 2);
+
+ Button closeButton(45, 90, 16, 16);
+ Window someWindow(15, 16, 60, 60);
+ Button button1(50, 1, 9, 9), button2(10, 10, 8, 8), button3(25, 48, 15, 8);
+ someWindow.AddElement(&button1);
+ someWindow.AddElement(&button2);
+ someWindow.AddElement(&button3);
+
+ MenuItems mi;
+ Menu mainMenu(0, 160);
+ mi.title = "File";
+ mi.item.push_back(NameAction("Load...", LoadROM));
+ mi.item.push_back(NameAction("Reset"));
+ mi.item.push_back(NameAction("Run", RunEmu));
+ mi.item.push_back(NameAction(""));
+ mi.item.push_back(NameAction("Quit", Quit));
+ mainMenu.Add(mi);
+ mi.title = "Settings";
+ mi.item.clear();
+ mainMenu.Add(mi);
+ mi.title = "Options";
+ mainMenu.Add(mi);
+
+ bool showMouse = true;
+
+//This is crappy!!! !!! FIX !!!
+ jaguar_reset();
+
+ while (!done)
+ {
+ while (SDL_PollEvent(&event))
+ {
+ if (event.type == SDL_ACTIVEEVENT)
+ {
+ if (event.active.state == SDL_APPMOUSEFOCUS)
+ showMouse = (event.active.gain ? true : false);
+ }
+ if (event.type == SDL_KEYDOWN)
+ {
+// if (event.key.keysym.sym == SDLK_ESCAPE)
+// done = true;
+// return false;
+ }
+ else if (event.type == SDL_MOUSEMOTION)
+ {
+ mouseX = event.motion.x, mouseY = event.motion.y;
+
+ if (vjs.useOpenGL)
+ mouseX /= 2, mouseY /= 2;
+
+ closeButton.HandleMouseMove(mouseX, mouseY);
+ someWindow.HandleMouseMove(mouseX, mouseY);
+ mainMenu.HandleMouseMove(mouseX, mouseY);
+ }
+ else if (event.type == SDL_MOUSEBUTTONDOWN)
+ {
+ uint32 mx = event.button.x, my = event.button.y;
+ if (vjs.useOpenGL)
+ mx /= 2, my /= 2;
+
+ // Handle that click!
+ if (GUIState == GUI_TOP_MENU)
+ {
+ if (menuChosen != -1)
+ GUIState = GUI_MENU_CLICKED;
+ }
+ else if (GUIState == GUI_MENU_CLICKED)
+ {
+ if (menuItemChosen != -1)
+ {
+// GUIState = GUI_WINDOW;
+ if (subMenuAction[menuChosen][menuItemChosen] != NULL)
+ subMenuAction[menuChosen][menuItemChosen]();
+ }
+// else
+ GUIState = GUI_TOP_MENU;
+ }
+
+ closeButton.HandleMouseButton(mx, my, true);
+ someWindow.HandleMouseButton(mx, my, true);
+ mainMenu.HandleMouseButton(mx, my, true);
+ }
+ else if (event.type == SDL_MOUSEBUTTONUP)
+ {
+ uint32 mx = event.button.x, my = event.button.y;
+
+ if (vjs.useOpenGL)
+ mx /= 2, my /= 2;
+
+ closeButton.HandleMouseButton(mx, my, false);
+ someWindow.HandleMouseButton(mx, my, false);
+ mainMenu.HandleMouseButton(mx, my, false);
+ }
+
+ // Draw the GUI...
+
+// The way we do things here is kinda stupid (redrawing the screen every frame), but
+// it's simple. Perhaps there may be a reason down the road to be more selective with
+// our clearing, but for now, this will suffice.
+ memset(backbuffer, 0x11, tom_getVideoModeWidth() * 240 * 2);
+
+ closeButton.Draw();
+ someWindow.Draw();
+ mainMenu.Draw();
+
+ // We always draw the top level menu...
+ if (GUIState == GUI_TOP_MENU)
+ menuChosen = -1;
+
+ uint32 xpos = 0;
+ for(uint32 i=0; i<NUM_MENU_ITEMS; i++)
+ {
+ uint16 colorFG = 0x1CFF, colorBG = 0x000F;
+ uint32 length = strlen(menu[i]) + 2;
+
+ if (((uint32)mouseX >= xpos && (uint32)mouseX < xpos + length * 8)
+ && mouseY < 8)
+ colorFG = 0x421F, colorBG = 0x1CFF, menuChosen = i, menuXPos = xpos;
+
+ if (GUIState != GUI_TOP_MENU && i == (uint32)menuChosen)
+ colorFG = 0x421F, colorBG = 0x1CFF;
+// BG: 0 00011 00011 11111 -> 0000 1100 0111 1111
+// FG: 0 10000 10000 11111 -> 0100 0010 0001 1111
+ DrawStringOpaque(backbuffer, xpos, 0, colorFG, colorBG, " %s ", menu[i]);
+ xpos += length * 8;
+ }
+
+ // We don't always draw the submenus...
+ if (GUIState == GUI_MENU_CLICKED)
+ {
+ menuItemChosen = -1;
+ uint32 length = 0;
+ for(int i=0; i<subMenuNumItems[menuChosen]; i++)
+ if (strlen(subMenu[menuChosen][i]) > length)
+ length = strlen(subMenu[menuChosen][i]);
+
+ uint32 ypos = 9;
+ for(int i=0; i<subMenuNumItems[menuChosen]; i++)
+ {
+ uint16 colorFG = 0x1CFF, colorBG = 0x000F;
+
+ if (((uint32)mouseX >= menuXPos && (uint32)mouseX < menuXPos + (length + 2) * 8)
+ && mouseY >= (9 + i * 8) && mouseY < (9 + (i + 1) * 8))
+ colorFG = 0x421F, colorBG = 0x1CFF, menuItemChosen = i;
+
+ DrawStringOpaque(backbuffer, menuXPos, ypos, colorFG, colorBG, " %-*.*s ", length, length, subMenu[menuChosen][i]);
+ ypos += 8;
+ }
+ }
+ // Windows? Isn't that an illegal monopoly or something? ;-)
+ else if (GUIState == GUI_WINDOW)
+ {
+ }
+
+ if (showMouse)
+ DrawTransparentBitmap(backbuffer, mouseX, mouseY, mousePic);
+
+ RenderBackbuffer();
+ }
+ }
+
+ return true;
+}
+
+//
+// GUI "action" functions
+//
+void LoadROM(void)
+{
+}
+
+void RunEmu(void)
+{
+//This is crappy... !!! FIX !!!
+ extern int16 * backbuffer;
+ extern bool finished;
+ extern bool showGUI;
+ uint32 nFrame = 0, nFrameskip = 0;
+ uint32 totalFrames = 0;
+ finished = false;
+ bool showMessage = true;
+ uint32 showMsgFrames = 60;
+ uint8 transparency = 0;
+
+ while (!finished)
+ {
+ // Set up new backbuffer with new pixels and data
+ JaguarExecute(backbuffer, true);
+ totalFrames++;
+//WriteLog("Frame #%u...\n", totalFrames);
+//extern bool doDSPDis;
+//if (totalFrames == 373)
+// doDSPDis = true;
+
+ // Some QnD GUI stuff here...
+ if (showGUI)
+ {
+ extern uint32 gpu_pc, dsp_pc;
+ DrawString(backbuffer, 8, 8, false, "GPU PC: %08X", gpu_pc);
+ DrawString(backbuffer, 8, 16, false, "DSP PC: %08X", dsp_pc);
+ }
+
+ if (showMessage)
+ {
+ DrawStringTrans(backbuffer, 8, 24*8, 0xFF0F, transparency, "Running...");
+
+ if (showMsgFrames == 0)
+ {
+ transparency++;
+
+ if (transparency == 33)
+ showMessage = false;
+ }
+ else
+ showMsgFrames--;
+ }
+
+ // Simple frameskip
+ if (nFrame == nFrameskip)
+ {
+ RenderBackbuffer();
+ nFrame = 0;
+ }
+ else
+ nFrame++;
+
+ joystick_exec();
+ }
+}
+
+void Quit(void)
+{
+ WriteLog("GUI: Quitting due to user request.\n");
+ log_done();
+ exit(0);
+}
+
+void About(void)
+{
+ extern int16 * backbuffer;
+ SDL_Event event;
+ uint16 * bgSave = (uint16 *)malloc(tom_getVideoModeWidth() * 240 * 2);
+ memcpy(bgSave, backbuffer, tom_getVideoModeWidth() * 240 * 2);
+
+ bool done = false;
+ while (!done)
+ {
+ while (SDL_PollEvent(&event))
+ {
+ if (event.type == SDL_KEYDOWN)
+ {
+ if (event.key.keysym.sym == SDLK_ESCAPE || event.key.keysym.sym == SDLK_RETURN)
+ done = true;
+ }
+ else if (event.type == SDL_MOUSEMOTION)
+ {
+ mouseX = event.motion.x, mouseY = event.motion.y;
+ if (vjs.useOpenGL)
+ mouseX /= 2, mouseY /= 2;
+ }
+ else if (event.type == SDL_MOUSEBUTTONDOWN)
+ {
+ uint32 mx = event.button.x, my = event.button.y;
+ if (vjs.useOpenGL)
+ mx /= 2, my /= 2;
+
+ done = true;
+ }
+
+ // Draw "About" box
+ memcpy(backbuffer, bgSave, tom_getVideoModeWidth() * 240 * 2);
+
+ DrawStringOpaque(backbuffer, 64, 64, 0x1CFF, 0x000F, " ");
+ DrawStringOpaque(backbuffer, 64, 72, 0x1CFF, 0x000F, " Virtual Jaguar by JLH & crew ");
+ DrawStringOpaque(backbuffer, 64, 80, 0x1CFF, 0x000F, " ");
+
+ DrawTransparentBitmap(backbuffer, mouseX, mouseY, mousePic);
+
+ RenderBackbuffer();
+ }
+ }
+
+ free(bgSave);
+}
+