Created
April 25, 2020 03:35
-
-
Save edo0xff/57ff3d47038bc3371255975da3b1e845 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
GAME OF LIFE - SFML implementation | |
*/ | |
#include <iostream> | |
#include <stdlib.h> | |
#include "SFML/Graphics.hpp" | |
#define WINDOW_WIDTH 1000 | |
#define WINDOW_HEIGHT 700 | |
struct Board | |
{ | |
int cells[WINDOW_WIDTH][WINDOW_HEIGHT] = {{0}}; | |
int cells_alive = 0; | |
}; | |
void init_board(Board *board); | |
void board_tick(Board *board); | |
void copy_cells(int (&cells)[WINDOW_WIDTH][WINDOW_HEIGHT], int (&temp)[WINDOW_WIDTH][WINDOW_HEIGHT]); | |
int get_alive_neightboards(Board *board, int x, int y); | |
sf::VertexArray get_board_vertex(Board *board); | |
int main(const int argc, const char *argv[]) | |
{ | |
Board board; | |
sf::Clock clock; | |
sf::RenderWindow window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "Game of life"); | |
init_board(&board); | |
window.setFramerateLimit(60); | |
while(window.isOpen()) | |
{ | |
sf::Event event; | |
while(window.pollEvent(event)) | |
{ | |
if(event.type == sf::Event::Closed) | |
window.close(); | |
} | |
sf::Vector2i mouse = sf::Mouse::getPosition(window); | |
board.cells[mouse.x][mouse.y] = 1; | |
board_tick(&board); | |
window.clear(); | |
window.draw(get_board_vertex(&board)); | |
window.display(); | |
} | |
return 0; | |
} | |
void init_board(Board *board) { | |
for (int x = 0; x < WINDOW_WIDTH; x++) | |
{ | |
for (int y = 0; y < WINDOW_HEIGHT; y++) | |
{ | |
board->cells[x][y] = rand() % 100; | |
if (board->cells[x][y] == 1) | |
board->cells_alive++; | |
else | |
board->cells[x][y] = 0; | |
} | |
} | |
} | |
void board_tick(Board *board) | |
{ | |
Board temp_board(*board); | |
board->cells_alive = 0; | |
for (int x = 0; x < WINDOW_WIDTH; x++) | |
{ | |
for (int y = 0; y < WINDOW_HEIGHT; y++) | |
{ | |
int neightboards = get_alive_neightboards(board, x, y); | |
if (board->cells[x][y] == 0 && neightboards == 3) | |
temp_board.cells[x][y] = 1; | |
else if (board->cells[x][y] == 1 && (neightboards < 2 || neightboards > 3)) | |
temp_board.cells[x][y] = 0; | |
if (temp_board.cells[x][y] == 1) | |
board->cells_alive++; | |
} | |
} | |
copy_cells(board->cells, temp_board.cells); | |
} | |
void copy_cells(int (&cells)[WINDOW_WIDTH][WINDOW_HEIGHT], int (&temp)[WINDOW_WIDTH][WINDOW_HEIGHT]) | |
{ | |
for (int x = 0; x < WINDOW_WIDTH; x++) | |
{ | |
for (int y = 0; y < WINDOW_HEIGHT; y++) | |
{ | |
cells[x][y] = temp[x][y]; | |
} | |
} | |
} | |
int get_alive_neightboards(Board *board, int x, int y) | |
{ | |
int count = 0; | |
count += board->cells[x-1][y-1] != NULL ? 1 : 0; | |
count += board->cells[x][y-1] != NULL ? 1 : 0; | |
count += board->cells[x+1][y-1] != NULL ? 1 : 0; | |
count += board->cells[x-1][y] != NULL ? 1 : 0; | |
count += board->cells[x+1][y] != NULL ? 1 : 0; | |
count += board->cells[x-1][y+1] != NULL ? 1 : 0; | |
count += board->cells[x][y+1] != NULL ? 1 : 0; | |
count += board->cells[x+1][y+1] != NULL ? 1 : 0; | |
return count; | |
} | |
sf::VertexArray get_board_vertex(Board *board) | |
{ | |
sf::VertexArray points(sf::Points, board->cells_alive); | |
int k = 0; | |
for (int x = 0; x < WINDOW_WIDTH; x++) | |
{ | |
for (int y = 0; y < WINDOW_HEIGHT; y++) | |
{ | |
if (board->cells[x][y] == 1) | |
points[k++].position = sf::Vector2f(x, y); | |
} | |
} | |
return points; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment