home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / sys / mac / programm / 22374 < prev    next >
Encoding:
Text File  |  1993-01-28  |  4.4 KB  |  159 lines

  1. Path: sparky!uunet!pageworks.com!world!eff!sol.ctr.columbia.edu!emory!gatech!concert!duke!news.duke.edu!phy.duke.edu!fang
  2. From: fang@phy.duke.edu (Fang Zhong)
  3. Newsgroups: comp.sys.mac.programmer
  4. Subject: Help needed for real time screen plot
  5. Keywords: CopyBits, QuickDraw, FIFO
  6. Message-ID: <9208@news.duke.edu>
  7. Date: 28 Jan 93 18:47:13 GMT
  8. Sender: news@news.duke.edu
  9. Lines: 147
  10. Nntp-Posting-Host: physics.phy.duke.edu
  11.  
  12.  
  13. A while back I asked a question on how to make a real time screen plot
  14. fast. I was told to look for examples in UMPG.  Enclosed is a working
  15. source code I assmebled using CopyBits. The result of the real time plot
  16. isn't much an improvement from that using QuickDraw only.  Several
  17. netters in UMPG pointed out that rowBytes = 4 * N is a key factor in the
  18. optimization.  I think that I followed this advice since I used the total
  19. width of a single monitor as my window length. 
  20.  
  21. Also, I want to plot my real time data on screen like plotted on a chart
  22. paper.  Only a section of the data is examed through the screen window.
  23. This requires a First In First Out (FIFO).  The total number of data
  24. points on screen is fixed.  So a data point on the left is pushed out and
  25. a new data is added to the right.  I would like to get some advices on
  26. how to do this efficiently.
  27.  
  28. Thanks in advance for any helps.
  29.  
  30. &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  31.  
  32. #include <MacHeaders.h>
  33.  
  34. WindowPtr   gWindow;
  35. GrafPtr     drawPort;
  36. short    xLength, yLength, n = 0;
  37.  
  38. Boolean     nFlag = false;
  39. float    *x, *y;
  40.  
  41. #define     Npts 200
  42.  
  43. WindowPtr   SetUpWindow(void);
  44. void     DrawWindow(void);
  45. void     SetUpDrawPort(WindowPtr);
  46.  
  47. main()
  48. {
  49.    time_t      stime;
  50.  
  51.    MaxApplZone();       
  52.  
  53.    InitGraf((Ptr) &qd.thePort);
  54.    InitFonts();
  55.    InitWindows();
  56.    InitMenus();
  57.    TEInit();
  58.    InitDialogs(nil);
  59.    InitCursor();
  60.  
  61.    gWindow = SetUpWindow();
  62.    SetUpDrawPort(gWindow);
  63.  
  64.    x = (float *) NewPtrClear (Npts * sizeof(float));
  65.    y = (float *) NewPtrClear (Npts * sizeof(float));
  66.  
  67.    stime = clock();
  68.  
  69.    do {
  70.       x[n] = (float) (clock() - stime) / CLK_TCK;
  71.       y[n] = sin(x[n]);       /* can be any data in real world */
  72.       DrawWindow();
  73.       if(++n == Npts) { n = 0; nFlag = true; }
  74.  
  75.    } while ( !Button() );
  76. }
  77.  
  78. WindowPtr SetUpWindow(void)
  79. {  
  80.    Rect  windRect = qd.screenBits.bounds;
  81.    WindowPtr   window;
  82.  
  83.    windRect.top += 38;
  84.    window = (WindowPtr) NewPtr(sizeof(WindowRecord));
  85.    window = NewWindow((Ptr) window, &windRect, "\pGraphic", true, noGrowDocProc, (WindowPtr) -1L, false, 0); 
  86.    xLength = windRect.right - windRect.left;
  87.    yLength = windRect.bottom - windRect.top;
  88.  
  89.    return window;
  90. }
  91.  
  92. void SetUpDrawPort(WindowPtr window)      /* Taken from UMPG examples */
  93. {
  94.  
  95.    GrafPtr     oldPort;
  96.    Rect     windRect;
  97.    BitMap      bm;
  98.  
  99.    windRect = window->portRect;
  100.    windRect.left = 0;
  101.    windRect.right = windRect.right - windRect.left;
  102.    windRect.top = 0;
  103.    windRect.bottom = windRect.bottom - windRect.top;
  104.  
  105.    GetPort(&oldPort);
  106.     drawPort = (GrafPtr) NewPtr(sizeof(GrafPort));
  107.    BlockMove((Ptr) &windRect, (Ptr) &bm.bounds, sizeof(Rect));
  108.    bm.rowBytes = ((bm.bounds.right + 15) / 16) * 2;
  109.    bm.baseAddr = NewPtrClear (bm.rowBytes * (long) bm.bounds.bottom);
  110.    if (bm.baseAddr != nil)
  111.    {
  112.       OpenPort(drawPort);
  113.       SetPortBits(&bm);
  114.       BlockMove((Ptr) &bm.bounds, (Ptr) &drawPort->portRect, sizeof(Rect));
  115.       RectRgn(drawPort->visRgn, &bm.bounds);
  116.       RectRgn(drawPort->clipRgn, &bm.bounds);
  117.    }
  118.     SetPort(oldPort);
  119. }
  120.  
  121. void DrawWindow(void)
  122. {
  123.    short i, j;
  124.    float xMin, xScale, yScale;
  125.  
  126.    if(n == 0 && !nFlag) return;
  127.  
  128.    j = nFlag ? n + 1 : 0;                 /* for FIFO */
  129.    xMin = x[j];
  130.    xScale = (float) xLength / (x[n] - xMin);
  131.    yScale = (float) yLength / 2;
  132.  
  133.    SetPort(drawPort);
  134.    EraseRect(&drawPort->portRect);
  135.  
  136.    MoveTo(0, (short) ((1 - y[j]) * yScale));
  137.  
  138.    if (nFlag) {                        /* FIFO starts */
  139.       for (i = n + 1; i < Npts; i++)
  140.          LineTo ((short) ((x[i] - xMin) * xScale),
  141.                (short) ((1 - y[i]) * yScale));
  142.    }
  143.  
  144.    for(i = 0; i <= n; i++)
  145.          LineTo ((short) ((x[i] - xMin) * xScale),
  146.                (short) ((1 - y[i]) * yScale));
  147.  
  148.    SetPort(gWindow);
  149.    CopyBits(&drawPort->portBits, &gWindow->portBits, &drawPort->portRect,
  150.              &gWindow->portRect, srcCopy, nil); 
  151.  
  152. }
  153. &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
  154.  
  155. -- 
  156.     Fang Zhong                1-919-660-2524
  157.     Duke University Dept. of Physics    fang@phy.duke.edu
  158.     Durham, N.C.      27706            
  159.