Ugly, can this be done easier?

Started by
6 comments, last by cozzie 9 years, 1 month ago

Hi all,

Just of curiosity, how would you achieve the same goal as I do with the code below?

It feels a bit 'clumsy', but I didn't find a way to do this easier/ less code repetition.


		D3DCOLOR selectedCol = D3DCOLOR_XRGB(255, 255, 0);
		D3DCOLOR defCol = D3DCOLOR_XRGB(255, 255, 255);
		D3DCOLOR tempCol;

		switch(mCurrentMenu)
		{
			case MENU_MAIN:
			{
				// ITEMS
				if(mGameStarted)
				{
					if(mCurrentMenuItem == 0) tempCol = selectedCol; else tempCol = defCol;
					mD3d.mD3dFont.PrintLarge("Resume game", startMenuX, startMenuY + 1 * offsetMenuItem, tempCol);	// 0
				}

				if(mCurrentMenuItem == 1) tempCol = selectedCol; else tempCol = defCol;
				mD3d.mD3dFont.PrintLarge("New game", startMenuX, startMenuY + 2 * offsetMenuItem, tempCol);			// 1
				if(mCurrentMenuItem == 2) tempCol = selectedCol; else tempCol = defCol;
				mD3d.mD3dFont.PrintLarge("Options", startMenuX, startMenuY + 3 * offsetMenuItem, tempCol);			// 2
				if(mCurrentMenuItem == 3) tempCol = selectedCol; else tempCol = defCol;
				mD3d.mD3dFont.PrintLarge("Leaderboard", startMenuX, startMenuY + 4 * offsetMenuItem, tempCol);		// 3
				if(mCurrentMenuItem == 4) tempCol = selectedCol; else tempCol = defCol;
				mD3d.mD3dFont.PrintLarge("Quit game", startMenuX, startMenuY + 5 * offsetMenuItem, tempCol);		// 4

				break;
			}
		}

Note; don't mind the switch has just one case for now, will be more later on

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

Advertisement
You could get rid of the if(mCurrentMenuItem == x) stuff by using ternary operators like this:
mD3d.mD3dFont.PrintLarge("Some text", startMenuX, startMenuY + x * offsetMenuItem, mCurrentMenuItem == x ? selectedCol : defCol);
And you can also reduce the amount of code by adding all your string into a list and then iterate over it, so you only need the print code once.

To demonstrate, something like


static const D3DCOLOR selectedCol = D3DCOLOR_XRGB(255, 255, 0);
static const D3DCOLOR defCol = D3DCOLOR_XRGB(255, 255, 255);

switch(mCurrentMenu)
{
	case MENU_MAIN:
	{
		static const char* const items[]
			= {
				"Resume game",
				"New game",
				"Options",
				"Leaderboard",
				"Quit game",
				nullptr
			};
		uint idxItem = mGameStarted ? 0 : 1;
		while (items[idxItem]!=nullptr) {
			mD3d.mD3dFont.PrintLarge(items[idxItem], startMenuX, startMenuY + idxItem * offsetMenuItem, mCurrentMenuItem==idxItem ? selectedCol : defCol);
                        idxItem++;
		}
	} break;
}

Because I don't know what type the first parameter of PrintLarge actually expects, I've used (undesired) char* here for simplicity.

Great, thanks.
Much cleaner this way, with both suggestions applied

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

As a further enhancement you should consider making the menus data driven. e.g. the position and text of each entry should be within a configuration file which can be adjusted without a recompile. This usually makes for more readable and reusable code.

As a further enhancement you should consider making the menus data driven. e.g. the position and text of each entry should be within a configuration file which can be adjusted without a recompile. This usually makes for more readable and reusable code.

class Menu
{
static const char* items[NUM_MENU_ITEMS];
// ..

public:
const char* ItemString (const int nID);
};

thanks, will think about that too, might be useful especially if/ when I have more menu's

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

Much better:


				for(size_t id=startFrom;id<mMainMenu.GetNrItems();++id)
				{
					D3DCOLOR myCol = D3DCOLOR_XRGB(mMainMenu.GetColor(id).r, mMainMenu.GetColor(id).g, mMainMenu.GetColor(id).b);
					mD3d.mD3dFont.PrintLarge(mMainMenu.GetItemText(id).c_str(), startMenuX, startMenuY + (id+1) * offsetMenuItem, myCol);
				}

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

This topic is closed to new replies.

Advertisement