home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 260.lha / Truchet / truchet.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-07-07  |  2.4 KB  |  144 lines

  1. #include <exec/types.h>
  2. #include <intuition/intuition.h>
  3. #include <stdio.h>
  4. #include <graphics/gfxmacros.h>
  5. #include <math.h>
  6.  
  7. /* 
  8. might be some extra system stuff defined
  9. due to block cut'n paste overkill...
  10. */
  11.  
  12. /* system functions */
  13. extern struct Window *OpenWindow();
  14. extern struct Library *OpenLibrary();
  15.  
  16. /* system variables */
  17. struct IntuitionBase *IntuitionBase;
  18. struct GfxBase *GfxBase;
  19. struct Window *w;
  20. struct IntuiMessage *msg;
  21. struct RastPort *rp;
  22.  
  23.  
  24. void open_all(), close_all(), draw_tile();
  25.  
  26. struct NewWindow myWindow =
  27. {
  28.       0,0,
  29.       640, 200,
  30.       0,
  31.       1,
  32.       CLOSEWINDOW,
  33.       SMART_REFRESH | WINDOWCLOSE | ACTIVATE | BORDERLESS | GIMMEZEROZERO,
  34.       NULL,
  35.       NULL,
  36.       (UBYTE *)"Truchet Tiles",
  37.       NULL,
  38.       NULL,
  39.       40, 20,
  40.       640, 200,
  41.       WBENCHSCREEN
  42. };
  43.  
  44. /* area fill patterns for tiles */
  45. UWORD tile0[] = 
  46. {
  47.     0x0080,0x0100,0x0600,0xf80f,
  48.     0x0030,0x0040,0x0080,0x0080
  49. };
  50.  
  51.  
  52. UWORD tile1[] = 
  53. {
  54.     0x0080,0x0040,0x0030,0xf80f,
  55.     0x0600,0x0100,0x0080,0x0080
  56. };
  57.  
  58.  
  59. main()
  60. {
  61.     short i, j, bit, seed;
  62.     double rnum;
  63.     printf("Enter a number between 1 and 1000.\n");
  64.     scanf("%h",&seed);
  65.     
  66.     open_all();
  67.  
  68.     SetAPen(rp,2);
  69.     SetBPen(rp,3);
  70.     SetDrMd(rp,JAM2);
  71.     
  72.     if (seed < 1 || seed > 1000) seed = 1;  
  73.     for (i = 0; i <= seed; ++i) rnum = drand48(); /*pseudo randomizer*/
  74.         for (i = 0; i <= 624; i += 16)        /* x loop */
  75.     {
  76.         for (j = 0; j <= 176; j += 16)        /* y loop */
  77.         {
  78.             rnum = drand48();  /* drand48() is a Lattice */
  79.             if (rnum <= 0.5)   /* random number function */
  80.             {
  81.                 bit = 0; 
  82.             }
  83.             else   /* bit picks one of the 2 tile types */
  84.             {
  85.                 bit = 1;
  86.             }
  87.             
  88.             draw_tile(i,j,bit);  /* draws the proper tile */
  89.         }
  90.     }
  91.     
  92.     Wait (1 << w->UserPort->mp_SigBit);  /* wait for close gadget */
  93.     close_all();                         /* close up shop */
  94. }
  95.  
  96.  
  97.  
  98.  
  99.  
  100. void draw_tile(x,y,b)
  101. short x, y, b;
  102. {
  103.  
  104.     if(b) 
  105.     {
  106.         SetAfPt(rp,tile1,3);    /*the bit flag (b here) picks one*/
  107.     }                /*type of tile or the other      */
  108.     else 
  109.     {
  110.         SetAfPt(rp,tile0,3);
  111.     }
  112.             
  113.     RectFill(rp,x,y,x+15,y+15);    /* area fill using pattern */
  114. }
  115.  
  116.  
  117.  
  118. void open_all()
  119. {
  120.     if((IntuitionBase = (struct IntuitionBase *)
  121.           OpenLibrary("intuition.library", 0L)) == NULL)
  122.             exit(1);
  123.     if ((GfxBase = (struct GfxBase *)
  124.         OpenLibrary("graphics.library", 0L)) == NULL)
  125.             exit(1);
  126.     
  127.     
  128.     if ((w = OpenWindow(&myWindow)) == NULL)
  129.         exit(1);
  130.     rp = w->RPort;
  131.  
  132. }
  133.  
  134.  
  135. void close_all()
  136. {
  137.     if (w) CloseWindow(w);
  138.     if (GfxBase) CloseLibrary(GfxBase);
  139.     if (IntuitionBase) CloseLibrary(IntuitionBase);
  140. }
  141.  
  142.  
  143.  
  144.