Archived

This topic is now archived and is closed to further replies.

MarsuGL

DirectInput and my input class...

Recommended Posts

Hello, i have played around with nehe''s lesson about direct input7 But i have worked on a class (it''s just the beginning of the work ) i have made the choice of include the Buffer as a private member of my input class, and to test is a key is pressed i call a method that use GetDeviceState(...) and then to test eack key i call an other method that is looking the buffer to see if the key has been pressed and that works : if (myinput->IsPressed(DIK_ESCAPE)) // Is ESC Being Pressed? { TerminateApplication (g_window); // Terminate The Program } the question is : is the IsPressed() call a good way to do or is it slow ? because this function is called often, should i continue to use this ? the code for people which want it (unfinished): Dominique ____________________________________ header ------- #ifndef __INPUT_HEADER__ #define __INPUT_HEADER__ #include class Cinput { public: Cinput(); ~Cinput(); int DI_Init(HINSTANCE hInstance,HWND hWnd); void GrabState(); int IsPressed(int key); void DX_End(); private: LPDIRECTINPUT7 g_DI; LPDIRECTINPUTDEVICE7 g_KDIDev; BYTE buffer[256]; }; #endif -------------------------cpp file------------------------ #include "Cinput.h" Cinput::Cinput() { g_DI=0; g_KDIDev=0; } Cinput::~Cinput() { } int Cinput::DI_Init(HINSTANCE hInstance,HWND hWnd) { // Create Direct Input if ( DirectInputCreateEx(hInstance, // Window Instance DIRECTINPUT_VERSION, // Direct Input Version IID_IDirectInput7, // Version 7 (void**)&g_DI, // Direct Input NULL ) ) // NULL Parameter { return(false); // Couldn''t Initialize Direct Input } // Create The Keyboard Device if ( g_DI->CreateDeviceEx( GUID_SysKeyboard, // Define Which Device Tto Create (KeyBoard,Mouse,or Joystick) IID_IDirectInputDevice7, // Version 7 (void**)&g_KDIDev, // KeyBoard Device NULL ) ) // NULL Parameter { return(false); // Couldn''t Create The Keyboard Device } // Set The Keyboard Data Format if ( g_KDIDev->SetDataFormat(&c_dfDIKeyboard) ) { return(false); // Could Not Set The Data Format } // Set The Cooperative Level if ( g_KDIDev->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_EXCLUSIVE) ) { return(false); // Could Not Set The Cooperative Level } if (g_KDIDev) // Did We Create The Keyboard Device? g_KDIDev->Acquire(); // If So, Acquire It else // If Not return(false); // Return False return(true); // Everything Ok, Return True } void Cinput::GrabState() { HRESULT hr = g_KDIDev->GetDeviceState(sizeof(this->buffer), &this->buffer); } int Cinput::IsPressed(int key) { return this->buffer[key] & 0x80; } void Cinput::DX_End() { if (g_DI) { if (g_KDIDev) { g_KDIDev->Unacquire(); g_KDIDev->Release(); g_KDIDev = NULL; } g_DI->Release(); g_DI = NULL; } }

Share this post


Link to post
Share on other sites