VGM.™ReggaeGasspol™ CO_ADMINISTRATOR
Jumlah posting : 44 Points : 106 Join date : 07.07.12 Age : 28 Lokasi : Karawang,Jawa Barat
| Subyek: [C++] Simple menu class Sat Jul 14, 2012 7:26 pm | |
| Nama fungsi itu berbicara untuk diri mereka sendiri, kecuali untuk InitControls. Untuk memulai hotkeys untuk menu, hanya menempatkan InitControls dalam satu kelompok dan membuat thread terpisah untuk itu. - Code:
-
void MainThread() { while(true) { MenuInstance->InitControls(); } } .... CreateThread(0,0,(LPTHREAD_START_ROUTINE)MainThrea d,0,0,0);
Itu hanya tampak bagian yang membingungkan. Gunakan indeks item menu untuk mendapatkan status (on / off). Menu.h - Code:
-
#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 - Code:
-
#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; } | |
|