+ /* bounce off the walls if desired
+ note: a ball is actually its upper left corner */
+ if(st->walls_p)
+ {
+ if(st->cbounce_p) /* with correct bouncing */
+ {
+ /* so long as it's out of range, keep bouncing */
+ /* limit the maximum number to bounce to 4.*/
+ int bounce_allowed = 4;
+
+ while( bounce_allowed && (
+ (st->balls[i].x >= (st->xlim - st->balls[i].size)) ||
+ (st->balls[i].y >= (st->ylim - st->balls[i].size)) ||
+ (st->balls[i].x <= 0) ||
+ (st->balls[i].y <= 0) )
+ )
+ {
+ bounce_allowed--;
+ if (st->balls[i].x >= (st->xlim - st->balls[i].size))
+ {
+ st->balls[i].x = (2*(st->xlim - st->balls[i].size) - st->balls[i].x);
+ st->balls[i].vx = -st->balls[i].vx;
+ }
+ if (st->balls[i].y >= (st->ylim - st->balls[i].size))
+ {
+ st->balls[i].y = (2*(st->ylim - st->balls[i].size) - st->balls[i].y);
+ st->balls[i].vy = -st->balls[i].vy;
+ }
+ if (st->balls[i].x <= 0)
+ {
+ st->balls[i].x = -st->balls[i].x;
+ st->balls[i].vx = -st->balls[i].vx;
+ }
+ if (st->balls[i].y <= 0)
+ {
+ st->balls[i].y = -st->balls[i].y;
+ st->balls[i].vy = -st->balls[i].vy;
+ }
+ }
+ }
+ else /* with old bouncing */
+ {
+ if (st->balls[i].x >= (st->xlim - st->balls[i].size))
+ {
+ st->balls[i].x = (st->xlim - st->balls[i].size - 1);
+ if (st->balls[i].vx > 0) /* why is this check here? */
+ st->balls[i].vx = -st->balls[i].vx;
+ }
+ if (st->balls[i].y >= (st->ylim - st->balls[i].size))
+ {
+ st->balls[i].y = (st->ylim - st->balls[i].size - 1);
+ if (st->balls[i].vy > 0)
+ st->balls[i].vy = -st->balls[i].vy;
+ }
+ if (st->balls[i].x <= 0)
+ {
+ st->balls[i].x = 0;
+ if (st->balls[i].vx < 0)
+ st->balls[i].vx = -st->balls[i].vx;
+ }
+ if (st->balls[i].y <= 0)
+ {
+ st->balls[i].y = 0;
+ if (st->balls[i].vy < 0)
+ st->balls[i].vy = -st->balls[i].vy;
+ }
+ }
+ }
+ new_x = st->balls[i].x;
+ new_y = st->balls[i].y;
+
+ if (!mono_p)