source: trunk/src/platform/xbox/xboxinput.cpp @ 1394

Last change on this file since 1394 was 1394, checked in by sam, 8 years ago

input: support up to 16 buttons in Xbox-like pads.

File size: 2.3 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#if defined _XBOX
16#   include <xtl.h>
17#   include <xbdm.h>
18#endif
19
20#include "core.h"
21#include "xboxinput.h"
22
23namespace lol
24{
25
26/*
27 * Xbox Input implementation class
28 */
29
30class XboxInputData
31{
32    friend class XboxInput;
33
34private:
35#if defined _XBOX
36    Array<int, Stick *> m_joysticks;
37#endif
38};
39
40/*
41 * Public XboxInput class
42 */
43
44XboxInput::XboxInput()
45  : m_data(new XboxInputData())
46{
47#if defined _XBOX
48    for (int i = 0; i < XUSER_MAX_COUNT; i++)
49    {
50        XINPUT_STATE state;
51        if (XInputGetState(i, &state) != ERROR_SUCCESS)
52            continue;
53
54        Stick *stick = Input::CreateStick();
55        stick->SetAxisCount(4);
56        stick->SetButtonCount(16);
57        m_data->m_joysticks.Push(i, stick);
58    }
59#endif
60
61    m_gamegroup = GAMEGROUP_BEFORE;
62}
63
64XboxInput::~XboxInput()
65{
66#if defined _XBOX
67    /* Unregister all the joysticks we added */
68    while (m_data->m_joysticks.Count())
69    {
70        Input::DestroyStick(m_data->m_joysticks[0].m2);
71        m_data->m_joysticks.Remove(0);
72    }
73#endif
74    delete m_data;
75}
76
77void XboxInput::TickGame(float seconds)
78{
79    Entity::TickGame(seconds);
80}
81
82void XboxInput::TickDraw(float seconds)
83{
84    Entity::TickDraw(seconds);
85
86#if defined _XBOX
87    for (int i = 0; i < m_data->m_joysticks.Count(); i++)
88    {
89        XINPUT_STATE state;
90        if (XInputGetState(m_data->m_joysticks[i].m1, &state) != ERROR_SUCCESS)
91            continue;
92
93        m_data->m_joysticks[i].m2->SetAxis(0, (float)state.Gamepad.sThumbLX / 32768.f);
94        m_data->m_joysticks[i].m2->SetAxis(1, -(float)state.Gamepad.sThumbLY / 32768.f);
95        m_data->m_joysticks[i].m2->SetAxis(2, (float)state.Gamepad.sThumbRX / 32768.f);
96        m_data->m_joysticks[i].m2->SetAxis(3, -(float)state.Gamepad.sThumbRY / 32768.f);
97
98        for (int b = 0; b < 16; b++)
99            m_data->m_joysticks[i].m2->SetButton(b, ((uint16_t)(state.Gamepad.wButtons) >> b) & 1);
100    }
101#endif
102}
103
104} /* namespace lol */
105
Note: See TracBrowser for help on using the repository browser.