home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 300-399 / ff303.lzh / Demon / Demon.c < prev    next >
C/C++ Source or Header  |  1990-01-08  |  3KB  |  140 lines

  1. /* Debris generator, jfr. Scientific American August 1989 */
  2. #include <intuition/intuition.h>
  3. #include <graphics/gfx.h>
  4. #include <graphics/gfxbase.h>
  5. #include <graphics/gfxmacros.h>
  6. #include <functions.h>
  7. #include <exec/memory.h>
  8.  
  9. #define SIZEX 120
  10. #define SIZEY 120
  11. #define RANGE 16
  12.  
  13. char *OldBuffer=NULL,*CurrentBuffer=NULL;
  14.  
  15. struct IntuitionBase *IntuitionBase=NULL;
  16. struct GfxBase *GfxBase=NULL;
  17.  
  18. struct NewScreen DebrisNewScreen = {
  19.     0,0,320,200,4,
  20.     0,4,NULL,CUSTOMSCREEN,NULL,NULL,NULL,NULL };
  21.  
  22. UWORD DebrisColours[16] = {
  23.     0x0E09, 0x0B09, 0x090A, 0x056B,
  24.     0x008C, 0x00AC, 0x00BB, 0x00B8,
  25.     0x04C0, 0x09D0, 0x0CE0, 0x0FF0,
  26.     0x0FD0, 0x0FA0, 0x0F70, 0x0F06};
  27.  
  28. struct NewWindow DebrisNewWindow = {
  29.     0,1,123,130,-1,-1,
  30.     CLOSEWINDOW, WINDOWCLOSE | SMART_REFRESH | ACTIVATE,
  31.     NULL, NULL, (UBYTE *)"Debris from Scientific American",
  32.     NULL, NULL, 0, 0, 0, 0, CUSTOMSCREEN };
  33.  
  34. struct Screen *DebrisScreen=NULL;
  35. struct Window *DebrisWindow=NULL;
  36.  
  37. OpenAll()
  38. {
  39.     register long i,j;
  40.     long seconds,micros;
  41.     
  42.     IntuitionBase = (struct IntuitionBase*)OpenLibrary("intuition.library",0L);
  43.     GfxBase = (struct GfxBase*)OpenLibrary("graphics.library",0L);
  44.     
  45.     if (!(DebrisScreen = OpenScreen(&DebrisNewScreen)))
  46.         abort("No Screen");
  47.     DebrisNewWindow.Screen = DebrisScreen;
  48.     if (!(DebrisWindow = OpenWindow(&DebrisNewWindow)))
  49.         abort("No Window");
  50.     LoadRGB4(&(DebrisScreen->ViewPort), DebrisColours, 16L);
  51.     CurrentTime(&seconds,µs);
  52.     seconds=micros; micros>>8; micros+=seconds;
  53.     srand(seconds);
  54.     if (!(CurrentBuffer=AllocMem((long)SIZEX*SIZEY,MEMF_PUBLIC)))
  55.         abort("No memory");
  56.     if (!(OldBuffer=AllocMem((long)SIZEX*SIZEY,MEMF_PUBLIC)))
  57.         abort("No buffer memory");
  58.     for (i=0; i < SIZEX; i++)
  59.         for (j=0; j<SIZEY; j++)
  60.         {
  61.             *(CurrentBuffer+i*SIZEY+j) = rand() % RANGE;
  62.             *(OldBuffer+i*SIZEY+j)=-1;
  63.         }
  64. }
  65.  
  66. abort(Tekst)
  67. char *Tekst;
  68. {
  69.     if (Tekst!=NULL) puts(Tekst);
  70.     
  71.     if (CurrentBuffer) FreeMem(CurrentBuffer,(long)SIZEX*SIZEY);
  72.     if (OldBuffer) FreeMem(OldBuffer,(long)SIZEX*SIZEY);
  73.     if (DebrisWindow) CloseWindow(DebrisWindow);
  74.     if (DebrisScreen) CloseScreen(DebrisScreen);
  75.  
  76.     if (IntuitionBase) CloseLibrary(IntuitionBase);
  77.     if (GfxBase) CloseLibrary(GfxBase);
  78.     exit(0);
  79. }
  80.  
  81. DoGeneration()
  82. {
  83.     register long i,j,k,l;
  84.  
  85.     for (i=0; i<SIZEX; i++)
  86.         for (j=0; j<SIZEY; j++)
  87.             *(OldBuffer+i*SIZEY+j) = *(CurrentBuffer+i*SIZEY+j);
  88.     for (i=1; i<SIZEX; i++)
  89.         for (j=1; j<SIZEY; j++)
  90.         {
  91.             k=i*SIZEY+j;
  92.             if (*(OldBuffer+k-SIZEY)==(*(CurrentBuffer+k)+1)%RANGE)
  93.                 *(CurrentBuffer+k)+=1;
  94.             else
  95.             {
  96.                 if (*(OldBuffer+k-1)==(*(CurrentBuffer+k)+1)%RANGE)
  97.                     *(CurrentBuffer+k)+=1;
  98.                 else
  99.                 {
  100.                     if (*(OldBuffer+k+1)==(*(CurrentBuffer+k)+1)%RANGE)
  101.                         *(CurrentBuffer+k)+=1;
  102.                     else
  103.                         if (*(OldBuffer+k+SIZEY)==(*(CurrentBuffer+k)+1)%RANGE)
  104.                             *(CurrentBuffer+k)+=1;
  105.                 }
  106.             }
  107.             if (*(CurrentBuffer+k)>=RANGE) *(CurrentBuffer+k)=0;
  108.             if (*(OldBuffer+k)!=*(CurrentBuffer+k))
  109.             {
  110.                 SetAPen(DebrisWindow->RPort,(long)*(CurrentBuffer+k));
  111.                 WritePixel(DebrisWindow->RPort,i+1,j+9L);
  112.             }
  113.         }
  114. }
  115.  
  116. DisplayGeneration()
  117. {
  118.     register long i,j;
  119.     
  120.     for (i=1;i<SIZEX;i++)
  121.         for (j=1;j<SIZEY;j++)
  122.         {
  123.             SetAPen(DebrisWindow->RPort,(long)*(CurrentBuffer+i*SIZEY+j));
  124.             WritePixel(DebrisWindow->RPort,i+1L,j+9L);
  125.         }
  126. }
  127.  
  128. main()
  129. {
  130.     struct IntuiMessage *Msg;
  131.     
  132.     OpenAll();
  133.     DisplayGeneration();
  134.     while (!(Msg = (struct IntuiMessage*)GetMsg(DebrisWindow->UserPort)))
  135.         DoGeneration();
  136.     ReplyMsg((struct Message*)Msg);
  137.     abort(0L);
  138. }
  139.  
  140.