test/testrumble.c
changeset 7639 9406b7dd2f2d
parent 7517 965d57022c01
child 8149 681eb46b8ac4
equal deleted inserted replaced
7638:783d1cff9b20 7639:9406b7dd2f2d
    24 
    24 
    25 /*
    25 /*
    26  * includes
    26  * includes
    27  */
    27  */
    28 #include <stdlib.h>
    28 #include <stdlib.h>
    29 #include <stdio.h>              /* printf */
       
    30 #include <string.h>             /* strstr */
    29 #include <string.h>             /* strstr */
    31 #include <ctype.h>              /* isdigit */
    30 #include <ctype.h>              /* isdigit */
    32 
    31 
    33 #include "SDL.h"
    32 #include "SDL.h"
    34 
    33 
    49 {
    48 {
    50     int i;
    49     int i;
    51     char *name;
    50     char *name;
    52     int index;
    51     int index;
    53 
    52 
       
    53 	/* Enable standard application logging */
       
    54     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
       
    55 
    54     name = NULL;
    56     name = NULL;
    55     index = -1;
    57     index = -1;
    56     if (argc > 1) {
    58     if (argc > 1) {
    57         name = argv[1];
    59         name = argv[1];
    58         if ((strcmp(name, "--help") == 0) || (strcmp(name, "-h") == 0)) {
    60         if ((strcmp(name, "--help") == 0) || (strcmp(name, "-h") == 0)) {
    59             printf("USAGE: %s [device]\n"
    61             SDL_Log("USAGE: %s [device]\n"
    60                    "If device is a two-digit number it'll use it as an index, otherwise\n"
    62                    "If device is a two-digit number it'll use it as an index, otherwise\n"
    61                    "it'll use it as if it were part of the device's name.\n",
    63                    "it'll use it as if it were part of the device's name.\n",
    62                    argv[0]);
    64                    argv[0]);
    63             return 0;
    65             return 0;
    64         }
    66         }
    71     }
    73     }
    72 
    74 
    73     /* Initialize the force feedbackness */
    75     /* Initialize the force feedbackness */
    74     SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK |
    76     SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK |
    75              SDL_INIT_HAPTIC);
    77              SDL_INIT_HAPTIC);
    76     printf("%d Haptic devices detected.\n", SDL_NumHaptics());
    78     SDL_Log("%d Haptic devices detected.\n", SDL_NumHaptics());
    77     if (SDL_NumHaptics() > 0) {
    79     if (SDL_NumHaptics() > 0) {
    78         /* We'll just use index or the first force feedback device found */
    80         /* We'll just use index or the first force feedback device found */
    79         if (name == NULL) {
    81         if (name == NULL) {
    80             i = (index != -1) ? index : 0;
    82             i = (index != -1) ? index : 0;
    81         }
    83         }
    85                 if (strstr(SDL_HapticName(i), name) != NULL)
    87                 if (strstr(SDL_HapticName(i), name) != NULL)
    86                     break;
    88                     break;
    87             }
    89             }
    88 
    90 
    89             if (i >= SDL_NumHaptics()) {
    91             if (i >= SDL_NumHaptics()) {
    90                 printf("Unable to find device matching '%s', aborting.\n",
    92                 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to find device matching '%s', aborting.\n",
    91                        name);
    93                        name);
    92                 return 1;
    94                 return 1;
    93             }
    95             }
    94         }
    96         }
    95 
    97 
    96         haptic = SDL_HapticOpen(i);
    98         haptic = SDL_HapticOpen(i);
    97         if (haptic == NULL) {
    99         if (haptic == NULL) {
    98             printf("Unable to create the haptic device: %s\n",
   100             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to create the haptic device: %s\n",
    99                    SDL_GetError());
   101                    SDL_GetError());
   100             return 1;
   102             return 1;
   101         }
   103         }
   102         printf("Device: %s\n", SDL_HapticName(i));
   104         SDL_Log("Device: %s\n", SDL_HapticName(i));
   103     } else {
   105     } else {
   104         printf("No Haptic devices found!\n");
   106         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "No Haptic devices found!\n");
   105         return 1;
   107         return 1;
   106     }
   108     }
   107 
   109 
   108     /* We only want force feedback errors. */
   110     /* We only want force feedback errors. */
   109     SDL_ClearError();
   111     SDL_ClearError();
   110 
   112 
   111     if (SDL_HapticRumbleSupported(haptic) == SDL_FALSE) {
   113     if (SDL_HapticRumbleSupported(haptic) == SDL_FALSE) {
   112         printf("\nRumble not supported!\n");
   114         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Rumble not supported!\n");
   113         return 1;
   115         return 1;
   114     }
   116     }
   115     if (SDL_HapticRumbleInit(haptic) != 0) {
   117     if (SDL_HapticRumbleInit(haptic) != 0) {
   116         printf("\nFailed to initialize rumble: %s\n", SDL_GetError());
   118         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to initialize rumble: %s\n", SDL_GetError());
   117         return 1;
   119         return 1;
   118     }
   120     }
   119     printf("Playing 2 second rumble at 0.5 magnitude.\n");
   121     SDL_Log("Playing 2 second rumble at 0.5 magnitude.\n");
   120     if (SDL_HapticRumblePlay(haptic, 0.5, 5000) != 0) {
   122     if (SDL_HapticRumblePlay(haptic, 0.5, 5000) != 0) {
   121        printf("\nFailed to play rumble: %s\n", SDL_GetError() );
   123        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to play rumble: %s\n", SDL_GetError() );
   122        return 1;
   124        return 1;
   123     }
   125     }
   124     SDL_Delay(2000);
   126     SDL_Delay(2000);
   125     printf("Stopping rumble.\n");
   127     SDL_Log("Stopping rumble.\n");
   126     SDL_HapticRumbleStop(haptic);
   128     SDL_HapticRumbleStop(haptic);
   127     SDL_Delay(2000);
   129     SDL_Delay(2000);
   128     printf("Playing 2 second rumble at 0.3 magnitude.\n");
   130     SDL_Log("Playing 2 second rumble at 0.3 magnitude.\n");
   129     if (SDL_HapticRumblePlay(haptic, 0.3f, 5000) != 0) {
   131     if (SDL_HapticRumblePlay(haptic, 0.3f, 5000) != 0) {
   130        printf("\nFailed to play rumble: %s\n", SDL_GetError() );
   132        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to play rumble: %s\n", SDL_GetError() );
   131        return 1;
   133        return 1;
   132     }
   134     }
   133     SDL_Delay(2000);
   135     SDL_Delay(2000);
   134 
   136 
   135     /* Quit */
   137     /* Quit */
   143 #else
   145 #else
   144 
   146 
   145 int
   147 int
   146 main(int argc, char *argv[])
   148 main(int argc, char *argv[])
   147 {
   149 {
   148     fprintf(stderr, "SDL compiled without Haptic support.\n");
   150     SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL compiled without Haptic support.\n");
   149     exit(1);
   151     exit(1);
   150 }
   152 }
   151 
   153 
   152 #endif
   154 #endif