home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 March / macformat-022.iso / Reader's Corner / Reader's Contibutions / David Hart / Source Code / Carpet 2.0 / Carpet.c next >
Encoding:
C/C++ Source or Header  |  1994-09-21  |  5.2 KB  |  234 lines  |  [TEXT/KAHL]

  1. /*
  2.     Filename    Carpet.c
  3.     Copyright © 1993 David Hart. All rights reserved.
  4.     Author        David Hart
  5.     Description    Carpet module for After Dark
  6.     
  7.     Version      Date        Comments
  8.     1.00    01/03/93    Original Version
  9.     1.01    07/03/93    Remove blackColor
  10.     1.02    08/03/93    Shape selection by popup menu
  11.     2.00    17/03/93    Updated for After Dark version 2.0
  12.     2.01    22/03/93    To draw on every monitor
  13. */
  14.  
  15. #include <QuickDraw.h>
  16. #include <Memory.h>
  17. #include <OSUtils.h>
  18. #include "GraphicsModule_Types.h"
  19.  
  20. /* Function prototypes */
  21. int        Rand(int range);
  22. void    PaintShapes(int xmid, int ymid, int x, int y, int dx, int dy, int type);
  23. void    PaintShape(Rect *rect, int dx, int dy, int type);
  24.  
  25. long    Colors[] = {
  26.     /*    blackColor, */
  27.         whiteColor,
  28.         redColor,
  29.         greenColor,
  30.         blueColor,
  31.         cyanColor,
  32.         magentaColor,
  33.         yellowColor };
  34.         
  35. /* Specific graphics module data structures */
  36. typedef struct MyStorage
  37.     {
  38.     int    dummy;
  39.     } MyStorage, *MyStoragePtr, **MyStorageHandle;
  40.  
  41. /*
  42.     DoInitialize is the first function called from After Dark.
  43.     Memory for the structure is allocated and the variables are initialized.
  44.     The allocated memory is assigned to "storage"
  45.     and the function returns "noErr" if there are no problems.
  46. */
  47. OSErr    DoInitialize(Handle *storage, RgnHandle blankRgn, GMParamBlockPtr params)
  48.     {
  49.     MyStorageHandle myStorage;
  50.     
  51.     /* allocate handle to my storage */
  52.     myStorage = (MyStorageHandle)NewHandle(sizeof(MyStorage));
  53.     
  54.     if (!myStorage)
  55.         return MemError();
  56.     
  57.     MoveHHi(myStorage);
  58.     HLock(myStorage);                    /* Lock it down */
  59.     
  60.     *storage = (Handle)myStorage;        /* Assign myStorage to passed handle */
  61.  
  62.     HUnlock(myStorage);
  63.     return noErr;
  64.     }
  65.  
  66. /*  
  67.     DoBlank is the next function called.
  68.     It is used to simply blank the screen.
  69. */ 
  70. OSErr    DoBlank(Handle storage, RgnHandle blankRgn, GMParamBlockPtr params)
  71.     {
  72.     FillRgn(blankRgn, params->qdGlobalsCopy->qdBlack);
  73.     return noErr;
  74.     }
  75.  
  76. /*
  77.     DoDrawFrame does almost all of the work.
  78. */
  79. OSErr    DoDrawFrame(Handle storage, RgnHandle blankRgn, GMParamBlockPtr params)
  80.     {
  81.     OSErr err = noErr;
  82.     MyStoragePtr myStorage;                /* to hold dereferenced storage handle */
  83.     int    x;
  84.     int    y;
  85.     int    dx;
  86.     int    dy;
  87.     int    xmid;
  88.     int    ymid;
  89.     int    width;
  90.     int    height;
  91.     int size;
  92.     int    color;
  93.     int    monitor;
  94.     Rect    rect;
  95.  
  96.     /* Lock our storage down so we can dereference it once for faster access */
  97.     MoveHHi(storage);
  98.     HLock(storage);
  99.     myStorage = (MyStoragePtr)*storage;
  100.     
  101.     /* For each monitor */
  102.     for (monitor = 0; monitor < params->monitors->monitorCount; monitor++)
  103.         {
  104.         rect = params->monitors->monitorList[monitor].bounds;
  105.         xmid = (rect.left + rect.right) / 2;
  106.         ymid = (rect.top + rect.bottom) / 2;
  107.         width = rect.right - rect.left;
  108.         height = rect.bottom - rect.top;
  109.         size = width > height ? width : height;
  110.         
  111.         /* Random x, y */
  112.         x = Rand(size/2)+1;
  113.         y = Rand(x)+1;
  114.         
  115.         /* Random dx, dy */
  116.         dx = params->controlValues[0];
  117.         dy = params->controlValues[1];
  118.         
  119.         dx = Rand(dx < 4 ? 4 : dx) + 1;
  120.         dy = Rand(dy < 2 ? 2 : dy) + 1;
  121.         
  122.         /* Random color */
  123.         color = Rand(7);
  124.         ForeColor(Colors[color]);
  125.         
  126.         /* Paint Shapes */
  127.         PaintShapes(xmid, ymid, x, y, dx, dy, params->controlValues[2]);
  128.         }
  129.     
  130.     HUnlock(storage);
  131.     return noErr;
  132.     }
  133.  
  134. /*
  135.     DoClose is called when the user quits the screen saver.
  136.     The memory is deallocated and the function returns "MemError"
  137.     which will tell After Dark if there was a problem
  138. */
  139. OSErr    DoClose(Handle storage, RgnHandle blankRgn, GMParamBlockPtr params)
  140.     {
  141.     MyStorageHandle myStorage = (MyStorageHandle)storage;
  142.         
  143.     /* Deallocate our storage */
  144.     if (myStorage)
  145.         {
  146.         MoveHHi(myStorage);
  147.         HLock(myStorage);
  148.         DisposHandle(storage);
  149.         }
  150.     
  151.     /* check for memory errors */
  152.     return MemError();
  153.     }
  154.  
  155. /*
  156.     DoSetUp is called if the user clicks on a button in the Control Panel.
  157. */
  158. OSErr    DoSetUp(RgnHandle blankRgn, short message, GMParamBlockPtr params)
  159.     {
  160.     return noErr;
  161.     }
  162.  
  163. /*
  164.     Rand returns value (0..range-1)
  165. */
  166. int    Rand(int range)
  167.     {
  168.     long    result = Random();
  169.     
  170.     if (result < 0)
  171.         result =- result;
  172.     
  173.     return ((result * range) / 32768);
  174.     }
  175.  
  176. /*
  177.     PaintShapes paints eight symmetrically placed shapes
  178. */
  179. void    PaintShapes(int xmid, int ymid, int x, int y, int dx, int dy, int type)
  180.     {
  181.     Rect    rect;
  182.     
  183.     /* x, y */
  184.     SetRect(&rect, xmid+(x-dx), ymid+(y-dy), xmid+(x+dx), ymid+(y+dy));
  185.     PaintShape(&rect, dx, dy, type);
  186.     
  187.     /* -x, y*/
  188.     SetRect(&rect, xmid-(x+dx), ymid+(y-dy), xmid-(x-dx), ymid+(y+dy));
  189.     PaintShape(&rect, dx, dy, type);
  190.     
  191.     /* x, -y */
  192.     SetRect(&rect, xmid+(x-dx), ymid-(y+dy), xmid+(x+dx), ymid-(y-dy));
  193.     PaintShape(&rect, dx, dy, type);
  194.     
  195.     /* -x, -y */
  196.     SetRect(&rect, xmid-(x+dx), ymid-(y+dy), xmid-(x-dx), ymid-(y-dy));
  197.     PaintShape(&rect, dx, dy, type);
  198.     
  199.     /* y, x*/
  200.     SetRect(&rect, xmid+(y-dy), ymid+(x-dx), xmid+(y+dy), ymid+(x+dx));
  201.     PaintShape(&rect, dx, dy, type);
  202.     
  203.     /* -y, x*/
  204.     SetRect(&rect, xmid-(y+dy), ymid+(x-dx), xmid-(y-dy), ymid+(x+dx));
  205.     PaintShape(&rect, dx, dy, type);
  206.     
  207.     /* y, -x*/
  208.     SetRect(&rect, xmid+(y-dy), ymid-(x+dx), xmid+(y+dy), ymid-(x-dx));
  209.     PaintShape(&rect, dx, dy, type);
  210.     
  211.     /* -y, -x*/
  212.     SetRect(&rect, xmid-(y+dy), ymid-(x+dx), xmid-(y-dy), ymid-(x-dx));
  213.     PaintShape(&rect, dx, dy, type);
  214.     }
  215.  
  216. /*
  217.     PaintShape paints single shape according to type
  218. */
  219. void    PaintShape(Rect *rect, int dx, int dy, int type)
  220.     {
  221.     switch (type)
  222.         {
  223.         case 1:    /* Rectangle */
  224.             PaintRect(rect);
  225.             break;
  226.         case 2:    /* Rounded */
  227.             PaintRoundRect(rect, dx, dy);
  228.             break;
  229.         case 3:    /* Rectangle */
  230.             PaintOval(rect);
  231.             break;
  232.         }
  233.     }
  234.