[Source]Simple menu class

Posts 1–15 of 45 · Page 1 of 3
[Source]Simple menu class
Uhg, not much to say, I made a simple menu class this morning for myself to use since I don't like everyone else's.

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.

[highlight=cpp]
void MainThread()
{
while(true)
{
MenuInstance->InitControls();
}
}

....

CreateThread(0,0,(LPTHREAD_START_ROUTINE)MainThrea d,0,0,0);
[/highlight]

That's the only seemingly confusing part. Use the menu item's index to get it's current status ( on/off ). When your module detaches I made a function to release the classes private members so you don't get leaks.

Here we go.

Menu.h:
[highlight=cpp]
#ifndef MENU_H
#define 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
[/highlight]

Menu.cpp
[highlight=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;
}
[/highlight]

Also.. I made a small example just in case you guys can't figure out how to use it.. The whole project is the attached file below.
Menu class.rarattachment deleted · 269 downloads before removal
YOU SUCK. YOU SUCK. first post. love you david
Looks gewd. Won't use however because im still learning C++
Ohai there Patrick. The thanks button is waiting to be pressed. |:
wow... Attachments Pending Approval? fail... C++ section now needs approval

Nice david ;d anyway i wouldn't expect a different result from ya =D unfortunately i don't know much (anything) about D3D to comment that much.
Oyaa credits to Brinuz for his linked lists tutorial.
Quote Originally Posted by Void View Post
Oyaa credits to Brinuz for his linked lists tutorial.
=o im surprised that someone used it lulz
I'm the only one who thanked it I think. |:
Quote Originally Posted by Melodia View Post
Cute ~
Why thank you Mel. /
Quote Originally Posted by Melodia View Post
Cute ~
Agree
Not Gellins base. But VOID'S base
It's just a class..
Quote Originally Posted by Void View Post
It's just a class..
Hey Void, does unlocking the cursor mid-game (eg, while you're in game killing people / re-spawning) allow mouse co-ordinates other than center to be grabbed?

(I don't have the game, so I can't find out - I have 1GB internet per month, and Combat Arms is well... rather large)
Posts 1–15 of 45 · Page 1 of 3

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us