Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Conways game of life
- * Implemented by LittleFox with SFML
- * License: CC-BY
- *
- * Compile with
- * gcc main.cpp -o gol /usr/lib/libsfml-graphics.so /usr/lib/libsfml-system.so /usr/lib/libsfml-window.so
- *
- * You'll have to install sfml 1.6 before building it.
- * Debian-based distributions:
- * # apt-get install libsfml-dev
- */
- #include <SFML/Graphics.hpp>
- #include <cstring>
- // Resolution of the window is [WIDTH * PXFACTOR] x [HEIGHT * PXFACTOR]
- #define HEIGHT 360
- #define WIDTH 640
- #define PXFACTOR 2
- #define MAXGENERATIONS 1500
- //! 2 Cell arrays (the one currently displayed + the next generation) and pointers to get the current and next one
- bool *_cellArray1, *_cellArray2, *_currentCells, *_nextCells;
- //! rendered pixeldata
- unsigned char *_pixels;
- /**
- * Returns the state of a given cell
- *
- * @param x X-position of the cell
- * @param y Y-position of the cell
- * @return state of the given cell or false if out-of-range
- */
- bool GetCell(int x, int y)
- {
- if(x >= 0 && x < WIDTH && y >= 0 && y < HEIGHT)
- return _currentCells[y * WIDTH + x];
- else
- return false;
- }
- /**
- * Returns the number of neighbors for a given cell
- *
- * @param x X-position of the cell
- * @param y Y-position of the cell
- * @return The number of alive cells in the 8-neighborhood
- */
- int GetNeighborCount(int x, int y)
- {
- int neighbors = 0;
- if(GetCell(x - 1, y - 1))
- neighbors++;
- if(GetCell(x, y - 1))
- neighbors++;
- if(GetCell(x + 1, y - 1))
- neighbors++;
- if(GetCell(x - 1, y + 1))
- neighbors++;
- if(GetCell(x, y + 1))
- neighbors++;
- if(GetCell(x + 1, y + 1))
- neighbors++;
- if(GetCell(x - 1, y))
- neighbors++;
- if(GetCell(x + 1, y))
- neighbors++;
- return neighbors;
- }
- //! Kills lonely cells or cells in over-populated areas
- void KillCells()
- {
- for(int y = 0; y < HEIGHT; y++)
- {
- for(int x = 0; x < WIDTH; x++)
- {
- if(!(GetNeighborCount(x, y) == 2 || GetNeighborCount(x, y) == 3))
- {
- _nextCells[y * WIDTH + x] = false;
- }
- }
- }
- }
- //! Awakes cells by reproduction
- void AwakeCells()
- {
- for(int y = 0; y < HEIGHT; y++)
- {
- for(int x = 0; x < WIDTH; x++)
- {
- if(GetNeighborCount(x, y) == 3)
- {
- _nextCells[y * WIDTH + x] = true;
- }
- }
- }
- }
- //! Renders the cell-matrix to the pixeldata-array for drawing
- void RenderCells()
- {
- for(unsigned int y = 0; y < HEIGHT; y++)
- {
- for(unsigned int x = 0; x < WIDTH; x++)
- {
- _pixels[(y * WIDTH + x) * 4 + 0] = GetCell(x, y) ? 255 : 0;
- _pixels[(y * WIDTH + x) * 4 + 1] = GetCell(x, y) ? 255 : 0;
- _pixels[(y * WIDTH + x) * 4 + 2] = GetCell(x, y) ? 255 : 0;
- _pixels[(y * WIDTH + x) * 4 + 3] = 255;
- }
- }
- }
- //! Generates a new, random map
- void GenerateRandomMap()
- {
- sf::Randomizer random;
- for(int y = 0; y < HEIGHT; y++)
- {
- for(int x = 0; x < WIDTH; x+=16)
- {
- int r = random.Random(0, 0xFFFF);
- for(int i = 0; i < 16; i++)
- {
- _currentCells[y * WIDTH + x + i] = (r & (1 << i)) != 0;
- }
- }
- }
- }
- /**
- * Runs the program
- *
- * @return statuscode
- */
- int main()
- {
- _cellArray1 = new bool[HEIGHT * WIDTH];
- _cellArray2 = new bool[HEIGHT * WIDTH];
- _currentCells = _cellArray1;
- _nextCells = _cellArray2;
- _pixels = new unsigned char[HEIGHT * PXFACTOR * WIDTH * PXFACTOR * 4];
- sf::RenderWindow *App = new sf::RenderWindow(sf::VideoMode(WIDTH * PXFACTOR, HEIGHT * PXFACTOR, true), "SFML window", sf::Style::Fullscreen | sf::Style::Close);
- GenerateRandomMap();
- sf::Sprite sprite;
- int generation = 0;
- while (App->IsOpened())
- {
- sf::Event event;
- while (App->GetEvent(event))
- {
- if(event.Type == sf::Event::KeyPressed)
- {
- switch(event.Key.Code)
- {
- case sf::Key::N:
- GenerateRandomMap();
- generation = 0;
- break;
- case sf::Key::Escape:
- App->Close();
- break;
- default:
- break;
- }
- }
- }
- App->Clear();
- AwakeCells();
- KillCells();
- RenderCells();
- bool* temp = _currentCells;
- _currentCells = _nextCells;
- _nextCells = temp;
- memcpy(_nextCells, _currentCells, HEIGHT * WIDTH * sizeof(bool));
- sf::Image image;
- image.LoadFromPixels(WIDTH, HEIGHT, _pixels);
- image.SetSmooth(false);
- sprite.SetImage(image);
- sprite.Resize(App->GetWidth(), App->GetHeight());
- sprite.SetPosition(0, 0);
- App->Draw(sprite);
- generation++;
- if(generation == MAXGENERATIONS)
- {
- GenerateRandomMap();
- generation = 0;
- }
- App->Display();
- }
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement