home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / The Hacks / moldyn / source / CMolDynView.cp < prev    next >
Encoding:
Text File  |  1999-06-25  |  6.8 KB  |  284 lines  |  [TEXT/CWIE]

  1. // ===========================================================================
  2. //    LOffscreenView.cp           ©1994-1998 Metrowerks Inc. All rights reserved.
  3. // ===========================================================================
  4. //
  5. //    A View whose image draws offscreen and then gets copied to the screen
  6.  
  7. #ifdef PowerPlant_PCH
  8.     #include PowerPlant_PCH
  9. #endif
  10.  
  11. #include <math.h>
  12. #include <Quickdraw.h>
  13. #include <limits.h>
  14.  
  15. #include "CMD_Fade.h"
  16. #include "CMolDynView.h"
  17.  
  18. PP_Begin_Namespace_PowerPlant
  19.  
  20.  
  21. // ---------------------------------------------------------------------------
  22. //    • CMolDynView                        Constructor                  [public]
  23. // ---------------------------------------------------------------------------
  24.  
  25. CMolDynView::CMolDynView()
  26.     : LView /* LOffscreenView */()
  27. {
  28.     MolDynInit();
  29. }
  30.  
  31.  
  32. // ---------------------------------------------------------------------------
  33. //    • ~CMolDynView                        Parameterized Constructor [public]
  34. // ---------------------------------------------------------------------------
  35.  
  36. CMolDynView::CMolDynView(
  37.     const SPaneInfo&    inPaneInfo,
  38.     const SViewInfo&    inViewInfo)
  39.     
  40.     : LView /* LOffscreenView */(inPaneInfo, inViewInfo)
  41. {
  42.     MolDynInit();
  43. }
  44.  
  45.  
  46. // ---------------------------------------------------------------------------
  47. //    • LOffscreenView                        Stream Constructor          [public]
  48. // ---------------------------------------------------------------------------
  49.  
  50. CMolDynView::CMolDynView(
  51.     LStream*    inStream)
  52.     
  53.     : LView /* LOffscreenView */(inStream)
  54. {
  55.     MolDynInit();
  56.     StartRepeating();
  57. }
  58.  
  59.  
  60. // ---------------------------------------------------------------------------
  61. //    • ~CMolDynView                        Destructor                  [public]
  62. // ---------------------------------------------------------------------------
  63.  
  64. CMolDynView::~CMolDynView()
  65. {
  66. }
  67.  
  68. void CMolDynView::MolDynInit()
  69. {
  70.     t = 0.0;
  71.     delta = 0.0;
  72.     twoPi = acos(-1.0);
  73.     twoPi = 2.0 * twoPi;
  74.     delta = twoPi / 36.0;
  75.     
  76. }
  77.  
  78. // LView.h
  79. void
  80. CMolDynView::SpendTime(
  81.     const EventRecord&        inMacEvent)
  82. {
  83. #pragma unused (inMacEvent)
  84.     Refresh();
  85. }
  86.  
  87. short isPressed(unsigned short k );
  88. const short key_option = 0x3a;
  89. const short key_shift  = 0x38;
  90. const short key_control  = 0x3b;
  91.  
  92. void CMolDynView::DrawSelf()
  93. {
  94.     unsigned long ticks = TickCount();
  95.     ticks += 60;
  96.     while (TickCount() < ticks)
  97.     {
  98.         const int boxWidth  = 256;        //    240 x 240 windows satisfy Andy A.'s criteria
  99.         const int boxHeight    = 256;
  100.  
  101.         const int boxMargin = 20;
  102.         
  103.         const int ballWidth  = 20;
  104.         const int ballHeight = 20;
  105.         t += delta;
  106.         if (t > twoPi - delta/16.0)
  107.             t = 0.0;    
  108.  
  109.         Rect        ballRect;
  110.         double xOff, yOff;
  111.         long int    newLeft,
  112.                         newTop;
  113.         //    Draw Atom at new location
  114.         RGBColor rgbRandom;
  115.         rgbRandom.red   = (unsigned short)Random();
  116.         rgbRandom.green = (unsigned short)Random();
  117.         rgbRandom.blue  = (unsigned short)Random();
  118.         RGBForeColor(&rgbRandom);
  119.         const double r = boxHeight / 2 - boxMargin;
  120.         xOff = r * cos(t);
  121.         yOff = r * sin(t);
  122.         newLeft = (boxWidth-ballWidth)   / 2 + xOff;
  123.         newTop  = (boxHeight-ballHeight) / 2 + yOff;
  124.         SetRect(&ballRect, (short)newLeft, (short)newTop, (short)(newLeft+ballWidth), (short)(newTop+ballHeight));
  125.         MoveTo((short)newLeft, (short)newTop);
  126.         PaintOval (&ballRect);
  127.         
  128.         // optimized versions of the fader require 24 bit color (32 bits per pixel)
  129.         // and only use the last byte of the RGB color in the Pixel.
  130.         CGrafPtr port;
  131.         GDHandle gdh;
  132.         GetGWorld(&port, &gdh);
  133.         PixMapHandle hPixMap = GetGWorldPixMap(port);
  134.         LockPixels(hPixMap);
  135.         unsigned long *pBase = (unsigned long *)GetPixBaseAddr(hPixMap);
  136.  
  137.         unsigned long *pRow, *p;
  138.         short widthInPixels;
  139.         
  140.         unsigned long rowBytes = (**hPixMap).rowBytes & 0x3fff;
  141.         
  142.         pBase += (port->portRect.top - (**hPixMap).bounds.top) * rowBytes >> 2;
  143.         pBase += (port->portRect.left - (**hPixMap).bounds.left);
  144.  
  145.         widthInPixels = (port->portRect.right - port->portRect.left);
  146.         
  147.         Boolean key_option_down = isPressed(key_option);
  148.         Boolean key_shift_down = isPressed(key_shift);
  149.  
  150.         unsigned long uTest = ((unsigned long)pBase % 16);
  151.         if (uTest == 8)
  152.         {
  153.             pBase += 2;
  154.             widthInPixels -= 2;
  155.         }
  156.         
  157.         uTest = ((unsigned long)pBase % 16);
  158.         if (uTest)
  159.             DebugStr((const unsigned char *)"\pOops!  The alignment is off!");
  160.             
  161.         short pixelSize = (**hPixMap).pixelSize;
  162.         if (pixelSize != 32)
  163.             DebugStr((const unsigned char *)"\pOops! Wrong bit depth!");
  164.         
  165.         if (key_option_down && key_shift_down && isPressed(key_control))
  166.         {
  167.             Debugger(); // Have to check the alignment
  168.         }
  169.  
  170.         if (key_option_down && key_shift_down)
  171.         {
  172.             md_vectorFade(pBase, boxHeight, widthInPixels, rowBytes);
  173.         }
  174.         else if (key_option_down)
  175.         {
  176.             pRow = pBase;
  177.             for (int i = 0; i < boxHeight; i++) {    // row
  178.                 p = pRow;
  179.                 for (int j = 0; j < widthInPixels; j++)
  180.                 {
  181.                     unsigned long uOrig = *p;
  182.  
  183.                     unsigned long uTemp = 0x000000ff & uOrig;
  184.                     uTemp += 40;
  185.                     if (uTemp > 0xff)
  186.                         uTemp = 0xff;
  187.                     *p = uTemp;
  188.  
  189.                     uTemp = (0x0000ff00 & uOrig) >> 8;
  190.                     uTemp += 40;
  191.                     if (uTemp > 0xff)
  192.                         uTemp = 0xff;
  193.                     *p += uTemp << 8;
  194.  
  195.                     uTemp = (0x00ff0000 & uOrig) >> 16;
  196.                     uTemp += 40;
  197.                     if (uTemp > 0xff)
  198.                         uTemp = 0xff;
  199.                     *p += uTemp << 16;
  200.                     
  201.                     p++;
  202.                 }
  203.                 pRow += rowBytes >> 2;
  204.             }
  205.         }
  206.         else
  207.         {
  208.             // pixel-by-pixel always works, but is slow
  209.             for (int i = 0; i < boxHeight; i++)
  210.                 for (int j = 0; j < boxHeight; j++)
  211.                 {
  212.                     RGBColor rgbColor;
  213.                     GetCPixel(j, i, &rgbColor);
  214.                     double dRed   = (rgbColor.red   + USHRT_MAX + 0.5) / 2.0;
  215.                     double dGreen = (rgbColor.green + USHRT_MAX + 0.5) / 2.0;
  216.                     double dBlue  = (rgbColor.blue  + USHRT_MAX + 0.5) / 2.0;
  217.                     rgbColor.red   = dRed;
  218.                     rgbColor.green = dGreen;
  219.                     rgbColor.blue  = dBlue;
  220.                     SetCPixel(j, i, &rgbColor);
  221.                 }
  222.         }
  223.         UnlockPixels(hPixMap);
  224.     }
  225. }
  226.  
  227.  
  228.  
  229. short isPressed(unsigned short k )
  230. // k =  any keyboard scan code, 0-127
  231. {
  232.     unsigned char km[16];
  233.  
  234.     GetKeys( (unsigned long *) km);
  235.     return ( ( km[k>>3] >> (k & 7) ) & 1);
  236. }
  237.     
  238. // ---------------------------------------------------------------------------
  239. //    • Unused codes scraps follow
  240. // ---------------------------------------------------------------------------
  241.         #if 0
  242.         // This fades from black to white.
  243.         // Assumes 32 bits per pixel
  244.         int iFade;
  245.         for (iFade = 0; iFade < 256; iFade++)
  246.         {
  247.             pRow = pBase;
  248.             for (int i = 0; i < 260; i++)
  249.             {    // row
  250.                 p = pRow;
  251.                 for (int j = 0; j < widthInPixels; j++)
  252.                 {
  253.                     *p++ = iFade << 16 | iFade << 8 | iFade;
  254.                 }
  255.                 pRow += rowBytes >> 2;
  256.             }
  257.         }
  258.         #endif
  259.  
  260.         #if 0
  261.         // One shade at a time fade
  262.         for (iFade = 0; iFade < 1; iFade++)
  263.         {
  264.             pRow = pBase;
  265.             for (int i = 0; i < 260; i++) {    // row
  266.                 p = pRow;
  267.                 
  268.                 for (int j = 0; j < widthInPixels; j++)
  269.                 {
  270.                     // Assumes 32-bit and gray!
  271.                     // Only use the last byte of the RGB color in the Pixel.
  272.                     unsigned long uTemp = (0x000000ff & *p);
  273.                     unsigned long uPlus = uTemp + 1;
  274.                     uTemp = (uTemp | uPlus) & 0x000000ff;
  275.                     *p++ = uTemp << 16 | uTemp << 8 | uTemp;
  276.                 }
  277.                 
  278.                 pRow += rowBytes >> 2;
  279.             }
  280.         }
  281.         #endif
  282.  
  283. PP_End_Namespace_PowerPlant
  284.