Using Predefined Standard Patterns with ROM Routines (33302)






This article was previously published under Q33302

SUMMARY

For the built-in Macintosh ROM routines that use patterns (such as FILLOVAL), you can use the standard patterns that are in Macintosh memory instead of defining and initializing an array.

MORE INFORMATION

The standard Macintosh patterns are at fixed offsets from the address in register A5. This address is available in the Macintosh global variable "currentA5" at &H904. The area at negative offsets from A5 are the QuickDraw globals. Various useful information is kept in this area, including the following standard patterns:
   pattern       offset from A5
   -------------------------------
   0% white      -12
   25% light     -36
   50% medium    -28
   75% dark      -44
   100% black    -20
				
The following is a code example:

This program demonstrates using a pattern from Macintosh memory:
   FOR i%=0 TO 4
       BACKPAT PEEKL(&H904) -44 + 8*i%
       CLS             'fill screen with the pattern
       PRINT -44+8*i%  'show offset that was used for the pattern
       WHILE MOUSE(0)<>1:WEND
   NEXT
				
The following is another example that draws a gray scale, and shows how to cache pointers safely:
   DIM r%(3)
   setrect r%(0),9,9,210,201
   FRAMERECT VARPTR(r%(0))  'draw frame
   setrect r%(0),10,10,50,200
   A5&=PEEKL(&H904)    'get value of currentA5.  This is a stable address

   '    The following is a dummy offsetrect so we can cache the rectangle
   '    address without worrying about the array moving in memory:
   offsetrect r%(0),0,0

   r&=VARPTR(r%(0))    'grab rectangle address
   FILLRECT r&,A5&-12  :offsetrect r%(0),40,0
   FILLRECT r&,A5&-36  :offsetrect r%(0),40,0
   FILLRECT r&,A5&-28  :offsetrect r%(0),40,0
   FILLRECT r&,A5&-44  :offsetrect r%(0),40,0
   FILLRECT r&,A5&-20  :offsetrect r%(0),40,0

   WHILE MOUSE(0)<>1:WEND
				
Each of the FILLRECTs above could be written in the following way and would eliminate the need to worry about variables moving in memory and invalidating the pointers. This is the preferred style for beginners:
   FILLRECT VARPTR(r%(0)),PEEKL(&H904)-44
				
Saving the pointers is more efficient than calculating addresses each time. The value of currentA5 may be cached safely in all BASIC programs.

Modification Type: Minor Last Reviewed: 1/8/2003
Keywords: KB33302