home *** CD-ROM | disk | FTP | other *** search
- /* macinit.c
- *
- * A bunch of initialization routines which prepare the mac, ready of operation.
- * The equivalent shutdown routines are also here
- *
- */
-
- #include <MacHeaders>
- #include <Palettes.h>
- #include <Quickdraw.h>
- #include <QDOffScreen.h>
- #include <Windows.h>
- #include "macwindows.h"
-
- void SetUpWindow(void) ;
- void CloseMacWindow() ;
- void OpenMacWindow(short width, short height) ;
- void DrawMacWindow(short x, short y, unsigned short r, unsigned short g, unsigned short b) ;
- void OpenInformationWindow() ;
- void DisplayInformation() ;
- void CloseInformationWindow() ;
- void CantOpen() ;
- void InitBuffer(short width, short height) ;
- void SizeImage() ;
- char IsAppWindow(WindowPtr *w) ;
- void write_picture() ;
- int pStrCopy(StringPtr a, StringPtr b);
-
- extern short magx, magy ;
-
- CWindowPtr imageWindow = NULL ;
- WindowPtr informationWindow = NULL;
- CWindowRecord imageRecord ;
- CGrafPtr imagePort, oldPort, fDrawingPort ;
- Rect windowBounds = { 40, 40, 150, 150 };
-
- char IsAppWindow(WindowPtr *w)
- {
- return (!w) ? 0 :(((WindowPeek) w)->windowKind == userKind) ;
- }
-
- void CloseMacWindow()
- {
- if(imageWindow) {
- DisposeWindow(imageWindow) ;
- DisposeGWorld(fDrawingPort) ;
- imageWindow = NULL ;
- }
- }
-
- void OpenMacWindow(short width, short height)
- {
- short rw, rh ;
-
- rw = width*magx - magx;
- rh = height*magy - magy ;
- SetRect(&windowBounds,0,39,rw,39+rh) ;
- if(imageWindow) CloseMacWindow() ;
- imageWindow = NewCWindow(0L,&windowBounds,"\pRayShade Output",true,documentProc,(WindowPtr) -1L,true, 0) ;
- InitBuffer(width,height) ;
- }
-
-
- void DrawMacWindow(short x, short y, unsigned short r, unsigned short g, unsigned short b)
- {
- GrafPtr currPort ;
- GDHandle currDevice ;
- RGBColor pix ;
- Rect box ;
- register short i,j ;
-
- pix.red = r ;
- pix.green = g ;
- pix.blue = b ;
-
- GetGWorld(&currPort,&currDevice) ;
- SetGWorld(fDrawingPort,NULL) ;
- LockPixels(&fDrawingPort->portPixMap) ;
- SetCPixel(x,y,&pix) ;
- UnlockPixels(&fDrawingPort->portPixMap) ;
- SetGWorld(currPort,currDevice) ;
-
- GetPort(&currPort) ;
- SetPort(imageWindow) ;
- if((magx==1) && (magy==1))
- SetCPixel(x,y,&pix) ;
- else {
- /* I should really use PaintRect here but for some reason it snarls up the CLUT which
- * mangles the images colour palette in 256 colour mode
- */
- for(i = x*magx ; i < (x+1)*magx ; i++)
- for(j = y*magy ; j < (y+1)*magy ; j++)
- SetCPixel(i,j,&pix) ;
- }
- SetPort(currPort) ;
- }
-
- void InitBuffer(short width, short height)
- {
- GDHandle currDevice ;
- GrafPtr currPort ;
- Rect r ;
- OSErr erry ;
- PixMapHandle pmh ;
-
- SetRect(&r,0,0,width-1,height-1) ;
- GetGWorld(&currPort,&currDevice) ;
- pmh = imageWindow->portPixMap ;
- erry = NewGWorld(&fDrawingPort,32,&r,(*pmh)->pmTable,NULL,0) ;
- SetGWorld(fDrawingPort,NULL) ;
- LockPixels(&fDrawingPort->portPixMap) ;
- EraseRect(&fDrawingPort->portRect) ;
- UnlockPixels(&fDrawingPort->portPixMap) ;
- SetGWorld(currPort,currDevice) ;
- }
-
- void RefreshWindow()
- {
- GrafPtr currPort ;
-
- if(imageWindow) {
- GetPort(&currPort) ;
- SetPort(imageWindow);
- CopyBits(&fDrawingPort->portPixMap, &imageWindow->portPixMap, &fDrawingPort->portRect,&imageWindow->portRect, ditherCopy, 0L);
- SetPort(currPort) ;
- }
- }
-
- void UpdateWind()
- {
- GrafPtr currPort ;
-
- if(imageWindow) {
- GetPort(&currPort) ;
- SetPort(imageWindow);
- BeginUpdate(imageWindow);
- CopyBits(&fDrawingPort->portPixMap, &imageWindow->portPixMap, &fDrawingPort->portRect,&imageWindow->portRect, ditherCopy, 0L);
- EndUpdate(imageWindow);
- SetPort(currPort) ;
- }
- }
-
- void SizeImage(Point p)
- {
- GrafPtr currPort;
- long theResult;
- Rect oldHorizBar;
- Rect r;
-
- GetPort(&currPort);
- SetPort(imageWindow);
-
- SetRect(&r, 50, 50, screenBits.bounds.right, screenBits.bounds.bottom);
- theResult = GrowWindow(imageWindow, p, &r);
- if (theResult == 0) return;
- SizeWindow(imageWindow, LoWord(theResult), HiWord(theResult), false);
- InvalRect(&imageWindow->portRect);
-
- SetPort(currPort);
- }
-
- static Point SFGwhere = { 90, 82 };
- static SFReply reply;
-
- /*
- * write_picture() writes the current picture to a
- * specified (open) file. It should be redone to
- * add error handling.
- */
-
-
-
- void write_picture()
- {
- GDHandle currDevice ;
- GrafPtr currPort ;
- PicHandle picHandle ;
- int i, temp ;
- long len ;
- Picture header;
- short theFile, vRef ;
- Str255 theFileName ;
- SFTypeList myTypes;
- OSErr err ;
-
-
- if(imageWindow) {
- myTypes[0]='TEXT';
- SFPutFile(SFGwhere, "\pSave file as", "\pRayshade Image", 0L, &reply);
- if (reply.good) {
- vRef = reply.vRefNum;
- pStrCopy(reply.fName, theFileName);
- FSDelete(theFileName, vRef) ;
- Create(theFileName, vRef, 'CEM8', 'PICT');
- if(FSOpen(theFileName, vRef, &theFile)==noErr) {
- theFileName[theFileName[0]+1] = '\0';
- printf("Saving PICT to %s\n",&theFileName[1]);
- GetGWorld(&currPort,&currDevice) ;
- SetGWorld(fDrawingPort,NULL) ;
-
- len = sizeof(long) ;
- temp = 0L ;
- for (i = 0; i < 512; i += len)
- FSWrite(theFile, &len, (Ptr) &temp);
-
- LockPixels(&fDrawingPort->portPixMap) ;
- picHandle = OpenPicture(&fDrawingPort->portRect) ;
- CopyBits(&fDrawingPort->portPixMap, &fDrawingPort->portPixMap, &fDrawingPort->portRect, &fDrawingPort->portRect, srcCopy, 0L);
- ClosePicture();
- UnlockPixels(&fDrawingPort->portPixMap) ;
-
- len = GetHandleSize((Handle) picHandle) ;
- HLock((Handle) picHandle) ;
- err = FSWrite(theFile, &len, *(Handle)picHandle) ;
-
- KillPicture(picHandle);
-
- SetGWorld(currPort,currDevice) ;
- FSClose(theFile) ;
- }
- else
- printf("File error has occurred\n") ;
- }
- else
- printf("Image save cancelled !\n") ;
- }
- }
-
- void write_frame_picture(int frameno)
- {
- GDHandle currDevice ;
- GrafPtr currPort ;
- PicHandle picHandle ;
- int i, temp ;
- long len ;
- Picture header;
- short theFile, vRef ;
- Str255 theFileName ;
- SFTypeList myTypes;
- OSErr err ;
-
- sprintf(&theFileName[1],"image.%d",frameno) ;
- theFileName[0] = strlen(&theFileName[1]) ;
- if(imageWindow) {
- GetVRefNum(0L,&vRef);
- FSDelete(theFileName, vRef) ;
- Create(theFileName, vRef, 'CEM8', 'PICT');
- if(FSOpen(theFileName, vRef, &theFile)==noErr) {
- GetGWorld(&currPort,&currDevice) ;
- SetGWorld(fDrawingPort,NULL) ;
-
- len = sizeof(long) ;
- temp = 0L ;
- for (i = 0; i < 512; i += len)
- FSWrite(theFile, &len, (Ptr) &temp);
-
- LockPixels(&fDrawingPort->portPixMap) ;
- picHandle = OpenPicture(&fDrawingPort->portRect) ;
- CopyBits(&fDrawingPort->portPixMap, &fDrawingPort->portPixMap, &fDrawingPort->portRect, &fDrawingPort->portRect, srcCopy, 0L);
- ClosePicture();
- UnlockPixels(&fDrawingPort->portPixMap) ;
- len = GetHandleSize((Handle) picHandle) ;
- HLock((Handle) picHandle) ;
- err = FSWrite(theFile, &len, *(Handle)picHandle) ;
-
- KillPicture(picHandle);
- SetGWorld(currPort,currDevice) ;
- FSClose(theFile) ;
- }
- else
- printf("File error has occurred: %s\n", &theFileName[1]) ;
- }
- }
-
-
- /* copies a pascal string from p1 to p2 */
- int pStrCopy (StringPtr p1, StringPtr p2)
- {
- register int len;
-
- len = *p2++ = *p1++;
- while (--len>=0) *p2++=*p1++;
- }
-