How to build a flexible Game Menu..

This is a discussion on How to build a flexible Game Menu.. within the Game Programming forums, part of the General Programming Boards category; Hello guys! It's been a while that I have been programming with SDL, creating games and such, and all that ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    66

    How to build a flexible Game Menu..

    Hello guys! It's been a while that I have been programming with SDL, creating games and such, and all that time, I've been thinking how to create a flexible game menu, that is consistent, and has sub menu's by just passing a set of data that contains the menu items.

    What I've created so far is a class, that accepts an array of size n in its constructor, calculate the text positions, and via polymorphism handle the states of the menu.
    This is quite flexible, but not if I wanted to implement sub menus.

    Here's the prototype of my menu class, which is a based on a state class.

    Code:
    class SDL_GameState
    {
    	public :
    		SDL_GameState() : KeyStates(SDL_GetKeyState(NULL)) {}
    
    		virtual bool LoadResources(const char *fileName);
    
    		virtual bool OnInit() = 0;
    		virtual void OnCleanUp() = 0;
    		
    		virtual void OnPause();
    		virtual void OnResume();
    		
    		virtual void OnKeyDown(SDL_KeyboardEvent&, SDL_Engine&);
    		virtual void OnKeyUp(SDL_KeyboardEvent&, SDL_Engine&);
    		virtual void OnMouseMove(SDL_MouseMotionEvent&, SDL_Engine&);
    		virtual void OnMouseButtonUp(SDL_MouseButtonEvent&, SDL_Engine&);
    		virtual void OnMouseButtonDown(SDL_MouseButtonEvent&, SDL_Engine&);
    		virtual void OnUserEvent(SDL_UserEvent&, SDL_Engine&);
    		virtual void HandleKeys();
    
    		virtual void OnLoop();
    		virtual void OnRender(SDL_Surface*);
    
    		virtual ~SDL_GameState();
    	
    	protected:	
    		Uint8 *KeyStates;
    		SDL_ResourceManager<SDL_Resource_Surface>  Surf_Man;
    		SDL_ResourceManager<SDL_Resource_MixChunk> Sound_Man;
    		SDL_ResourceManager<SDL_Resource_MixMusic> Music_Man;
    		SDL_ResourceManager<SDL_Resource_TTF>      TTF_Man;
    		SDL_ResourceManager<SDL_Resource_Button>   BTN_Man;
    };
    
    class cGameMenu : public SDL_GameState {
    	protected:
    		std::vector<MenuItem> MenuList;
    		const char **MenuItems;
    		int MenuCount;
    		int Selection;
    	public:
    		cGameMenu(const char*[], int);
    		bool OnInit();
    		virtual void OnKeyDown(SDL_KeyboardEvent&, SDL_Engine&);
    		void OnRender(SDL_Surface*);
    		void OnCleanUp();
    		~cGameMenu();
    };
    The menu class constructor is passed an array of string like this.

    Code:
    const char *InGameMenu[] = {"Resume", "Exit"};
    
    cGameMenu newMenu(InGameMenu, 2);
    And here's an example of OnKeyUp function.

    Code:
    void cInGameMenu::OnKeyDown(SDL_KeyboardEvent& KEvent, SDL_Engine& SEngine) {
    	switch(KEvent.keysym.sym) {
    		case SDLK_UP:
    			Selection = (Selection == 0? MenuList.size() - 1 : Selection - 1);
    			break;
    		case SDLK_DOWN:
    			Selection = (Selection == MenuList.size() - 1? 0 : Selection + 1);
    			break;
    		case SDLK_RETURN:
    			if(MenuList[Selection].MenuLabel == "Resume") {
    				SEngine.PopState();
    			}
    			else if(MenuList[Selection].MenuLabel == "Exit") {
    				SEngine.Quit();
    			}
    			break;
    		default:
    			break;
    	}
    }
    Last edited by ThLstN; 12-12-2008 at 07:02 PM.

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Dont you think that you should separate this into a keyboard class and a menu class? I don't think a menu cares what key is pressed just which item is selected. A keyboard doesn't care what menu item is selected just what key has been pressed.

    Before you look at the implementation of your menu class you should look at the behavior first. What does it need to do instead of how it does the job? Once you define what functionality it needs then you can structure your class accordingly and then slowly evolve your design.
    Last edited by VirtualAce; 12-13-2008 at 10:14 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. New Project, text game, design stage.
    By Shamino in forum Game Programming
    Replies: 9
    Last Post: 05-23-2007, 06:39 AM
  3. Game Engine Link Prob
    By swgh in forum Game Programming
    Replies: 2
    Last Post: 01-25-2006, 11:14 PM
  4. Game Designer vs Game Programmer
    By the dead tree in forum Game Programming
    Replies: 8
    Last Post: 04-28-2005, 09:17 PM
  5. u got me wrong fellas
    By clover in forum Game Programming
    Replies: 4
    Last Post: 10-18-2003, 04:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21