From: fang@phy.duke.edu (Fang Zhong)
Newsgroups: comp.sys.mac.programmer
Subject: Help needed for real time screen plot
Keywords: CopyBits, QuickDraw, FIFO
Date: 28 Jan 93 18:47:13 GMT


A while back I asked a question on how to make a real time screen plot
fast. I was told to look for examples in UMPG.  Enclosed is a working
source code I assmebled using CopyBits. The result of the real time plot
isn't much an improvement from that using QuickDraw only.  Several
netters in UMPG pointed out that rowBytes = 4 * N is a key factor in the
optimization.  I think that I followed this advice since I used the total
width of a single monitor as my window length. 

Also, I want to plot my real time data on screen like plotted on a chart
paper.  Only a section of the data is examed through the screen window.
This requires a First In First Out (FIFO).  The total number of data
points on screen is fixed.  So a data point on the left is pushed out and
a new data is added to the right.  I would like to get some advices on
how to do this efficiently.

Thanks in advance for any helps.

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

#include <MacHeaders.h>

WindowPtr   gWindow;
GrafPtr     drawPort;
short    xLength, yLength, n = 0;

Boolean     nFlag = false;
float    *x, *y;

#define     Npts 200

WindowPtr   SetUpWindow(void);
void     DrawWindow(void);
void     SetUpDrawPort(WindowPtr);

main()
{
   time_t      stime;

   MaxApplZone();       

   InitGraf((Ptr) &qd.thePort);
   InitFonts();
   InitWindows();
   InitMenus();
   TEInit();
   InitDialogs(nil);
   InitCursor();

   gWindow = SetUpWindow();
   SetUpDrawPort(gWindow);

   x = (float *) NewPtrClear (Npts * sizeof(float));
   y = (float *) NewPtrClear (Npts * sizeof(float));

   stime = clock();

   do {
      x[n] = (float) (clock() - stime) / CLK_TCK;
      y[n] = sin(x[n]);       /* can be any data in real world */
      DrawWindow();
      if(++n == Npts) { n = 0; nFlag = true; }

   } while ( !Button() );
}

WindowPtr SetUpWindow(void)
{  
   Rect  windRect = qd.screenBits.bounds;
   WindowPtr   window;

   windRect.top += 38;
   window = (WindowPtr) NewPtr(sizeof(WindowRecord));
   window = NewWindow((Ptr) window, &windRect, "\pGraphic", true, noGrowDocProc, (WindowPtr) -1L, false, 0); 
   xLength = windRect.right - windRect.left;
   yLength = windRect.bottom - windRect.top;

   return window;
}

void SetUpDrawPort(WindowPtr window)      /* Taken from UMPG examples */
{

   GrafPtr     oldPort;
   Rect     windRect;
   BitMap      bm;

   windRect = window->portRect;
   windRect.left = 0;
   windRect.right = windRect.right - windRect.left;
   windRect.top = 0;
   windRect.bottom = windRect.bottom - windRect.top;

   GetPort(&oldPort);
    drawPort = (GrafPtr) NewPtr(sizeof(GrafPort));
   BlockMove((Ptr) &windRect, (Ptr) &bm.bounds, sizeof(Rect));
   bm.rowBytes = ((bm.bounds.right + 15) / 16) * 2;
   bm.baseAddr = NewPtrClear (bm.rowBytes * (long) bm.bounds.bottom);
   if (bm.baseAddr != nil)
   {
      OpenPort(drawPort);
      SetPortBits(&bm);
      BlockMove((Ptr) &bm.bounds, (Ptr) &drawPort->portRect, sizeof(Rect));
      RectRgn(drawPort->visRgn, &bm.bounds);
      RectRgn(drawPort->clipRgn, &bm.bounds);
   }
    SetPort(oldPort);
}

void DrawWindow(void)
{
   short i, j;
   float xMin, xScale, yScale;

   if(n == 0 && !nFlag) return;

   j = nFlag ? n + 1 : 0;                 /* for FIFO */
   xMin = x[j];
   xScale = (float) xLength / (x[n] - xMin);
   yScale = (float) yLength / 2;

   SetPort(drawPort);
   EraseRect(&drawPort->portRect);

   MoveTo(0, (short) ((1 - y[j]) * yScale));

   if (nFlag) {                        /* FIFO starts */
      for (i = n + 1; i < Npts; i++)
         LineTo ((short) ((x[i] - xMin) * xScale),
               (short) ((1 - y[i]) * yScale));
   }

   for(i = 0; i <= n; i++)
         LineTo ((short) ((x[i] - xMin) * xScale),
               (short) ((1 - y[i]) * yScale));

   SetPort(gWindow);
   CopyBits(&drawPort->portBits, &gWindow->portBits, &drawPort->portRect,
             &gWindow->portRect, srcCopy, nil); 

}
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

-- 
	Fang Zhong				1-919-660-2524
	Duke University Dept. of Physics	fang@phy.duke.edu
	Durham, N.C.      27706			
Newsgroups: comp.sys.mac.programmer