23.01.2018 Views

MICROSOFT_PRESS_EBOOK_PROGRAMMING_WINDOWS_8_APPS_WITH_HTML_CSS_AND_JAVASCRIPT_PDF

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

auto stateResult = XInputGetState(m_index, &m_xinputState);<br />

if (stateResult == ERROR_SUCCESS)<br />

{<br />

m_isControllerConnected = true;<br />

controllerState.connected = true;<br />

controllerState.controllerId = m_index;<br />

controllerState.packetNumber = m_xinputState.dwPacketNumber;<br />

controllerState.LeftTrigger = m_xinputState.Gamepad.bLeftTrigger;<br />

controllerState.RightTrigger = m_xinputState.Gamepad.bRightTrigger;<br />

// And so on [copying all the other properties omitted.]<br />

}<br />

else<br />

{<br />

m_isControllerConnected = false;<br />

}<br />

}<br />

return controllerState;<br />

The constructor for a Controller object is also very simple:<br />

Controller::Controller(uint32 index)<br />

{<br />

m_index = index;<br />

m_lastEnumTime = ::GetTickCount64() - EnumerateTimeout;<br />

}<br />

In a JavaScript app—once the reference to the component has been added—the GameController<br />

namespace contains the component’s public API, and we can utilize it as if it were built right into<br />

Windows. In the case of the sample, it first instantiates a Controller object (with index of zero) and then<br />

kicks off animation frames (program.js):<br />

app.onactivated = function (eventObj) {<br />

if (eventObj.detail.kind ===<br />

Windows.ApplicationModel.Activation.ActivationKind.launch) {<br />

// [Other setup omitted]<br />

// Instantiate the Controller object from the WinRT component<br />

controller = new GameController.Controller(0);<br />

};<br />

};<br />

// Start rendering loop<br />

requestAnimationFrame(renderLoop);<br />

The renderLoop function then just calls the component’s getState method and applies the results to a canvas<br />

drawing before repeating the loop (also in program.js, though much code omitted):<br />

694

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!