Code Structure For Menu/UI

Hi guys.

Just wondering if someone can advise on how to structure something like Game Menu. I was googling and did not find anything useful. I am coding a little game and Menu part with few sections like START GAME - SELECT LEVEL - SETTINGS (SOUND, MUSIC AND VIBRATION ON/OF) - CREDITS - EXIT.

And just menu alone seems to have tones of code. I have my state machine working but it seems every subsection of the menu has its own state sort if and duplicate code.

Any tips or links to material how to organize code like that would be highly appreciated.

Thanks.
Juris.
Could you post your general structure? You should only need a single state machine for the entire menu tree.

I've found that coroutines are very useful when modelling anything UI-related. In particular, they obviate the need to explicitly use state machines, making the code much, much more readable and maintainable. You should take a look at boost::coroutine.
I have basic state machine like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "RT_Renderer.h"
#include "RT_Sprite.h"
#include "RT_Texture.h"
#include "RT_Engine.h"

#ifndef GAMESTATE_H
#define GAMESTATE_H

namespace RTEngineFramework {
	class RTEngineState {
	public:		
		virtual void update() = 0;
		virtual void render() = 0;
		virtual bool onEnter() = 0;
		virtual bool onExit() = 0;
		virtual string getStateID() const = 0;		
	};

	class StateMachine {
	public:
		void pushState( RTEngineState * pState );
		void changeState( RTEngineState * pState );
		void popState();
		void Update();
		void Render();
		string getStateID();
		string getStateStack();
	private:
		vector<RTEngineState *> EngineState;
	};
};

extern RTEngineFramework::StateMachine * RTStateMachine;

#endif 


It does the job perfectly. I have Default state (Loading Engine Logo and stuff), Logo State, (Loading Game Logos, Videos, etc.), Menu State (All menu elements), Game State.

Here is my Menu image: http://goo.gl/pXxzZs

So the Menu state is what I am working on right now. So I have State for each Button in the menu right now and it looks like a lot of repeating code like SWITCH statements, position of the Menu (which button is selected), etc. And this is done for Every SubMenu.

So just looking for a better way. Don't want to use Boost at the moment.
Topic archived. No new replies allowed.