home *** CD-ROM | disk | FTP | other *** search
Text File | 1999-06-25 | 6.8 KB | 284 lines | [TEXT/CWIE] |
- // ===========================================================================
- // LOffscreenView.cp ©1994-1998 Metrowerks Inc. All rights reserved.
- // ===========================================================================
- //
- // A View whose image draws offscreen and then gets copied to the screen
-
- #ifdef PowerPlant_PCH
- #include PowerPlant_PCH
- #endif
-
- #include <math.h>
- #include <Quickdraw.h>
- #include <limits.h>
-
- #include "CMD_Fade.h"
- #include "CMolDynView.h"
-
- PP_Begin_Namespace_PowerPlant
-
-
- // ---------------------------------------------------------------------------
- // • CMolDynView Constructor [public]
- // ---------------------------------------------------------------------------
-
- CMolDynView::CMolDynView()
- : LView /* LOffscreenView */()
- {
- MolDynInit();
- }
-
-
- // ---------------------------------------------------------------------------
- // • ~CMolDynView Parameterized Constructor [public]
- // ---------------------------------------------------------------------------
-
- CMolDynView::CMolDynView(
- const SPaneInfo& inPaneInfo,
- const SViewInfo& inViewInfo)
-
- : LView /* LOffscreenView */(inPaneInfo, inViewInfo)
- {
- MolDynInit();
- }
-
-
- // ---------------------------------------------------------------------------
- // • LOffscreenView Stream Constructor [public]
- // ---------------------------------------------------------------------------
-
- CMolDynView::CMolDynView(
- LStream* inStream)
-
- : LView /* LOffscreenView */(inStream)
- {
- MolDynInit();
- StartRepeating();
- }
-
-
- // ---------------------------------------------------------------------------
- // • ~CMolDynView Destructor [public]
- // ---------------------------------------------------------------------------
-
- CMolDynView::~CMolDynView()
- {
- }
-
- void CMolDynView::MolDynInit()
- {
- t = 0.0;
- delta = 0.0;
- twoPi = acos(-1.0);
- twoPi = 2.0 * twoPi;
- delta = twoPi / 36.0;
-
- }
-
- // LView.h
- void
- CMolDynView::SpendTime(
- const EventRecord& inMacEvent)
- {
- #pragma unused (inMacEvent)
- Refresh();
- }
-
- short isPressed(unsigned short k );
- const short key_option = 0x3a;
- const short key_shift = 0x38;
- const short key_control = 0x3b;
-
- void CMolDynView::DrawSelf()
- {
- unsigned long ticks = TickCount();
- ticks += 60;
- while (TickCount() < ticks)
- {
- const int boxWidth = 256; // 240 x 240 windows satisfy Andy A.'s criteria
- const int boxHeight = 256;
-
- const int boxMargin = 20;
-
- const int ballWidth = 20;
- const int ballHeight = 20;
- t += delta;
- if (t > twoPi - delta/16.0)
- t = 0.0;
-
- Rect ballRect;
- double xOff, yOff;
- long int newLeft,
- newTop;
- // Draw Atom at new location
- RGBColor rgbRandom;
- rgbRandom.red = (unsigned short)Random();
- rgbRandom.green = (unsigned short)Random();
- rgbRandom.blue = (unsigned short)Random();
- RGBForeColor(&rgbRandom);
- const double r = boxHeight / 2 - boxMargin;
- xOff = r * cos(t);
- yOff = r * sin(t);
- newLeft = (boxWidth-ballWidth) / 2 + xOff;
- newTop = (boxHeight-ballHeight) / 2 + yOff;
- SetRect(&ballRect, (short)newLeft, (short)newTop, (short)(newLeft+ballWidth), (short)(newTop+ballHeight));
- MoveTo((short)newLeft, (short)newTop);
- PaintOval (&ballRect);
-
- // optimized versions of the fader require 24 bit color (32 bits per pixel)
- // and only use the last byte of the RGB color in the Pixel.
- CGrafPtr port;
- GDHandle gdh;
- GetGWorld(&port, &gdh);
- PixMapHandle hPixMap = GetGWorldPixMap(port);
- LockPixels(hPixMap);
- unsigned long *pBase = (unsigned long *)GetPixBaseAddr(hPixMap);
-
- unsigned long *pRow, *p;
- short widthInPixels;
-
- unsigned long rowBytes = (**hPixMap).rowBytes & 0x3fff;
-
- pBase += (port->portRect.top - (**hPixMap).bounds.top) * rowBytes >> 2;
- pBase += (port->portRect.left - (**hPixMap).bounds.left);
-
- widthInPixels = (port->portRect.right - port->portRect.left);
-
- Boolean key_option_down = isPressed(key_option);
- Boolean key_shift_down = isPressed(key_shift);
-
- unsigned long uTest = ((unsigned long)pBase % 16);
- if (uTest == 8)
- {
- pBase += 2;
- widthInPixels -= 2;
- }
-
- uTest = ((unsigned long)pBase % 16);
- if (uTest)
- DebugStr((const unsigned char *)"\pOops! The alignment is off!");
-
- short pixelSize = (**hPixMap).pixelSize;
- if (pixelSize != 32)
- DebugStr((const unsigned char *)"\pOops! Wrong bit depth!");
-
- if (key_option_down && key_shift_down && isPressed(key_control))
- {
- Debugger(); // Have to check the alignment
- }
-
- if (key_option_down && key_shift_down)
- {
- md_vectorFade(pBase, boxHeight, widthInPixels, rowBytes);
- }
- else if (key_option_down)
- {
- pRow = pBase;
- for (int i = 0; i < boxHeight; i++) { // row
- p = pRow;
- for (int j = 0; j < widthInPixels; j++)
- {
- unsigned long uOrig = *p;
-
- unsigned long uTemp = 0x000000ff & uOrig;
- uTemp += 40;
- if (uTemp > 0xff)
- uTemp = 0xff;
- *p = uTemp;
-
- uTemp = (0x0000ff00 & uOrig) >> 8;
- uTemp += 40;
- if (uTemp > 0xff)
- uTemp = 0xff;
- *p += uTemp << 8;
-
- uTemp = (0x00ff0000 & uOrig) >> 16;
- uTemp += 40;
- if (uTemp > 0xff)
- uTemp = 0xff;
- *p += uTemp << 16;
-
- p++;
- }
- pRow += rowBytes >> 2;
- }
- }
- else
- {
- // pixel-by-pixel always works, but is slow
- for (int i = 0; i < boxHeight; i++)
- for (int j = 0; j < boxHeight; j++)
- {
- RGBColor rgbColor;
- GetCPixel(j, i, &rgbColor);
- double dRed = (rgbColor.red + USHRT_MAX + 0.5) / 2.0;
- double dGreen = (rgbColor.green + USHRT_MAX + 0.5) / 2.0;
- double dBlue = (rgbColor.blue + USHRT_MAX + 0.5) / 2.0;
- rgbColor.red = dRed;
- rgbColor.green = dGreen;
- rgbColor.blue = dBlue;
- SetCPixel(j, i, &rgbColor);
- }
- }
- UnlockPixels(hPixMap);
- }
- }
-
-
-
- short isPressed(unsigned short k )
- // k = any keyboard scan code, 0-127
- {
- unsigned char km[16];
-
- GetKeys( (unsigned long *) km);
- return ( ( km[k>>3] >> (k & 7) ) & 1);
- }
-
- // ---------------------------------------------------------------------------
- // • Unused codes scraps follow
- // ---------------------------------------------------------------------------
- #if 0
- // This fades from black to white.
- // Assumes 32 bits per pixel
- int iFade;
- for (iFade = 0; iFade < 256; iFade++)
- {
- pRow = pBase;
- for (int i = 0; i < 260; i++)
- { // row
- p = pRow;
- for (int j = 0; j < widthInPixels; j++)
- {
- *p++ = iFade << 16 | iFade << 8 | iFade;
- }
- pRow += rowBytes >> 2;
- }
- }
- #endif
-
- #if 0
- // One shade at a time fade
- for (iFade = 0; iFade < 1; iFade++)
- {
- pRow = pBase;
- for (int i = 0; i < 260; i++) { // row
- p = pRow;
-
- for (int j = 0; j < widthInPixels; j++)
- {
- // Assumes 32-bit and gray!
- // Only use the last byte of the RGB color in the Pixel.
- unsigned long uTemp = (0x000000ff & *p);
- unsigned long uPlus = uTemp + 1;
- uTemp = (uTemp | uPlus) & 0x000000ff;
- *p++ = uTemp << 16 | uTemp << 8 | uTemp;
- }
-
- pRow += rowBytes >> 2;
- }
- }
- #endif
-
- PP_End_Namespace_PowerPlant
-