Skip to content

Instantly share code, notes, and snippets.

@Kethen
Last active January 10, 2025 10:45
Show Gist options
  • Save Kethen/86a6841130902e49f91dd2b7aa11d696 to your computer and use it in GitHub Desktop.
Save Kethen/86a6841130902e49f91dd2b7aa11d696 to your computer and use it in GitHub Desktop.
SDL2 ffb debug snippet for the desperate
static FILE* log_file = NULL;
#define LOG(...) { \
if(log_file == NULL){ \
log_file = fopen("./ffb_log.txt", "w"); \
} \
fprintf(log_file, __VA_ARGS__); \
fflush(log_file); \
}
static void dump_direction(SDL_HapticDirection *d){
const char *name = "wdf";
switch(d->type){
case SDL_HAPTIC_POLAR:
name = "polar";
break;
case SDL_HAPTIC_CARTESIAN:
name = "cartesian";
break;
case SDL_HAPTIC_SPHERICAL:
name = "spherical";
break;
case SDL_HAPTIC_STEERING_AXIS:
name = "steering axis";
break;
}
LOG("direction: %s %d %d %d\n", name, d->dir[0], d->dir[1], d->dir[2]);
}
static void dump_effect(const char *caller, SDL_HapticEffect *e){
const char *name = "wtf";
if(effect_is_periodic(e)){
SDL_HapticPeriodic *ee = (SDL_HapticPeriodic *)e;
switch(ee->type){
case SDL_HAPTIC_SINE:
name = "sine";
break;
case SDL_HAPTIC_TRIANGLE:
name = "triangle";
break;
case SDL_HAPTIC_SAWTOOTHUP:
name = "sawtooth up";
break;
case SDL_HAPTIC_SAWTOOTHDOWN:
name = "sawtooth down";
break;
/*
case SDL_HAPTIC_SQUARE:
name = "square";
break;
*/
}
LOG("%s: periodic effect %s:\n", caller, name);
dump_direction(&ee->direction);
LOG("length: %u\n", ee->length);
LOG("delay: %u\n", ee->delay);
LOG("button: %x\n", ee->button);
LOG("interval: %u\n", ee->interval);
LOG("period: %u\n", ee->period);
LOG("magnitude: %d\n", ee->magnitude);
LOG("offset: %d\n", ee->offset);
LOG("phase: %u\n", ee->phase);
LOG("attack length: %u\n", ee->attack_length);
LOG("attack level: %u\n", ee->attack_level);
LOG("fade length: %u\n", ee->fade_length);
LOG("fade level: %u\n", ee->fade_level);
}else if(effect_is_condition(e)){
SDL_HapticCondition *ee = (SDL_HapticCondition *)e;
switch(ee->type){
case SDL_HAPTIC_SPRING:
name = "spring";
break;
case SDL_HAPTIC_DAMPER:
name = "damper";
break;
case SDL_HAPTIC_FRICTION:
name = "friction";
break;
}
LOG("%s: condition effect %s:\n", caller, name);
dump_direction(&ee->direction);
LOG("length: %u\n", ee->length);
LOG("delay: %u\n", ee->delay);
LOG("button: %x\n", ee->button);
LOG("interval: %u\n", ee->interval);
LOG("right sat %u %u %u\n", ee->right_sat[0], ee->right_sat[1], ee->right_sat[2]);
LOG("left sat %u %u %u\n", ee->left_sat[0], ee->left_sat[1], ee->left_sat[2]);
LOG("right coeff %d %d %d\n", ee->right_coeff[0], ee->right_coeff[1], ee->right_coeff[2]);
LOG("left coeff %d %d %d\n", ee->left_coeff[0], ee->left_coeff[1], ee->left_coeff[2]);
LOG("deadband %u %u %u\n", ee->deadband[0], ee->deadband[1], ee->deadband[2]);
LOG("center %d %d %d\n", ee->center[0], ee->center[1], ee->center[2]);
}else{
switch(e->type){
case SDL_HAPTIC_CONSTANT:{
SDL_HapticConstant *ee = (SDL_HapticConstant *)e;
LOG("%s: constant effect\n", caller);
dump_direction(&ee->direction);
LOG("length: %u\n", ee->length);
LOG("delay: %u\n", ee->delay);
LOG("button: %x\n", ee->button);
LOG("interval: %u\n", ee->interval);
LOG("level: %d\n", ee->level);
LOG("attack length: %u\n", ee->attack_length);
LOG("attack level: %u\n", ee->attack_level);
LOG("fade length: %u\n", ee->fade_length);
LOG("fade level: %u\n", ee->fade_level);
break;
}
case SDL_HAPTIC_RAMP:{
SDL_HapticRamp *ee = (SDL_HapticRamp *)e;
LOG("%s: ramp effect\n", caller);
dump_direction(&ee->direction);
LOG("length: %u\n", ee->length);
LOG("delay: %u\n", ee->delay);
LOG("button: %x\n", ee->button);
LOG("interval: %u\n", ee->interval);
LOG("start: %d\n", ee->start);
LOG("end: %d\n", ee->end);
LOG("attack length: %u\n", ee->attack_length);
LOG("attack level: %u\n", ee->attack_level);
LOG("fade length: %u\n", ee->fade_length);
LOG("fade level: %u\n", ee->fade_level);
break;
}
default:{
LOG("%s: haptic effect %d\n", caller, e->type);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment