home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Snippets / BackInUse 1.0.2 / BackInUse.c < prev    next >
Encoding:
Text File  |  1995-12-01  |  5.5 KB  |  186 lines  |  [TEXT/CWIE]

  1. // BackInUse
  2. // by Ken Long
  3. // updated for CW7 on 951201
  4.  
  5. //•---------------------------------------------------------------------------•//
  6. //• "This Old Hack," modernized to run on Think C™ v.5.0.4, by Kenneth A. Long,
  7. //• on 24 July 1993, without the help of Norm Abrahm.  Free, so don't sell.
  8. //•---------------------------------------------------------------------------•//
  9. //• BackInUse.c        Original comments were left in and I added some.
  10. //•---------------------------------------------------------------------------•//
  11. //• Program INUSE.C for the Megamax C compiler
  12. //•  (C) 1986 William Woody, commercial rights reserved
  13.  
  14. //• If you think this program is cute, please send me computer mail and tell
  15. //• me so!  I personally think it's a cute hack, myself, but what do I know.
  16.  
  17. //• Though I don't believe this program has any commercial value,  (or ANY
  18. //• monitary value whatsoever), I have reserved commercial rights anyways.
  19. //• Silly me...  But if you want to give it to your friends, please leave
  20. //• my name on the darn thing; I need to feel appreciated.
  21. //•---------------------------------------------------------------------------•//
  22.  
  23. #include <stdio.h>
  24. #include <quickdraw.h>        //• What they don't tell you is that you need to
  25.                             //• include this for the SRAND macro to work...
  26.  
  27. //extern int Random ();
  28. extern char *malloc ();
  29.  
  30. #define MAXX 480
  31. #define MAXY 300
  32. #define MAXM 400
  33.  
  34. WindowPtr theWindow;
  35. WindowRecord wRecord;
  36. Rect dragRect, linesRect;
  37. Rect windowBounds = { 40, 2, 380, 508 };
  38.  
  39. struct foo {
  40.     double a,b;            //• This is a point which gets to fly around
  41.     struct foo *next;
  42. };
  43.  
  44. main ()
  45. {
  46.     short number,n,x,done = 0;
  47.     struct foo *top,*Ptr,*Ptr2;
  48.     
  49. //    struct {
  50. //        short top, left, bottom, right;
  51. //    } linesRect;
  52.     
  53.     long ticks;        //• For the delay in the "splash" screen.
  54.     
  55.     double mx,my,nx,ny;
  56.     
  57.     MaxApplZone();
  58.  
  59. #ifdef THINK_C
  60.     InitGraf(&thePort);
  61. #else
  62.     InitGraf(&qd.thePort);
  63. #endif
  64.     InitFonts();
  65.     FlushEvents(everyEvent, 0);
  66.     InitWindows();
  67.     InitMenus();
  68.     TEInit();
  69.     InitDialogs(0L);
  70.     InitCursor();
  71.  
  72.     //• We can't drag because mouseDown quits us.  
  73.     //• But it's here if we want to rig it in later.
  74.     dragRect = qd.screenBits.bounds;    
  75.     
  76.     theWindow = NewWindow (&wRecord, &windowBounds, "\pIn Use", true, 0, 
  77.                      (WindowPtr)-1L, false, 0L);
  78.     SetPort (theWindow);
  79.  
  80.  
  81.     //• We add a Rect to the window that we can draw in and erase.
  82.     SetRect (&linesRect,0,0,506,380);
  83. //        40, 2, 380, 508
  84.     //• Make sure there's a clean slate to draw in.
  85.     EraseRect (&linesRect);
  86.         
  87.     MoveTo (10,25);
  88.     TextFont (systemFont);    //• System Font is zero, folks.
  89.     TextSize (12);
  90.     DrawString ("\pProgram InUse.C for the Megamax C compiler");
  91.     MoveTo (10,40);
  92.     DrawString ("\pCopyright©1985 by William Woody, commercial rights reserved.");
  93.     MoveTo (10,55);
  94.     DrawString ("\pTo halt the program, press the mouse Button.");
  95.     TextFont (courier);        //• Change font to Courier 12 bold.
  96.     TextSize (12);    
  97.     TextFace (bold);
  98.     MoveTo (10,70);
  99.     DrawString ("\pMade to run on CodeWarrior 7");
  100.     MoveTo (10,85);
  101.     DrawString ("\pBy Kenneth A. Long");    
  102.     GetDateTime ((unsigned long*) &qd.randSeed);    //• Seed the random number generator
  103.     TextFont (0);
  104.     TextSize (12);    //• Set all the font stuff back so our SysParams are OK.
  105.     TextFace (0);
  106.     Delay (240L, &ticks);
  107.     EraseRect (&linesRect);        //• Get ready for action, after bragging.
  108.     
  109.     while  (!done) 
  110.     {
  111.         EraseRect (&linesRect);        //• Added to not stack drawings up.
  112.         number =  (Random () & 3) + 2;    //• Number of points generated.
  113.         x = 0;
  114.             
  115.         top = Ptr = (struct foo *) malloc(sizeof(struct foo));
  116.  
  117.         //• While horizontal incrementing value is not the same 
  118.         //• as number of points...
  119.         while  (x++ != number)
  120.     
  121.         //• ...this stuff is what it is (?).
  122.         Ptr =  (Ptr->next = (struct foo *) malloc (sizeof(struct foo)));
  123.         Ptr =  (Ptr->next = top);
  124.         
  125.         mx = 0;
  126.         my = 0;
  127.         nx = 9999999.0;        //• Large numbers for finding minimum value.
  128.         ny = 9999999.0;
  129.         do 
  130.         {
  131.             Ptr->a =  (double) Random ();
  132.             if  (Ptr->a > mx) mx = Ptr->a;
  133.             if  (Ptr->a < nx) nx = Ptr->a;
  134.             Ptr->b =  (double) Random ();
  135.             if  (Ptr->b > my) my = Ptr->b;
  136.             if  (Ptr->b < ny) ny = Ptr->b;
  137.             Ptr = Ptr->next;
  138.             
  139.         } 
  140.         //• As long as Ptr ain't the same as top...
  141.         while  (Ptr != top);
  142.             do         //• ...make up the points and create "a".
  143.             {        //• A circularly linked list of points.
  144.                 Ptr->a =  (Ptr->a - nx) * MAXX /  (mx - nx);
  145.                 Ptr->b =  (Ptr->b - ny) * MAXY /  (my - ny);
  146.                 Ptr = Ptr->next;    //• Ptr points to next point.
  147.             } 
  148.         //• Again, as long as Ptr ain't the same as top...
  149.         while  (Ptr != top);
  150.             n = 0;        //• ..."n" has 0 value.
  151.             MoveTo ( (int) Ptr->a, (int) Ptr->b);
  152.             
  153.             //• While that's true, and while incrementing "n" is not the 
  154.             //• same as 400 times the number of points generated...
  155.             while  (n++ != MAXM * number)    
  156.             {    //• ...move the points around, like this:
  157.                 
  158.                 //• Give "Ptr->a" a value of ITSELF times 40, PLUS
  159.                 //• the value of "Ptr->next->a" (see above "do loop"),
  160.                 //• then divide that product by 41.  That's the H coord.
  161.                 Ptr->a =  (Ptr->a * 40 + Ptr->next->a) / 41;
  162.                 
  163.                 //• Same for be to get the V coord.
  164.                 Ptr->b =  (Ptr->b * 40 + Ptr->next->b) / 41;
  165.                 
  166.                 //• Then draw the line.
  167.                 LineTo ( (int)Ptr->a, (int)Ptr->b);
  168.                 
  169.                 Ptr = Ptr->next;
  170.                 if  (Button ())    //• Halt program when Button is pressed.
  171.                 {
  172.                     done = 1;    //• It's one DONE puppy!
  173.                     break;        //• Unconditionally get the hell out.
  174.                 }
  175.             }    //• done with "while".
  176.             
  177.             Ptr = top;    //• Clean up the circular linked list.
  178.             do 
  179.             {
  180.                 Ptr =  (Ptr2 = Ptr)->next;
  181.                 free (Ptr2);
  182.             } 
  183.             while  (Ptr != top);    //• As long as Ptr aint the top.
  184.         EraseRect (&linesRect);
  185.     }
  186. }