Googlyeyes WIP: Constrain pupil within background

This commit is contained in:
Bill Kendrick 2022-12-24 11:59:42 -08:00
parent 16d4700066
commit 4129a8753e

View file

@ -232,6 +232,7 @@ googlyeyes_drag(magic_api * api ATTRIBUTE_UNUSED, int which, SDL_Surface * canva
int oy ATTRIBUTE_UNUSED, int x, int y, SDL_Rect * update_rect) int oy ATTRIBUTE_UNUSED, int x, int y, SDL_Rect * update_rect)
{ {
SDL_Rect dest; SDL_Rect dest;
int max_radius;
/* Set the destination for the main background */ /* Set the destination for the main background */
update_rect->x = eye_x - googlyeyes_img_bkgd[which]->w / 2; update_rect->x = eye_x - googlyeyes_img_bkgd[which]->w / 2;
@ -246,11 +247,21 @@ googlyeyes_drag(magic_api * api ATTRIBUTE_UNUSED, int which, SDL_Surface * canva
SDL_BlitSurface(googlyeyes_img_bkgd[which], NULL, canvas, update_rect); SDL_BlitSurface(googlyeyes_img_bkgd[which], NULL, canvas, update_rect);
/* 2. Draw the pupil */ /* 2. Draw the pupil */
if (sqrt(((x - eye_x) * (x - eye_x)) + ((y - eye_y) * (y - eye_y))) > max_radius = ((googlyeyes_img_bkgd[which]->w - googlyeyes_img_pupil[which]->w) / 2);
((googlyeyes_img_bkgd[which]->w - googlyeyes_img_pupil[which]->w) / 2) if (sqrt(((x - eye_x) * (x - eye_x)) + ((y - eye_y) * (y - eye_y))) > max_radius) {
) { /* If drag position would place pupil outside the circular bounds
x = eye_x; * of the background, put it on the edge, "looking towards" (pointing at)
y = eye_y; * the mouse. */
/* (Calculations borrowed from Xeyes' "computePupil()",
* https://github.com/freedesktop/xorg-xeyes/blob/master/Eyes.c) */
double dx, dy, angle;
dx = (double) (x - eye_x);
dy = (double) (y - eye_y);
angle = atan2(dy, dx);
x = eye_x + (cos(angle) * max_radius);
y = eye_y + (sin(angle) * max_radius);
} }
dest.x = x - googlyeyes_img_pupil[which]->w / 2; dest.x = x - googlyeyes_img_pupil[which]->w / 2;