/*----------------------------------------------------------------------------
 *     anim.c
 *  
 *    This file contains the routines to animate the arm based on the joint
 * angle changes selected by the user.  A double buffered Intuition screen
 * is used.
 *----------------------------------------------------------------------------
*/

#include  <exec/types.h>
#include  <lattice/stdio.h>
#include  <intuition/intuition.h>
#include  "defs.h"
#include  "ds.h"

extern struct RasInfo   *ri ;
extern struct BitMap    b,  b2 ;
extern struct RastPort  rp,  rp2 ;
extern struct Screen    *screen ;
extern ROTATION_INFO    rot_info ;


open_db_screen_for_animation ()
{
 int  num_frames ;
 int  i ;

 /*---------------------------------------------------------------------------
  *   The number of frames required to display the animation, and the start
  * angles and angle increments per frame (for each axis of each joint) has 
  * been previously calculated.
  *   This routine draws a frame, updates the joint angles, then switches
  * screen memory to the the bitplanes just drawn into (via its rastport).
  *   The swapped out screen memory is then drawn into for the next frame,
  * and the process repeats until the required number of frames are drawn.
  *---------------------------------------------------------------------------
 */
 screen->RastPort.BitMap  = &b2 ;      /* put 2nd set of bitplanes */
 ri->BitMap  = &b2 ;                   /* on the screen */
 MakeScreen (screen) ;
 RethinkDisplay () ;

 num_frames  = SPFix (rot_info.num_frames) ;

 for (i = 0;  i <= num_frames ;  i = i + 2)   {

    set_up_frame () ;                  /* update joint angles */
    transform_links () ;               /* new eye coordinates for view */
    set_screen_coords () ;
    draw_image (&rp) ;                 /* draw into 1st set of bit planes */

    screen->RastPort.BitMap  = &b ;    /* swap screen memory to 1st screen */
    ri->BitMap  = &b ;
    MakeScreen (screen) ;        
    RethinkDisplay () ;

    set_up_frame () ;                  /* update joint angles */
    transform_links () ;               /* new eye coordinates for view */
    set_screen_coords () ;
    draw_image (&rp2) ;                /* draw into 2nd set of bit planes */

    screen->RastPort.BitMap  = &b2 ;   /* swap screen memory to 2nd screen */
    ri->BitMap  = &b2 ;
    MakeScreen (screen) ;
    RethinkDisplay () ;
 }

 SetRast (&rp, COLOR0) ;
 for (i = 0;  i < 200000;  ++i)      /* let user have a look at last frame */
     ;
 screen->RastPort.BitMap  = &b ;
 ri->BitMap  = &b ;
 MakeScreen (screen) ;
 RethinkDisplay () ;
 SetRast (&rp2, COLOR0) ;
}


set_up_frame ()
{
 int    i ;

 /*---------------------------------------------------------------------------
  *    Increment all the joint angles by the required amount for the 
  * next frame.  Set the local link transformations based on these new angles.
  *---------------------------------------------------------------------------
 */
 for (i = X;  i <= Z;  ++i)  {
     rot_info.angle [SH][i].current  = SPAdd (rot_info.angle [SH][i].current,
                                              rot_info.angle [SH][i].incr) ;
     rot_info.angle [WR][i].current  = SPAdd (rot_info.angle [WR][i].current,
                                              rot_info.angle [WR][i].incr) ;
 }
 rot_info.angle [EL][X].current  = SPAdd (rot_info.angle [EL][X].current,
                                           rot_info.angle [EL][X].incr) ;

 fix_all_joint_transformations () ;    /* update local link transformations */
}
