826d3ffb34f5f2fdf9567368fbc4ac949968314a
[supertux.git] / src / control / haptic_manager.cpp
1 /*
2  *  SuperTux
3  *  Copyright (C) 2010 Florian Forster <octo at verplant.org>
4  *
5  *  This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include "control/haptic_manager.hpp"
20 #include "util/log.hpp"
21
22 HapticManager::HapticManager () /* {{{ */
23 {
24   int i;
25
26   _device = NULL;
27   memset (_effects, 0, sizeof (_effects));
28   for (i = 0; i < ST_HAPTIC_EFFECT_MAX; i++)
29     _effect_ids[i] = -1;
30 } /* }}} HapticManager */
31
32 /* Public methods */
33 void HapticManager::addJoystick (SDL_Joystick *j) { /* {{{ */
34   int supported;
35
36   if (j == NULL)
37     return;
38
39   if (_device != NULL)
40     return;
41
42   if (SDL_JoystickIsHaptic (j) <= 0)
43     return;
44
45   _device = SDL_HapticOpenFromJoystick (j);
46   if (_device == NULL)
47     return;
48
49   supported = SDL_HapticQuery (_device);
50   if ((supported & SDL_HAPTIC_SINE) == 0)
51   {
52     SDL_HapticClose (_device);
53     _device = NULL;
54     return;
55   }
56
57   /* Setup buttjump */
58   _effects[ST_HAPTIC_EFFECT_BUTTJUMP].type = SDL_HAPTIC_SINE;
59   _effects[ST_HAPTIC_EFFECT_BUTTJUMP].periodic.direction.type = SDL_HAPTIC_POLAR;
60   _effects[ST_HAPTIC_EFFECT_BUTTJUMP].periodic.direction.dir[0] = 18000; /* south */
61   _effects[ST_HAPTIC_EFFECT_BUTTJUMP].periodic.period = 50;
62   _effects[ST_HAPTIC_EFFECT_BUTTJUMP].periodic.magnitude = 0x4FFF;
63   _effects[ST_HAPTIC_EFFECT_BUTTJUMP].periodic.length = 250;
64   _effects[ST_HAPTIC_EFFECT_BUTTJUMP].periodic.attack_length = 0;
65   _effects[ST_HAPTIC_EFFECT_BUTTJUMP].periodic.fade_length = 0;
66
67   _effect_ids[ST_HAPTIC_EFFECT_BUTTJUMP] = SDL_HapticNewEffect (_device,
68       &_effects[ST_HAPTIC_EFFECT_BUTTJUMP]);
69   if (_effect_ids[ST_HAPTIC_EFFECT_BUTTJUMP] < 0)
70   {
71     SDL_HapticClose (_device);
72     _device = NULL;
73     return;
74   }
75
76   log_debug << "Haptic manager: Successfully added joystick." << std::endl;
77   return;
78 } /* }}} void addJoystick */
79
80 void HapticManager::playEffect (haptic_effect_t idx) { /* {{{ */
81   if ((idx < 0) || (idx >= ST_HAPTIC_EFFECT_MAX))
82     return;
83
84   if (_device == NULL)
85     return;
86
87   if (_effect_ids[idx] < 0)
88     return;
89
90   SDL_HapticRunEffect (_device, _effect_ids[idx],
91       /* iterations = */ 1);
92 } /* }}} void playEffect */
93
94 /* vim: set sw=2 sts=2 et fdm=marker : */