home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Science⁄Math / VideoToolbox / Demos / NoiseVBL.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-07  |  7.2 KB  |  202 lines  |  [TEXT/KAHL]

  1. /*
  2. NoiseVBL.c
  3.  
  4. There are two ways to synchronize your program to a video display. Apple
  5. recommends using the vertical blanking level interrupt (VBL). That approach is
  6. illustrated here. The other approach, which I normally prefer, is to use a side
  7. effect of loading the video cards clut: most video drivers don't return until
  8. the vertical blanking interval. The interrupt approach is more or less
  9. guaranteed to work on all video cards, since Apple more or less requires that it
  10. be supported by all video drivers. Its only disadvantage is that 
  11. interrupts must be enabled. I have the impression--though I haven't collected
  12. hard data--that interrupts steal a lot of time, depending on how busy AppleTalk,
  13. the disk, SCSI, keyboard, mouse, and even miscellaneous INITs that are driven by
  14. VBL interrupts. So when I really want to crank, to display movies, I prefer to
  15. use SetPriority(7) to temporarily suspend all interrupts. In that case the
  16. video-driver side effect approach is your only choice. However, be warned that
  17. some video drivers don't wait, and others take more than a frame to load the
  18. clut, so do some careful timing before you rely on it, i.e. run TimeVideo.
  19.  
  20. This program shows a noise movie, a dynamic random checkerboard, at 66.7 Hz! The
  21. key routine, which shows each frame, is in the file CopyBitsQuickly.c For best
  22. results you should allocate lots of memory to this program, e.g. 3MB. It
  23. pre-computes as many noise frames as space allows. After showing each frame once
  24. it cycles through them again. Thus to get a true impression of white noise you
  25. need to compute lots of noise frames, e.g. 100. This may require several
  26. megabytes, depending what your screen's pixel depth is set to.
  27.  
  28. HISTORY:
  29. 11/23/88 Denis Pelli wrote it.
  30.  
  31. 4/20/91 I updated this. It now has essentially no hardware dependencies. It does
  32. require that the video be in a slot, since it uses slot interrupts. A basic
  33. problem with the scheme is that interrupts seem to be shut out while the
  34. interrupt service routines are running. As a result the timing information is
  35. inaccurate since many 1 ms interrupts are lost during the call to
  36. CopyBitsQuickly(). I think the only solution to this would be to put the time
  37. consuming call to CopyBitsQuickly back in the main program, waiting for the
  38. interrupt service routine to give it permission to start each new frame.
  39.  
  40. 4/15/92 Removed all bugs and updated to work with THINK C 5. Now properly set A5
  41. in the interrupt service routines, using the method suggested in Tech Note 180.
  42. I removed the old attempt to disable AppleTalk, which now seems to hang up when
  43. it attempts to reenable AppleTalk. I also removed the clut manipulations.
  44.  
  45. 4/16/92    Put the noise in its own window. Run essentially continuously until
  46. user quits.
  47.  
  48. 8/19/92 Replaced obsolete TimeIt.c by new Timer.c. Timing seems to be fine now.
  49.  
  50. 8/22/92 dgp Moved most of the interrupt service routine code to 
  51. VBLInterruptServiceRoutine.c. Just for fun, I made the program use an alternate monitor,
  52. if available. Fixed a small bug that prevented display of noise on an alternate
  53. monitor if it had a different pixelSize than the main screen.
  54.  
  55. 9/15/92 dgp Updated to use new Timer.c.
  56.  
  57. 2/18/93    dgp    Added fpu test.
  58. 2/20/93    dgp    Call Require(), increased stack space.
  59. 7/7/93    dgp replaced call to CopyBitsQuickly by CopyBits, for compatibility with
  60. Radius PowerView.
  61. */
  62.  
  63. #include "VideoToolbox.h"
  64. #if THINK_C
  65.     #include <console.h>
  66. #endif
  67.  
  68. /* The user may wish to adjust these. WIDTH should be a multiple of 4. */
  69. #define MAXFRAMES    200        // number of frames in movie
  70. #define WIDTH    256            /* width of displayed noise movie, in pixels */
  71. #define HEIGHT    256            /* height of displayed noise movie, in pixels */
  72. #define RANDOMPHASE 1        /* randomly shift each movie frame */
  73.  
  74. /* This is just for reference. The original is in VideoToolbox.h
  75. struct VBLTaskAndA5 {
  76.     volatile VBLTask vbl;
  77.     long ourA5;
  78.     void (*subroutine)(struct VBLTaskAndA5 *vblData);
  79.     GDHandle device;
  80.     long slot;
  81.     volatile long newFrame;                // Boolean
  82.     volatile long framesLeft;            // count down to zero
  83.     long framesDesired;
  84.     void *ptr;                            // use this for whatever you want
  85. };
  86. typedef struct VBLTaskAndA5 VBLTaskAndA5;
  87. */
  88.  
  89. void main(void);
  90.  
  91. void main()
  92. {
  93.     int i,j,error;
  94.     int dx=32,dy=32,dt=1,duration;
  95.     long frames;
  96.     unsigned long seed;
  97.     double s;
  98.     static char string[64];
  99.     Rect r;
  100.     short noiseI=0,noiseN=MAXFRAMES;
  101.     static PixMap noiseImage[MAXFRAMES];
  102.     static short noiseSequence[MAXFRAMES]; /* shuffled sequence */
  103.     VBLTaskAndA5 noiseVBL;
  104.     WindowPtr window,oldPort;
  105.     EventRecord theEvent;
  106.     int frameDone;
  107.     Timer *timer;
  108.     
  109.     StackGrow(10000);
  110.     Require(gestalt8BitQD);
  111.     GetDateTime(&seed);
  112.     srand(seed);
  113.     /* INITIALIZE QuickDraw */
  114.     #if THINK_C
  115.         console_options.nrows = 3;
  116.         printf("\n");
  117.     #else
  118.         InitGraf((Ptr) &thePort);
  119.         InitFonts();
  120.         InitWindows();
  121.         InitCursor();
  122.     #endif
  123.  
  124.     // pick a screen
  125.     noiseVBL.device=GetScreenDevice(1);
  126.     if(noiseVBL.device==NULL)noiseVBL.device=GetScreenDevice(0);
  127.  
  128.     printf("Enter width of check in pixels (%d)?",dx);
  129.     gets(string);
  130.     sscanf(string,"%d",&dx);
  131.     printf("%d\n",dx);
  132.     printf("Enter height of check in pixels (%d)?",dy);
  133.     gets(string);
  134.     sscanf(string,"%d",&dy);
  135.     printf("%d\n",dy);
  136.     printf("Enter duration of check in frames (%d)?",dt);
  137.     gets(string);
  138.     sscanf(string,"%d",&dt);
  139.     printf("%d\n",dt);
  140.  
  141.     GetPort(&oldPort);
  142.     SetRect(&r,0,0,WIDTH,WIDTH);
  143.     CenterRectInRect(&r,&(*noiseVBL.device)->gdRect);
  144.     OffsetRect(&r,-(r.left%32),0);
  145.     window=NewCWindow(NULL,&r,"\pComputing noise ...",1,noGrowDocProc,(WindowPtr) -1L,0,0L);
  146.     SetPort(window);
  147.     for(i=0;i<MAXFRAMES;i++){
  148.         noiseImage[i].pixelSize=(**(**GetWindowDevice(window)).gdPMap).pixelSize;
  149.         noiseImage[i].bounds=window->portRect;
  150.         noiseImage[i].baseAddr=NULL;
  151.         error=MakeNoise1(dx,dy,RANDOMPHASE,&noiseImage[i]);
  152.         if(error)break;
  153.     }
  154.     noiseN=i;
  155.     SetPort(oldPort);
  156.     printf("Using %d different frames of noise.\n",noiseN);
  157.     for(i=0;i<noiseN;i++)noiseSequence[i]=i; /* initialize sequence */
  158.     FlushEvents(-1L,0);
  159.     printf("Hit any key to quit.\n");
  160.     SetWTitle(window,"\pNoiseVBL");
  161.     timer=NewTimer();
  162.     do{
  163.         SelectWindow(window);
  164.         Shuffle(noiseSequence,noiseN);    /* shuffle the images */
  165.         noiseI=0;
  166.         noiseVBL.subroutine=NULL;    // use default
  167.         error=VBLInstall(&noiseVBL,noiseVBL.device,MAXFRAMES);
  168.         if(error)PrintfExit("VBLInstall: error %d\n",error);
  169.         noiseVBL.vbl.vblCount=1;    // Enable interrupt service routine
  170.         while(!noiseVBL.newFrame);    // wait for first frame
  171.         noiseVBL.newFrame=0;
  172.         while(!noiseVBL.newFrame);    // wait for second frame
  173.         StartTimer(timer);
  174.         frames=noiseVBL.framesLeft;
  175.         while(noiseVBL.framesLeft>1){
  176.             if(noiseVBL.newFrame){
  177.                 noiseVBL.newFrame=0;
  178.                 CopyBits((BitMap *)&(noiseImage[noiseSequence[noiseI]])
  179.                     ,(BitMap *)*((CGrafPtr)window)->portPixMap
  180.                     ,&noiseImage[0].bounds,&window->portRect,srcCopy,NULL);
  181.                 if(noiseVBL.framesLeft%dt==0){
  182.                     noiseI++;
  183.                     if(noiseI>=noiseN) {
  184.                         Shuffle(noiseSequence,noiseN);
  185.                         noiseI=0;
  186.                     }
  187.                 }
  188.             }
  189.         }
  190.         while(!noiseVBL.newFrame);    // wait for last frame
  191.         s=StopTimer(timer)/1000000.;
  192.         frames-=noiseVBL.framesLeft;
  193.         VBLRemove(&noiseVBL);
  194.         printf("%.1f Hz \r",frames/s);
  195.     }while(!GetNextEvent(keyDown+keyUp+mouseDown,&theEvent));
  196.     FlushEvents(-1L,0);
  197.     for (i=0;i<noiseN;i++) DisposPtr((Ptr) noiseImage[i].baseAddr);
  198.     DisposeWindow(window);
  199.     DisposeTimer(timer);
  200.     abort();
  201. }
  202.