Reputation: 11872
Ello All,
I'm pretty new to c++ and I've been trying to get this to work for longer than I care to admit. So I've gone off of the following refs and gotten the controller to work in a console app.
Here is the result xbox360Controller.h xbox360Controller.cpp
From there I'm trying to get it to work with cocos2d-x using steve tranby's post at the bottom of this thread (he adds to the )and adapting that to the 360 gamepad.
While I've gotten the keypad events to work (it was standard windows input so not too bad)
LRESULT CCEGLView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
BOOL bProcessed = FALSE;
CCLog("Message sent = %d",message);
//note* only showing relavant sections of code for brevity
switch (message)
{
case WM_KEYDOWN:
if (wParam == VK_F1 || wParam == VK_F2)
{
CCDirector* pDirector = CCDirector::sharedDirector();
if (GetKeyState(VK_LSHIFT) getKeypadDispatcher()->dispatchKeypadMSG(wParam == VK_F1 ? kTypeBackClicked : kTypeMenuClicked);
}
}
else if (wParam == VK_ESCAPE)
{
CCDirector::sharedDirector()->getKeypadDispatcher()->dispatchKeypadMSG(kTypeBackClicked);
}
else
{
CCDirector::sharedDirector()->getKeypadDispatcher()->dispatchKeypadDown(wParam);
}
if ( m_lpfnAccelerometerKeyHook!=NULL )
{
(*m_lpfnAccelerometerKeyHook)( message,wParam,lParam );
}
break;
default:
if (m_wndproc)
{
m_wndproc(message, wParam, lParam, &bProcessed);
if (bProcessed) break;
}
return DefWindowProc(m_hWnd, message, wParam, lParam);
}
if (m_wndproc && !bProcessed)
{
m_wndproc(message, wParam, lParam, &bProcessed);
}
return 0;
}
I can't figure out where to put the controller logic. I've tried in the WindowProc method and realized that didnt work as it only fires as a callback to WindowProc events(I probably don't have the correct lingo for it, sorry) The closest I've gotten it to something that fires often enough to check is at the point
static LRESULT CALLBACK _WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
CCDirector* pDirector = CCDirector::sharedDirector();
XboxController* player1 = new XboxController(GamePadIndex_One);
if(player1->IsConnected())
{
player1->Update();
for(int i =0;iState._buttons[i]==true)
{
//CCApplication::sharedApplication()->getKe
pDirector->getKeypadDispatcher()->dispatchKeypadDown(i);
}
}
}
delete player1;
if (s_pMainWindow && s_pMainWindow->getHWnd() == hWnd)
{
return s_pMainWindow->WindowProc(uMsg, wParam, lParam);
}
else
{
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
}
The full code is here
and the source is at github Here.
Anyone know the right place to put the XboxController instance in order for it to respond to KeypadDown
properly?
Upvotes: 0
Views: 944
Reputation: 499
First of, WindowProc is a callback function that is called after there's an message sent to the window from Windows.
So, if there's no message, this function would not get called.
XInput api is not message-based api, it does not generate messages. It requires the app to read its state as frequent as possible. Usually it's read in every game loop, just before the game logic is processed. Alternatively, you could have a separated thread to poll the state every 33ms or so.
I'd to recommend you to take a look at how XInput work, the concept of Windows programming, and the basic concept of game engine architecture.
Upvotes: 1