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 USE_D3D9 |
---|
16 | # include <d3d9.h> |
---|
17 | # include <xinput.h> |
---|
18 | #endif |
---|
19 | |
---|
20 | #include "core.h" |
---|
21 | #include "d3d9input.h" |
---|
22 | |
---|
23 | namespace lol |
---|
24 | { |
---|
25 | |
---|
26 | /* |
---|
27 | * D3d9 Input implementation class |
---|
28 | */ |
---|
29 | |
---|
30 | class D3d9InputData |
---|
31 | { |
---|
32 | friend class D3d9Input; |
---|
33 | |
---|
34 | private: |
---|
35 | #if defined USE_D3D9 |
---|
36 | Array<int, Stick *> m_joysticks; |
---|
37 | #endif |
---|
38 | }; |
---|
39 | |
---|
40 | /* |
---|
41 | * Public D3d9Input class |
---|
42 | */ |
---|
43 | |
---|
44 | D3d9Input::D3d9Input() |
---|
45 | : m_data(new D3d9InputData()) |
---|
46 | { |
---|
47 | #if defined USE_D3D9 |
---|
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 | |
---|
64 | D3d9Input::~D3d9Input() |
---|
65 | { |
---|
66 | #if defined USE_D3D9 |
---|
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 | |
---|
77 | void D3d9Input::TickGame(float seconds) |
---|
78 | { |
---|
79 | Entity::TickGame(seconds); |
---|
80 | } |
---|
81 | |
---|
82 | void D3d9Input::TickDraw(float seconds) |
---|
83 | { |
---|
84 | Entity::TickDraw(seconds); |
---|
85 | |
---|
86 | #if defined USE_D3D9 |
---|
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 | |
---|