home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / demos / xgas / timestep.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-18  |  2.7 KB  |  108 lines

  1. /*
  2.  * timestep.c
  3.  *   xgas: Copyright 1990 Larry Medwin: @(#)timestep.c    1.1 2/9/90
  4.  */
  5.  
  6. #include "xgas.h"
  7.  
  8. /*
  9.  * DO TIMESTEP
  10.  *
  11.  * do the calculations required for one timestep and
  12.  *    replace this routine on the event handler "callback list."
  13.  */
  14. void doTimestep( data, id)    /* ARGSUSED */
  15.     LabData *data;
  16.     XtIntervalId *id;
  17. {
  18.     oneTimestep( data->lab, data);
  19.     
  20.     /* Put back callback (see Young's clock example) */
  21.     data->timer =
  22.     XtAppAddTimeOut(XtWidgetToApplicationContext(data->lab),
  23.             (unsigned long)data->delay, 
  24.             (XtTimerCallbackProc)doTimestep, (XtPointer)data);
  25. }
  26.  
  27. /*
  28.  *  ONE TIMESTEP
  29.  *    used as callback for "step" pushbutton and called by doTimestep()
  30.  */
  31. void oneTimestep( w, data)    /* ARGSUSED */
  32.     Widget w;
  33.     LabData *data;
  34. {
  35.     int i, k;
  36.     Arg wargs[1];
  37.     char str[20];
  38.  
  39.     /* MOVE MOLECULES */
  40.     for( i=0; i<data->nmolecules; i++) {
  41.  
  42.     /* Compute next position */
  43.     dynamics( &data->molecules[i], data);
  44.     }
  45.  
  46.     /*
  47.      *  DRAW MOLECULES with XOR:
  48.      *    Draw new molecule positions and erase old positions
  49.      *        at the same time.
  50.      *    Draw 2*nmolecules rectangles:
  51.      *      allPos[i][timestep%2] are the current positions
  52.      *        (save them)
  53.      *      allPos[i][(timestep+1)%2] are the old positions
  54.      *        (draw the new rectangles here)
  55.      */
  56.  
  57.     /* Which half of allPos array gets the new positions? */
  58.     k = (data->timestep + 1) % 2;
  59.  
  60.     /* Copy molecules to allPos array */
  61.     for( i=0; i<data->nmolecules; i++) {
  62.     data->allPos[ 2*i + k].x
  63.         = (short) data->molecules[i].pos.x * data->scale.x;
  64.     data->allPos[ 2*i + k].y
  65.         = (short) data->molecules[i].pos.y * data->scale.y;
  66.     }
  67.  
  68.     /* Now draw all 2*nmolecule rectangles */
  69.     XFillRectangles( XtDisplay(data->lab), XtWindow(data->lab),
  70.     data->MoleculeGC, (XRectangle *) data->allPos,
  71.     2 * data->nmolecules);
  72.  
  73.     /* Advance time */
  74.     data->timestep++;
  75.     data->time = data->timestep * data->timestepSize;
  76.  
  77.     /* Make new time string */
  78.     sprintf( str, "%6.3f msec", 1.0e-3 * data->time);
  79.  
  80.     /* Tell the clock widget */
  81.     XtSetArg( wargs[0], XtNlabel, str);
  82.     XtSetValues( data->clock, wargs, 1);
  83. }
  84.  
  85. /* RUN and PAUSE CALLBACKS */
  86. void run_callback(w, data, call_data) /* ARGSUSED */
  87.      Widget     w;
  88.      LabData    *data;
  89.      caddr_t    call_data;
  90. {
  91.      if(! data->timer)
  92.     data->timer =
  93.        XtAppAddTimeOut(XtWidgetToApplicationContext(w),
  94.                (unsigned long)data->delay,
  95.                (XtTimerCallbackProc)doTimestep,(XtPointer)data);
  96. }
  97.  
  98. void pause_callback(w, data, call_data)    /* ARGSUSED */
  99.      Widget     w;
  100.      LabData    *data;
  101.      caddr_t    call_data;
  102. {
  103.      if(data->timer) {
  104.     XtRemoveTimeOut( data->timer);
  105.     data->timer = 0;
  106.      }
  107. }
  108.