-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
119 lines (100 loc) · 3.4 KB
/
main.cpp
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
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <ctime>
#include "gol.hpp"
void start(bool& r, clock_t& tt, clock_t& lt)
{
r = true;
lt = tt = clock();
}
int main(int argc, char *argv[])
{
sf::RenderWindow window(sf::VideoMode(1290, 900), "GAME OF LIFE", sf::Style::Titlebar | sf::Style::Close);
window.setPosition(sf::Vector2i(10, 10));
sf::Event event;
window.setFramerateLimit(30);
sf::Color color = sf::Color::Black;
// to measure time
bool running = false;
double time_counter = 0;
double interval = .5;
clock_t this_time;
clock_t last_time;
GameOfLife gol(1290, 900);
if(argc > 1)
if (gol.import_layout(static_cast<std::string>(argv[1])));
start(running, this_time, last_time);
while (window.isOpen())
{
window.clear(color);
while (window.pollEvent(event))
{
// closing window
if (event.type == sf::Event::Closed)
window.close();
else if (event.type == sf::Event::KeyPressed)
{
if (event.key.code == sf::Keyboard::Escape)
window.close();
// single iteration
else if (event.key.code == sf::Keyboard::Space)
gol.evolve();
// continous simulation
else if (event.key.code == sf::Keyboard::Enter)
{
if (!running)
start(running, this_time, last_time);
else
running = false;
}
// speeds up simulation
else if (event.key.code == sf::Keyboard::RBracket)
interval /= 2;
// slows down simulation
else if (event.key.code == sf::Keyboard::LBracket)
interval *= 2;
// resets grid (and simulation)
else if (event.key.code == sf::Keyboard::R)
{
running = false;
gol.clear();
}
// export to file
else if (event.key.code == sf::Keyboard::E)
gol.export_layout();
}
/// start selecting
else if(event.type == sf::Event::MouseButtonPressed)
{
gol.press(event.mouseButton.x, event.mouseButton.y, event.mouseButton.button);
}
// end selecting
else if(event.type == sf::Event::MouseButtonReleased)
{
gol.unpress(event.mouseButton.button);
}
// move mouse while selecting
else if (event.type == sf::Event::MouseMoved)
{
if (gol.pressed())
gol.move_mouse(event.mouseMove.x, event.mouseMove.y);
}
}
// next iteration is called if simulation in on
if (running)
{
this_time = clock();
time_counter += static_cast<double>(this_time - last_time);
last_time = this_time;
if(time_counter > interval * CLOCKS_PER_SEC)
{
time_counter -= interval * CLOCKS_PER_SEC;
if (gol.evolve())
running = false;
}
}
// drawing grid
window.draw(gol);
window.display();
}
}