+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;
+
+uint16 mousePic[] = {
+ 6, 8,
+
+ 0x03E0,0x0000,0x0000,0x0000,0x0000,0x0000, // +
+ 0x0300,0x03E0,0x0000,0x0000,0x0000,0x0000, // @+
+ 0x0300,0x03E0,0x03E0,0x0000,0x0000,0x0000, // @++
+ 0x0300,0x0300,0x03E0,0x03E0,0x0000,0x0000, // @@++
+ 0x0300,0x0300,0x03E0,0x03E0,0x03E0,0x0000, // @@+++
+ 0x0300,0x0300,0x0300,0x03E0,0x03E0,0x03E0, // @@@+++
+ 0x0300,0x0300,0x0300,0x0000,0x0000,0x0000, // @@@
+ 0x0300,0x0000,0x0000,0x0000,0x0000,0x0000 // @
+/*
+ 0xFFFF,0x0000,0x0000,0x0000,0x0000,0x0000, // +
+ 0xE318,0xFFFF,0x0000,0x0000,0x0000,0x0000, // @+
+ 0xE318,0xFFFF,0xFFFF,0x0000,0x0000,0x0000, // @++
+ 0xE318,0xE318,0xFFFF,0xFFFF,0x0000,0x0000, // @@++
+ 0xE318,0xE318,0xFFFF,0xFFFF,0xFFFF,0x0000, // @@+++
+ 0xE318,0xE318,0xE318,0xFFFF,0xFFFF,0xFFFF, // @@@+++
+ 0xE318,0xE318,0xE318,0x0000,0x0000,0x0000, // @@@
+ 0xE318,0x0000,0x0000,0x0000,0x0000,0x0000 // @
+*/
+};
+// 1 111 00 11 100 1 1100 -> F39C
+// 1 100 00 10 000 1 0000 -> C210
+// 1 110 00 11 000 1 1000 -> E318
+// 0 000 00 11 111 0 0000 -> 03E0
+// 0 000 00 11 000 0 0000 -> 0300
+
+uint16 closeBox[] = {
+ 7, 7,
+
+ 0x0000,0x4B5E,0x4B5E,0x4B5E,0x4B5E,0x4B5E,0x0000, // +++++
+ 0x4B5E,0xFFFF,0x0000,0x0000,0x0000,0xFFFF,0x0217, // +@ @.
+ 0x4B5E,0x0000,0xFFFF,0x0000,0xFFFF,0x0000,0x0217, // + @ @ .
+ 0x4B5E,0x0000,0x0000,0xFFFF,0x0000,0x0000,0x0217, // + @ .
+ 0x4B5E,0x0000,0xFFFF,0x0000,0xFFFF,0x0000,0x0217, // + @ @ .
+ 0x4B5E,0xFFFF,0x0000,0x0000,0x0000,0xFFFF,0x0217, // +@ @.
+ 0x0000,0x0217,0x0217,0x0217,0x0217,0x0217,0x0000 // .....
+};
+
+char separator[] = "--------------------------------------------------------";
+
+//
+// Local GUI classes
+//
+
+class Element
+{
+ public:
+ Element(uint32 x = 0, uint32 y = 0, uint32 w = 0, uint32 h = 0)
+ { extents.x = x, extents.y = y, extents.w = w, extents.h = h; }
+ 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;
+ uint32 state;
+ // 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): Element(x, y, w, h),
+ activated(false), clicked(false), inside(false), fgColor(0xFFFF),
+ bgColor(0x03E0), pic(NULL) {}
+ Button(uint32 x, uint32 y, uint32 w, uint32 h, uint16 * p): Element(x, y, w, h),
+ activated(false), clicked(false), inside(false), fgColor(0xFFFF),
+ bgColor(0x03E0), pic(p) {}
+ Button(uint32 x, uint32 y, uint32 w, uint32 h, string s): Element(x, y, w, h),
+ activated(false), clicked(false), inside(false), fgColor(0xFFFF),
+ bgColor(0x03E0), pic(NULL), text(s) {}
+ 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);
+ bool ButtonClicked(void) { return activated; }
+
+ protected:
+ bool activated, clicked, inside;
+ uint16 fgColor, bgColor;
+ uint16 * pic;
+ string text;
+};
+
+void Button::HandleMouseMove(uint32 x, uint32 y)
+{
+ inside = Inside(x, y);
+}
+
+void Button::HandleMouseButton(uint32 x, uint32 y, bool mouseDown)
+{
+ if (inside)
+ {
+ if (mouseDown)
+ clicked = true;
+
+ if (clicked && !mouseDown)
+ clicked = false, activated = true;
+ }
+ else
+ clicked = activated = false;
+}
+
+void Button::Draw(uint32 offsetX/*= 0*/, uint32 offsetY/*= 0*/)
+{
+ uint32 addr = (extents.x + offsetX) + ((extents.y + offsetY) * pitch);
+
+ for(uint32 y=0; y<extents.h; y++)
+ {
+ for(uint32 x=0; x<extents.w; x++)
+ {
+ // Doesn't clip in y axis! !!! FIX !!!
+ if (extents.x + x < pitch)
+ screenBuffer[addr + x + (y * pitch)]
+ = (clicked && inside ? fgColor : (inside ? 0x43F0 : bgColor));
+ }
+ }
+
+//WriteLog("Button::Draw [%08X]\n", this);
+ if (pic != NULL)
+//{
+//WriteLog("--> Button: About to draw pic [%08X].\n", pic);
+ DrawTransparentBitmap(screenBuffer, extents.x + offsetX, extents.y + offsetY, pic);
+//}
+
+ if (text.length() > 0)
+//{
+//WriteLog("--> Button: About to draw string [%s].\n", text.c_str());
+ DrawString(screenBuffer, extents.x + offsetX, extents.y + offsetY, false, "%s", text.c_str());
+//}
+}
+
+class Window: public Element
+{
+ public:
+ Window(uint32 x = 0, uint32 y = 0, uint32 w = 0, uint32 h = 0): Element(x, y, w, h),
+ /*clicked(false), inside(false),*/ fgColor(0x4FF0), bgColor(0xFE10),
+ close(w - 8, 1, 7, 7, closeBox) { list.push_back(&close); }
+ 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 AddElement(Element * e);
+ bool WindowActive(void) { return !close.ButtonClicked(); }
+
+ protected:
+// bool clicked, inside;
+ uint16 fgColor, bgColor;
+ Button close;
+//We have to use a list of Element *pointers* because we can't make a list that will hold
+//all the different object types in the same list...
+ vector<Element *> list;
+};
+
+void Window::HandleMouseMove(uint32 x, uint32 y)
+{
+ // Handle the items this window contains...
+ for(uint32 i=0; i<list.size(); i++)
+ // Make coords relative to upper right corner of this window...
+ list[i]->HandleMouseMove(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; i<list.size(); i++)
+ // Make coords relative to upper right corner of this window...
+ list[i]->HandleMouseButton(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; y<extents.h; y++)
+ {
+ for(uint32 x=0; x<extents.w; x++)
+ {
+ // Doesn't clip in y axis! !!! FIX !!!
+ if (extents.x + x < pitch)
+ screenBuffer[addr + x + (y * pitch)] = bgColor;
+ }
+ }
+
+ // Handle the items this window contains...
+ for(uint32 i=0; i<list.size(); i++)
+ list[i]->Draw(extents.x, extents.y);
+}
+
+void Window::AddElement(Element * e)
+{
+ list.push_back(e);
+}
+
+class Text: public Element
+{
+ public:
+ Text(uint32 x = 0, uint32 y = 0, uint32 w = 0, uint32 h = 0): Element(x, y, w, h),
+ fgColor(0x4FF0), bgColor(0xFE10) {}
+ Text(uint32 x, uint32 y, string s): Element(x, y, 0, 0),
+ fgColor(0x4FF0), bgColor(0xFE10), text(s) {}
+ 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:
+ uint16 fgColor, bgColor;
+ string text;
+};
+
+void Text::Draw(uint32 offsetX/*= 0*/, uint32 offsetY/*= 0*/)
+{
+ if (text.length() > 0)
+ DrawString(screenBuffer, extents.x + offsetX, extents.y + offsetY, false, "%s", text.c_str());
+}
+
+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) {}
+ bool 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); }
+
+ string title;
+ vector<NameAction> 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): Element(x, y, w, h), clicked(false), inside(0),
+ insidePopup(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);
+ //This is wrong. !!! FIX !!!
+ bool ItemChosen(void) { return (clicked && insidePopup); }
+ //This is bad... !!! FIX !!!
+ NameAction & GetItem(void)
+// { if (ItemChosen()) return itemList[menuChosen].item[menuItemChosen]; return NULL; }
+ { return itemList[menuChosen].item[menuItemChosen]; }
+
+ protected:
+ bool clicked;
+ uint32 inside, insidePopup;
+ uint16 fgColor, bgColor, fgColorHL, bgColorHL;
+ int menuChosen, menuItemChosen;
+
+ private:
+ vector<MenuItems> itemList;
+};
+
+void Menu::HandleKey(SDLKey Key)
+{
+}
+
+void Menu::HandleMouseMove(uint32 x, uint32 y)
+{
+ inside = insidePopup = 0;
+
+ if (Inside(x, y))
+ {
+ // Find out *where* we are inside the menu bar
+ uint32 xpos = extents.x;
+
+ for(uint32 i=0; i<itemList.size(); i++)
+ {
+ uint32 width = (itemList[i].title.length() + 2) * 8;
+
+ if (x >= xpos && x < xpos + width)
+ {
+ inside = i + 1;
+ menuChosen = i;
+ break;
+ }
+
+ xpos += width;
+ }
+ }
+
+ if (!Inside(x, y) && !clicked)
+ {
+ menuChosen = -1;
+ }
+
+ if (itemList[menuChosen].Inside(x, y) && clicked)
+ {
+ insidePopup = ((y - itemList[menuChosen].extents.y) / 8) + 1;
+ menuItemChosen = insidePopup - 1;
+ }
+}
+
+void Menu::HandleMouseButton(uint32 x, uint32 y, bool mouseDown)
+{
+ if (!clicked)
+ {
+ if (mouseDown)
+ {
+ if (inside)
+ clicked = true;
+ else
+ menuChosen = -1; // clicked is already false...!
+ }
+ }
+ else // clicked == true
+ {
+ if (insidePopup && !mouseDown) // I.e., mouse-button-up
+ {
+ if (itemList[menuChosen].item[menuItemChosen].action != NULL)
+ {
+ itemList[menuChosen].item[menuItemChosen].action();
+
+ clicked = false, menuChosen = menuItemChosen = -1;
+
+ SDL_Event event;
+ 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...!
+ }
+ }
+
+ if (!inside && !insidePopup && mouseDown)
+ clicked = false, menuChosen = menuItemChosen = -1;
+ }
+}
+
+void Menu::Draw(uint32 offsetX/*= 0*/, uint32 offsetY/*= 0*/)
+{
+ uint32 xpos = extents.x + offsetX;
+
+ for(uint32 i=0; i<itemList.size(); i++)
+ {
+ uint16 color1 = fgColor, color2 = bgColor;
+ if (inside == (i + 1) || (menuChosen != -1 && (uint32)menuChosen == i))
+ color1 = fgColorHL, color2 = bgColorHL;
+
+ DrawStringOpaque(screenBuffer, xpos, extents.y + offsetY, color1, color2,
+ " %s ", itemList[i].title.c_str());
+ xpos += (itemList[i].title.length() + 2) * 8;
+ }
+
+ // Draw sub menu (but only if active)
+ if (clicked)
+ {
+ uint32 ypos = extents.y + 9;
+
+ for(uint32 i=0; i<itemList[menuChosen].item.size(); i++)
+ {
+ uint16 color1 = fgColor, color2 = bgColor;
+
+ if (insidePopup == i + 1)
+ color1 = fgColorHL, color2 = bgColorHL, menuItemChosen = i;
+
+ if (itemList[menuChosen].item[i].name.length() > 0)
+ DrawStringOpaque(screenBuffer, itemList[menuChosen].extents.x, ypos,
+ color1, color2, " %-*.*s ", itemList[menuChosen].charLength,
+ itemList[menuChosen].charLength, itemList[menuChosen].item[i].name.c_str());
+ else
+ DrawStringOpaque(screenBuffer, itemList[menuChosen].extents.x, ypos,
+ fgColor, bgColor, "%.*s", itemList[menuChosen].charLength + 2, separator);
+
+ ypos += 8;
+ }
+ }
+}
+
+void Menu::Add(MenuItems mi)
+{
+ for(uint32 i=0; i<mi.item.size(); i++)
+ if (mi.item[i].name.length() > 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;
+}
+
+//Do we even *need* this?
+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! ;-)
+//
+