supertux/main, control/haptic_manager: Add SDL 1.2 compatibility code.
[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 #if SDL_VERSION_ATLEAST(1,3,0)
23 HapticManager::HapticManager () /* {{{ */
24 {
25   int i;
26
27   _device = NULL;
28   memset (_effects, 0, sizeof (_effects));
29   for (i = 0; i < ST_HAPTIC_EFFECT_MAX; i++)
30     _effect_ids[i] = -1;
31 } /* }}} HapticManager */
32
33 /* Public methods */
34 void HapticManager::addJoystick (SDL_Joystick *j) { /* {{{ */
35   int supported;
36
37   if (j == NULL)
38     return;
39
40   if (_device != NULL)
41     return;
42
43   if (SDL_JoystickIsHaptic (j) <= 0)
44     return;
45
46   _device = SDL_HapticOpenFromJoystick (j);
47   if (_device == NULL)
48     return;
49
50   supported = SDL_HapticQuery (_device);
51   if ((supported & SDL_HAPTIC_SINE) == 0)
52   {
53     SDL_HapticClose (_device);
54     _device = NULL;
55     return;
56   }
57
58   /* Setup buttjump */
59   _effects[ST_HAPTIC_EFFECT_BUTTJUMP].type = SDL_HAPTIC_SINE;
60   _effects[ST_HAPTIC_EFFECT_BUTTJUMP].periodic.direction.type = SDL_HAPTIC_POLAR;
61   _effects[ST_HAPTIC_EFFECT_BUTTJUMP].periodic.direction.dir[0] = 18000; /* south */
62   _effects[ST_HAPTIC_EFFECT_BUTTJUMP].periodic.period = 50;
63   _effects[ST_HAPTIC_EFFECT_BUTTJUMP].periodic.magnitude = 0x4FFF;
64   _effects[ST_HAPTIC_EFFECT_BUTTJUMP].periodic.length = 250;
65   _effects[ST_HAPTIC_EFFECT_BUTTJUMP].periodic.attack_length = 0;
66   _effects[ST_HAPTIC_EFFECT_BUTTJUMP].periodic.fade_length = 0;
67
68   _effect_ids[ST_HAPTIC_EFFECT_BUTTJUMP] = SDL_HapticNewEffect (_device,
69       &_effects[ST_HAPTIC_EFFECT_BUTTJUMP]);
70   if (_effect_ids[ST_HAPTIC_EFFECT_BUTTJUMP] < 0)
71   {
72     SDL_HapticClose (_device);
73     _device = NULL;
74     return;
75   }
76
77   log_debug << "Haptic manager: Successfully added joystick." << std::endl;
78   return;
79 } /* }}} void addJoystick */
80
81 void HapticManager::playEffect (haptic_effect_t idx) { /* {{{ */
82   if ((idx < 0) || (idx >= ST_HAPTIC_EFFECT_MAX))
83     return;
84
85   if (_device == NULL)
86     return;
87
88   if (_effect_ids[idx] < 0)
89     return;
90
91   SDL_HapticRunEffect (_device, _effect_ids[idx],
92       /* iterations = */ 1);
93 } /* }}} void playEffect */
94 #else /* if SDL < 1.3 */
95 /* If the SDL version is too old, provide dummy methods that don't to anything.
96  * This avoid using defines all over the place. */
97 HapticManager::HapticManager () {
98   log_debug << "Haptic manager: Disabled because SDL version is too old." << std::endl;
99 }
100
101 void HapticManager::addJoystick (SDL_Joystick *j) {
102   /* do nothing. */
103 }
104
105 void HapticManager::playEffect (haptic_effect_t idx) {
106   /* do nothing. */
107 }
108 #endif /* SDL < 1.3 */
109
110 /* vim: set sw=2 sts=2 et fdm=marker : */