The function's name speak for themselves, except for InitControls. To initiate the hotkeys for the menu, just put InitControls in a loop and create a seperate thread for it.
void MainThread()
{
while(true)
{
MenuInstance->InitControls();
}
}
....
CreateThread(0,0,(LPTHREAD_START_ROUTINE)MainThrea d,0,0,0);
That's the only seemingly confusing part. Use the menu item's index to get it's current status ( on/off ).
Menu.h:
#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>
struct MenuItem {
int index;
char* text;
bool status;
MenuItem* pNext;
};
class cMenu {
private:
IDirect3DDevice9* m_pDevice;
ID3DXFont* m_pFont;
MenuItem* MenuItems;
int CurrSelected;
int m_nItems;
public:
cMenu(IDirect3DDevice9*,ID3DXFont*);
void Update(IDirect3DDevice9*,ID3DXFont*);
void AddItem(char*,int);
void RenderMenu(void);
void InitControls(void);
void Release(void);
bool GetStatus(int);
};
#endif
Menu.cpp
#include "Menu.h"
cMenu::cMenu(IDirect3DDevice9 *pDevice, ID3DXFont *pFont)
{
cMenu::m_pDevice = pDevice;
cMenu::m_pFont = pFont;
cMenu::MenuItems = NULL;
cMenu::CurrSelected = 1;
cMenu::m_nItems = 0;
}
void cMenu::Update(IDirect3DDevice9 *pDevice, ID3DXFont *pFont)
{
cMenu::m_pDevice = pDevice;
cMenu::m_pFont = pFont;
}
void cMenu::AddItem(char *text, int index)
{
MenuItem* Ptr = new MenuItem;
MenuItem* Before = new MenuItem;
MenuItem* New = new MenuItem;
New->index = index;
New->text = text;
New->status = false;
New->pNext = NULL;
if(cMenu::MenuItems == NULL)
{
cMenu::MenuItems = New;
cMenu::MenuItems->pNext = NULL;
} else {
Ptr = cMenu::MenuItems;
while(Ptr->pNext != NULL)
{
Ptr = Ptr->pNext;
}
Ptr->pNext = New;
}
cMenu::m_nItems++;
}
void cMenu::RenderMenu()
{
MenuItem* Ptr = new MenuItem;
MenuItem* Before = new MenuItem;
MenuItem* New = new MenuItem;
RECT rect;
rect.top = 20;
rect.left = 20;
rect.right = 150;
rect.bottom = 20;
Ptr = cMenu::MenuItems;
do
{
if(!(Ptr->index == CurrSelected) && !Ptr->status)
{
cMenu::m_pFont->DrawText(0,Ptr->text,-1,&rect,DT_LEFT|DT_NOCLIP,D3DCOLOR_XRGB(255,0,0)) ;
cMenu::m_pFont->DrawText(0,"Off",-1,&rect,DT_RIGHT|DT_NOCLIP,D3DCOLOR_XRGB(255,0,0)) ;
}
if(Ptr->index == CurrSelected && !Ptr->status)
{
cMenu::m_pFont->DrawText(0,Ptr->text,-1,&rect,DT_LEFT|DT_NOCLIP,D3DCOLOR_XRGB(0,255,0)) ;
cMenu::m_pFont->DrawText(0,"Off",-1,&rect,DT_RIGHT|DT_NOCLIP,D3DCOLOR_XRGB(0,255,0)) ;
}
if(!(Ptr->index == CurrSelected) && Ptr->status)
{
cMenu::m_pFont->DrawText(0,Ptr->text,-1,&rect,DT_LEFT|DT_NOCLIP,D3DCOLOR_XRGB(255,0,0)) ;
cMenu::m_pFont->DrawText(0,"On",-1,&rect,DT_RIGHT|DT_NOCLIP,D3DCOLOR_XRGB(255,0,0)) ;
}
if(Ptr->index == CurrSelected && Ptr->status)
{
cMenu::m_pFont->DrawText(0,Ptr->text,-1,&rect,DT_LEFT|DT_NOCLIP,D3DCOLOR_XRGB(0,255,0)) ;
cMenu::m_pFont->DrawText(0,"On",-1,&rect,DT_RIGHT|DT_NOCLIP,D3DCOLOR_XRGB(0,255,0)) ;
}
rect.top += 17;
Ptr = Ptr->pNext;
}while(Ptr != NULL);
}
void cMenu::InitControls()
{
MenuItem* Ptr = new MenuItem;
if(GetAsyncKeyState(VK_DOWN))
{
if(CurrSelected != cMenu::m_nItems)
CurrSelected += 1;
Sleep(100);
}
if(GetAsyncKeyState(VK_UP))
{
if(CurrSelected != 1)
CurrSelected -= 1;
Sleep(100);
}
if(GetAsyncKeyState(VK_RIGHT))
{
Ptr = cMenu::MenuItems;
while(Ptr != NULL)
{
if(Ptr->index == CurrSelected)
{
break;
}
Ptr = Ptr->pNext;
}
Ptr->status = !Ptr->status;
Sleep(100);
}
if(GetAsyncKeyState(VK_LEFT))
{
Ptr = cMenu::MenuItems;
while(Ptr != NULL)
{
if(Ptr->index == CurrSelected)
{
break;
}
Ptr = Ptr->pNext;
}
Ptr->status = !Ptr->status;
Sleep(100);
}
}
void cMenu::Release()
{
cMenu::m_pDevice->Release();
cMenu::m_pFont->Release();
}
bool cMenu::GetStatus(int index)
{
MenuItem* Ptr = new MenuItem;
Ptr = cMenu::MenuItems;
while(Ptr != NULL)
{
if(Ptr->index == index)
{
break;
}
Ptr = Ptr->pNext;
}
return Ptr->status;
}
The whole project is the attached file below.
by Void
Please register or login to download attachments.