Skip to content

Commit

Permalink
feat: ✨ Made a Grid
Browse files Browse the repository at this point in the history
  • Loading branch information
Polaris66 committed Jul 25, 2023
1 parent 61ea7fd commit 499498e
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 10 deletions.
15 changes: 15 additions & 0 deletions Cell.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include "headers/Cell.hpp"
#include <SFML/Graphics.hpp>

Cell::Cell()
{
}

Cell::Cell(int i, int j, float x)
{
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));
}
Binary file added Cell.o
Binary file not shown.
41 changes: 41 additions & 0 deletions Grid.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <cstring>
#include "headers/Cell.hpp"
#include "headers/Grid.hpp"
#include <SFML/Graphics.hpp>
#include <iostream>

Grid::Grid(int _size, int _x, int _y)
{
size = _size;
x = _x;
y = _y;

cells = new Cell[x * y];
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
cells[y * i + j] = Cell(i, j, size);
}
}
}

Cell *Grid::getCell(int xpos, int ypos)
{
return cells + (y * xpos + ypos);
}

void Grid::update()
{
}

void Grid::render(sf::RenderWindow &window)
{
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
window.draw(cells[i * y + j].shape);
}
}
}
Binary file added Grid.o
Binary file not shown.
5 changes: 5 additions & 0 deletions headers/Cell.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
#pragma once
#include <SFML/Graphics.hpp>

class Cell
{
private:
bool nextAliveState;

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

Cell();
Cell(int i, int j, float size);
};
13 changes: 7 additions & 6 deletions headers/Grid.hpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
#include <Cell.hpp>

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

public:
int width, height;
Grid(int _width, int _height);
~Grid();
int size;
Grid(int _size, int _x, int _y);

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

Cell &getCell(int xpos, int ypos, int localWidth);
Cell *getCell(int xpos, int ypos);
};
13 changes: 9 additions & 4 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
#include "headers/Grid.hpp"
#include <SFML/Graphics.hpp>

int main()
{
int width = 800;
int height = 600;

int size = 40;

int x = width / size;
int y = height / size;
// Create the main window
sf::RenderWindow window(sf::VideoMode(width, height), "Game of Life");

sf::CircleShape shape(50.f);
shape.setFillColor(sf::Color(100, 250, 50));

Grid grid(size, x, y);
// Main Loop
while (window.isOpen())
{
Expand All @@ -25,8 +29,9 @@ int main()

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

grid.render(window);

// Display Stuff
window.draw(shape);
window.display();
}

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

0 comments on commit 499498e

Please sign in to comment.