// // main.c // second // // Created by Jan Kordylewski on 11/20/05. // Copyright __MyCompanyName__ 2005. All rights reserved. // void Initialize(void); // function prototypes void DrawLine( void ); void doEventLoop( void ); void DrawToBuffer(void); void CopyToWindow (void); int ourRandom( int min, int max ); void ourSetPixel(unsigned short horizontal,unsigned short vertical,unsigned char R,unsigned char G,unsigned char B,unsigned short rowbytes,Ptr pixbase); void ourGetPixel(unsigned short horizontal,unsigned short vertical,unsigned char* R,unsigned char* G,unsigned char* B,unsigned short rowbytes,Ptr pixbase); void InitVideo(void); #define CLIP(val, low, high) {if(valhigh)val=high;} SeqGrabComponent gSeqGrabber; SGChannel gVideoChannel; WindowPtr ourWindow; Rect windRect; GWorldPtr ourBuffer,videogworld; //long Degree=0,count=0,FPS =0; long temp2=0,temp3=0,temp4=0,temp5=0,temp6=0,temp7=0,temp8=0; long bard1=100,bard2=300,bard3=230,bard4=250,bard5=130; long bob1=1,bob2=-1,bob3=-1,bob4=1,bob5=-1; long count=0; long startTicks=-1000; void DrawToBuffer(void) // this is where the interesting stuff happens, this is where we actually set our pixels { long x , y,temp; Point mousepoint; unsigned short bufferRowBytes,videoRowBytes; Ptr bufferBaseAddress,videoBaseAddress; PixMapHandle bufferPixmap,videoPixmap; unsigned char R,G,B; float distance; GetMouse(&mousepoint); for (x = 0;x< 1;x++)SGIdle(gSeqGrabber); videoPixmap=GetGWorldPixMap(videogworld); videoRowBytes = ((*(videoPixmap))->rowBytes) & 0x7fff; videoBaseAddress = GetPixBaseAddr(videoPixmap ); bufferPixmap=GetGWorldPixMap(ourBuffer); bufferRowBytes = ((*(bufferPixmap))->rowBytes) & 0x7fff; bufferBaseAddress = GetPixBaseAddr(bufferPixmap ); distance = 640 - mousepoint.h; for(x=0;x<640;x++){ for(y=0;y<480;y++){ ourGetPixel(x,y,&R,&G,&B,videoRowBytes,videoBaseAddress); temp = R; temp = temp-10; if (temp > 255) temp = 255; else if (temp < 0) temp = 0; R = temp; temp = G; temp = temp-10; if (temp > 255) temp = 255; else if (temp < 0) temp = 0; G = temp; temp = B; temp = temp-10; if (temp > 255) temp = 255; else if (temp < 0) temp = 0; B = temp; ourSetPixel(x,y,R,G,B,bufferRowBytes,bufferBaseAddress); } } if (count>120){ for(x=0;x<640;x++){ for(y=bard1;y=640){ temp = temp-640; } ourGetPixel(temp,y,&R,&G,&B,videoRowBytes,videoBaseAddress); ourSetPixel(x,y,R,G,B,bufferRowBytes,bufferBaseAddress); } } temp2=temp2+2; if (temp2>640){ temp2=0; } } if(count>120){ for(x=0;x<640;x++){ for(y=bard2;y=640){ temp = temp-640; } ourGetPixel(temp,y,&R,&G,&B,videoRowBytes,videoBaseAddress); ourSetPixel(x,y,R,G,B,bufferRowBytes,bufferBaseAddress); } } temp3=temp3+2; if (temp3>640){ temp3=0; } } if(count>144){ for(x=bard3;x=480){ temp = temp-480; } ourGetPixel(x,temp,&R,&G,&B,videoRowBytes,videoBaseAddress); ourSetPixel(x,y,R,G,B,bufferRowBytes,bufferBaseAddress); } if (x%10==0){ temp4=temp4+2; if (temp4>480){ temp4=0; } } } } if(count>80){ for(x=0;x<640;x++){ for(y=bard4;y=480){ // temp = temp-480; // } temp = x - temp5; if (temp<=0){ temp = 640+temp; } ourGetPixel(temp,y,&R,&G,&B,videoRowBytes,videoBaseAddress); ourSetPixel(x,y,R,G,B,bufferRowBytes,bufferBaseAddress); } temp5=temp5+2; if (temp5>640){ temp5=0; } } } if(count>166){ for(x=bard5;x=480){ temp = temp-480; } ourGetPixel(x,temp,&R,&G,&B,videoRowBytes,videoBaseAddress); ourSetPixel(x,y,R,G,B,bufferRowBytes,bufferBaseAddress); } if (x%10==0){ temp6=temp6+2; if (temp6>480){ temp6=0; } } } } bard1=bard1+bob1; bard2=bard2+bob2; bard3--; //bard3=bard3+bob3; bard4=bard4+bob4; bard5--; //bard5=bard5+bob5; if (bard1>=468||bard1<=0){ bob1=bob1*-1; //bard1=0; } if (bard2<=0||bard2>=468){ bob2=bob2*-1; //bard2=468; } if (bard3<=0){ //||bard3>=630){ //bob3=bob3*-1; bard3=640; } if (bard4>=468||bard4<=0){ bob4=bob4*-1; //bard4=0; } if (bard5<=0){ //||bard1>=630){ //bob5=bob5*-1; bard5=640; } if (count<300){ count++; } } 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(); SGDisposeChannel(gSeqGrabber,gVideoChannel); CloseComponent(gSeqGrabber); DisposeGWorld(videogworld); DisposeGWorld(ourBuffer); } void Initialize(void){ OSErr error; Rect videoRect; Str255 pathName ="car.jpg",pathName2 ="?"; SetRect(&windRect,100,100,740,580); SetRect(&videoRect,0,0,640,480); InitCursor(); ourWindow = NewCWindow( 0L, &windRect, "\p!!go go gadget copter!!", true, noGrowDocProc,(WindowPtr)-1L, true, 0L ); if ( ourWindow == nil ) ExitToShell(); ShowWindow( ourWindow ); SetPortWindowPort( ourWindow ); SetRect(&windRect,0,0,640,480); error =NewGWorld(&ourBuffer, 32, &videoRect, nil, nil,0 ); // creating our offscreen buffer if (error != noErr ) ExitToShell(); error =NewGWorld(&videogworld, 32, &videoRect, nil, nil,0 ); if (error != noErr ) ExitToShell(); InitVideo(); // Calling our method that opens a picture file and returns a picture handle } /*************** 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){ SGSettingsDialog(gSeqGrabber, gVideoChannel, 0, nil, 0L, nil, nil); SysBeep(10); if (evtWind != FrontWindow()) SelectWindow( evtWind ); else{ thePoint = anEvent.where; GlobalToLocal( &thePoint ); } } else if (clickArea == inGoAway) if (TrackGoAway( evtWind, anEvent.where )) return; } } DrawToBuffer(); // after checking for various events we call our drawing finctions CopyToWindow(); } } int ourRandom( int min, int max ){ // method that returns a random number between min and max return( (Random()+32768) /((32768*2)/ (max-min)))+ min; } void ourSetPixel(unsigned short horizontal,unsigned short vertical,unsigned char R,unsigned char G,unsigned char B,unsigned short rowbytes,Ptr pixbase){ Ptr AdressOfRed; AdressOfRed = rowbytes * vertical +pixbase+horizontal*4+1; *(AdressOfRed)=R; *(AdressOfRed+1)=G; *(AdressOfRed+2)=B; } void ourGetPixel(unsigned short horizontal,unsigned short vertical,unsigned char* R,unsigned char* G,unsigned char* B,unsigned short rowbytes,Ptr pixbase){ Ptr AdressOfRed; AdressOfRed = rowbytes * vertical +pixbase+horizontal*4+1; *R=*(AdressOfRed); *G=*(AdressOfRed+1); *B=*(AdressOfRed+2); } void InitVideo(){ ComponentDescription theDesc; ComponentResult theresult; Component sgCompID ; Rect videoRect; EnterMovies(); gSeqGrabber = 0L; gVideoChannel = 0L; theDesc.componentType = SeqGrabComponentType; theDesc.componentSubType = 0L; theDesc.componentManufacturer = 0L; file://'appl'; theDesc.componentFlags = 0L; theDesc.componentFlagsMask = 0L; sgCompID = FindNextComponent(nil, &theDesc); gSeqGrabber = OpenComponent(sgCompID); SGInitialize(gSeqGrabber); SetRect(&videoRect,0,0,640,480); NewGWorld ( &videogworld, 32, &videoRect, nil, nil,0 ); SGSetGWorld(gSeqGrabber,videogworld, nil);//videogworld5 SGNewChannel(gSeqGrabber, VideoMediaType, &gVideoChannel); SGSetChannelUsage(gVideoChannel, seqGrabPreview | seqGrabRecord | seqGrabPlayDuringRecord); //if (SGSetFrameRate(gVideoChannel,30) != noErr) SysBeep(10); SGSetChannelBounds(gVideoChannel, &videoRect); SGStartPreview(gSeqGrabber); }