source: trunk/src/input/controller.cpp @ 2836

Last change on this file since 2836 was 2836, checked in by sam, 7 years ago

input: fix a memory leak in the controller code.

  • Property svn:eol-style set to LF
File size: 3.1 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2013 Benjamin Litzelmann
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#include "core.h"
16
17#ifdef LOL_INPUT_V2
18
19namespace lol
20{
21
22///////////////////////////////////////////////////////////////////////////////
23// KeyBinding
24
25void KeyBinding::Bind(const char* device_name, const char* key_name)
26{
27    m_device = InputDevice::Get(device_name);
28    if (!m_device)
29    {
30        Log::Warn("Trying to bind controller to device %s which doesn't exist", device_name);
31        return;
32    }
33
34    m_keyindex = m_device->GetKeyIndex(key_name);
35
36    if (m_keyindex < 0)
37    {
38        Log::Warn("Trying to bind controller to key %s.%s which doesn't exist", device_name, key_name);
39    }
40}
41
42///////////////////////////////////////////////////////////////////////////////
43// AxisBinding
44
45void AxisBinding::Bind(const char* device_name, const char* axis_name)
46{
47    m_device = InputDevice::Get(device_name);
48    if (!m_device)
49    {
50        Log::Warn("Trying to bind controller to device %s which doesn't exist", device_name);
51        return;
52    }
53
54    m_axisindex = m_device->GetAxisIndex(axis_name);
55
56    if (m_axisindex < 0)
57    {
58        Log::Warn("Trying to bind controller to axis %s.%s which doesn't exist", device_name, axis_name);
59    }
60}
61
62
63///////////////////////////////////////////////////////////////////////////////
64// Controller
65
66Array<Controller*> Controller::controllers;
67
68Controller::Controller(int nb_keys, int nb_axis)
69{
70    m_gamegroup = GAMEGROUP_BEFORE;
71    m_keys.Resize(nb_keys);
72    m_axis.Resize(nb_axis);
73    m_activate_nextframe = false;
74    m_deactivate_nextframe = false;
75    m_active = false;
76    controllers.Push(this);
77}
78
79Controller::~Controller()
80{
81    for (int i = 0; i < controllers.Count(); ++i)
82    {
83        if (controllers[i] == this)
84        {
85            controllers.Remove(i);
86            break;
87        }
88    }
89}
90
91void Controller::TickGame(float seconds)
92{
93    Entity::TickGame(seconds);
94
95    for (int i = 0; i < m_keys.Count(); ++i)
96        m_keys[i].Update();
97
98    for (int i = 0; i < m_axis.Count(); ++i)
99        m_axis[i].Update();
100
101    if (m_activate_nextframe)
102        m_active = true;
103
104    if (m_deactivate_nextframe)
105        m_active = false;
106
107    m_activate_nextframe = false;
108    m_deactivate_nextframe = false;
109}
110
111void Controller::Activate()
112{
113    m_activate_nextframe = true;
114    m_deactivate_nextframe = false;
115}
116
117void Controller::Deactivate()
118{
119    m_deactivate_nextframe = true;
120    m_activate_nextframe = false;
121}
122
123Array<Controller*> Controller::DeactivateAll()
124{
125    Array<Controller*> result;
126
127    for (int i = 0; i < controllers.Count(); ++i)
128    {
129        if (controllers[i]->m_active || controllers[i]->m_activate_nextframe)
130        {
131            result.Push(controllers[i]);
132            controllers[i]->Deactivate();
133        }
134    }
135
136    return result;
137}
138
139} /* namespace lol */
140
141#endif // LOL_INPUT_V2
Note: See TracBrowser for help on using the repository browser.