Advertisement
snowywhitee

Untitled

Dec 16th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.88 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <direct.h>
  7.  
  8. void GameLife(int** life, int hei, int wid)
  9. {
  10.     int N;
  11.     //temporary matrix
  12.     int** tmp = (int**)malloc(sizeof(int*) * hei);
  13.     for (int j = 0; j < hei; ++i)
  14.     {
  15.         tmp[j] = (int*)malloc(sizeof(int) * wid);
  16.     }
  17.    
  18.     int x, y;
  19.     int inf = 0;
  20.    
  21.     //actual -> copy -> tmp
  22.     for (y = 0; y < hei; y++)
  23.     {
  24.         for (x = 0; x < wid; x++)
  25.         {
  26.             tmp[y][x] = life[y][x];
  27.         }
  28.     }
  29.    
  30.     //go through
  31.     for (y = 1; y < hei - 1; y++)
  32.     {
  33.         for (x = 1; x < wid - 1; x++)
  34.         {
  35.             //N - how many dots around
  36.             N = life[y + 1][x - 1] + life[y + 1][x] + life[y + 1][x + 1] + life[y][x - 1] + life[y][x + 1] + life[y - 1][x - 1] + life[y - 1][x] + life[y - 1][x + 1];
  37.        
  38.             if (tmp[y][x] == 1) //if black
  39.             {
  40.                 if (N == 2) tmp[y][x] = life[y][x];
  41.                 if (N == 3) tmp[y][x] = life[y][x];
  42.                 if (N > 3) tmp[y][x] = 0;
  43.                 if (N < 2) tmp[y][x] = 0;
  44.             }
  45.             else
  46.             {
  47.                 if (N == 3) tmp[y][x] = 1; //if white && N = 3 ->  black
  48.             }
  49.             N = 0;
  50.         }
  51.     }
  52.    
  53.     //change game field && count changes
  54.     for (y = 0; y < hei; y++)
  55.         for (x = 0; x < wid; x++)
  56.         {
  57.             if (life[y][x] == tmp[y][x])
  58.                 inf++;
  59.             life[y][x] = tmp[y][x];
  60.         }
  61.    
  62.     //free space
  63.     for (int i = 0; i < hei; ++i)
  64.     {
  65.         free(tmp[i]);
  66.     }
  67.     free(tmp);
  68.    
  69.     //if all matched - game over
  70.     if (inf == hei * wid)
  71.         exit(0);
  72. }
  73.  
  74. struct Bmp {
  75.     int Width;
  76.     int Height;
  77.     int Size;
  78. };
  79.  
  80. int main(int argc, char* argv[])
  81. {
  82.     struct Bmp Image;
  83.     //default settings
  84.     unsigned char header[54];
  85.     int i, j, k, l, m;
  86.     int maxiter = INT_MAX, dumpfreq = 1;
  87.     char* dirname = NULL;
  88.     FILE* file = NULL;
  89.  
  90.     for (i = 0; i < argc; i++)
  91.     {
  92.         if (!strcmp("--input", argv[i]))
  93.         {
  94.             file = fopen(argv[1 + 1], "rb");
  95.         }
  96.         if (!strcmp("--output", argv[i]))
  97.         {
  98.             dirname = argv[i + 1];
  99.             _mkdir(dirname);
  100.         }
  101.         if (!strcmp("--max_iter", argv[i]))
  102.         {
  103.             maxiter = strtol(argv[i + 1], 0, 10); //str->long integer
  104.         }
  105.         if (!strcmp("--dump_freq", argv[i]))
  106.         {
  107.             dumpfreq = strtol(argv[i + 1], 0, 10);
  108.         }
  109.     }
  110.  
  111.     //READ IMAGE SIZE, HEIGHT, WIDTH, BYTE
  112.     fread(header, sizeof(unsigned char), 54, file);
  113.    
  114.     Image.Width = header[21] * 256 * 256 * 256 + header[20] * 256 * 256 + header[19] * 256 + header[18];
  115.     Image.Height = header[25] * 256 * 256 * 256 + header[24] * 256 * 256 + header[23] * 256 + header[22];
  116.     Image.Size = header[5] * 256 * 256 * 256 + header[4] * 256 * 256 + header[3] * 256 + header[2];
  117.    
  118.     unsigned char* imagebyte = (unsigned char*)malloc((Image.Size - 54) * sizeof(unsigned char));
  119.     fread(imagebyte, sizeof(unsigned char), Image.Size, file);
  120.  
  121.     //matrix height x width
  122.     int** img = (int**)malloc(Image.Height * sizeof(int*));
  123.     for (i = 0; i < Image.Height; i++)
  124.         img[i] = (int*)malloc(Image.Width * sizeof(int));
  125.  
  126.     k = 0;
  127.     for (i = Image.Height - 1; i >= 0; i--)
  128.     {
  129.         for (j = 0; j < Image.Width; j++)
  130.         {
  131.             if (imagebyte[k] == 255) //if white
  132.                 img[i][j] = 0;
  133.             else
  134.                 img[i][j] = 1; //if black
  135.             k += 3;
  136.         }
  137.     }
  138.    
  139.     //from 1 frame to max
  140.     for (l = 1; l <= maxiter; l++)
  141.     {
  142.         GameLife(img, Image.Height, Image.Width);
  143.         //if told so go again
  144.         if (l % dumpfreq != 0) continue;
  145.        
  146.        
  147.         //save frames
  148.         char way[256];
  149.         char filename[16];
  150.         strcpy(way, dirname);
  151.         strcat(way, "\\");
  152.         strcat(way, _itoa(l, filename, 10));
  153.         strcat(way, ".bmp");
  154.         FILE* life = fopen(way, "wb");
  155.         fwrite(header, 1, 54, life);
  156.         m = 0;
  157.        
  158.         // MAKE BITMAP
  159.         for (i = Image.Height - 1; i >= 0; i--)
  160.         {
  161.             for (j = 0; j < Image.Width; j++)
  162.             {
  163.                 for (k = 0; k < 3; k++)
  164.                 {
  165.                     if (img[i][j] == 1)
  166.                         imagebyte[m] = 0;
  167.                     else
  168.                         imagebyte[m] = 255;
  169.                     m++;
  170.                 }
  171.             }
  172.         }
  173.         fwrite(imagebyte, sizeof(unsigned char), Image.Size, life);
  174.         fclose(life);
  175.     }
  176.    
  177.     //clean up
  178.     for (i = 0; i < Image.Height; i++)
  179.     {
  180.         free(img[i]);
  181.     }
  182.     free(img);
  183.     free(imagebyte);
  184.    
  185.    
  186.     return 0;
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement