home *** CD-ROM | disk | FTP | other *** search
- /*
- PrintfGWorld.c
-
- void PrintfGWorld(GWorldPtr our);
- Uses "printf" to print out the GWorld as a bitmap, representing blank as '.' and
- non-blank as '#'. The origin is designated '+'. Uses the upper lefthand pixel
- as the definition of blank (a poor but convenient assumption). This is very crude,
- and intended only for debugging.
-
- void PrintStringAsBitmap(unsigned char *s);
- Uses DrawString to render the string in a GWorld, and then calls PrintfGWorld to
- render the GWorld onto the console. For debugging.
-
- HISTORY:
- 1/17/94 dgp wrote it.
- 6/18/94 dgp Call SetGWorld(GetMainDevice())) before calling printf.
- 6/22/94 dgp Fixed bug: I was "restoring" to a GWorldPtr that was never initialized.
- 9/5/94 dgp removed assumption in printf's that int==short.
- */
- #include "VideoToolbox.h"
- void PrintfGWorld(GWorldPtr our);
- void PrintStringAsBitmap(unsigned char *s);
- #if __MWERKS__
- #include <SIOUX.h>
- #endif
-
- void PrintfGWorld(GWorldPtr world)
- // Print GWorld on the console as a bitmap.
- // Assume upper lefthand pixel is blank, which is rendered as '.'.
- // Any other pixel value is represented as '#'.
- {
- Rect r;
- register unsigned long *pix,blank;
- register int i,n;
- int y;
- char *bit;
- GDHandle oldDevice;
- long qD=0;
-
- Gestalt(gestaltQuickdrawVersion,&qD);
- if(qD>=gestalt8BitQD){
- oldDevice = GetGDevice();
- SetGDevice(GetMainDevice()); // needed for printf
- }
- r=world->portRect;
- r.left=0;
- #if (THINK_C || THINK_CPLUS)
- r.right=console_options.ncols;
- #elif __MWERKS__
- r.right=SIOUXSettings.columns;
- #else
- r.right=72;
- #endif
- CenterRectInRect(&r,&world->portRect);
- SectRect(&r,&world->portRect,&r);
- n=r.right-r.left;
- bit=(char *)malloc((1+n)*sizeof(char));
- if(bit==NULL)PrintfExit("PrintStringAsBitmap: Couldn't allocate %ld bytes.\n",(long)n+1);
- bit[n]=0;
- pix=(unsigned long *)malloc(n*sizeof(unsigned long));
- if(pix==NULL)PrintfExit("PrintStringAsBitmap: Couldn't allocate %ld bytes.\n"
- ,n*sizeof(long));
- GetWindowPixelsQuickly((WindowPtr)world,world->portRect.left,world->portRect.top,pix,1);
- blank=pix[0];
- for(y=r.top;y<r.bottom;y++){
- GetWindowPixelsQuickly((WindowPtr)world,r.left,y,pix,n);
- for(i=0;i<n;i++){
- if(pix[i]!=blank) bit[i]='#'; // non-blank
- else bit[i]='.'; // blank
- }
- if(y==0 && -r.left>=0 && -r.left<n) bit[-r.left]='+'; // the origin
- printf("%s\n",bit);
- }
- free(pix);
- free(bit);
- if(qD>=gestalt8BitQD)SetGDevice(oldDevice);
- }
-
- void PrintStringAsBitmap(unsigned char *s)
- {
- GWorldPtr world,old;
- GDHandle oldDevice;
- FontInfo f;
- Rect r;
- int error;
-
- // Draw string into a new GWorld
- GetFontInfo(&f);
- SetRect(&r,0,-f.ascent,StringWidth(s),f.descent); // nominal size
- InsetRect(&r,-(f.widMax/2+2),-(f.leading+2)); // add margin
- error=NewGWorld(&world,1,&r,NULL,NULL,0);
- if(error)PrintfExit("PrintStringAsBitmap: NewGWorld error %d.\n",error);
- GetGWorld(&old,&oldDevice);
- SetGWorld(world,NULL);
- TextFace(old->txFace);
- TextFont(old->txFont);
- TextSize(old->txSize);
- EraseRect(&world->portRect);
- MoveTo(0,0);
- DrawString(s);
- SetGWorld(old,oldDevice);
-
- PrintfGWorld(world);
-
- DisposeGWorld(world);
- }
-
-