-
SDL Game Menu
So I'm using SDL to make a game. I am starting with the game menu first. For example, it will have "New Game", "Load Game", "Exit", etc. I want to use the mouse to pick the choice, I don't want to use the arrow keys or anything like that. So would I just have to keep track of where the mouse is, and if it equals the area of the text, then it will execute whatever action, correct? Would I have to make sometype of boundary around the text to detect a mouse event?
-
Yes. Basically compare where the mouse is on the screen in respect to your text or bitmaps. I used this cool little effect in allegro which is similar to SDL where the bitmap would change on a rollover. I will see if I can dig up that code.
-
Yes. If you know you'll need more menus such as in game and options menus then it might be best to create a GUI managment system. make each button a class which stores it's co-ordinates. When a mouse event happens such as a move or click send it to the GUI system which intereates through all it's items and determins which one the mouse co-ordinates are inside.
-
-
You can either return a value from the menu indicating the selection choice and respond based on the value; function pointers; class message systems; or any method of your choice to get the job done.
As long as it works and leads the user to the correct area of your application.....who cares how it works, just that it does.
Technically in Windows every control or item is a window in itself and thus has all the functionality of a Window or it can have. Meaning that each can have it's own WinProc, etc, etc. You don't have to get this complicated.
Might I suggest a framework like this:
Code:
struct MenuItemStruc
{
std::string lpszText;
float x,y;
float left,top,right,bottom;
float width,height;
DWORD dwID;
};
class CMenu;
class CMenuItem
{
MenuItemStruc m_MenuItemProps;
//Possible pointers to previous and next menus
//Enables menu items to lead to pop-ups and/or can
//implement a PREV/NEXT setup
CMenu *m_pNextMenu;
CMenu *m_pPrevMenu;
//Selection ID of menu item
//Returned when this menu item has been selected
bool m_bHasFrame; //If frame is visible this is true
public:
...
...
bool Create(float x,float y,std::string lpszText,bool bHasFrame=false);
void SetText(std::string lpszText);
...
};
class CMenu
{
std::vector<CMenuItem *> m_vMenuItems;
....
....
public:
...
...
bool AddItem(CMenuItem *pItem);
bool AddItem(MenuItemStruc item);
...
...
};
That should get you started.
-
Hmm...Seems tougher than I thought. I might just go with what prog said for now, since this is my first game. Than i'll make the choice of making a GUI after this first game, and use that for all my games. What would be the best method for checking if the mouse is on text? Should I use a simple "if statement" or something more complex?
-
Do a simple bounding box check
Code:
if ((MouseX>=TextX && MouseX<=TextX+TextWidth) && (MouseY>=TextY && MouseY<=TextY+TextHeight))
-
Ok, thats what I thought. Thanks!!!!!!!!!!!!!!