Creating a menu system

Started by
2 comments, last by dentrum 21 years, 4 months ago
Hello! I''m thinking of creating a menu system for my future games but I can''t figure out how to do it so it''s easy to implement in my games. I have thought of a class, CMenu, that will hold the title position and other things about the menu itself. Then I thought there would be a vector in that class with another class that i call CMenuItem. That class would represent all the different items that could be on a menu, a button, a dropdown or a checkbox. I don''t know if this is a good way to do it and I don''t know how to get game to respond to the users choices. For example, if he presses a button, how do I know which button he pressed and get the game to call the right function.
Advertisement

try to think somethign like this:

while(game still running)
{
draw all menu stuff thats visable

if(user hit a mouse button)
{
get the coordinates where the mouse is
for(each thing that can be clicked on)
{
check if the coordinates are on that thing where
it is on the screen. if it is then call the appropriate
functins. if you find a hit then get out of the loop
as soon as the function has been called to save checking
for somthign that couldnt have been clicked on
}
}
}

this is like a basic any GUI thing. keep the functions you call
short and fast to make the whole thing responsive (ever click on somehting in a windoes app and have the app freeze for 5 mintues?).

how you structure your classes is up to you, but i would recomend keeping it simple like this. one class is just a list of components. anotehr class is a component, and this class is subclassed by the diffeent components themselves. you can then go through the list and do to each componnet what you need to no matter what it is, but still have it behave differently according to what it it (GUIs are one place where OOP really shines).

now the next problem your gonna run into is how to put this into a game. one good way to do this is to think of your game like a state machine (look it up on google if youve never heard of it). one state could be show menus, the other could be game is playing, the other game is paused etc...

unless of course you wanted to know how to do menus in a win32 app not draw them yourself, in which case this is of very little use to you.

[edited by - antistuff on December 2, 2002 2:44:09 AM]
Here''s the first of an excellent series of articles on implementing a GUI.
quote:
for(each thing that can be clicked on)
{
check if the coordinates are on that thing where
it is on the screen. if it is then call the appropriate
functins. if you find a hit then get out of the loop
as soon as the function has been called to save checking
for somthign that couldnt have been clicked on
}


To get the clicked item you don't need to test every item,
you just have to do the following:

    int ItemHeight = x; //Put here the height of your menu itemsPOINT MouseCoords;  //Fill it with the mouse coords. relative to the top left corner of the sub-menuint ItemSelected = MouseCoords.y/ItemHeight;   


Hope it helps

Kamikaze

[edited by - Kamikaze15 on December 2, 2002 6:27:15 PM]

This topic is closed to new replies.

Advertisement