1 | // |
---|
2 | // Lol Engine |
---|
3 | // |
---|
4 | // Copyright: (c) 2010-2013 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://www.wtfpl.net/ for more details. |
---|
9 | // |
---|
10 | |
---|
11 | #if defined HAVE_CONFIG_H |
---|
12 | # include "config.h" |
---|
13 | #endif |
---|
14 | |
---|
15 | #if defined USE_XINPUT |
---|
16 | # include <d3d9.h> |
---|
17 | # include <xinput.h> |
---|
18 | #endif |
---|
19 | |
---|
20 | #include "core.h" |
---|
21 | #include "d3d9input.h" |
---|
22 | |
---|
23 | #ifdef LOL_INPUT_V2 |
---|
24 | #include "input/inputdevice_internal.h" |
---|
25 | #endif // LOL_INPUT_V2 |
---|
26 | |
---|
27 | namespace lol |
---|
28 | { |
---|
29 | |
---|
30 | /* |
---|
31 | * D3d9 Input implementation class |
---|
32 | */ |
---|
33 | |
---|
34 | class D3d9InputData |
---|
35 | { |
---|
36 | friend class D3d9Input; |
---|
37 | |
---|
38 | private: |
---|
39 | #if defined USE_XINPUT |
---|
40 | #if defined LOL_INPUT_V2 |
---|
41 | Array<int, InputDeviceInternal*> m_joysticks; |
---|
42 | #else |
---|
43 | Array<int, Stick *> m_joysticks; |
---|
44 | #endif // LOL_INPUT_V2 |
---|
45 | #endif // USE_XINPUT |
---|
46 | }; |
---|
47 | |
---|
48 | /* |
---|
49 | * Public D3d9Input class |
---|
50 | */ |
---|
51 | |
---|
52 | D3d9Input::D3d9Input() |
---|
53 | : m_data(new D3d9InputData()) |
---|
54 | { |
---|
55 | #if defined USE_XINPUT |
---|
56 | for (int i = 0; i < XUSER_MAX_COUNT; i++) |
---|
57 | { |
---|
58 | XINPUT_STATE state; |
---|
59 | if (XInputGetState(i, &state) != ERROR_SUCCESS) |
---|
60 | continue; |
---|
61 | #if defined LOL_INPUT_V2 |
---|
62 | // TODO: we can put more friendly name here, such as LeftAxisX, ButtonX... |
---|
63 | InputDeviceInternal* stick = new InputDeviceInternal(String::Printf("Joystick%d", i+1).C()); |
---|
64 | for (int j = 0; j < 4; ++j) |
---|
65 | stick->AddAxis(String::Printf("Axis%d", j+1).C()); |
---|
66 | for (int j = 0; j < 16; ++j) |
---|
67 | stick->AddKey(String::Printf("Button%d", j+1).C()); |
---|
68 | |
---|
69 | m_data->m_joysticks.Push(i, stick); |
---|
70 | #else |
---|
71 | Stick *stick = Input::CreateStick(); |
---|
72 | stick->SetAxisCount(4); |
---|
73 | stick->SetButtonCount(16); |
---|
74 | m_data->m_joysticks.Push(i, stick); |
---|
75 | #endif // LOL_INPUT_V2 |
---|
76 | } |
---|
77 | #endif |
---|
78 | |
---|
79 | m_gamegroup = GAMEGROUP_BEFORE; |
---|
80 | } |
---|
81 | |
---|
82 | D3d9Input::~D3d9Input() |
---|
83 | { |
---|
84 | #if defined USE_XINPUT |
---|
85 | /* Unregister all the joysticks we added */ |
---|
86 | while (m_data->m_joysticks.Count()) |
---|
87 | { |
---|
88 | #if defined LOL_INPUT_V2 |
---|
89 | delete m_data->m_joysticks[0].m2; |
---|
90 | #else |
---|
91 | Input::DestroyStick(m_data->m_joysticks[0].m2); |
---|
92 | #endif // LOL_INPUT_V2 |
---|
93 | m_data->m_joysticks.Remove(0); |
---|
94 | } |
---|
95 | #endif |
---|
96 | delete m_data; |
---|
97 | } |
---|
98 | |
---|
99 | void D3d9Input::TickGame(float seconds) |
---|
100 | { |
---|
101 | Entity::TickGame(seconds); |
---|
102 | } |
---|
103 | |
---|
104 | void D3d9Input::TickDraw(float seconds) |
---|
105 | { |
---|
106 | Entity::TickDraw(seconds); |
---|
107 | |
---|
108 | #if defined USE_XINPUT |
---|
109 | for (int i = 0; i < m_data->m_joysticks.Count(); i++) |
---|
110 | { |
---|
111 | XINPUT_STATE state; |
---|
112 | if (XInputGetState(m_data->m_joysticks[i].m1, &state) != ERROR_SUCCESS) |
---|
113 | continue; |
---|
114 | |
---|
115 | m_data->m_joysticks[i].m2->SetAxis(0, (float)state.Gamepad.sThumbLX / 32768.f); |
---|
116 | m_data->m_joysticks[i].m2->SetAxis(1, -(float)state.Gamepad.sThumbLY / 32768.f); |
---|
117 | m_data->m_joysticks[i].m2->SetAxis(2, (float)state.Gamepad.sThumbRX / 32768.f); |
---|
118 | m_data->m_joysticks[i].m2->SetAxis(3, -(float)state.Gamepad.sThumbRY / 32768.f); |
---|
119 | |
---|
120 | for (int b = 0; b < 16; b++) |
---|
121 | #if defined LOL_INPUT_V2 |
---|
122 | m_data->m_joysticks[i].m2->SetKey(b, ((uint16_t)(state.Gamepad.wButtons) >> b) & 1); |
---|
123 | #else |
---|
124 | m_data->m_joysticks[i].m2->SetButton(b, ((uint16_t)(state.Gamepad.wButtons) >> b) & 1); |
---|
125 | #endif // LOL_INPUT_V2 |
---|
126 | } |
---|
127 | #endif |
---|
128 | } |
---|
129 | |
---|
130 | } /* namespace lol */ |
---|
131 | |
---|