summary |
shortlog |
changelog |
graph |
tags |
bookmarks |
branches |
files |
changeset |
file |
latest |
revisions |
annotate |
diff |
raw |
help
2 Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
13 Copyright (c) 2008, Edgar Simo Serra
16 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
18 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
19 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
20 * Neither the name of the Simple Directmedia Layer (SDL) nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
22 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <stdio.h> /* printf */
30 #include <string.h> /* strstr */
31 #include <ctype.h> /* isdigit */
35 #ifndef SDL_HAPTIC_DISABLED
37 #include "SDL_haptic.h"
39 static SDL_Haptic *haptic;
45 static void abort_execution(void);
46 static void HapticPrintSupported(SDL_Haptic * haptic);
50 * @brief The entry point of this force feedback demo.
51 * @param[in] argc Number of arguments.
52 * @param[in] argv Array of argc arguments.
55 main(int argc, char **argv)
60 SDL_HapticEffect efx[5];
63 unsigned int supported;
69 if ((strcmp(name, "--help") == 0) || (strcmp(name, "-h") == 0)) {
70 printf("USAGE: %s [device]\n"
71 "If device is a two-digit number it'll use it as an index, otherwise\n"
72 "it'll use it as if it were part of the device's name.\n",
78 if ((i < 3) && isdigit(name[0]) && ((i == 1) || isdigit(name[1]))) {
84 /* Initialize the force feedbackness */
85 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK |
87 printf("%d Haptic devices detected.\n", SDL_NumHaptics());
88 if (SDL_NumHaptics() > 0) {
89 /* We'll just use index or the first force feedback device found */
91 i = (index != -1) ? index : 0;
93 /* Try to find matching device */
95 for (i = 0; i < SDL_NumHaptics(); i++) {
96 if (strstr(SDL_HapticName(i), name) != NULL)
100 if (i >= SDL_NumHaptics()) {
101 printf("Unable to find device matching '%s', aborting.\n",
107 haptic = SDL_HapticOpen(i);
108 if (haptic == NULL) {
109 printf("Unable to create the haptic device: %s\n",
113 printf("Device: %s\n", SDL_HapticName(i));
114 HapticPrintSupported(haptic);
116 printf("No Haptic devices found!\n");
120 /* We only want force feedback errors. */
123 /* Create effects. */
124 memset(&efx, 0, sizeof(efx));
126 supported = SDL_HapticQuery(haptic);
128 printf("\nUploading effects\n");
129 /* First we'll try a SINE effect. */
130 if (supported & SDL_HAPTIC_SINE) {
131 printf(" effect %d: Sine Wave\n", nefx);
132 efx[nefx].type = SDL_HAPTIC_SINE;
133 efx[nefx].periodic.period = 1000;
134 efx[nefx].periodic.magnitude = 0x4000;
135 efx[nefx].periodic.length = 5000;
136 efx[nefx].periodic.attack_length = 1000;
137 efx[nefx].periodic.fade_length = 1000;
138 id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
140 printf("UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
145 /* Now we'll try a SAWTOOTHUP */
146 if (supported & SDL_HAPTIC_SAWTOOTHUP) {
147 printf(" effect %d: Sawtooth Up\n", nefx);
148 efx[nefx].type = SDL_HAPTIC_SQUARE;
149 efx[nefx].periodic.period = 500;
150 efx[nefx].periodic.magnitude = 0x5000;
151 efx[nefx].periodic.length = 5000;
152 efx[nefx].periodic.attack_length = 1000;
153 efx[nefx].periodic.fade_length = 1000;
154 id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
156 printf("UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
161 /* Now the classical constant effect. */
162 if (supported & SDL_HAPTIC_CONSTANT) {
163 printf(" effect %d: Constant Force\n", nefx);
164 efx[nefx].type = SDL_HAPTIC_CONSTANT;
165 efx[nefx].constant.direction.type = SDL_HAPTIC_POLAR;
166 efx[nefx].constant.direction.dir[0] = 20000; /* Force comes from the south-west. */
167 efx[nefx].constant.length = 5000;
168 efx[nefx].constant.level = 0x6000;
169 efx[nefx].constant.attack_length = 1000;
170 efx[nefx].constant.fade_length = 1000;
171 id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
173 printf("UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
178 /* The cute spring effect. */
179 if (supported & SDL_HAPTIC_SPRING) {
180 printf(" effect %d: Condition Spring\n", nefx);
181 efx[nefx].type = SDL_HAPTIC_SPRING;
182 efx[nefx].condition.length = 5000;
183 for (i = 0; i < SDL_HapticNumAxes(haptic); i++) {
184 efx[nefx].condition.right_sat[i] = 0x7FFF;
185 efx[nefx].condition.left_sat[i] = 0x7FFF;
186 efx[nefx].condition.right_coeff[i] = 0x2000;
187 efx[nefx].condition.left_coeff[i] = 0x2000;
188 efx[nefx].condition.center[i] = 0x1000; /* Displace the center for it to move. */
190 id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
192 printf("UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
197 /* The pretty awesome inertia effect. */
198 if (supported & SDL_HAPTIC_INERTIA) {
199 printf(" effect %d: Condition Inertia\n", nefx);
200 efx[nefx].type = SDL_HAPTIC_SPRING;
201 efx[nefx].condition.length = 5000;
202 for (i = 0; i < SDL_HapticNumAxes(haptic); i++) {
203 efx[nefx].condition.right_sat[i] = 0x7FFF;
204 efx[nefx].condition.left_sat[i] = 0x7FFF;
205 efx[nefx].condition.right_coeff[i] = 0x2000;
206 efx[nefx].condition.left_coeff[i] = 0x2000;
208 id[nefx] = SDL_HapticNewEffect(haptic, &efx[nefx]);
210 printf("UPLOADING EFFECT ERROR: %s\n", SDL_GetError());
217 ("\nNow playing effects for 5 seconds each with 1 second delay between\n");
218 for (i = 0; i < nefx; i++) {
219 printf(" Playing effect %d\n", i);
220 SDL_HapticRunEffect(haptic, id[i], 1);
221 SDL_Delay(6000); /* Effects only have length 5000 */
226 SDL_HapticClose(haptic);
237 abort_execution(void)
239 printf("\nAborting program execution.\n");
241 SDL_HapticClose(haptic);
249 * Displays information about the haptic device.
252 HapticPrintSupported(SDL_Haptic * haptic)
254 unsigned int supported;
256 supported = SDL_HapticQuery(haptic);
257 printf(" Supported effects [%d effects, %d playing]:\n",
258 SDL_HapticNumEffects(haptic), SDL_HapticNumEffectsPlaying(haptic));
259 if (supported & SDL_HAPTIC_CONSTANT)
260 printf(" constant\n");
261 if (supported & SDL_HAPTIC_SINE)
263 if (supported & SDL_HAPTIC_SQUARE)
265 if (supported & SDL_HAPTIC_TRIANGLE)
266 printf(" triangle\n");
267 if (supported & SDL_HAPTIC_SAWTOOTHUP)
268 printf(" sawtoothup\n");
269 if (supported & SDL_HAPTIC_SAWTOOTHDOWN)
270 printf(" sawtoothdown\n");
271 if (supported & SDL_HAPTIC_RAMP)
273 if (supported & SDL_HAPTIC_FRICTION)
274 printf(" friction\n");
275 if (supported & SDL_HAPTIC_SPRING)
277 if (supported & SDL_HAPTIC_DAMPER)
279 if (supported & SDL_HAPTIC_INERTIA)
280 printf(" intertia\n");
281 if (supported & SDL_HAPTIC_CUSTOM)
283 printf(" Supported capabilities:\n");
284 if (supported & SDL_HAPTIC_GAIN)
286 if (supported & SDL_HAPTIC_AUTOCENTER)
287 printf(" autocenter\n");
288 if (supported & SDL_HAPTIC_STATUS)
295 main(int argc, char *argv[])
297 fprintf(stderr, "SDL compiled without Haptic support.\n");