home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Entertainment / tblt / tblt⁄misc.c < prev    next >
Encoding:
Text File  |  1986-09-07  |  3.2 KB  |  122 lines  |  [TEXT/MACA]

  1. /*
  2.  * misc.c - miscellaneous routines
  3.  *
  4.  */
  5.  
  6. #include <memory.h>
  7. #include <quickdraw.h>
  8. #include <osutil.h>
  9. #include <resource.h>
  10.  
  11. /*
  12.  * PictToMap() - convert a resource picture into a bitmap containing the
  13.  *  result of drawing that picture.
  14.  * The resulting bitmap's upper-left corner is [0,0].
  15.  */
  16.  
  17. BitMap *
  18. PictToMap(pic)
  19. PicHandle pic;
  20. {
  21.     GrafPtr saveport;        /* current port (to be restored)    */
  22.     GrafPort aport;        /* a temp grafport for drawing into    */
  23.     BitMap *mapptr;        /* the result                      */
  24.     short width, height;    /* dimensions of the picture        */
  25.     char *malloc();
  26.  
  27.     LoadResource(pic);
  28.     HLock((Handle) pic);
  29.     width = (*pic)->picFrame.right - (*pic)->picFrame.left;
  30.     height = (*pic)->picFrame.bottom - (*pic)->picFrame.top;
  31.     HUnlock((Handle) pic);
  32.  
  33.     mapptr = (BitMap *) malloc(sizeof(BitMap));
  34.     SetRect(&mapptr->bounds, 0, 0, width, height);
  35.     mapptr->rowBytes = (width + 7) / 8;
  36.     if (mapptr->rowBytes & 1) {
  37.     ++(mapptr->rowBytes);
  38.     }
  39.     mapptr->baseAddr = (QDPtr) malloc((int) mapptr->rowBytes * height);
  40.  
  41.     GetPort(&saveport);
  42.     OpenPort(&aport);
  43.     SetPortBits(mapptr);
  44.     DrawPicture(pic, &mapptr->bounds);
  45.     SetPort(saveport);
  46.     ClosePort(&aport);
  47.  
  48.     return(mapptr);
  49. }
  50.  
  51. /*
  52.  * randint() - return a random number in the range 0 through range-1.
  53.  *  This routine seeds the number generator on the first call.
  54.  */
  55. int
  56. randint(range)
  57. int range;
  58. {
  59.     static int first = 1;    /* true if this is the first call    */
  60.     long secs;        /* value returned from GetDateTime()    */
  61.  
  62.     if (first) {
  63.     first = 0;
  64.     GetDateTime(&secs);
  65.     randSeed = secs;
  66.     }
  67.     return((int)((unsigned) Random() % range));
  68. }
  69.  
  70. /*
  71.  * setupmemory() - initialize the memory.
  72.  */
  73. setupmemory()
  74. {
  75. #define maxStackSize 8192    /* max size of stack; the heap gets the rest */
  76.  
  77.     typedef long   *lomemptr;    /* a pointer to low memory locations */
  78.  
  79.     lomemptr nilptr;        /* will have value NIL           */
  80.     lomemptr stackbaseptr;    /* points to current stack base      */
  81.  
  82. /* 
  83.   If you define a GrowZone function to handle bad memory problems,
  84.   you should define it at the top level (not nested), and set it
  85.   here. We don't.
  86. */
  87. /*  SetGrowZone(&mygrowzone);     */
  88.  
  89. /* 
  90.   Place an illegal address in the memory location that would be
  91.   referenced by an accidentally-NULL handle, so the error will be caught
  92.   at handle-reference time (as an Address error, ID=02) instead of later on.
  93. */
  94.     nilptr = (lomemptr) 0;
  95.     *nilptr = -1;
  96.  
  97. /* 
  98.   If you needed to use an Application heap limit other than the
  99.   default (which allows 8K for the stack), you'd set it here,
  100.   possible using this technique of explicitly specifying the maximum
  101.   stack size and allocating the rest to the heap.  Should be
  102.   independent of memory size.
  103. */
  104.     stackbaseptr = (lomemptr) 0x908; /* CurStackBase from Tlasm/sysequ.text */
  105.     SetApplLimit((Ptr) (*stackbaseptr - maxStackSize));
  106.  
  107. /* 
  108.   Expand the application heap zone to its maximum size, without
  109.   purging any purgeable resources.  This saves memory compactions
  110.   and heap expansions later.
  111. */
  112.     MaxApplZone();
  113.  
  114. /* 
  115.   get plenty of master pointers now; if we let the Memory Manager
  116.   allocate them as needed, they'd form non-relocatable islands in
  117.   the heap.
  118. */
  119.     MoreMasters();
  120.     MoreMasters();
  121. }
  122.