-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.c
290 lines (244 loc) · 9.14 KB
/
function.c
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include "glodal_types.h"
#include "list.h"
#include "function.h"
//--------------------------------------------------------------------------------
void change_generation (punit Squeares, int *total_amount, GLfloat *Live_cell, GLfloat *Dead_cell)
{
for (int i = 0; i< *total_amount; i++){
switch (Squeares[i].nextstate) {
case Newborn:{
Squeares[i].realstate = Live;
Squeares[i].color [0] = Live_cell [0];
Squeares[i].color [1] = Live_cell [1];
Squeares[i].color [2] = Live_cell [2];
break;
}
case Will_die:{
Squeares[i].realstate = Dead;
Squeares[i].color [0] = Dead_cell [0];
Squeares[i].color [1] = Dead_cell [1];
Squeares[i].color [2] = Dead_cell [2];
break;
}
default:
break;
}
Squeares[i].nextstate = Empty;
}
}
void SelectBut ( Button *Item, float r, float g, float b)
{
Item->color [0] = r;
Item->color [1] = g;
Item->color [2] = b;
}
//--------------------------------------------------------------------------------
char count_pattern (punit Squeares, int i, int j, int column, int rovs)
{
unit cell = {0};
int count = 0;
int index = 0;
//-----------------upper Cell------------------------
if (i>0){
if (j>0){
index = (i*column+j)-column - 1;
cell.realstate = Squeares[index].realstate;
if (cell.realstate == Live)
count++;
}
index = (i*column+j)-column;
cell.realstate = Squeares[index].realstate;
if (cell.realstate == Live)
count++;
if (j<column-1){
index = (i*column+j)-column + 1;
cell.realstate = Squeares[index].realstate;
if (cell.realstate == Live)
count++;
}
}
//-----------------on same line---------------------------
if (j!=0){
index = (i * column + j) - 1;
cell.realstate = Squeares[index].realstate;
if (cell.realstate == Live)
count++;
}
index = (i * column + j) + 1;
cell.realstate =Squeares[index].realstate;
if (cell.realstate == Live){
count++;
}
//----------------under Cell-------------------------------
if (i < rovs-1){
if (j!=0){
index = (i * column+j)+ column - 1;
cell.realstate =Squeares[index].realstate;
if (cell.realstate == Live)
count++;
}
index = (i * column + j)+ column;
cell.realstate =Squeares[index].realstate;
if (cell.realstate == Live)
count++;
if (j<column-1){
index = (i * column + j)+ column + 1;
cell.realstate =Squeares[index].realstate;
if (cell.realstate == Live)
count++;
}
}
return count;
}
//--------------------------------------------------------------------------------
void draw_unit (int i, punit Item)
{
glColor3fv ( Item[i].color);
glRectf (Item[i].x1,
Item[i].y1,
Item[i].x2,
Item[i].y2
);
}
//-----------------------------------------------------------------------------
void draw_option_list (sList* List)
{
sSaveGame *pt = List->pHead;
while (pt != NULL){
glColor3fv ( pt->tack.color);
glRectf (pt->tack.x1,
pt->tack.y1,
pt->tack.x2,
pt->tack.y2
);
DrawOptLisText(pt);
pt = pt->next;
}
}
//-----------------------------------------------------------------------------
void draw_option_menu ( pButton Item, int amount)
{
for (int i =0; i < amount-1; ++i){
glColor3fv ( Item[i].color);
glRectf (Item[i].x1,
Item[i].y1,
Item[i].x2,
Item[i].y2
);
}
}
//-----------------------------------------------------------------------------
void draw_text_menu (pButton Menu_buttons, char** Menu_name, int count)
{
glColor3fv(Menu_buttons->text_color);
for (int i =0; i<count; ++i){
glRasterPos2f (Menu_buttons[i].x_text, Menu_buttons[i].y_text);
int j=0;
while (Menu_name [i][j] != '\0') {
glutBitmapCharacter (GLUT_BITMAP_TIMES_ROMAN_24, Menu_name [i][j]);
j++;
}
}
}
//-----------------------------------------------------------------------------
void DrawOptLisText (sSaveGame *pt)
{
glColor3fv(pt->tack.text_color);
glRasterPos2f (pt->tack.x_text, pt->tack.y_text);
for (int i=0; i< Size; ++i){
glutBitmapCharacter (GLUT_BITMAP_HELVETICA_18, pt->name[i]);
}
}
//-----------------------------------------------------------------------------
void choose_option (pButton Options_menu, eMenuOptionState *gameOption, double x, double y)
{
if (Options_menu[Pattern].x1 <= x && x <= Options_menu[Pattern].x2)
if (Options_menu[Pattern].y1 <= y && y <= Options_menu[Pattern].y2){
*gameOption = Pattern;
return;
}
if (Options_menu[Export_menu].x1 <= x && x <= Options_menu[Export_menu].x2)
if (Options_menu[Export_menu].y1 <= y && y <= Options_menu[Export_menu].y2){
*gameOption = Export_menu;
return;
}
if (Options_menu[Save].x1 <= x && x <= Options_menu[Save].x2)
if (Options_menu[Save].y1 <= y && y <= Options_menu[Save].y2){
*gameOption = Save;
return;
}
if (Options_menu[About].x1 <= x && x <= Options_menu[About].x2)
if (Options_menu[About].y1 <= y && y <= Options_menu[About].y2){
*gameOption = About;
return;
}
if (Options_menu[Return].x1 <= x && x <= Options_menu[Return].x2)
if (Options_menu[Return].y1 <= y && y <= Options_menu[Return].y2){
*gameOption = Return;
return;
}
}
//------------------------------------------------------------------------
void define_time (char* buffer)
{
time_t rawtime;
struct tm * timeinfo;
time( &rawtime );
timeinfo = localtime( &rawtime );
strftime (buffer,Size,"%X_%x",timeinfo);
}
//---------------------------------------------------------------------
void Print_About (GLuint Height, GLuint Width)
{
sSaveGame new_win;
int i = 0;
int j = 0;
char * Text[] = {
" ",
"The Game of Life is a cellular-automaton, developed by John Conway in 1970.",
"The game is played on an grid of square cells, and its evolution is only determined by its initial state.",
"The rules of the game are simple, and describe the evolution of the grid:",
" Birth: a cell that is dead at time t will be alive at time t + 1 if exactly 3 of its eight neighbors were alive at time t.",
" Death: a cell can die by:",
" Overcrowding: if a cell is alive at time t + 1 and 4 or more of its neighbors",
"are also alive at time t, the cell will be dead at time t + 1",
" Exposure: If a live cell at time t has only 1 live neighbor or no live neighbors, it will be dead at time t + 1.",
" Survival: a cell survives from time t to time t + 1 if and only if 2 or 3 of its neighbors are alive at time t."
" ",
" ",
" MENU OPTION",
" ",
" Run - option starts counting of the next cell stages according to the rules.",
" Step - counts only one next life stage of the cells",
" Stop - stops processing of life – death change.",
" Clear - brings all cells into the initial stage.",
" Exit - ends the program (also keyboard Esc ).",
" Menu - opens additional services.",
" Save - allows to save the state of all cells, and add it to the overall list in Pattern. The name of the saved stage will consist of ",
"current data time. To delete the record you need to choose appropriate game stage and press Delete on your keyboard",
" Export - allows to save a current stage of the field in CSV format, all live cells in the moment will be marked as asterisks.",
" Return to game - back to the main window."
};
new_win.tack.x1= Width - (Width - menuline);
new_win.tack.y1= 10;
new_win.tack.x2= Width - menuline;
new_win.tack.y2 = Height - (menuline*2);
new_win.tack.x_text = new_win.tack.x1;
new_win.tack.y_text = new_win.tack.y1;
new_win.tack.color[0] = 1.0;
new_win.tack.color[1] = 1.0;
new_win.tack.color[2] = 1.0;
draw_option_menu ( &new_win.tack, 2);
glColor3f(0.3, 0.5, 0.3);
while (i != 23){
new_win.tack.y_text += MinButHeight;
glRasterPos2f (new_win.tack.x_text,
new_win.tack.y_text);
while (Text[i][j] != '\0'){
glutBitmapCharacter (GLUT_BITMAP_HELVETICA_18, Text[i][j]);
++j;
}
j=0;
++i;
}
}