Advertisement
LittleFox94

Conways Game of Life - C++/SFML1.6

Aug 29th, 2013
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.27 KB | None | 0 0
  1. /**
  2.  * Conways game of life
  3.  * Implemented by LittleFox with SFML
  4.  * License: CC-BY
  5.  *
  6.  * Compile with
  7.  * gcc main.cpp -o gol /usr/lib/libsfml-graphics.so /usr/lib/libsfml-system.so /usr/lib/libsfml-window.so
  8.  *
  9.  * You'll have to install sfml 1.6 before building it.
  10.  * Debian-based distributions:
  11.  * # apt-get install libsfml-dev
  12.  */
  13.  
  14. #include <SFML/Graphics.hpp>
  15. #include <cstring>
  16.  
  17. // Resolution of the window is [WIDTH * PXFACTOR] x [HEIGHT * PXFACTOR]
  18. #define HEIGHT 360
  19. #define WIDTH 640
  20. #define PXFACTOR 2
  21. #define MAXGENERATIONS 1500
  22.  
  23. //! 2 Cell arrays (the one currently displayed + the next generation) and pointers to get the current and next one
  24. bool *_cellArray1, *_cellArray2, *_currentCells, *_nextCells;
  25.  
  26. //! rendered pixeldata
  27. unsigned char *_pixels;
  28.  
  29. /**
  30.  * Returns the state of a given cell
  31.  *
  32.  * @param x X-position of the cell
  33.  * @param y Y-position of the cell
  34.  * @return state of the given cell or false if out-of-range
  35.  */
  36. bool GetCell(int x, int y)
  37. {
  38.     if(x >= 0 && x < WIDTH && y >= 0 && y < HEIGHT)
  39.         return _currentCells[y * WIDTH + x];
  40.     else
  41.         return false;
  42. }
  43.  
  44. /**
  45.  * Returns the number of neighbors for a given cell
  46.  *
  47.  * @param x X-position of the cell
  48.  * @param y Y-position of the cell
  49.  * @return The number of alive cells in the 8-neighborhood
  50.  */
  51. int GetNeighborCount(int x, int y)
  52. {
  53.     int neighbors = 0;
  54.  
  55.     if(GetCell(x - 1, y - 1))
  56.         neighbors++;
  57.  
  58.     if(GetCell(x, y - 1))
  59.         neighbors++;
  60.  
  61.     if(GetCell(x + 1, y - 1))
  62.         neighbors++;
  63.  
  64.     if(GetCell(x - 1, y + 1))
  65.         neighbors++;
  66.  
  67.     if(GetCell(x, y + 1))
  68.         neighbors++;
  69.  
  70.     if(GetCell(x + 1, y + 1))
  71.         neighbors++;
  72.  
  73.     if(GetCell(x - 1, y))
  74.         neighbors++;
  75.  
  76.     if(GetCell(x + 1, y))
  77.         neighbors++;
  78.  
  79.     return neighbors;
  80. }
  81.  
  82. //! Kills lonely cells or cells in over-populated areas
  83. void KillCells()
  84. {
  85.     for(int y = 0; y < HEIGHT; y++)
  86.     {
  87.         for(int x = 0; x < WIDTH; x++)
  88.         {
  89.             if(!(GetNeighborCount(x, y) == 2 || GetNeighborCount(x, y) == 3))
  90.             {
  91.                 _nextCells[y * WIDTH + x] = false;
  92.             }
  93.         }
  94.     }
  95. }
  96.  
  97. //! Awakes cells by reproduction
  98. void AwakeCells()
  99. {
  100.     for(int y = 0; y < HEIGHT; y++)
  101.     {
  102.         for(int x = 0; x < WIDTH; x++)
  103.         {
  104.             if(GetNeighborCount(x, y) == 3)
  105.             {
  106.                 _nextCells[y * WIDTH + x] = true;
  107.             }
  108.         }
  109.     }
  110. }
  111.  
  112. //! Renders the cell-matrix to the pixeldata-array for drawing
  113. void RenderCells()
  114. {
  115.     for(unsigned int y = 0; y < HEIGHT; y++)
  116.         {
  117.             for(unsigned int x = 0; x < WIDTH; x++)
  118.             {
  119.                 _pixels[(y * WIDTH + x) * 4 + 0] = GetCell(x, y) ? 255 : 0;
  120.                 _pixels[(y * WIDTH + x) * 4 + 1] = GetCell(x, y) ? 255 : 0;
  121.                 _pixels[(y * WIDTH + x) * 4 + 2] = GetCell(x, y) ? 255 : 0;
  122.                 _pixels[(y * WIDTH + x) * 4 + 3] = 255;
  123.             }
  124.         }
  125. }
  126.  
  127. //! Generates a new, random map
  128. void GenerateRandomMap()
  129. {
  130.     sf::Randomizer random;
  131.  
  132.     for(int y = 0; y < HEIGHT; y++)
  133.     {
  134.         for(int x = 0; x < WIDTH; x+=16)
  135.         {
  136.             int r = random.Random(0, 0xFFFF);
  137.  
  138.             for(int i = 0; i < 16; i++)
  139.             {
  140.                 _currentCells[y * WIDTH + x + i] = (r & (1 << i)) != 0;
  141.             }
  142.         }
  143.     }
  144. }
  145.  
  146. /**
  147.  * Runs the program
  148.  *
  149.  * @return statuscode
  150.  */
  151. int main()
  152. {
  153.     _cellArray1 = new bool[HEIGHT * WIDTH];
  154.     _cellArray2 = new bool[HEIGHT * WIDTH];
  155.     _currentCells = _cellArray1;
  156.     _nextCells = _cellArray2;
  157.  
  158.     _pixels = new unsigned char[HEIGHT * PXFACTOR * WIDTH * PXFACTOR * 4];
  159.  
  160.     sf::RenderWindow *App = new sf::RenderWindow(sf::VideoMode(WIDTH * PXFACTOR, HEIGHT * PXFACTOR, true), "SFML window", sf::Style::Fullscreen | sf::Style::Close);
  161.  
  162.     GenerateRandomMap();
  163.  
  164.     sf::Sprite sprite;
  165.     int generation = 0;
  166.  
  167.     while (App->IsOpened())
  168.     {
  169.         sf::Event event;
  170.         while (App->GetEvent(event))
  171.         {
  172.             if(event.Type == sf::Event::KeyPressed)
  173.             {
  174.                 switch(event.Key.Code)
  175.                 {
  176.                     case sf::Key::N:
  177.                         GenerateRandomMap();
  178.                         generation = 0;
  179.                         break;
  180.                     case sf::Key::Escape:
  181.                         App->Close();
  182.                         break;
  183.                     default:
  184.                         break;
  185.                 }
  186.             }
  187.         }
  188.  
  189.         App->Clear();
  190.  
  191.         AwakeCells();
  192.         KillCells();
  193.         RenderCells();
  194.  
  195.         bool* temp = _currentCells;
  196.         _currentCells = _nextCells;
  197.         _nextCells = temp;
  198.  
  199.         memcpy(_nextCells, _currentCells, HEIGHT * WIDTH * sizeof(bool));
  200.  
  201.         sf::Image image;
  202.         image.LoadFromPixels(WIDTH, HEIGHT, _pixels);
  203.         image.SetSmooth(false);
  204.         sprite.SetImage(image);
  205.         sprite.Resize(App->GetWidth(), App->GetHeight());
  206.         sprite.SetPosition(0, 0);
  207.  
  208.         App->Draw(sprite);
  209.  
  210.         generation++;
  211.  
  212.         if(generation == MAXGENERATIONS)
  213.         {
  214.             GenerateRandomMap();
  215.             generation = 0;
  216.         }
  217.  
  218.         App->Display();
  219.     }
  220.  
  221.     return EXIT_SUCCESS;
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement