-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
204 lines (152 loc) · 5.78 KB
/
main.cpp
File metadata and controls
204 lines (152 loc) · 5.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include <SFML/Graphics.hpp>
#include <iostream>
#include <thread>
#include "cellgrid.h"
using namespace std;
/*
* Conoway's Game of Life Simulation
* Created by Liam Morrison 11/13/2021
* Uses SFML Graphics API
*/
// Prototypes
void drawCellGrid(CellGrid &cellGrid, sf::RenderWindow &window);
void drawControl(sf::RenderWindow &window, sf::Color color);
vector<vector<Cell*>*> *allocateGrid();
void deallocateGrid(vector<vector<Cell*>*> *grid);
// Constants
// Maximum is 49, and 37 with this configuration
const int SCREEN_WIDTH = 800, SCREEN_HEIGHT = 608;
const float CELL_WIDTH = 16, CELL_HEIGHT = 16;
const int GRID_WIDTH = SCREEN_WIDTH/CELL_WIDTH, GRID_HEIGHT = SCREEN_HEIGHT/CELL_HEIGHT;
const int CONTROL_BUFFER = 200;
const vector<vector<int>> aliveStartingCells = {};//{1, 0}, {2, 0}, {1, 1}, {2, 1}, {0, 1}, {0, 2}, {1, 2}, {3, 1}, {3, 2}, {3, 3}, {4, 2}, {2, 3}};
int main()
{
cout << "Starting a Game of Life..." << endl;
sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT+CONTROL_BUFFER), "Conoway's Game of Life");
vector<vector<Cell*>*> *originCells = allocateGrid();
CellGrid cellGrid(GRID_WIDTH, GRID_HEIGHT, *originCells);
int mouseX = 0;
int mouseY = 0;
bool runSimulation = false;
bool justClicked = false;
sf::Color color = sf::Color::Green;
cout << "Conway's Game of Life" << endl;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::MouseButtonPressed) {
mouseX = sf::Mouse::getPosition(window).x/CELL_WIDTH;
mouseY = sf::Mouse::getPosition(window).y/CELL_HEIGHT;
justClicked = true;
}
if (event.type == sf::Event::KeyPressed) {
runSimulation = !runSimulation;
}
}
// Draw cells
window.clear();
drawCellGrid(cellGrid, window);
drawControl(window, color);
window.display();
if (justClicked) {
if (mouseX < GRID_WIDTH && mouseX >= 0 && mouseY < GRID_HEIGHT && mouseY >= 0) {
if (*cellGrid.getCellGrid()[mouseX][mouseY]->getState() == ALIVE) {
cellGrid.getCellGrid()[mouseX][mouseY]->setState(DEAD);
*cellGrid.getNextGeneration()[mouseX]->at(mouseY) = DEAD;
} else {
cellGrid.getCellGrid()[mouseX][mouseY]->setState(ALIVE);
*cellGrid.getNextGeneration()[mouseX]->at(mouseY) = ALIVE;
}
} else {
if (color == sf::Color::Red) {
color = sf::Color::Green;
} else {
color = sf::Color::Red;
}
runSimulation = !runSimulation;
}
justClicked = false;
}
// Update cells
if (runSimulation) {
this_thread::sleep_for(50ms);
cellGrid.determineNextGeneration();
cellGrid.birthNextGeneration();
}
}
deallocateGrid(originCells);
delete originCells;
return 0;
}
// Allocates memory for Cell Grid
vector<vector<Cell*>*> *allocateGrid() {
vector<vector<Cell*>*> *originCells = new vector<vector<Cell*>*>();
for (int x = 0; x < GRID_WIDTH; x++) {
for (int y = 0; y < GRID_HEIGHT; y++) {
originCells->push_back(new vector<Cell*>());
originCells->at(x)->push_back(new Cell(DEAD));
originCells->at(x)->at(y)->setState(DEAD);
for (int i = 0; i < aliveStartingCells.size(); i++) {
if (x == aliveStartingCells[i][0] && y == aliveStartingCells[i][1]) {
originCells->at(x)->at(y)->setState(ALIVE);
}
}
}
}
return originCells;
}
// Deallocates grid to avoid memory leaks
void deallocateGrid(vector<vector<Cell*>*> *grid) {
for (int x = 0; x < GRID_WIDTH; x++) {
for (int y = 0; y < GRID_HEIGHT; y++) {
delete grid->at(x)->at(y);
}
delete [] grid->at(x);
}
}
// draws cellGrid using SFML
void drawCellGrid(CellGrid &cellGrid, sf::RenderWindow &window) {
float drawLocationX = 0;
float drawLocationY = 0;
for (int x = 0; x < GRID_WIDTH; x++) {
for (int y = 0; y < GRID_HEIGHT; y++) {
sf::RectangleShape drawableCell;
sf::Vector2f drawableCellSize;
drawableCellSize.x = CELL_WIDTH;
drawableCellSize.y = CELL_HEIGHT;
drawableCell.setSize(drawableCellSize);
sf::Vector2f drawableCellPosition;
drawableCellPosition.x = drawLocationX;
drawableCellPosition.y = drawLocationY;
drawableCell.setPosition(drawableCellPosition);
if (*cellGrid.getCellGrid()[x][y]->getState() == ALIVE) {
drawableCell.setFillColor(sf::Color::White);
} else {
drawableCell.setFillColor(sf::Color::Black);
}
window.draw(drawableCell);
drawLocationY += CELL_HEIGHT;
}
drawLocationX += CELL_WIDTH;
drawLocationY = 0;
}
}
// Draws the control button for simulation
void drawControl(sf::RenderWindow &window, sf::Color color) {
sf::RectangleShape control;
sf::Vector2f controlV;
controlV.x = SCREEN_WIDTH;
controlV.y = CONTROL_BUFFER;
control.setSize(controlV);
sf::Vector2f controlPosition;
controlPosition.x = 0;
controlPosition.y = SCREEN_HEIGHT;
control.setPosition(controlPosition);
control.setFillColor(color);
window.draw(control);
}