home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / VGALIB.ZIP / CELLS.C next >
C/C++ Source or Header  |  1992-09-05  |  3KB  |  121 lines

  1. #include<stdlib.h>
  2. #include<stdio.h>
  3. #include<conio.h>
  4. #define INCL_BASE
  5. #include<os2.h>
  6. #include<string.h>
  7. #include"vgalib.h"
  8.  
  9. #define HANDLE 0
  10. #define BUFSIZE 12
  11. #define HRES 320 
  12. #define VRES 200
  13. #define COLORSET 32
  14.  
  15. #define xs 319 
  16. #define ys 199
  17. #define xc 320 
  18. #define yc 200 
  19.  
  20. PCH ptr0;
  21. struct _VIOPHYSBUF phys;
  22. struct _VIOMODEINFO orig,moda;
  23. char status;
  24. char st;
  25. char temp;
  26. unsigned int temp2;
  27. unsigned long numchange;
  28. unsigned short VWAIT;
  29.  
  30. int i,j,k,l,x,y;
  31. unsigned char oz[HRES][VRES],nz[HRES][VRES];
  32. int nx[5]={1,-1,0,0};
  33. int ny[5]={0,0,1,-1};
  34.  
  35. main() {
  36. printf("Cells - A Cellular Automata Demo for OS/2 2.0  \n");
  37. printf("        By John E. Stone                       \n");
  38. printf("For comments send E-Mail to: johns@cs.umr.edu  \n");
  39. printf("\n\n\n\n\n");
  40. printf("The number of states is how many different colors\n");
  41. printf("a pixel can take on.  The larger the number of states\n");
  42. printf("the longer it takes to complete, and it may even become\n");
  43. printf("stagnant with more than 20 states.\n\n");
  44. printf("\nProcess ID=%d\n",getpid());
  45. printf("Would you like cells to continue processing when it\n");
  46. printf("isn't visible on the screen? (1=no, 0=yes)                 \n");
  47. scanf("%u",&VWAIT);
  48. if ((VWAIT<0) || (VWAIT>1)) VWAIT=0;
  49.  
  50. printf("\n\nEnter the number of states in the system: (5-25) \n");
  51. scanf("%d",&temp2);
  52. st=10;
  53. srand(getpid());
  54.  
  55. if ((temp2>4) || (temp2<26)) { st=temp2; }
  56.  
  57.  
  58. /* setup for graphics mode */
  59. phys.pBuf=(unsigned char *) 0xA0000;
  60. phys.cb=65536;
  61.  
  62. moda.cb=12;
  63. moda.fbType=3;
  64. moda.color=8;
  65. moda.row=25;
  66. moda.col=40;
  67. moda.hres=320;
  68. moda.vres=200;
  69.  
  70. VioGetMode(&orig, HANDLE);
  71. VioSetMode(&moda, HANDLE);
  72. VioGetPhysBuf(&phys, 0);
  73. ptr0=MAKEP(phys.asel[0], 0);
  74. VioScrLock(1, &status, HANDLE); /* Wait for exclusive screenlock */
  75.  
  76. cls(ptr0,0);
  77.  
  78. for (i=0; i<=xs; i++) {
  79.   for (j=0; j<=ys; j++) {
  80.          nz[i][j]=(char) rand() % st;
  81.          oz[i][j]=nz[i][j];
  82.   }
  83. }
  84. numchange=1;
  85.  
  86. while ((kbhit()==0) && (numchange>0)) {
  87. numchange=0;
  88. for (i=0; i<=xs; i++) {
  89.   for (j=0; j<=ys; j++) {
  90.     for (x=0; x<=3; x++)  {
  91.              k=(i+nx[x]+xc) % xc;
  92.              l=(j+ny[x]+yc) % yc;
  93.              if (oz[k][l]==((oz[i][j]+1) % st)) {
  94.                  nz[i][j]=(oz[k][l] % st);
  95.          numchange++;
  96.          }
  97.     }
  98.   }
  99. }
  100.  
  101. memcpy(oz,nz,sizeof(nz));
  102. VioScrLock(VWAIT,&status,HANDLE);
  103. if (!status) {
  104. for (i=0; i<VRES; i++) {
  105.   for (j=0; j<HRES; j++) {
  106.     temp=COLORSET+nz[j][i];
  107.     putpixel(ptr0,j,i,temp);
  108.   }
  109. }
  110. }
  111.  
  112. VioScrUnLock(HANDLE);
  113. }
  114.  
  115. VioScrLock(WAIT,&status,HANDLE);
  116. cls(ptr0,0);
  117.  
  118. VioScrUnLock(HANDLE);
  119.  
  120. } /* end of cells.c */
  121.