home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / macraysh.sit / Code / Source / macwindows.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-11  |  6.9 KB  |  284 lines

  1. /* macinit.c
  2.  * 
  3.  * A bunch of initialization routines which prepare the mac, ready of operation.
  4.  * The equivalent shutdown routines are also here
  5.  *
  6.  */
  7.  
  8. #include <MacHeaders>
  9. #include <Palettes.h>
  10. #include <Quickdraw.h>
  11. #include <QDOffScreen.h>
  12. #include <Windows.h>
  13. #include "macwindows.h"
  14.  
  15. void SetUpWindow(void) ;
  16. void CloseMacWindow() ;
  17. void OpenMacWindow(short width, short height) ;
  18. void DrawMacWindow(short x, short y, unsigned short r, unsigned short g, unsigned short b) ;
  19. void OpenInformationWindow() ;
  20. void DisplayInformation() ;
  21. void CloseInformationWindow() ;
  22. void CantOpen() ;
  23. void InitBuffer(short width, short height) ;
  24. void SizeImage() ;
  25. char IsAppWindow(WindowPtr *w) ;
  26. void write_picture() ;
  27. int pStrCopy(StringPtr a, StringPtr b);
  28.  
  29. extern short magx, magy ;
  30.  
  31. CWindowPtr    imageWindow = NULL ;
  32. WindowPtr    informationWindow = NULL;
  33. CWindowRecord imageRecord ;
  34. CGrafPtr    imagePort, oldPort, fDrawingPort ;
  35. Rect        windowBounds = { 40, 40, 150, 150 };
  36.  
  37. char IsAppWindow(WindowPtr *w) 
  38. {
  39.     return (!w) ? 0 :(((WindowPeek) w)->windowKind == userKind) ;
  40. }
  41.  
  42. void CloseMacWindow()
  43. {
  44.     if(imageWindow) {
  45.         DisposeWindow(imageWindow) ;
  46.         DisposeGWorld(fDrawingPort) ;
  47.          imageWindow = NULL ;
  48.     }
  49. }
  50.  
  51. void OpenMacWindow(short width, short height)
  52. {
  53.     short rw, rh ;
  54.     
  55.     rw = width*magx - magx;
  56.     rh = height*magy - magy ;
  57.     SetRect(&windowBounds,0,39,rw,39+rh) ;
  58.     if(imageWindow)    CloseMacWindow() ;
  59.     imageWindow = NewCWindow(0L,&windowBounds,"\pRayShade Output",true,documentProc,(WindowPtr) -1L,true, 0) ; 
  60.     InitBuffer(width,height) ;
  61. }
  62.  
  63.  
  64. void DrawMacWindow(short x, short y, unsigned short r, unsigned short g, unsigned short b)
  65. {    
  66.     GrafPtr currPort ;
  67.     GDHandle currDevice ;
  68.     RGBColor pix ;
  69.     Rect box ;
  70.     register short i,j ;
  71.     
  72.     pix.red = r ;
  73.     pix.green = g ;
  74.     pix.blue = b ;
  75.  
  76.     GetGWorld(&currPort,&currDevice) ;
  77.     SetGWorld(fDrawingPort,NULL) ;
  78.     LockPixels(&fDrawingPort->portPixMap) ;
  79.     SetCPixel(x,y,&pix) ;
  80.     UnlockPixels(&fDrawingPort->portPixMap) ;
  81.     SetGWorld(currPort,currDevice) ;
  82.  
  83.     GetPort(&currPort) ;
  84.     SetPort(imageWindow) ;
  85.     if((magx==1) && (magy==1))
  86.         SetCPixel(x,y,&pix) ;
  87.     else {
  88.         /* I should really use PaintRect here but for some reason it snarls up the CLUT which
  89.          * mangles the images colour palette in 256 colour mode
  90.          */
  91.         for(i = x*magx ; i < (x+1)*magx ; i++)
  92.             for(j = y*magy ; j < (y+1)*magy ; j++)
  93.                 SetCPixel(i,j,&pix) ;
  94.     }
  95.     SetPort(currPort) ;
  96. }
  97.  
  98. void InitBuffer(short width, short height)
  99. {
  100.     GDHandle currDevice ;
  101.     GrafPtr currPort ;
  102.     Rect r ;
  103.     OSErr erry ;
  104.     PixMapHandle pmh ;
  105.  
  106.     SetRect(&r,0,0,width-1,height-1) ;
  107.     GetGWorld(&currPort,&currDevice) ;
  108.     pmh = imageWindow->portPixMap ;
  109.     erry = NewGWorld(&fDrawingPort,32,&r,(*pmh)->pmTable,NULL,0) ;
  110.     SetGWorld(fDrawingPort,NULL) ;
  111.     LockPixels(&fDrawingPort->portPixMap) ;
  112.     EraseRect(&fDrawingPort->portRect) ;
  113.     UnlockPixels(&fDrawingPort->portPixMap) ;
  114.     SetGWorld(currPort,currDevice) ;
  115. }
  116.  
  117. void RefreshWindow()
  118. {
  119.     GrafPtr currPort ;
  120.  
  121.     if(imageWindow) {
  122.         GetPort(&currPort) ;
  123.         SetPort(imageWindow);
  124.         CopyBits(&fDrawingPort->portPixMap, &imageWindow->portPixMap, &fDrawingPort->portRect,&imageWindow->portRect, ditherCopy, 0L);
  125.         SetPort(currPort) ;
  126.     }
  127. }
  128.  
  129. void UpdateWind()
  130. {
  131.     GrafPtr currPort ;
  132.  
  133.     if(imageWindow) {
  134.         GetPort(&currPort) ;
  135.         SetPort(imageWindow);
  136.         BeginUpdate(imageWindow);
  137.         CopyBits(&fDrawingPort->portPixMap, &imageWindow->portPixMap, &fDrawingPort->portRect,&imageWindow->portRect, ditherCopy, 0L);
  138.         EndUpdate(imageWindow);
  139.         SetPort(currPort) ;
  140.     }
  141. }
  142.  
  143. void SizeImage(Point p)
  144. {
  145.     GrafPtr    currPort;
  146.     long    theResult;
  147.     Rect    oldHorizBar;
  148.     Rect     r;
  149.     
  150.     GetPort(&currPort);
  151.     SetPort(imageWindow);
  152.  
  153.     SetRect(&r, 50, 50, screenBits.bounds.right, screenBits.bounds.bottom);
  154.     theResult = GrowWindow(imageWindow, p, &r);
  155.     if (theResult == 0) return;
  156.     SizeWindow(imageWindow, LoWord(theResult), HiWord(theResult), false);
  157.     InvalRect(&imageWindow->portRect);
  158.  
  159.     SetPort(currPort);
  160. }
  161.  
  162. static Point SFGwhere = { 90, 82 };
  163. static SFReply reply;
  164.  
  165. /*
  166.  * write_picture() writes the current picture to a
  167.  * specified (open) file.  It should be redone to
  168.  * add error handling.
  169.  */
  170.  
  171.  
  172.  
  173. void write_picture()
  174. {
  175.     GDHandle currDevice ;
  176.     GrafPtr currPort ;
  177.     PicHandle picHandle ;
  178.     int i, temp ;
  179.     long len ;
  180.     Picture header;
  181.     short theFile, vRef ;
  182.     Str255 theFileName ;
  183.     SFTypeList myTypes;
  184.     OSErr err ;
  185.     
  186.  
  187.     if(imageWindow) {
  188.         myTypes[0]='TEXT';
  189.         SFPutFile(SFGwhere, "\pSave file as", "\pRayshade Image", 0L, &reply);
  190.         if (reply.good) {
  191.             vRef = reply.vRefNum;
  192.             pStrCopy(reply.fName, theFileName);
  193.             FSDelete(theFileName, vRef) ;
  194.             Create(theFileName, vRef, 'CEM8', 'PICT');
  195.             if(FSOpen(theFileName, vRef, &theFile)==noErr) {
  196.                 theFileName[theFileName[0]+1] = '\0';
  197.                 printf("Saving PICT to %s\n",&theFileName[1]);
  198.                 GetGWorld(&currPort,&currDevice) ;
  199.                   SetGWorld(fDrawingPort,NULL) ;
  200.                 
  201.                 len = sizeof(long) ;
  202.                     temp = 0L ;
  203.                 for (i = 0; i < 512; i += len) 
  204.                     FSWrite(theFile, &len, (Ptr) &temp);
  205.             
  206.                 LockPixels(&fDrawingPort->portPixMap) ;
  207.                 picHandle = OpenPicture(&fDrawingPort->portRect) ;
  208.                 CopyBits(&fDrawingPort->portPixMap, &fDrawingPort->portPixMap, &fDrawingPort->portRect, &fDrawingPort->portRect, srcCopy, 0L);
  209.                 ClosePicture();
  210.                 UnlockPixels(&fDrawingPort->portPixMap) ;
  211.  
  212.                 len = GetHandleSize((Handle) picHandle) ;
  213.                 HLock((Handle) picHandle) ;
  214.                 err = FSWrite(theFile, &len, *(Handle)picHandle) ;
  215.             
  216.                 KillPicture(picHandle);
  217.  
  218.                 SetGWorld(currPort,currDevice) ;
  219.                 FSClose(theFile) ;
  220.             }
  221.             else
  222.                 printf("File error has occurred\n") ;
  223.         }
  224.         else
  225.             printf("Image save cancelled !\n") ;
  226.     }
  227. }
  228.  
  229. void write_frame_picture(int frameno)
  230. {
  231.     GDHandle currDevice ;
  232.     GrafPtr currPort ;
  233.     PicHandle picHandle ;
  234.     int i, temp ;
  235.     long len ;
  236.     Picture header;
  237.     short theFile, vRef ;
  238.     Str255 theFileName ;
  239.     SFTypeList myTypes;
  240.     OSErr err ;
  241.  
  242.     sprintf(&theFileName[1],"image.%d",frameno) ;
  243.     theFileName[0] = strlen(&theFileName[1]) ;
  244.     if(imageWindow) {
  245.         GetVRefNum(0L,&vRef);
  246.         FSDelete(theFileName, vRef) ;
  247.         Create(theFileName, vRef, 'CEM8', 'PICT');
  248.         if(FSOpen(theFileName, vRef, &theFile)==noErr) {
  249.             GetGWorld(&currPort,&currDevice) ;
  250.               SetGWorld(fDrawingPort,NULL) ;
  251.             
  252.             len = sizeof(long) ;
  253.                 temp = 0L ;
  254.             for (i = 0; i < 512; i += len) 
  255.                 FSWrite(theFile, &len, (Ptr) &temp);
  256.         
  257.             LockPixels(&fDrawingPort->portPixMap) ;
  258.             picHandle = OpenPicture(&fDrawingPort->portRect) ;
  259.             CopyBits(&fDrawingPort->portPixMap, &fDrawingPort->portPixMap, &fDrawingPort->portRect, &fDrawingPort->portRect, srcCopy, 0L);
  260.             ClosePicture();
  261.             UnlockPixels(&fDrawingPort->portPixMap) ;
  262.                 len = GetHandleSize((Handle) picHandle) ;
  263.             HLock((Handle) picHandle) ;
  264.             err = FSWrite(theFile, &len, *(Handle)picHandle) ;
  265.         
  266.             KillPicture(picHandle);
  267.             SetGWorld(currPort,currDevice) ;
  268.             FSClose(theFile) ;
  269.         }
  270.         else
  271.             printf("File error has occurred: %s\n", &theFileName[1]) ;
  272.     }
  273. }
  274.  
  275.  
  276. /* copies a pascal string from p1 to p2 */
  277. int pStrCopy (StringPtr p1, StringPtr p2)
  278. {
  279.     register int len;
  280.     
  281.     len = *p2++ = *p1++;
  282.     while (--len>=0) *p2++=*p1++;
  283. }
  284.