-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
156 lines (124 loc) · 5.07 KB
/
main.cpp
File metadata and controls
156 lines (124 loc) · 5.07 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
#include <SFML/Graphics.hpp>
#include "lib.h"
#include <iostream>
#include "TextField.h"
#include <string>
#include <fstream>
using namespace sf;
int main()
{
RenderWindow window(VideoMode(WINDOW_GORISONTAL_SIZE, WINDOW_VERTICAL_SIZE), "Game of Life"); //main window
GameField field;
int size_to_draw = 50;
float timer = 0;
float delay = 1;
Clock clock;
sf::Font font;
if (!font.loadFromFile("20339.ttf"))
{
std::cout << "error";
}
TextField textField;
textField.setFont(font); //font
textField.setLength(20);
textField.setPlaceholder("Enter file name");
textField.setPosition(sf::Vector2f(WINDOW_GORISONTAL_SIZE - 260,150)); //координаты для ввода
Button RunButton(WINDOW_GORISONTAL_SIZE - 125, 25, 100, 40, font, "Run/Stop");
Button SaveButton(WINDOW_GORISONTAL_SIZE - 125, 75, 100, 40, font, "Save");
Button LoadButton(WINDOW_GORISONTAL_SIZE - 125, 200, 100, 40, font, "Load");
Button NextButton(WINDOW_GORISONTAL_SIZE - 125, 275, 100, 40, font, "Next Step");
Button SpeedUpButton(WINDOW_GORISONTAL_SIZE - 125, 325, 100, 40, font, "SpeedUp");
Button SpeedDownButton(WINDOW_GORISONTAL_SIZE - 125, 375, 175, 40, font, "SpeedDown");
Button SpeedValue(WINDOW_GORISONTAL_SIZE - 125, 425, 100, 40, font, "");
while (window.isOpen())
{
float time = clock.getElapsedTime().asSeconds();
clock.restart();
timer += time;
//getting mouse position
Vector2i pos = Mouse::getPosition(window);
int x = pos.x / (CELL_SIZE + CELL_BORDER_SIZE);
int y = pos.y / (CELL_SIZE + CELL_BORDER_SIZE);
int Cell_On_Mouse = x * GAME_SIZE + y;
Event event;
while (window.pollEvent(event))
{
HandleClose(window, event);
field.ArrangeField(event, Cell_On_Mouse);
RunButton.HandlePressed(event, pos.x, pos.y);
SaveButton.HandlePressed(event, pos.x, pos.y);
LoadButton.HandlePressed(event, pos.x, pos.y);
//NextButton.HandlePressed(event, pos.x, pos.y);
textField.input(event);
if (NextButton.HandlePressed(event, pos.x, pos.y)) field.iterate();
if (SpeedUpButton.HandlePressed(event, pos.x, pos.y))
{
delay = delay - 0.1;
}
if (SpeedDownButton.HandlePressed(event, pos.x, pos.y))
{
delay = delay + 0.1;
}
}
if (SaveButton.get_pressedStatus())
{
string path;
string one = "1";
string zero = "0";
path = textField.getText();
std::ofstream myfile;
myfile.open(path.c_str());
vector<Cell> Cells = field.get_arrCells();
for (int i = 0; i < GAME_SIZE; i++)
{
for (int j = 0; j < GAME_SIZE; j++)
{
int number = i * GAME_SIZE + j;
if (Cells[number].get_state() == alive)
{
myfile << one << endl;
} else {
myfile << zero << endl;
}
}
}
//SaveButton.setStatus();
myfile.close();
}
if (LoadButton.get_pressedStatus())
{
string path;
path = textField.getText();
std::ifstream myfile;
myfile.open(path.c_str());
//vector<Cell> Cells = field.get_arrCells();
field.Load(myfile);
//LoadButton.setStatus(false);
myfile.close();
}
window.clear(sf::Color::White); //setting the color of main window
//std::string value = std::to_string(delay);
if ((timer > delay) && RunButton.get_pressedStatus())
{
field.iterate(); //iterate if the time for iteration has come && RunButton is in "run state"
timer = 0;
}
field.draw(window, size_to_draw);//drawing the Game in the window
window.draw(RunButton.get_picture()); //drawing the RunButton
window.draw(RunButton.get_label());
window.draw(SaveButton.get_picture()); //drawing the RunButton
window.draw(SaveButton.get_label());
window.draw(LoadButton.get_picture()); //drawing the RunButton
window.draw(LoadButton.get_label());
window.draw(NextButton.get_picture());
window.draw(NextButton.get_label());
window.draw(SpeedUpButton.get_picture());
window.draw(SpeedUpButton.get_label());
window.draw(SpeedDownButton.get_picture());
window.draw(SpeedDownButton.get_label());
textField.render(window);
//speedField.render(window);
window.display();
}
return 0;
}