source: trunk/src/platform/d3d9/d3d9input.cpp @ 2812

Last change on this file since 2812 was 2812, checked in by lolbot, 8 years ago

fixed 10 files out of 2754:

  • removed 386 CR characters
  • removed 4 trailing whitespaces
  • replaced 189 tabs with spaces
File size: 3.2 KB
RevLine 
[1361]1//
2// Lol Engine
3//
[2216]4// Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
[1361]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
[2183]8//   http://www.wtfpl.net/ for more details.
[1361]9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
[1712]15#if defined USE_XINPUT
[1361]16#   include <d3d9.h>
17#   include <xinput.h>
18#endif
19
20#include "core.h"
21#include "d3d9input.h"
22
[2810]23#ifdef LOL_INPUT_V2
24#include "input/inputdevice_internal.h"
25#endif // LOL_INPUT_V2
26
[1361]27namespace lol
28{
29
30/*
31 * D3d9 Input implementation class
32 */
33
34class D3d9InputData
35{
36    friend class D3d9Input;
37
38private:
[1712]39#if defined USE_XINPUT
[2810]40#if defined LOL_INPUT_V2
41    Array<int, InputDeviceInternal*> m_joysticks;
42#else
[1361]43    Array<int, Stick *> m_joysticks;
[2810]44#endif // LOL_INPUT_V2
45#endif // USE_XINPUT
[1361]46};
47
48/*
49 * Public D3d9Input class
50 */
51
52D3d9Input::D3d9Input()
53  : m_data(new D3d9InputData())
54{
[1712]55#if defined USE_XINPUT
[1361]56    for (int i = 0; i < XUSER_MAX_COUNT; i++)
57    {
58        XINPUT_STATE state;
59        if (XInputGetState(i, &state) != ERROR_SUCCESS)
60            continue;
[2810]61#if defined LOL_INPUT_V2
[2812]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());
[1361]68
[2812]69        m_data->m_joysticks.Push(i, stick);
[2810]70#else
[1361]71        Stick *stick = Input::CreateStick();
72        stick->SetAxisCount(4);
[1394]73        stick->SetButtonCount(16);
[1361]74        m_data->m_joysticks.Push(i, stick);
[2810]75#endif // LOL_INPUT_V2
[1361]76    }
77#endif
78
79    m_gamegroup = GAMEGROUP_BEFORE;
80}
81
82D3d9Input::~D3d9Input()
83{
[1712]84#if defined USE_XINPUT
[1361]85    /* Unregister all the joysticks we added */
86    while (m_data->m_joysticks.Count())
87    {
[2810]88#if defined LOL_INPUT_V2
[2812]89        delete m_data->m_joysticks[0].m2;
[2810]90#else
[2812]91        Input::DestroyStick(m_data->m_joysticks[0].m2);
[2810]92#endif // LOL_INPUT_V2
[2812]93        m_data->m_joysticks.Remove(0);
[1361]94    }
95#endif
96    delete m_data;
97}
98
99void D3d9Input::TickGame(float seconds)
100{
101    Entity::TickGame(seconds);
102}
103
104void D3d9Input::TickDraw(float seconds)
105{
106    Entity::TickDraw(seconds);
107
[1712]108#if defined USE_XINPUT
[1361]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);
[1394]119
120        for (int b = 0; b < 16; b++)
[2810]121#if defined LOL_INPUT_V2
[2812]122            m_data->m_joysticks[i].m2->SetKey(b, ((uint16_t)(state.Gamepad.wButtons) >> b) & 1);
[2810]123#else
[2812]124            m_data->m_joysticks[i].m2->SetButton(b, ((uint16_t)(state.Gamepad.wButtons) >> b) & 1);
[2810]125#endif // LOL_INPUT_V2
[2812]126    }
[1361]127#endif
128}
129
130} /* namespace lol */
131
Note: See TracBrowser for help on using the repository browser.