Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <math.h>
- #include <stdlib.h>
- #include <SFML/Graphics.hpp>
- #include <SFML/Graphics/RectangleShape.hpp>
- #include <SFML/Graphics/Color.hpp>
- int width = 190;
- int height = 106;
- int scale = 10;
- int arrSize = width * height;
- using namespace std;
- class Cell {
- public:
- int alive;
- };
- int main() {
- cout << "Arr Size " << arrSize;
- sf::RenderWindow window(sf::VideoMode(width * scale, height * scale), "Grid");
- window.setFramerateLimit(5);
- Cell cells[width][height];
- Cell tempCells[width][height];
- //rectangle.setSize(sf::Vector2<float>(scale,scale));
- //rectangle.setPosition(10, 20);
- for ( int x = 0; x < width; x++ ) {
- for ( int y = 0; y < height; y++ ) {
- Cell rect = {alive: 0};
- cells[x][y] = rect;
- }
- }
- bool playing = false;
- // Start the game loop
- while (window.isOpen())
- {
- // Process events
- sf::Event event;
- while (window.pollEvent(event))
- {
- // Close window: exit
- if (event.type == sf::Event::Closed)
- window.close();
- }
- if (sf::Mouse::isButtonPressed(sf::Mouse::Right)) {
- sf::Vector2i localPosition = sf::Mouse::getPosition(window);
- int x = (int)localPosition.x / scale;
- int y = (int)localPosition.y / scale;
- if(x < width && x > -1 && y < height && y > -1 ) {
- cells[x][y].alive = 0;
- }
- }
- if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
- sf::Vector2i localPosition = sf::Mouse::getPosition(window);
- int x = (int)localPosition.x / scale;
- int y = (int)localPosition.y / scale;
- if(x < width && x > -1 && y < height && y > -1 ) {
- cells[x][y].alive = 1;
- }
- }
- // Clear screen
- window.clear();
- // Draw the string
- for ( int x = 0; x < width; x++ ) {
- for ( int y = 0; y < height; y++ ) {
- tempCells[x][y] = cells[x][y];
- }
- }
- if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
- for ( int x = 0; x < width; x++ ) {
- for ( int y = 0; y < height; y++ ) {
- tempCells[x][y] = cells[x][y];
- int neighbours = 0;
- if(cells[x-1][y-1].alive == 1) { neighbours++; }
- if(cells[x-1][y+0].alive == 1) { neighbours++; }
- if(cells[x-1][y+1].alive == 1) { neighbours++; }
- if(cells[x+0][y+1].alive == 1) { neighbours++; }
- if(cells[x+0][y-1].alive == 1) { neighbours++; }
- if(cells[x+1][y-1].alive == 1) { neighbours++; }
- if(cells[x+1][y+0].alive == 1) { neighbours++; }
- if(cells[x+1][y+1].alive == 1) { neighbours++; }
- if(neighbours > 0) {
- cout << "X: " << x << " Y: " << y << " N: " << neighbours << endl;
- }
- if(neighbours < 2 || neighbours > 3 && cells[x][y].alive == 1) { tempCells[x][y].alive = 0; }
- if(neighbours == 3 && cells[x][y].alive == 0) { tempCells[x][y].alive = 1; }
- }
- }
- }
- for ( int x = 0; x < width; x++ ) {
- for ( int y = 0; y < height; y++ ) {
- cells[x][y] = tempCells[x][y];
- }
- }
- for ( int x = 2; x < width - 2; x++ ) {
- for ( int y = 2; y < height - 2; y++ ) {
- Cell rect = cells[x][y];
- sf::RectangleShape rectangle;
- rectangle.setSize( sf::Vector2f( scale, scale ) );
- rectangle.setPosition( x * scale, y * scale );
- rectangle.setFillColor( sf::Color( 255 * rect.alive, 255 * rect.alive, 255 * rect.alive ) );
- rectangle.setOutlineColor( sf::Color( 0, 100, 100) );
- rectangle.setOutlineThickness(1.0f);
- window.draw( rectangle );
- }
- }
- // Update the window
- window.display();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement