From: Shamus Hammons Date: Fri, 19 Sep 2003 17:39:57 +0000 (+0000) Subject: The GUI is coming! X-Git-Tag: 1.0.6~5 X-Git-Url: http://shamusworld.gotdns.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8dd35f67f193f9b152a195bd9b2a3f938ac1840e;p=virtualjaguar The GUI is coming! --- diff --git a/src/gui.cpp b/src/gui.cpp index 404af7e..ea11ebd 100644 --- a/src/gui.cpp +++ b/src/gui.cpp @@ -21,6 +21,14 @@ using namespace std; // For STL stuff // Private function prototypes +void DrawTransparentBitmap(int16 * screen, uint32 x, uint32 y, uint16 * bitmap); +void DrawStringTrans(int16 * screen, uint32 x, uint32 y, uint16 color, uint8 opacity, const char * text, ...); +void DrawStringOpaque(int16 * screen, uint32 x, uint32 y, uint16 color1, uint16 color2, const char * text, ...); +void LoadROM(void); +void RunEmu(void); +void Quit(void); +void About(void); + // Local global variables int mouseX, mouseY; @@ -53,6 +61,314 @@ uint16 mousePic[] = { // 0 000 00 11 111 0 0000 -> 03E0 // 0 000 00 11 000 0 0000 -> 0300 +#define NUM_MENU_ITEMS 3 +char * menu[NUM_MENU_ITEMS] = { "File", "Settings", "Options" }; + +char * menu1[4] = { "Load...", "Reset", "Run", "Quit" }; +char * menu2[3] = { "Video...", "Audio...", "Misc..." }; +char * menu3[1] = { "About..." }; + +char ** subMenu[NUM_MENU_ITEMS] = { menu1, menu2, menu3 }; +uint8 subMenuNumItems[NUM_MENU_ITEMS] = { 4, 3, 1 }; + +void (* menu1Action[4])(void) = { LoadROM, NULL, RunEmu, Quit }; +void (* menu2Action[3])(void) = { NULL, NULL, NULL }; +void (* menu3Action[1])(void) = { About }; +void (** subMenuAction[NUM_MENU_ITEMS])(void) = { menu1Action, menu2Action, menu3Action }; + +// +// Local GUI classes +// + +class Element +{ + public: + virtual void HandleKey(SDLKey key) = 0; + virtual void HandleMouseMove(uint32 x, uint32 y) = 0; + virtual void HandleMouseButton(uint32 x, uint32 y, bool mouseDown) = 0; + virtual void Draw(uint32, uint32) = 0; +//Needed? virtual ~Element() = 0; +//We're not allocating anything in the base class, so the answer would be NO. + bool Inside(uint32 x, uint32 y); + // Class method + static void SetScreenAndPitch(int16 * s, uint32 p) { screenBuffer = s, pitch = p; } + + protected: + SDL_Rect extents; + // Class variables... + static int16 * screenBuffer; + static uint32 pitch; +}; + +int16 * Element::screenBuffer = NULL; +uint32 Element::pitch = 0; + +bool Element::Inside(uint32 x, uint32 y) +{ + return (x >= (uint32)extents.x && x < (uint32)(extents.x + extents.w) + && y >= (uint32)extents.y && y < (uint32)(extents.y + extents.h) ? true : false); +} + +class Button: public Element +{ + public: + Button(uint32 x = 0, uint32 y = 0, uint32 w = 0, uint32 h = 0): clicked(false), + inside(false), fgColor(0xFFFF), bgColor(0x03E0) + { extents.x = x, extents.y = y, extents.w = w, extents.h = h; } + virtual void HandleKey(SDLKey key) {} + virtual void HandleMouseMove(uint32 x, uint32 y); + virtual void HandleMouseButton(uint32 x, uint32 y, bool mouseDown); + virtual void Draw(uint32 offsetX = 0, uint32 offsetY = 0); + + protected: + bool clicked, inside; + uint16 fgColor, bgColor; +}; + +void Button::HandleMouseMove(uint32 x, uint32 y) +{ + inside = Inside(x, y); +} + +void Button::HandleMouseButton(uint32 x, uint32 y, bool mouseDown) +{ + if (inside && mouseDown) + clicked = true; + else + clicked = false; +} + +void Button::Draw(uint32 offsetX/*= 0*/, uint32 offsetY/*= 0*/) +{ + uint32 addr = (extents.x + offsetX) + ((extents.y + offsetY) * pitch); + + for(uint32 y=0; y list; +}; + +void Window::HandleMouseMove(uint32 x, uint32 y) +{ + // Handle the items this window contains... + for(uint32 i=0; iHandleMouseMove(x - extents.x, y - extents.y); +} + +void Window::HandleMouseButton(uint32 x, uint32 y, bool mouseDown) +{ + // Handle the items this window contains... + for(uint32 i=0; iHandleMouseButton(x - extents.x, y - extents.y, mouseDown); +} + +void Window::Draw(uint32 offsetX/*= 0*/, uint32 offsetY/*= 0*/) +{ + uint32 addr = (extents.x + offsetX) + ((extents.y + offsetY) * pitch); + + for(uint32 y=0; yDraw(extents.x, extents.y); +} + +void Window::AddElement(Element * e) +{ + list.push_back(e); +} + +struct NameAction +{ + string name; + void (* action)(void); + bool isWindow; + + NameAction(string n, void (* a)(void) = NULL, bool w = false): name(n), action(a), + isWindow(w) {} +}; + +class MenuItems +{ + public: + MenuItems(): charLength(0) {} + + string title; + vector item; + uint32 charLength; + SDL_Rect extents; +}; + +class Menu: public Element +{ + public: + Menu(uint32 x = 0, uint32 y = 0, uint32 w = 0, uint32 h = 8, + uint16 fgc = 0x1CFF, uint16 bgc = 0x000F, uint16 fgch = 0x421F, + uint16 bgch = 0x1CFF): clicked(false), inside(0), fgColor(fgc), bgColor(bgc), + fgColorHL(fgch), bgColorHL(bgch), menuChosen(-1), menuItemChosen(-1) + { extents.x = x, extents.y = y, extents.w = w, extents.h = h; } + virtual void HandleKey(SDLKey key); + virtual void HandleMouseMove(uint32 x, uint32 y); + virtual void HandleMouseButton(uint32 x, uint32 y, bool mouseDown); + virtual void Draw(uint32 offsetX = 0, uint32 offsetY = 0); + void Add(MenuItems mi); + + protected: + bool clicked; + uint32 inside; + uint16 fgColor, bgColor, fgColorHL, bgColorHL; + int menuChosen, menuItemChosen; + + private: + vector itemList; +}; + +void Menu::HandleKey(SDLKey Key) +{ +} + +void Menu::HandleMouseMove(uint32 x, uint32 y) +{ + if (!Inside(x, y)) + inside = 0; + else + { + // Find out *where* we are inside the menu bar + uint32 xpos = extents.x; + + for(uint32 i=0; i= xpos && x < xpos + width) + { + inside = i + 1; + break; + } + + xpos += width; + } + } +} + +void Menu::HandleMouseButton(uint32 x, uint32 y, bool mouseDown) +{ + if (inside && mouseDown) + clicked = true, menuChosen = inside - 1; +// else + if (!inside && mouseDown) + clicked = false, menuChosen = -1; +} + +void Menu::Draw(uint32 offsetX/*= 0*/, uint32 offsetY/*= 0*/) +{ + uint32 xpos = extents.x + offsetX; + + for(uint32 i=0; i= menuXPos && (uint32)mouseX < menuXPos + (length + 2) * 8) +// && mouseY >= (extents.y + 9 + i * 8) && mouseY < (extents.y + 9 + (i + 1) * 8)) +// color1 = fgColorHL, color2 = bgColorHL, menuItemChosen = i; + + DrawStringOpaque(screenBuffer, itemList[menuChosen].extents.x, ypos, + color1, color2, " %-*.*s ", itemList[menuChosen].charLength, + itemList[menuChosen].charLength, itemList[menuChosen].item[i].name.c_str()); + ypos += 8; + } + } +} + +void Menu::Add(MenuItems mi) +{ + for(uint32 i=0; i mi.charLength) + mi.charLength = mi.item[i].name.length(); + + // Set extents here as well... + mi.extents.x = extents.x + extents.w, mi.extents.y = extents.y + 9; + mi.extents.w = (mi.charLength + 2) * 8, mi.extents.h = mi.item.size() * 8; + + itemList.push_back(mi); + extents.w += (mi.title.length() + 2) * 8; +} + +class RootWindow: public Window +{ + public: + RootWindow(Menu * m, Window * w = NULL): menu(m), window(w) {} +//Do we even need to care about this crap? +// { extents.x = extents.y = 0, extents.w = 320, extents.h = 240; } + virtual void HandleKey(SDLKey key) {} + virtual void HandleMouseMove(uint32 x, uint32 y) {} + virtual void HandleMouseButton(uint32 x, uint32 y, bool mouseDown) {} + virtual void Draw(uint32 offsetX = 0, uint32 offsetY = 0) {} + + private: + Menu * menu; + Window * window; + int16 * rootImage[1280 * 240 * 2]; +}; + + + +// +// GUI stuff--it's not crunchy, it's GUI! ;-) +// + void InitGUI(void) { SDL_ShowCursor(SDL_DISABLE); @@ -96,6 +412,394 @@ void DrawString(int16 * screen, uint32 x, uint32 y, bool invert, const char * te } } +// +// 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> 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= 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 length) + length = strlen(subMenu[menuChosen][i]); + + uint32 ypos = 9; + for(int i=0; i= 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); +} + // // Draw "picture" // Uses zero as transparent color @@ -124,6 +828,9 @@ void DrawTransparentBitmap(int16 * screen, uint32 x, uint32 y, uint16 * bitmap) // bool UserSelectFile(char * path, char * filename) { +//Testing... +//GUIMain(); + extern int16 * backbuffer; vector fileList;