home *** CD-ROM | disk | FTP | other *** search
/ Online Today 2000 January / Onto0100.iso / mac / MACAST 1.0 / MACAST Documentation / Plugin Development / SimpleBLR / Source / main.cp next >
Encoding:
Text File  |  1999-09-16  |  7.9 KB  |  348 lines  |  [TEXT/CWIE]

  1. /*
  2.     XL
  3.     ⌐1999, @soft
  4.  
  5.     Description:    Simple and NOT COMMENTED plugin for MACAST (BLR).
  6.     Version:        1.1
  7.     Released:        9/16/99
  8.     Compatibility:    MACAST 1.0
  9.     Version history:
  10.  
  11.      Date    Who        Changes
  12.     ------+------+------------------------------------------------------
  13.     091699    SKA        Updated to work with BLR API 1.2 and added an alternative
  14.                     display mode.
  15.     071199    SKA        Fixed some bugs to prevent excessive lameness with
  16.                     non-default skins.
  17.     061499    SKA        Fixed some bugs with skin compatibility in MACAST.
  18.     042799    SKA        Started the work and ended the work. Wee.
  19. */
  20.  
  21. #include "MACAST_BLR.h"
  22.  
  23. #include <string.h>
  24.  
  25. // Sorry, I am too lazy to comment all this. Deal with it. =)
  26. // It's ugly and not practical, but hey, it's an example.
  27.  
  28. const StringPtr    gCopyright = "\p= XL =\rby slava";
  29.  
  30. const StringPtr gModeNames[] = 
  31. {
  32.     "\pXL",
  33.     "\pXXL",
  34. };
  35.  
  36. static UInt16*        gArray;
  37. static UInt16        gArraySize;
  38. static UInt16        gArrayHeight;
  39. static float        gArrayDivider;
  40.  
  41. static Rect            gDigitsRect[5];
  42. static Rect            gIndicatorsRect[3];
  43. static GWorldPtr    gDigitsWorld = nil;
  44. static GWorldPtr     gIndicatorsWorld = nil;
  45. static UInt8*        gFFTArray;
  46.  
  47. BLRInfoBlock    gPlugInfo = 
  48. {
  49.     BLR_INFOBLOCK_HEADER('exmp','=XL='),    // new and easy format to define header of BLRInfoBlock.
  50.         // BLR_INFOBLOCK_HEADER needs two arguments, author and plugin id.
  51.         // They are used to identify your plugin among others, and to store
  52.         // preferences. Please do not use 'exmp' as your author code, use something
  53.         // else instead.
  54.         
  55.     BLRInitialize,
  56.     BLRTerminate,
  57.     BLRGetCopyright,
  58.     BLRRender,
  59.     BLRClick,
  60.     nil,    // no idle
  61.     nil,    // no error reporter
  62.     nil,    // no settings
  63.     nil,    // no listener
  64.     BLRGetModes,            // ÑÑ NEW for API 1.2: Display modes
  65.     BLRGetModeName,            // ÑÑ NEW for API 1.2: Display modes
  66.  
  67.     BLR_INFOBLOCK_FOOTER
  68. };
  69.  
  70. short resFile;
  71.  
  72. OSStatus    BLRInitialize(FSSpecPtr fs, Rect inRect, UInt32*)
  73. {
  74.     resFile = FSpOpenResFile(fs, fsRdPerm);
  75.     
  76.     // ugly setup
  77.     for (short i=0;i<4;i++)
  78.     {
  79.         gDigitsRect[i].left = i * 22 + (inRect.left + 15);
  80.         gDigitsRect[i].right = gDigitsRect[i].left + 22;
  81.         gDigitsRect[i].top = inRect.top + 4;
  82.         gDigitsRect[i].bottom = gDigitsRect[i].top + 40;
  83.     }
  84.     
  85.     gDigitsRect[4] = gDigitsRect[2];
  86.     gDigitsRect[4].right = gDigitsRect[4].left + 3;
  87.     ::OffsetRect(&gDigitsRect[2], 6, 0);
  88.     ::OffsetRect(&gDigitsRect[3], 6, 0);
  89.     
  90.     for (short i=0;i<3;i++)
  91.     {
  92.         gIndicatorsRect[i].right = inRect.right - 10;
  93.         gIndicatorsRect[i].left = gIndicatorsRect[i].right - 16;
  94.         gIndicatorsRect[i].top = i*12 + inRect.top+8;
  95.         gIndicatorsRect[i].bottom = gIndicatorsRect[i].top + 7;
  96.     }
  97.     
  98.     Rect temp;
  99.     
  100.     OSErr err;
  101.     
  102.     PicHandle pict = (PicHandle)::GetResource('PICT', 3478);
  103.     if (pict)
  104.     {
  105.         Rect r;
  106.         ::HLock((Handle)pict);
  107.         r = (*pict)->picFrame;
  108.         ::OffsetRect(&r, -r.left, -r.top);
  109.         
  110.         err = ::NewGWorld(&gDigitsWorld, nil, &r, nil, nil, nil);
  111.         
  112.         CGrafPtr    mSavePort;
  113.         GDHandle    mSaveDevice;
  114.  
  115.         ::GetGWorld(&mSavePort, &mSaveDevice);
  116.         ::SetGWorld(gDigitsWorld, nil);
  117.         ::LockPixels(::GetGWorldPixMap(gDigitsWorld));
  118.         ::EraseRect(&r);
  119.         ::DrawPicture(pict, &r);
  120.         ::UnlockPixels(::GetGWorldPixMap(gDigitsWorld));
  121.         ::SetGWorld(mSavePort, mSaveDevice);
  122.         ::ReleaseResource((Handle)pict);
  123.     }
  124.  
  125.     // aquire indicators gworld.
  126.     gPlugInfo.ma->GetStatusLights(&gIndicatorsWorld, &temp);
  127.     
  128.     UInt16 size;
  129.     // aquire fft array
  130.     gPlugInfo.ma->GetValues(&gFFTArray, &size);
  131.     
  132.     // well it isn't random. it's an array of values that I use to draw.
  133.     // it's gArray for historical reasons =)
  134.     gArraySize = (inRect.right-inRect.left)/2;
  135.     gArray = (UInt16*)NewPtrClear(sizeof(UInt16) * gArraySize);
  136.     
  137.     gArrayHeight = inRect.bottom - inRect.top - 1;
  138.     if (gArrayHeight <= 80)
  139.     {
  140.         gArrayDivider = 80.0 / gArrayHeight;
  141.     }
  142.     else
  143.     {
  144.         gArrayDivider = 1.0;
  145.     }
  146.     return errBLRNoErr;    
  147. }
  148.  
  149. OSStatus    BLRTerminate(UInt32*)
  150. {
  151.     if (resFile != -1)
  152.         CloseResFile(resFile);
  153.     
  154.     DisposeGWorld(gDigitsWorld);
  155.     DisposePtr((Ptr)gArray);
  156.     
  157.     gDigitsWorld = nil;
  158.     gArray = nil;
  159.  
  160.     return errBLRNoErr;
  161. }
  162.  
  163. OSStatus    BLRGetCopyright(StringPtr outCopyright, UInt32*)
  164. {
  165.     BlockMoveData(&gCopyright[1], &outCopyright[1], gCopyright[0]);
  166.     outCopyright[0] = gCopyright[0];
  167.  
  168.     return errBLRNoErr;
  169. }
  170.  
  171. OSStatus BLRRender(GWorldPtr inWorld, Rect inRect, UInt32*)
  172. {
  173.     // calc values
  174.     short a=4;
  175.     for (short i=0;i<gArraySize;i++,a+=2)
  176.     {
  177.         gArray[i] = gFFTArray[a]/gArrayDivider;
  178.                 
  179.         if (gArray[i] > gArrayHeight)
  180.             gArray[i] = gArrayHeight;
  181.     }
  182.     
  183.     
  184.     // render.
  185.     UInt16    min, sec;
  186.     GrafPtr port;
  187.     Rect rect;
  188.     UInt32    timer;
  189.     UInt32 status;
  190.     static short middle = (inRect.bottom-inRect.top)/2 - (inRect.bottom-inRect.top)/4;
  191.     
  192.     gPlugInfo.ma->GetStatus(&timer, &status);
  193.     
  194.     min = timer / 60;
  195.     sec = timer % 60;
  196.  
  197.     CGrafPtr    mSavePort;
  198.     GDHandle    mSaveDevice;
  199.  
  200.     ::GetGWorld(&mSavePort, &mSaveDevice);
  201.     ::SetGWorld(inWorld, nil);
  202.     ::LockPixels(::GetGWorldPixMap(inWorld));
  203.         
  204.     ::ForeColor(blackColor); 
  205.     ::BackColor(whiteColor);
  206.  
  207.     GetPort(&port);
  208.     
  209.     ::LockPixels(::GetGWorldPixMap(gDigitsWorld));
  210.  
  211.     SetRect(&rect, (min/10)*22, 0, (min/10)*22+22, 34);
  212.     ::CopyBits(&((GrafPtr)gDigitsWorld)->portBits, &port->portBits,
  213.             &rect, 
  214.             &gDigitsRect[0], 
  215.             adMax, nil);
  216.  
  217.     SetRect(&rect, (min%10)*22, 0, (min%10)*22+22, 34);
  218.     ::CopyBits(&((GrafPtr)gDigitsWorld)->portBits, &port->portBits,
  219.             &rect, 
  220.             &gDigitsRect[1], 
  221.             adMax, nil);
  222.  
  223.     SetRect(&rect, (sec/10)*22, 0, (sec/10)*22+22, 34);
  224.     ::CopyBits(&((GrafPtr)gDigitsWorld)->portBits, &port->portBits,
  225.             &rect, 
  226.             &gDigitsRect[2], 
  227.             adMax, nil);
  228.             
  229.     SetRect(&rect, (sec%10)*22, 0, (sec%10)*22+22, 34);
  230.     ::CopyBits(&((GrafPtr)gDigitsWorld)->portBits, &port->portBits,
  231.             &rect, 
  232.             &gDigitsRect[3], 
  233.             adMax, nil);
  234.  
  235.     SetRect(&rect, 220, 0, 223, 34);
  236.     ::CopyBits(&((GrafPtr)gDigitsWorld)->portBits, &port->portBits,
  237.             &rect, 
  238.             &gDigitsRect[4], 
  239.             adMax, nil);
  240.     ::UnlockPixels(::GetGWorldPixMap(gDigitsWorld));
  241.         
  242.     // let's copy indicators now
  243.     // repeat
  244.     if (status & statRepeat)
  245.     {
  246.         if (status & statRepeat1)
  247.             ::SetRect(&rect, 0, 14, 16, 21);
  248.         else
  249.             ::SetRect(&rect, 0, 7, 16, 14);
  250.     } else
  251.         ::SetRect(&rect, 0, 0, 16, 7);
  252.     
  253.     ::LockPixels(::GetGWorldPixMap(gIndicatorsWorld));
  254.     
  255.     ::CopyBits(&((GrafPtr)gIndicatorsWorld)->portBits, &port->portBits,
  256.             &rect, 
  257.             &gIndicatorsRect[0], 
  258.             srcCopy, nil);
  259.  
  260.     // sleep
  261.     if (status & statSleep)
  262.         ::SetRect(&rect, 16, 7, 32, 14);
  263.     else
  264.         ::SetRect(&rect, 16, 0, 32, 7);
  265.         
  266.     ::CopyBits(&((GrafPtr)gIndicatorsWorld)->portBits, &port->portBits,
  267.             &rect, 
  268.             &gIndicatorsRect[1], 
  269.             srcCopy, nil);
  270.     
  271.     // random
  272.     if (status & statRandom)
  273.         ::SetRect(&rect, 32, 7, 48, 14);
  274.     else
  275.         ::SetRect(&rect, 32, 0, 48, 7);
  276.     
  277.     ::CopyBits(&((GrafPtr)gIndicatorsWorld)->portBits, &port->portBits,
  278.             &rect, 
  279.             &gIndicatorsRect[2], 
  280.             srcCopy, nil);
  281.     ::UnlockPixels(::GetGWorldPixMap(gIndicatorsWorld));
  282.     
  283.     if (status & statStopped)
  284.     {
  285.         for (short i=0;i<gArraySize;i++)
  286.             gArray[i] = nil;
  287.     }
  288.     
  289.     ::PenMode(adMax);
  290.  
  291.     gPlugInfo.ma->SetStdColors();
  292.  
  293.     // ÑÑ╩NEW for BLR API 1.2: Display modes
  294.     SInt16 curMode = gPlugInfo.ma->GetMode();
  295.     
  296.     if (!curMode)        // mode #0 (first, 'xl')
  297.     {
  298.         MoveTo(inRect.left + 2, inRect.bottom - 4);
  299.         for (short i=0;i<gArraySize;i++)
  300.         {
  301.             Line(0, -gArray[i]);
  302.             Move(2, gArray[i]);
  303.         }
  304.     }
  305.     else                // mode #1 (second, 'xxl')
  306.     {
  307.         RGBColor darkerColor;
  308.         GetForeColor(&darkerColor);
  309.         
  310.         darkerColor.red   = (UInt16) (darkerColor.red   >> 1);
  311.         darkerColor.green = (UInt16) (darkerColor.green >> 1);
  312.         darkerColor.blue  = (UInt16) (darkerColor.blue  >> 1);
  313.         RGBForeColor(&darkerColor);
  314.         
  315.         PenSize(2,1);
  316.         MoveTo(inRect.left + 2, inRect.bottom - 4);
  317.         for (short i=0;i<gArraySize;i++)
  318.         {
  319.             Line(0, -gArray[i]);
  320.             Move(2, gArray[i]);
  321.         }        
  322.     }
  323.  
  324.     ::PenNormal();
  325.     
  326.     ::UnlockPixels(::GetGWorldPixMap(inWorld));
  327.     ::SetGWorld(mSavePort, mSaveDevice);
  328.     return errBLRNoErr;
  329. }
  330.  
  331. Boolean BLRClick(Point, UInt32*)
  332. {
  333.     return false;
  334. }
  335.  
  336. SInt16 BLRGetModes()
  337. {
  338.     return 2;    // we support 2 modes
  339. }
  340.  
  341. // Get mode name for the current mode (we're using a static array with mode names)
  342. OSStatus BLRGetModeName(SInt16 inMode, StringPtr outName)
  343. {
  344.     BlockMoveData(&gModeNames[inMode][1], &outName[1], gModeNames[inMode][0]);
  345.     outName[0] = gModeNames[inMode][0];
  346.  
  347.     return errBLRNoErr;
  348. }