Merge from https://bitbucket.org/keestux/sdl ... SDL_gesture code cleanup.

This commit is contained in:
Ryan C. Gordon 2011-11-08 16:24:02 -05:00
commit 311a3e9453

View file

@ -63,7 +63,7 @@ typedef struct {
} SDL_DollarTemplate;
typedef struct {
SDL_GestureID id;
SDL_TouchID id;
SDL_FloatPoint res;
SDL_FloatPoint centroid;
SDL_DollarPath dollarPath;
@ -80,7 +80,8 @@ int SDL_numGestureTouches = 0;
SDL_bool recordAll;
#if 0
static void PrintPath(SDL_FloatPoint *path) {
static void PrintPath(SDL_FloatPoint *path)
{
int i;
printf("Path:");
for (i=0; i<DOLLARNPOINTS; i++) {
@ -90,7 +91,8 @@ static void PrintPath(SDL_FloatPoint *path) {
}
#endif
int SDL_RecordGesture(SDL_TouchID touchId) {
int SDL_RecordGesture(SDL_TouchID touchId)
{
int i;
if (touchId < 0) recordAll = SDL_TRUE;
for (i = 0; i < SDL_numGestureTouches; i++) {
@ -103,7 +105,8 @@ int SDL_RecordGesture(SDL_TouchID touchId) {
return (touchId < 0);
}
static unsigned long SDL_HashDollar(SDL_FloatPoint* points) {
static unsigned long SDL_HashDollar(SDL_FloatPoint* points)
{
unsigned long hash = 5381;
int i;
for (i = 0; i < DOLLARNPOINTS; i++) {
@ -114,7 +117,8 @@ static unsigned long SDL_HashDollar(SDL_FloatPoint* points) {
}
static int SaveTemplate(SDL_DollarTemplate *templ, SDL_RWops * src) {
static int SaveTemplate(SDL_DollarTemplate *templ, SDL_RWops * src)
{
if (src == NULL) return 0;
@ -129,7 +133,8 @@ static int SaveTemplate(SDL_DollarTemplate *templ, SDL_RWops * src) {
}
int SDL_SaveAllDollarTemplates(SDL_RWops *src) {
int SDL_SaveAllDollarTemplates(SDL_RWops *src)
{
int i,j,rtrn = 0;
for (i = 0; i < SDL_numGestureTouches; i++) {
SDL_GestureTouch* touch = &SDL_gestureTouch[i];
@ -140,7 +145,8 @@ int SDL_SaveAllDollarTemplates(SDL_RWops *src) {
return rtrn;
}
int SDL_SaveDollarTemplate(SDL_GestureID gestureId, SDL_RWops *src) {
int SDL_SaveDollarTemplate(SDL_GestureID gestureId, SDL_RWops *src)
{
int i,j;
for (i = 0; i < SDL_numGestureTouches; i++) {
SDL_GestureTouch* touch = &SDL_gestureTouch[i];
@ -156,56 +162,53 @@ int SDL_SaveDollarTemplate(SDL_GestureID gestureId, SDL_RWops *src) {
//path is an already sampled set of points
//Returns the index of the gesture on success, or -1
static int SDL_AddDollarGesture(SDL_GestureTouch* inTouch,SDL_FloatPoint* path) {
static int SDL_AddDollarGesture_one(SDL_GestureTouch* inTouch, SDL_FloatPoint* path)
{
SDL_DollarTemplate* dollarTemplate;
SDL_DollarTemplate *templ;
int index;
index = inTouch->numDollarTemplates;
dollarTemplate =
(SDL_DollarTemplate *)SDL_realloc(inTouch->dollarTemplate,
(index + 1) *
sizeof(SDL_DollarTemplate));
if (!dollarTemplate) {
SDL_OutOfMemory();
return -1;
}
inTouch->dollarTemplate = dollarTemplate;
templ = &inTouch->dollarTemplate[index];
SDL_memcpy(templ->path, path, DOLLARNPOINTS*sizeof(SDL_FloatPoint));
templ->hash = SDL_HashDollar(templ->path);
inTouch->numDollarTemplates++;
return index;
}
static int SDL_AddDollarGesture(SDL_GestureTouch* inTouch, SDL_FloatPoint* path)
{
int index;
int i = 0;
if (inTouch == NULL) {
if (SDL_numGestureTouches == 0) return -1;
for (i = 0; i < SDL_numGestureTouches; i++) {
inTouch = &SDL_gestureTouch[i];
dollarTemplate =
(SDL_DollarTemplate *)SDL_realloc(inTouch->dollarTemplate,
(inTouch->numDollarTemplates + 1) *
sizeof(SDL_DollarTemplate));
if(!dollarTemplate) {
SDL_OutOfMemory();
index = SDL_AddDollarGesture_one(inTouch, path);
if (index < 0)
return -1;
}
inTouch->dollarTemplate = dollarTemplate;
templ =
&inTouch->dollarTemplate[inTouch->numDollarTemplates];
SDL_memcpy(templ->path,path,DOLLARNPOINTS*sizeof(SDL_FloatPoint));
templ->hash = SDL_HashDollar(templ->path);
inTouch->numDollarTemplates++;
}
return inTouch->numDollarTemplates - 1;
// Use the index of the last one added.
return index;
} else {
SDL_DollarTemplate* dollarTemplate =
( SDL_DollarTemplate *)SDL_realloc(inTouch->dollarTemplate,
(inTouch->numDollarTemplates + 1) *
sizeof(SDL_DollarTemplate));
if(!dollarTemplate) {
SDL_OutOfMemory();
return -1;
}
inTouch->dollarTemplate = dollarTemplate;
templ =
&inTouch->dollarTemplate[inTouch->numDollarTemplates];
SDL_memcpy(templ->path,path,DOLLARNPOINTS*sizeof(SDL_FloatPoint));
templ->hash = SDL_HashDollar(templ->path);
inTouch->numDollarTemplates++;
return inTouch->numDollarTemplates - 1;
return SDL_AddDollarGesture_one(inTouch, path);
}
return -1;
}
int SDL_LoadDollarTemplates(SDL_TouchID touchId, SDL_RWops *src) {
int SDL_LoadDollarTemplates(SDL_TouchID touchId, SDL_RWops *src)
{
int i,loaded = 0;
SDL_GestureTouch *touch = NULL;
if (src == NULL) return 0;
@ -224,7 +227,8 @@ int SDL_LoadDollarTemplates(SDL_TouchID touchId, SDL_RWops *src) {
if (touchId >= 0) {
//printf("Adding loaded gesture to 1 touch\n");
if(SDL_AddDollarGesture(touch,templ.path)) loaded++;
if (SDL_AddDollarGesture(touch, templ.path) >= 0)
loaded++;
}
else {
//printf("Adding to: %i touches\n",SDL_numGestureTouches);
@ -242,7 +246,8 @@ int SDL_LoadDollarTemplates(SDL_TouchID touchId, SDL_RWops *src) {
}
static float dollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ,float ang) {
static float dollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ,float ang)
{
// SDL_FloatPoint p[DOLLARNPOINTS];
float dist = 0;
SDL_FloatPoint p;
@ -257,7 +262,8 @@ static float dollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ,float
}
static float bestDollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ) {
static float bestDollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ)
{
//------------BEGIN DOLLAR BLACKBOX----------------//
//-TRANSLATED DIRECTLY FROM PSUDEO-CODE AVAILABLE AT-//
//-"http://depts.washington.edu/aimgroup/proj/dollar/"-//
@ -294,7 +300,8 @@ static float bestDollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ)
}
//DollarPath contains raw points, plus (possibly) the calculated length
static int dollarNormalize(const SDL_DollarPath *path,SDL_FloatPoint *points) {
static int dollarNormalize(const SDL_DollarPath *path,SDL_FloatPoint *points)
{
int i;
float interval;
float dist;
@ -308,10 +315,8 @@ static int dollarNormalize(const SDL_DollarPath *path,SDL_FloatPoint *points) {
//Calculate length if it hasn't already been done
if (length <= 0) {
for (i=1;i < path->numPoints; i++) {
float dx = path->p[i ].x -
path->p[i-1].x;
float dy = path->p[i ].y -
path->p[i-1].y;
float dx = path->p[i ].x - path->p[i-1].x;
float dy = path->p[i ].y - path->p[i-1].y;
length += (float)(SDL_sqrt(dx*dx+dy*dy));
}
}
@ -387,7 +392,8 @@ static int dollarNormalize(const SDL_DollarPath *path,SDL_FloatPoint *points) {
return numPoints;
}
static float dollarRecognize(const SDL_DollarPath *path,int *bestTempl,SDL_GestureTouch* touch) {
static float dollarRecognize(const SDL_DollarPath *path,int *bestTempl,SDL_GestureTouch* touch)
{
SDL_FloatPoint points[DOLLARNPOINTS];
int i;
@ -404,7 +410,8 @@ static float dollarRecognize(const SDL_DollarPath *path,int *bestTempl,SDL_Gestu
return bestDiff;
}
int SDL_GestureAddTouch(SDL_Touch* touch) {
int SDL_GestureAddTouch(SDL_Touch* touch)
{
SDL_GestureTouch *gestureTouch = (SDL_GestureTouch *)SDL_realloc(SDL_gestureTouch,
(SDL_numGestureTouches + 1) *
sizeof(SDL_GestureTouch));
@ -431,16 +438,19 @@ int SDL_GestureAddTouch(SDL_Touch* touch) {
return 0;
}
static SDL_GestureTouch * SDL_GetGestureTouch(SDL_TouchID id) {
static SDL_GestureTouch * SDL_GetGestureTouch(SDL_TouchID id)
{
int i;
for (i = 0; i < SDL_numGestureTouches; i++) {
//printf("%i ?= %i\n",SDL_gestureTouch[i].id,id);
if(SDL_gestureTouch[i].id == id) return &SDL_gestureTouch[i];
if (SDL_gestureTouch[i].id == id)
return &SDL_gestureTouch[i];
}
return NULL;
}
static int SDL_SendGestureMulti(SDL_GestureTouch* touch,float dTheta,float dDist) {
int SDL_SendGestureMulti(SDL_GestureTouch* touch,float dTheta,float dDist)
{
SDL_Event event;
event.mgesture.type = SDL_MULTIGESTURE;
event.mgesture.touchId = touch->id;
@ -453,7 +463,8 @@ static int SDL_SendGestureMulti(SDL_GestureTouch* touch,float dTheta,float dDist
}
static int SDL_SendGestureDollar(SDL_GestureTouch* touch,
SDL_GestureID gestureId,float error) {
SDL_GestureID gestureId,float error)
{
SDL_Event event;
event.dgesture.type = SDL_DOLLARGESTURE;
event.dgesture.touchId = touch->id;
@ -470,7 +481,8 @@ static int SDL_SendGestureDollar(SDL_GestureTouch* touch,
}
static int SDL_SendDollarRecord(SDL_GestureTouch* touch,SDL_GestureID gestureId) {
static int SDL_SendDollarRecord(SDL_GestureTouch* touch,SDL_GestureID gestureId)
{
SDL_Event event;
event.dgesture.type = SDL_DOLLARRECORD;
event.dgesture.touchId = touch->id;
@ -649,4 +661,3 @@ void SDL_GestureProcessEvent(SDL_Event* event)
}
/* vi: set ts=4 sw=4 expandtab: */