home *** CD-ROM | disk | FTP | other *** search
/ Graphics Programming Black Book (Special Edition) / BlackBook.bin / disk1 / source / chapter18 / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-18  |  1.9 KB  |  91 lines

  1. // MAIN.C
  2. //
  3. // David Stafford
  4.  
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <conio.h>
  8. #include <time.h>
  9. #include <bios.h>
  10. #include "life.h"
  11.  
  12. // functions in VIDEO.C
  13. void enter_display_mode( void );
  14. void exit_display_mode( void );
  15. void show_text( int x, int y, char *text );
  16.  
  17.  
  18. void InitCellmap( void )
  19.   {
  20.   unsigned int i, j, t, x, y, init;
  21.  
  22.   for( init = (HEIGHT * WIDTH * 3) / 2; init; init-- )
  23.     {
  24.     x = random( WIDTH * 3 );
  25.     y = random( HEIGHT );
  26.  
  27.     CellMap[ (y * WIDTH) + x / 3 ] |= 0x1000 << (2 - (x % 3));
  28.     }
  29.  
  30.   for( i = j = 0; i < WIDTH * HEIGHT; i++ )
  31.     {
  32.     if( CellMap[ i ] & 0x7000 )
  33.       {
  34.       ChangeList1[ j++ ] = (short)&CellMap[ i ];
  35.       }
  36.     }
  37.  
  38.   NextGen();   // Set cell states, prime the pump.
  39.   }
  40.  
  41.  
  42. void main( void )
  43.   {
  44.   unsigned long generation = 0;
  45.   char gen_text[ 80 ];
  46.   long start_time, end_time;
  47.   unsigned int seed;
  48.  
  49.   printf( "Seed (0 for random seed): " );
  50.   scanf( "%d", &seed );
  51.   if( seed == 0 )  seed = (unsigned) time(NULL);
  52.   srand( seed );
  53.  
  54.   #ifndef NODRAW
  55.   enter_display_mode();
  56.   show_text( 0, 10, "Generation:" );
  57.   #endif
  58.  
  59.   InitCellmap();       // randomly initialize cell map
  60.  
  61.   _bios_timeofday( _TIME_GETCLOCK, &start_time );
  62.  
  63.   do
  64.     {
  65.     NextGen();
  66.     generation++;
  67.  
  68.     #ifndef NOCOUNTER
  69.     sprintf( gen_text, "%10lu", generation );
  70.     show_text( 0, 12, gen_text );
  71.     #endif
  72.     }
  73.   #ifdef GEN
  74.   while( generation < GEN );
  75.   #else
  76.   while( !kbhit() );
  77.   #endif
  78.  
  79.   _bios_timeofday( _TIME_GETCLOCK, &end_time );
  80.   end_time -= start_time;
  81.  
  82.   #ifndef NODRAW
  83.   getch();    // clear keypress
  84.   exit_display_mode();
  85.   #endif
  86.  
  87.   printf( "Total generations: %ld\nSeed: %u\n", generation, seed );
  88.   printf( "%ld ticks\n", end_time );
  89.   printf( "Time: %f generations/second\n",
  90.           (double)generation / (double)end_time * 18.2 );
  91.   }