April 5, 2008
WikiAnimated Rects
#wiki
Animated Rects
Source Code
void Initialize(void); // function prototypes
void DrawLine( void );
void doEventLoop( void );
void DrawToBuffer(void);
void CopyToWindow (void);
void ourLine(int left,int top,int right ,int bottom);
void ourRect(int left,int top,int right ,int bottom);
void ourFramedRect(int left,int top,int right ,int bottom);
void ourOval(int left,int top,int right ,int bottom);
void ourFramedOval(int left,int top,int right ,int bottom);
int ourRandom( int min, int max );
//globals
WindowPtr ourWindow;
Rect windRect;
GWorldPtr ourBuffer;
int count=0;
void DrawToBuffer(void) // this is where the interesting stuff happens, this is where we actually draw our geometry
{
long y;
Point mousepoint;
count ++ ; // increment the global variable count by 1
count = count % 10; // in lingo : count = count mod 10 , (if its greater than 9 it goes down to 0)
GetMouse(&mousepoint); // put the mouse position into a point variable
SetGWorld(ourBuffer,nil); * set our buffer as the "port" for future drawing , if we don't do this * the default will be to draw directly to our window which will create a flicker.
EraseRect(&windRect); // erasing(to white) the content of our buffer which removes the previous drawings
for(y=0;y<70;y++){ // translation to lingo - repeat with y = 0 to 70
ourFramedRect(mousepoint.h -y*10-count,
mousepoint.v -y*10-count,
mousepoint.h +y*10+count,
mousepoint.v +y*10+count); // calling our method that draws Rects
}
}
void CopyToWindow (void){ // copy all our buffer to the window, completely replaceing
// everything that was there
Rect sourceRect,destRect;
SetPortWindowPort(ourWindow);
GetPortBounds( GetWindowPort(ourWindow), &destRect );
GetPortBounds( ourBuffer, &sourceRect );
CopyBits( GetPortBitMapForCopyBits( ourBuffer ), GetPortBitMapForCopyBits(GetWindowPort(ourWindow)),
&sourceRect, &destRect, srcCopy, NULL );
}
void main( void )
{
Initialize();
doEventLoop();
}
void Initialize(void){
OSErr error;
SetRect(&windRect,100,100,740,580);
InitCursor();
ourWindow = NewCWindow( 0L, &windRect, "\pBuffered Rects", true, noGrowDocProc,(WindowPtr)-1L, true, 0L );
if ( ourWindow == nil ) ExitToShell();
ShowWindow( ourWindow );
SetPortWindowPort( ourWindow );
SetRect(&windRect,0,0,640,480);
error =NewGWorld(&ourBuffer, 1, &windRect, nil, nil,0 ); // creating our offscreen buffer
if (error != noErr ) ExitToShell();
}
/*************** The Event Loop ***************/
void doEventLoop()
{
EventRecord anEvent;
WindowPtr evtWind;
short clickArea;
Rect screenRect;
Point thePoint;
for (;;)
{
if (WaitNextEvent( everyEvent, &anEvent, 0, nil ))
{
if (anEvent.what == mouseDown)
{
clickArea = FindWindow( anEvent.where, &evtWind );
if (clickArea == inDrag)
{
GetRegionBounds( GetGrayRgn(), &screenRect );
DragWindow( evtWind, anEvent.where, &screenRect );
}
else if (clickArea == inContent)
{
if (evtWind != FrontWindow())
SelectWindow( evtWind );
else
{
thePoint = anEvent.where;
GlobalToLocal( &thePoint );
//Handle click in window content here
}
}
else if (clickArea == inGoAway)
if (TrackGoAway( evtWind, anEvent.where ))
return;
}
}
DrawToBuffer(); // after checking for various events we call our drawing finctions
CopyToWindow();
}
}
void ourLine(int left,int top,int right ,int bottom){ // method for drawing lines arguments are left, top, right , bottom
MoveTo(left,top); // move the pen to the starting position of the line
LineTo(right,bottom); // draw line to the position specified in the current port
}
void ourRect(int left,int top,int right ,int bottom){ // method for drawing filled rectangles, arguments are left, top, right , bottom
Rect theRect;
long temp;
if (left > right) {temp = left; left = right ; right = temp;} // making sure that the left is smaller than right and bottom biger than top
if (top > bottom) {temp = top; top = bottom ; bottom = temp;} // otherwise it will not draw.
SetRect(&theRect,left,top,right,bottom); // seting a Rect variable to hold the coordinates of the rectangle
PaintRect(&theRect); // painting the rectangle to the current port
}
void ourFramedRect(int left,int top,int right ,int bottom){ // method for drawing hollow rectangles, arguments are left, top, right , bottom
Rect theRect;
long temp;
if (left > right) {temp = left; left = right ; right = temp;} // same as ourRect() only calls FrameRect() instead of PaintRect
if (top > bottom) {temp = top; top = bottom ; bottom = temp;}
SetRect(&theRect,left,top,right,bottom);
FrameRect(&theRect);
}
void ourOval(int left,int top,int right ,int bottom){ //method for drawing filled ovals, arguments are left, top, right , bottom
Rect theRect;
long temp;
if (left > right) {temp = left; left = right ; right = temp;}
if (top > bottom) {temp = top; top = bottom ; bottom = temp;} // same as ourRect() only calls PaintOval() instead of PaintRect
SetRect(&theRect,left,top,right,bottom);
PaintOval(&theRect);
}
void ourFramedOval(int left,int top,int right ,int bottom){ // method for drawing hollow ovals, arguments are left, top, right , bottom
Rect theRect;
long temp;
if (left > right) {temp = left; left = right ; right = temp;}
if (top > bottom) {temp = top; top = bottom ; bottom = temp;} // same as ourRect() only calls FrameOval() instead of PaintRect
SetRect(&theRect,left,top,right,bottom);
FrameOval(&theRect);
}
int ourRandom( int min, int max ){ // method that returns a random number between min and max
return( (Random()+32768) /((32768*2/) (max-min)))+ min;
}
Continue Reading
Back to Archive