Skip to content

Commit

Permalink
feat: ✨ Made the main Logic
Browse files Browse the repository at this point in the history
Main Logic Works, Have to add customizability, UI etc.
  • Loading branch information
Polaris66 committed Jul 26, 2023
1 parent 499498e commit b3ec7c2
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 16 deletions.
50 changes: 43 additions & 7 deletions Cell.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,51 @@
#include "headers/Cell.hpp"
#include <SFML/Graphics.hpp>

#include "Headers/Grid.hpp"
#include "Headers/Cell.hpp"

Cell::Cell()
{
}

Cell::Cell(int i, int j, float x)
Cell::Cell(int i, int j, float size)
{
x = i;
y = j;
alive = rand() % 2;
shape = sf::RectangleShape(sf::Vector2f(size, size));
shape.setPosition(i * size, j * size);
}

void Cell::render()
{
shape = sf::RectangleShape(sf::Vector2f(x, x));
shape.setPosition(i * x, j * x);
shape.setFillColor(sf::Color::White);
shape.setOutlineThickness(1.0f);
shape.setOutlineColor(sf::Color(255, 0, 0));
if (alive)
{
shape.setFillColor(sf::Color::White);
}
else
{
shape.setFillColor(sf::Color::Black);
}
}

void Cell::setNeighbors(int _neighbors)
{
neighbors = _neighbors;
}

void Cell::update()
{

if (neighbors > 3 && alive)
{
alive = false;
}
else if (neighbors == 3 and !alive)
{
alive = true;
}
else if (neighbors < 2 && alive)
{
alive = false;
}
}
Binary file modified Cell.o
Binary file not shown.
45 changes: 41 additions & 4 deletions Grid.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include <cstring>
#include "headers/Cell.hpp"
#include "headers/Grid.hpp"
#include <SFML/Graphics.hpp>

#include <iostream>
#include <cstring>

#include "Headers/Grid.hpp"
#include "Headers/Cell.hpp"

Grid::Grid(int _size, int _x, int _y)
{
Expand All @@ -22,11 +24,44 @@ Grid::Grid(int _size, int _x, int _y)

Cell *Grid::getCell(int xpos, int ypos)
{
if (xpos > x || ypos > y || xpos < 0 || ypos < 0)
{
return nullptr;
}
return cells + (y * xpos + ypos);
}

void Grid::setNeighbors(int xpos, int ypos)
{
int neighbors = 0;
for (int i = xpos - 1; i <= xpos + 1; i++)
{
for (int j = ypos - 1; j <= ypos + 1; j++)
{
if (!(xpos == i && ypos == j))
{
Cell *cell = getCell(i, j);
if (cell)
{
neighbors += cell->alive;
}
}
}
}

getCell(xpos, ypos)->setNeighbors(neighbors);
}

void Grid::update()
{
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
setNeighbors(i, j);
getCell(i, j)->update();
}
}
}

void Grid::render(sf::RenderWindow &window)
Expand All @@ -35,7 +70,9 @@ void Grid::render(sf::RenderWindow &window)
{
for (int j = 0; j < y; j++)
{
window.draw(cells[i * y + j].shape);
Cell *cell = getCell(i, j);
cell->render();
window.draw(cell->shape);
}
}
}
Binary file modified Grid.o
Binary file not shown.
7 changes: 5 additions & 2 deletions headers/Cell.hpp → Headers/Cell.hpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
#pragma once
#include <SFML/Graphics.hpp>

class Cell
{
private:
bool nextAliveState;
int x, y;
int neighbors;

public:
sf::RectangleShape shape;
bool alive;
void render();
void update();

void setNeighbors(int _neighbors);

Cell();
Cell(int i, int j, float size);
};
4 changes: 3 additions & 1 deletion headers/Grid.hpp → Headers/Grid.hpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
#pragma once

#include "Cell.hpp"
class Grid
{
private:
int x, y;
int size;
Cell *cells = nullptr;

public:
int size;
Grid(int _size, int _x, int _y);

void update();
void render(sf::RenderWindow &window);

Cell *getCell(int xpos, int ypos);
void setNeighbors(int xpos, int ypos);
};
12 changes: 10 additions & 2 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "headers/Grid.hpp"
#include <SFML/Graphics.hpp>
#include <chrono>

#include "Headers/Grid.hpp"

int main()
{
Expand All @@ -14,8 +16,9 @@ int main()
sf::RenderWindow window(sf::VideoMode(width, height), "Game of Life");

Grid grid(size, x, y);
sf::Clock clock;
// Main Loop
while (window.isOpen())
while (1 == window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
Expand All @@ -29,6 +32,11 @@ int main()

window.clear(sf::Color::Black);

if (clock.getElapsedTime().asSeconds() > 0.01f)
{
grid.update();
clock.restart();
}
grid.render(window);

// Display Stuff
Expand Down
Binary file modified main.o
Binary file not shown.
Binary file modified sfml-app
Binary file not shown.

0 comments on commit b3ec7c2

Please sign in to comment.