Browse Source

Initial commit

seba 10 years ago
commit
e22b4910d5
11 changed files with 1136 additions and 0 deletions
  1. 2
    0
      .gitignore
  2. 21
    0
      Makefile
  3. BIN
      defaultfont.png
  4. 14
    0
      example.fld
  5. 571
    0
      gameoflife.cpp
  6. 86
    0
      gameoflife.h
  7. BIN
      libsegl.so.1
  8. 386
    0
      life.cpp
  9. 27
    0
      readme.txt
  10. 22
    0
      start.fld
  11. 7
    0
      waves.fld

+ 2
- 0
.gitignore View File

@@ -0,0 +1,2 @@
1
+GameOfLife
2
+*.o

+ 21
- 0
Makefile View File

@@ -0,0 +1,21 @@
1
+CC = g++
2
+LINK = `sdl-config --libs` -lGL -lGLU -lSDL_image -Wl,-rpath,.
3
+OBJ = `sdl-config --cflags` -Wall -c -I../libsegl
4
+LIBS = libsegl.so.1
5
+
6
+prog: life.o gameoflife.o
7
+	$(CC) $(LINK) $+ -o GameOfLife $(LIBS)
8
+
9
+run: prog
10
+	./GameOfLife
11
+
12
+life.o: gameoflife.o
13
+
14
+%.o: %.cpp %.h
15
+	$(CC)  $(OBJ) $<
16
+
17
+%.o: %.cpp
18
+	$(CC) $(OBJ) $<
19
+
20
+clean:
21
+	rm -f *.o

BIN
defaultfont.png View File


+ 14
- 0
example.fld View File

@@ -0,0 +1,14 @@
1
+13 13 1
2
+0000000000000
3
+0000000000000
4
+0000000000000
5
+0000011100000
6
+0000010100000
7
+0000010100000
8
+0000000000000
9
+0000010100000
10
+0000010100000
11
+0000011100000
12
+0000000000000
13
+0000000000000
14
+0000000000000

+ 571
- 0
gameoflife.cpp View File

@@ -0,0 +1,571 @@
1
+#include "gameoflife.h"
2
+
3
+GameOfLife::GameOfLife(int _x, int _y) {
4
+	init();
5
+	x = _x;
6
+	y = _y;
7
+	allocate();
8
+}
9
+
10
+GameOfLife::GameOfLife(std::string file) {
11
+	init();
12
+	load(file);
13
+}
14
+
15
+GameOfLife::GameOfLife(int _x, int _y, int r_life, int r_dead) {
16
+	init();
17
+	fillRandom(_x, _y, r_life, r_dead);
18
+}
19
+
20
+void GameOfLife::init() {
21
+	view3d = true;
22
+	feld = 0;
23
+	x = y = 0;
24
+	generation = 0;
25
+	thickness = 0.5f;
26
+	cellwidth = 1.0f;	
27
+	torus = true;
28
+	fullcelluse = true;
29
+	radius = 0.3f;
30
+	height = 1.0f;
31
+	parts = 12;
32
+	sectobuild = 0.91f;
33
+	b_secdone = 0.0f;
34
+	secpertick = 1.0f;
35
+	t_secdone = 0.0f;
36
+	editmode = false;
37
+	
38
+	// Calc cylinder sinvals
39
+	sinval = new float[parts];
40
+	cosval = new float[parts];
41
+	
42
+	for(int i=0; i<parts; i++) {
43
+		sinval[i] = radius * sin(segl::deg2rad(360.0f/parts*i));
44
+		cosval[i] = radius * cos(segl::deg2rad(360.0f/parts*i));
45
+	}
46
+	
47
+	quad = gluNewQuadric();
48
+}
49
+
50
+void GameOfLife::allocate() {
51
+	feld = new State*[x];
52
+	for(int i=0; i<x; i++) {
53
+		feld[i] = new State[y];
54
+		
55
+		// clean it
56
+		for(int j=0; j<y; j++)
57
+			feld[i][j] = dead;
58
+	}
59
+}
60
+
61
+void GameOfLife::cleanup() {
62
+	if(feld) {
63
+		for(int i=0; i<x; i++) {
64
+			delete[](feld[i]);
65
+		}
66
+		delete(feld);
67
+		feld = 0;
68
+	}
69
+}
70
+		
71
+void GameOfLife::renderBrett() {
72
+	float x1 = cellwidth*x / 2.0f;
73
+	float y1 = thickness   / 2.0f;
74
+	float z1 = cellwidth*y / 2.0f;
75
+	
76
+	// Brettquader
77
+	GLfloat mat_diffuse[] = { 1.0f, 0.0f, 0.0f, 1.0f };
78
+	glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
79
+	glColor3f(1.0f, 0.0f, 0.0f);
80
+	glBegin(GL_QUADS);
81
+		// Platte
82
+		glNormal3f(0.0f, 1.0f, 0.0f);
83
+		glVertex3f(-x1,  y1, -z1);
84
+		glVertex3f( x1,  y1, -z1);
85
+		glVertex3f( x1,  y1,  z1);
86
+		glVertex3f(-x1,  y1,  z1);
87
+		
88
+		// Boden
89
+		glNormal3f(0.0f,-1.0f, 0.0f);
90
+		glVertex3f(-x1, -y1, -z1);
91
+		glVertex3f( x1, -y1, -z1);
92
+		glVertex3f( x1, -y1,  z1);
93
+		glVertex3f(-x1, -y1,  z1);
94
+		
95
+		// Seiten
96
+		glNormal3f(0.0f, 0.0f, 1.0f);
97
+		glVertex3f(-x1, -y1, z1);
98
+		glVertex3f( x1, -y1, z1);
99
+		glVertex3f( x1,  y1, z1);
100
+		glVertex3f(-x1,  y1, z1);
101
+		
102
+		glNormal3f(0.0f, 0.0f,-1.0f);
103
+		glVertex3f(-x1, -y1,-z1);
104
+		glVertex3f( x1, -y1,-z1);
105
+		glVertex3f( x1,  y1,-z1);
106
+		glVertex3f(-x1,  y1,-z1);
107
+		
108
+		glNormal3f(-1.0f, 0.0f, 0.0f);
109
+		glVertex3f(-x1, -y1, z1);
110
+		glVertex3f(-x1,  y1, z1);
111
+		glVertex3f(-x1,  y1,-z1);
112
+		glVertex3f(-x1, -y1,-z1);
113
+		
114
+		glNormal3f(1.0f, 0.0f, 0.0f);
115
+		glVertex3f( x1, -y1, z1);
116
+		glVertex3f( x1,  y1, z1);
117
+		glVertex3f( x1,  y1,-z1);
118
+		glVertex3f( x1, -y1,-z1);
119
+	glEnd();
120
+	
121
+	//Gitter
122
+	glPushMatrix();
123
+	
124
+	
125
+	mat_diffuse[0] = 0.0f;
126
+	mat_diffuse[1] = 1.0f;
127
+	mat_diffuse[2] = 0.0f;
128
+	glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
129
+		
130
+	glColor3f(0.0f, 1.0f, 0.0f);
131
+	for(int a=0; a<1; a++) {
132
+		glTranslatef(0.0f, 0.01f, 0.0f);
133
+		glBegin(GL_LINES);
134
+			glNormal3f(0.0f, 1.0f, 0.0f);
135
+			for(float i=-x1; i<=x1; i+=cellwidth) {
136
+				glVertex3f(i, y1, -z1);
137
+				glVertex3f(i, y1,  z1);
138
+			}
139
+		
140
+			for(float i=-z1; i<=z1; i+=cellwidth) {
141
+				glVertex3f(-x1, y1, i);
142
+				glVertex3f( x1, y1, i);			
143
+			}
144
+		glEnd();
145
+	}	
146
+	glPopMatrix();
147
+}
148
+
149
+void GameOfLife::renderStein(State s) {
150
+	float sin1, sin2 = sinval[0], cos1, cos2 = cosval[0];
151
+	
152
+	float height2 = 0.0f;
153
+	switch(s) {
154
+		case alife:
155
+			height2 = height;
156
+		break;
157
+		case born:
158
+			height2 = height * (b_secdone/sectobuild);
159
+		break;
160
+		case dies:
161
+			height2 = height * ((sectobuild-b_secdone)/sectobuild);
162
+		break;
163
+		case dead:
164
+			// unlikely to happen...
165
+			height2 = 0.0f;
166
+		break;
167
+	}
168
+	if(height2<0.001f)
169
+		return;
170
+	
171
+	glColor3f(0.2f, 0.2, 0.9f);
172
+	
173
+// 	for(float i=0, step=360.0f/parts; i<360.0f; i+= step) {
174
+	for(int i=0; i<parts; i++) {
175
+		sin1 = sin2;
176
+		cos1 = cos2;
177
+		sin2 = sinval[(i+1)%parts];
178
+		cos2 = cosval[(i+1)%parts];
179
+// 		sin2 = radius * sin(deg2rad(i+step));
180
+// 		cos2 = radius * cos(deg2rad(i+step));
181
+		GLfloat mat_diffuse[] = { 0.2f, 0.2, 0.9f, 1.0f };
182
+		glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
183
+		
184
+		/*
185
+		glPushMatrix();
186
+			glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
187
+			gluCylinder(quad, radius, radius, height2, 12, 1);
188
+			glTranslatef(0.0f, 0.0f, height2);
189
+			gluDisk(quad, 0.0f, radius, 12, 1);
190
+		glPopMatrix(); 
191
+		*/
192
+		
193
+		glBegin(GL_QUADS);
194
+			glNormal3f(0.0f, 0.0f,-1.0f);
195
+			glVertex3f(-radius, 0.0f, 	-radius);
196
+			glVertex3f(-radius, height2,-radius);
197
+			glVertex3f( radius, height2,-radius);
198
+			glVertex3f( radius, 0.0f, 	-radius);
199
+			
200
+			glNormal3f(0.0f, 0.0f, 1.0f);
201
+			glVertex3f( radius, 0.0f, 	 radius);
202
+			glVertex3f( radius, height2, radius);
203
+			glVertex3f(-radius, height2, radius);
204
+			glVertex3f(-radius, 0.0f, 	 radius);
205
+			
206
+			glNormal3f(-1.0f, 0.0f, 0.0f);
207
+			glVertex3f(-radius, 0.0f,	-radius);
208
+			glVertex3f(-radius, 0.0f,	 radius);
209
+			glVertex3f(-radius, height2, radius);
210
+			glVertex3f(-radius, height2,-radius);
211
+			
212
+			glNormal3f( 1.0f, 0.0f, 0.0f);
213
+			glVertex3f( radius, 0.0f,	 radius);
214
+			glVertex3f( radius, 0.0f,	-radius);
215
+			glVertex3f( radius, height2,-radius);
216
+			glVertex3f( radius, height2, radius);
217
+			
218
+			glNormal3f(0.0f, 1.0f, 0.0f);
219
+			glVertex3f(-radius, height2, radius);
220
+			glVertex3f( radius, height2, radius);
221
+			glVertex3f( radius, height2,-radius);
222
+			glVertex3f(-radius, height2,-radius);
223
+			
224
+			
225
+		glEnd();
226
+		/*
227
+		glBegin(GL_TRIANGLES);
228
+			// Boden Unten
229
+			glNormal3f(0.0f,-1.0f, 0.0f);
230
+			glVertex3f(0.0f, 0.0f, 0.0f);
231
+			glVertex3f(sin1, 0.0f, cos1);
232
+			glVertex3f(sin2, 0.0f, cos2);
233
+			
234
+			// Boden Oben
235
+			glNormal3f(0.0f, 1.0f, 0.0f);
236
+			glVertex3f(0.0f, height2, 0.0f);
237
+			glVertex3f(sin1, height2, cos1);
238
+			glVertex3f(sin2, height2, cos2);
239
+
240
+			//Seite
241
+			glNormal3f((sin1+sin2)/2.0f, 0.0f, (cos1+cos2)/2.0f);
242
+			glVertex3f(sin1, 0.0f, cos1);
243
+			glVertex3f(sin2, 0.0f, cos2);
244
+			glVertex3f(sin2, height2, cos2);
245
+			
246
+			glNormal3f((sin1+sin2)/2.0f, 0.0f, (cos1+cos2)/2.0f);
247
+			glVertex3f(sin2, height2, cos2);
248
+			glVertex3f(sin1, height2, cos1);
249
+			glVertex3f(sin1, 0.0f,   cos1);
250
+		glEnd();
251
+		*/
252
+	}
253
+}
254
+
255
+void GameOfLife::translateTo(int _x, int _y) {
256
+	glTranslatef(-cellwidth/2.0f*x+cellwidth*_x+cellwidth/2.0f, thickness/2.0f, -cellwidth/2.0f*y+cellwidth*_y+cellwidth/2.0f);
257
+}
258
+
259
+void GameOfLife::lockStates() {
260
+	for(int i=0; i<x; i++) {
261
+		for(int j=0; j<y; j++) {
262
+			if(feld[i][j]==dies)
263
+				feld[i][j] = dead;
264
+			else if(feld[i][j]==born)
265
+				feld[i][j] = alife;
266
+		}
267
+	}
268
+}
269
+
270
+void GameOfLife::set3D(bool on) {
271
+	view3d = on;
272
+}
273
+
274
+bool GameOfLife::load(std::string file) {
275
+	std::ifstream data(file.c_str());
276
+	if(!data)
277
+		return false;
278
+	
279
+	std::string tmp;
280
+	data >> x;
281
+	data >> y;
282
+	data >> torus;
283
+	if(x<=0 || y <=0)
284
+		return false;
285
+	
286
+	cleanup();
287
+	allocate();
288
+	int j=0;
289
+	getline(data, tmp); // Remove \n
290
+	while(getline(data, tmp) && j<y) {
291
+		for(int i=0; i<x && i<(int)tmp.length(); i++) {
292
+			feld[i][j] = (tmp[i]=='1') ? born : dead;
293
+		}
294
+		j++;
295
+	}
296
+	
297
+	return true;
298
+}
299
+
300
+bool GameOfLife::save(std::string file) {
301
+	std::ofstream data(file.c_str());
302
+	if(!data)
303
+		return false;
304
+	
305
+	data << x << " " << y << " " << (torus?1:0) << std::endl;
306
+	for(int j=0; j<y; j++) {
307
+		for(int i=0; i<x; i++) {
308
+			data << ((feld[i][j]==alife||feld[i][j]==born) ? '1' : '0');
309
+		}
310
+		data << std::endl;
311
+	}
312
+	return true;
313
+}
314
+
315
+void GameOfLife::fillRandom(int _x, int _y, int r_life, int r_dead) {
316
+	cleanup();
317
+	
318
+	x = _x;
319
+	y = _y;
320
+	allocate();
321
+	
322
+	srand(time(0));
323
+	if(r_life+r_dead==0) {
324
+		r_life = rand()%100 + 1;
325
+		r_dead = rand()%100 + 1;
326
+	}
327
+	
328
+	float ratio = r_life/(float)(r_life+r_dead);
329
+	
330
+	for(int i=0; i<x; i++)  {
331
+		for(int j=0; j<y; j++) {
332
+			if((rand()%100)/100.0f < ratio) {
333
+				feld[i][j] = born;
334
+			} else {
335
+				// should be set to dead, but to be sure...
336
+				feld[i][j] = dead;
337
+			}
338
+		}
339
+	}
340
+}
341
+
342
+// void GameOfLife::toggle(int _x, int _y, State stat) {
343
+// 	if(feld && _x>=0 && _x<x && _y>=0 && _y<y) {
344
+// 		feld[_x][_y] = stat;
345
+// 	}
346
+// }
347
+
348
+// const bool** GameOfLife::getFeld() {
349
+// 	return (const bool**)feld;	
350
+// }
351
+
352
+void GameOfLife::setEditMode(bool e) {
353
+	editmode = e;
354
+	if(e) {
355
+		lockStates();
356
+	} else {
357
+		
358
+	}
359
+}
360
+
361
+void GameOfLife::move(int up, int right) {
362
+	if(up>0)
363
+		setpos.y = ((int)setpos.y+y+1)%y;
364
+	else if(up<0)
365
+		setpos.y = ((int)setpos.y+y-1)%y;	
366
+	if(right>0)
367
+		setpos.x = ((int)setpos.x+x+1)%x;
368
+	else if(right<0)
369
+		setpos.x = ((int)setpos.x+x-1)%x;	
370
+	
371
+}
372
+
373
+void GameOfLife::toggle() {
374
+	if(feld[(int)setpos.x][(int)setpos.y]==born||feld[(int)setpos.x][(int)setpos.y]==alife) {
375
+		feld[(int)setpos.x][(int)setpos.y] = dead;
376
+	} else {
377
+		feld[(int)setpos.x][(int)setpos.y] = alife;
378
+	}
379
+}
380
+
381
+void GameOfLife::render(float sec) {
382
+	if(!feld)
383
+		return;
384
+	
385
+	// Cylinder-building-timer
386
+	if(b_secdone<=sectobuild) {
387
+		b_secdone += sec;
388
+		if(b_secdone>=sectobuild) {
389
+			lockStates();
390
+		}
391
+	}
392
+	
393
+	// Tick-timer
394
+	t_secdone += sec;
395
+	if(t_secdone>=secpertick) {
396
+		tick();
397
+		t_secdone = 0.0f;
398
+		b_secdone = 0.0f;
399
+	}
400
+	
401
+	if(view3d) {
402
+		renderBrett();
403
+		
404
+		for(int j=0; j<y; j++) {
405
+			for(int i=0; i<x; i++) {
406
+				if(feld[i][j]!=dead) {
407
+					glPushMatrix();
408
+					translateTo(i, j);
409
+					renderStein(feld[i][j]);
410
+					glPopMatrix();
411
+				}
412
+				if(editmode) {
413
+					if(i==setpos.x && j==setpos.y) {
414
+						glPushMatrix();
415
+						float mat_diffuse[3] = { 0.0f, 1.0f, 0.0f };
416
+						glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
417
+						translateTo(i, j);
418
+						glTranslatef(0.0f, 0.01f, 0.0f);
419
+						glBegin(GL_QUADS);
420
+							glColor3f(0.0f, 1.0f, 0.0f);
421
+							glNormal3f(0.0f, 1.0f, 0.0f);
422
+							glVertex3f(-cellwidth/2.0f, 0.0f, cellwidth/2.0f);
423
+							glVertex3f( cellwidth/2.0f, 0.0f, cellwidth/2.0f);
424
+							glVertex3f( cellwidth/2.0f, 0.0f,-cellwidth/2.0f);
425
+							glVertex3f(-cellwidth/2.0f, 0.0f,-cellwidth/2.0f);
426
+						glEnd();
427
+						glPopMatrix();
428
+					}
429
+				}
430
+			}
431
+		}
432
+	} else {
433
+		SDL_Surface *screen = SDL_GetVideoSurface();
434
+		float wperp = screen->w /(float)x;
435
+		float hperp = screen->h /(float)y;
436
+		float border;
437
+		
438
+		glPushMatrix();
439
+		if(wperp<hperp) {
440
+			glTranslatef(0.0f, (hperp-wperp)*y/2.0f, 0.0f);
441
+			hperp = wperp;
442
+		} else {
443
+			glTranslatef((wperp-hperp)*x/2.0f, 0.0f, 0.0f);
444
+		}
445
+		
446
+		border = 0.0f;
447
+		
448
+		glBegin(GL_QUADS);
449
+		for(int j=0; j<y; j++) {
450
+			for(int i=0; i<x; i++) {
451
+				if(feld[i][j]==alife||feld[i][j]==born) {
452
+					glColor3f(0.0f, 0.0f, 1.0f);
453
+				} else {
454
+					glColor3f(1.0f, 0.0f, 0.0f);
455
+				}
456
+				glVertex2f(hperp*i+border,		hperp*j+border);
457
+				glVertex2f(hperp*i+border,		hperp*(j+1)-border);
458
+				glVertex2f(hperp*(i+1)-border,	hperp*(j+1)-border);
459
+				glVertex2f(hperp*(i+1)-border,	hperp*j+border);
460
+			}
461
+		}
462
+		glEnd();
463
+		
464
+		if(editmode) {
465
+			glBegin(GL_QUADS);
466
+				glColor3f(0.0f, 1.0f, 0.0f);
467
+				glVertex2f(hperp*setpos.x+border,		hperp*setpos.y+border);
468
+				glVertex2f(hperp*setpos.x+border,		hperp*(setpos.y+1)-border);
469
+				glVertex2f(hperp*(setpos.x+1)-border,	hperp*(setpos.y+1)-border);
470
+				glVertex2f(hperp*(setpos.x+1)-border,	hperp*setpos.y+border);
471
+			glEnd();
472
+			
473
+		}
474
+		
475
+		
476
+// 		glTranslatef(0.0f, 0.0f, 0.1f);
477
+		glColor3f(0.0f, 0.0f, 0.0f);
478
+		glBegin(GL_LINES);
479
+			for(float i=0; i<x; i+=1.0f) {
480
+				glVertex2f(i*hperp, 0);
481
+				glVertex2f(i*hperp, y*hperp);
482
+			}
483
+		
484
+			for(float i=0; i<y; i+=1.0f) {
485
+				glVertex2f(0,		i*hperp);
486
+				glVertex2f(x*hperp, i*hperp);			}
487
+		
488
+		glEnd();
489
+		glPopMatrix();
490
+	}
491
+}
492
+
493
+
494
+float GameOfLife::getTickSec() {
495
+	return secpertick;
496
+}
497
+
498
+float GameOfLife::getBuildSec() {
499
+	return sectobuild;
500
+}
501
+
502
+void GameOfLife::setTickSec(float s) {
503
+	std::cout << "Set to " << s << std::endl;
504
+	secpertick = s;
505
+}
506
+
507
+void GameOfLife::setBuildSec(float s) {
508
+	sectobuild = s;
509
+}
510
+
511
+void GameOfLife::setTorus(bool t) {
512
+	torus = t;
513
+}
514
+
515
+unsigned long GameOfLife::getGeneration() {
516
+	return generation;
517
+}
518
+
519
+void GameOfLife::tick() {
520
+	if(sectobuild>secpertick)
521
+		lockStates();
522
+	
523
+	int near;
524
+	int x1, y1;
525
+	for(int i=0; i<x; i++) {
526
+		for(int j=0; j<y; j++) {
527
+			near = 0;
528
+			for(int a=i-1; a<=(i+1); a++) {
529
+				for(int b=j-1; b<=(j+1); b++) {
530
+					if((a==i && b==j) || (!torus && (a<0 || b<0 || a>=x || b>=y)))
531
+						continue;
532
+					if(torus) {
533
+						x1 = (a+x)%x;
534
+						y1 = (b+y)%y;
535
+					} else {
536
+						x1 = a;
537
+						y1 = b;
538
+					}
539
+					
540
+					if(feld[x1][y1]==alife || (fullcelluse && (feld[x1][y1]==dies)))
541
+						near++;
542
+				}
543
+			}
544
+			
545
+			if(feld[i][j]==alife) {
546
+				if(near!=2 && near!=3)
547
+					feld[i][j] = dies;
548
+			} else if(feld[i][j]==dead) {
549
+				if(near==3)
550
+					feld[i][j] = born;
551
+			}
552
+		}
553
+	}
554
+	if(sectobuild<0.1f)
555
+		lockStates();
556
+	generation++;
557
+}
558
+
559
+void GameOfLife::clear() {
560
+	for(int i=0; i<x; i++) {
561
+		for(int j=0; j<y; j++) {
562
+			feld[i][j] = dead;
563
+		}
564
+	}
565
+}
566
+
567
+GameOfLife::~GameOfLife() {
568
+	cleanup();
569
+	delete[](sinval);
570
+	delete[](cosval);
571
+}

+ 86
- 0
gameoflife.h View File

@@ -0,0 +1,86 @@
1
+#ifndef __GAMEOFLIFE_H
2
+#define __GAMEOFLIFE_H
3
+
4
+#include <string>
5
+#include <fstream>
6
+#include <SDL_opengl.h>
7
+#include <cstdlib>
8
+#include <ctime>
9
+#include <SDL.h>
10
+#include "emath.h"
11
+#include "punkt2d.h"
12
+
13
+typedef enum { dead=0, alife, dies, born } State;
14
+
15
+class GameOfLife {
16
+	private:
17
+		State **feld;
18
+		int x, y;
19
+		bool torus;
20
+		bool fullcelluse;
21
+		unsigned long generation;
22
+		
23
+		GLUquadricObj *quad;
24
+		
25
+		// Brett
26
+		float thickness;
27
+		float cellwidth;
28
+		
29
+		// Cylinder
30
+		float radius;
31
+		float height;
32
+		int parts;
33
+		float *sinval;
34
+		float *cosval;
35
+		
36
+		//render
37
+		bool view3d;
38
+		float sectobuild;
39
+		float b_secdone;
40
+		float secpertick;
41
+		float t_secdone;
42
+		
43
+		// Edit
44
+		segl::Punkt2D setpos;
45
+		bool editmode;
46
+		
47
+		void init();
48
+		void allocate();
49
+		void cleanup();
50
+		
51
+		void renderBrett();
52
+		void renderStein(State s);
53
+		
54
+		void translateTo(int _x, int _y);
55
+		void lockStates();
56
+	public:
57
+		GameOfLife(int _x, int _y);
58
+		GameOfLife(int _x, int _y, int r_life, int r_dead);
59
+		GameOfLife(std::string file);
60
+		~GameOfLife();
61
+		
62
+		void set3D(bool on);
63
+		bool load(std::string file);
64
+		bool save(std::string file);
65
+		
66
+		void fillRandom(int _x, int _y, int r_life, int r_dead);
67
+// 		void toggle(int _x, int _y);
68
+		
69
+		void setEditMode(bool e);
70
+		void move(int up, int right);
71
+		void toggle();
72
+		
73
+		float getTickSec();
74
+		float getBuildSec();
75
+		void setTickSec(float s);
76
+		void setBuildSec(float s);
77
+		void setTorus(bool t);
78
+		unsigned long getGeneration();
79
+		
80
+// 		const bool** getFeld();
81
+		void tick();
82
+		void render(float sec);
83
+		void clear();
84
+};
85
+
86
+#endif

BIN
libsegl.so.1 View File


+ 386
- 0
life.cpp View File

@@ -0,0 +1,386 @@
1
+#include <iostream>
2
+#include <sstream>
3
+#include <string>
4
+#include <SDL.h>
5
+#include <SDL_opengl.h>
6
+#include "gameoflife.h"
7
+#include "glcamera.h"
8
+#include "fpsmanager.h"
9
+#include "glfontengine.h"
10
+
11
+void setVideo(bool view3d, int width, int height);
12
+void displayUsage();
13
+
14
+int main(int argc, char **argv) {
15
+	int width = 1024;
16
+	int height= 768;
17
+	bool view3d = true;
18
+	bool renderosd = true;
19
+	bool paused = false;
20
+	bool editmode = false;
21
+// 	bool showcam2d = false;
22
+	GameOfLife feld(20, 20, 1, 2);
23
+	
24
+	{
25
+		std::string loadfile = "";
26
+		int feld_x = 20;
27
+		int feld_y = 20;
28
+		int rand_alife = 1;
29
+		int rand_dead  = 3;
30
+		bool torus = true;
31
+		for(int i=1; i<argc; i++) {
32
+			std::string arg1(argv[i]);
33
+			
34
+			if(arg1=="-h"||arg1=="--help") {
35
+				displayUsage();
36
+				return 0;
37
+			} else if(arg1=="-v"||arg1=="--version") {
38
+				std::cout << "Conway's \"Game of Life\" in 2d/3d v1.0"  << std::endl <<
39
+						"Written by seba (www.seba-geek.de)" << std::endl;
40
+				return 0;
41
+			}
42
+
43
+			//cmds mit parametern
44
+			if(i==argc-1) {
45
+				std::cerr << "Error: Unknown option or no parameter given" << std::endl << "Use --help for help" << std::endl;
46
+				return 0;
47
+			}
48
+			
49
+			if(arg1=="-fx"||arg1=="--field-x") {
50
+				feld_x = atoi(argv[i+1]);
51
+				if(feld_x<=0)
52
+					feld_x = 20;
53
+			} else if(arg1=="-fy"||arg1=="--field-y") {
54
+				feld_y = atoi(argv[i+1]);
55
+				if(feld_y<=0)
56
+					feld_y = 20;				
57
+			} else if(arg1=="-l"||arg1=="--load") {
58
+				loadfile = argv[i+1];
59
+			} else if(arg1=="-rd"||arg1=="--rand-dead") {
60
+				rand_dead = atoi(argv[i+1]);
61
+				if(rand_dead<0)
62
+					rand_dead = 3;
63
+			} else if(arg1=="-ra"||arg1=="--rand-alife") {
64
+				rand_alife = atoi(argv[i+1]);
65
+				if(rand_alife<0)
66
+					rand_alife = 1;	
67
+			} else if(arg1=="-t"||arg1=="--torus") {
68
+				torus = (bool)atoi(argv[i+1]);
69
+			} else {
70
+				std::cerr << "Error: Unknown option '" << arg1 << "'" << std::endl << "Use --help for help" << std::endl;
71
+				return 0;
72
+			}
73
+			i++; // parameter used
74
+		}
75
+		if(loadfile=="") {
76
+			feld.fillRandom(feld_x, feld_y, rand_alife, rand_dead);
77
+			feld.setTorus(torus);
78
+		} else {
79
+			if(!feld.load(loadfile)) {
80
+				std::cerr << "Error while loading \"" << loadfile << "\", no field loaded" << std::endl;
81
+			}
82
+		}
83
+	}
84
+	
85
+	
86
+	
87
+
88
+	
89
+	// SDL Initialisieren
90
+	if(SDL_Init(SDL_INIT_VIDEO)==-1) {
91
+		std::cerr << "SDL konnte nicht initialisiert werden: " << SDL_GetError() << std::endl;
92
+		return 1;
93
+	}
94
+	
95
+	SDL_EnableKeyRepeat(150, 50);
96
+	
97
+	// Videomode setzen
98
+	int videoflags = SDL_OPENGL | SDL_GL_DOUBLEBUFFER | SDL_HWPALETTE;
99
+	
100
+	
101
+// 	if(fullscreen)
102
+// 		videoflags |= SDL_FULLSCREEN;
103
+	
104
+	if(!SDL_SetVideoMode(width, height, 32, videoflags)) {
105
+		std::cerr << "Konnte Videomode nicht setzen: " << SDL_GetError() << std::endl;
106
+		return 1;
107
+	}
108
+	
109
+	// OpenGL
110
+	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
111
+	
112
+	glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
113
+	glClearDepth(1.0f);
114
+	glDepthFunc(GL_LEQUAL);
115
+	glShadeModel(GL_SMOOTH);
116
+	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
117
+	glEnable(GL_DEPTH_TEST);
118
+	
119
+	glViewport(0, 0, (GLsizei)width, (GLsizei)height);	
120
+	
121
+	glMatrixMode(GL_PROJECTION);
122
+	glLoadIdentity();
123
+	gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 0.2f, 100.0f);
124
+	glMatrixMode(GL_MODELVIEW);
125
+	
126
+	// Licht
127
+	
128
+	GLfloat mat_diffuse[] = { 1.0f, 0.0f, 0.2f, 1.0f };
129
+// 	GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
130
+// 	GLfloat mat_shininess[] = { 50.0f };
131
+	glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
132
+// 	glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
133
+// 	glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
134
+	
135
+	GLfloat light_ambient[]  = { 0.0f, 0.0f, 0.0f, 1.0f };
136
+	GLfloat light_diffuse[]  = { 1.0f, 1.0f, 1.0f, 1.0f };
137
+// 	GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
138
+	GLfloat light_position[] = { 100.0f,100.0f, 0.0f };
139
+// 	GLfloat spot_direction[] = { 1.0f, -1.0f, 0.0f };
140
+	
141
+	glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient);
142
+	glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse);
143
+// 	glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, spot_direction);
144
+// 	glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 2.0f);
145
+// 	glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 45.0f);
146
+// 	glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
147
+	glLightfv(GL_LIGHT1, GL_POSITION, light_position);
148
+	
149
+	glEnable(GL_LIGHTING);
150
+	glEnable(GL_LIGHT1);
151
+
152
+	/*
153
+	GLfloat light1_ambient[] = { 0.2, 0.2, 0.2, 1.0 };
154
+	GLfloat light1_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
155
+	GLfloat light1_specular[] = { 1.0, 1.0, 1.0, 1.0 };
156
+	GLfloat light1_position[] = { -2.0, 2.0, 1.0, 1.0 };
157
+	GLfloat spot_direction[] = { -1.0, -1.0, 0.0 };
158
+	glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient);
159
+	glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
160
+	glLightfv(GL_LIGHT1, GL_SPECULAR, light1_specular);
161
+	glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
162
+	glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 1.5);
163
+	glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, 0.5);
164
+	glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0.2);
165
+	glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 45.0);
166
+	glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, spot_direction);
167
+	glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 2.0);
168
+	glEnable(GL_LIGHT1);
169
+
170
+	glEnable(GL_LIGHTING);
171
+	*/
172
+	
173
+	// Font
174
+	
175
+	
176
+	if(!segl::GLFontEngine::addFont("defaultfont.png", "default")) {
177
+		std::cerr << "\"defaultfont.png\" not found! No fonts will be displayed!" << std::endl;
178
+	}
179
+	segl::GLFontEngine fontengine("default");
180
+	fontengine.setSize(21);
181
+	
182
+	// SDL/Spielvariablen
183
+	SDL_Event event;
184
+	bool quit = false;
185
+	segl::FPSManager fpsman(60);
186
+	segl::GLCamera camera;
187
+	camera.setPosDirNorm(segl::Punkt3D(0.0f, 5.0f, 15.0f), segl::Punkt3D(0.0f, 0.0f, -1.0f), segl::Punkt3D(0.0f, 1.0f, 0.0f));
188
+	Uint32 lastframe = SDL_GetTicks(), thisframe = SDL_GetTicks();
189
+	
190
+	while(!quit) {
191
+		if(SDL_PollEvent(&event)) {
192
+			switch(event.type) {
193
+				case SDL_QUIT:
194
+					quit = true;
195
+				break;
196
+				case SDL_KEYDOWN:
197
+					switch(event.key.keysym.sym) {
198
+						case SDLK_t:
199
+							view3d = !view3d;
200
+							setVideo(view3d, width, height);
201
+							feld.set3D(view3d);
202
+							if(view3d)
203
+								glEnable(GL_LIGHTING);
204
+							else
205
+								glDisable(GL_LIGHTING);
206
+						break;
207
+						case SDLK_p:
208
+							paused = !paused;
209
+						break;
210
+						case SDLK_o:
211
+							renderosd = !renderosd;
212
+						break;
213
+						case SDLK_c:
214
+							if(editmode && event.key.keysym.mod & KMOD_SHIFT) {
215
+								feld.clear();
216
+							} else if(!view3d) {
217
+								
218
+							}
219
+						break;
220
+						case SDLK_b:
221
+							if(feld.getBuildSec()==0.0f) {
222
+								if(feld.getTickSec()<0.1f)
223
+									feld.setBuildSec(feld.getTickSec()-0.01f);
224
+								else
225
+									feld.setBuildSec(feld.getTickSec()-0.1f);
226
+							} else {
227
+								feld.setBuildSec(0.0f);
228
+							}
229
+						break;
230
+						case SDLK_ESCAPE:
231
+							quit = true;
232
+						break;
233
+						case SDLK_PLUS:
234
+							if(feld.getTickSec()>=0.1f) {
235
+								feld.setTickSec(feld.getTickSec()+0.1f);
236
+								if(feld.getBuildSec()!=0.0f)
237
+									feld.setBuildSec(feld.getBuildSec()+0.1f);
238
+							} else {
239
+								feld.setTickSec(feld.getTickSec()+0.01f);
240
+								if(feld.getBuildSec()!=0.0f)
241
+									feld.setBuildSec(feld.getBuildSec()+0.01f);								
242
+							}
243
+						break;
244
+						case SDLK_MINUS:
245
+							if(feld.getTickSec()>0.1f) {
246
+								feld.setTickSec(feld.getTickSec()-0.1f);
247
+								if(feld.getBuildSec()!=0.0f)
248
+									feld.setBuildSec(feld.getBuildSec()-0.1f);
249
+							} else if(feld.getTickSec()>=0.01f) {
250
+								feld.setTickSec(feld.getTickSec()-0.01f);
251
+								if(feld.getBuildSec()!=0.0f)
252
+									feld.setBuildSec(feld.getBuildSec()-0.01f);								
253
+							}
254
+						break;
255
+						case SDLK_e:
256
+							editmode = !editmode;
257
+							feld.setEditMode(editmode);
258
+							if(editmode)
259
+								paused = true;
260
+							else
261
+								paused = false;
262
+						break;
263
+						// Edit Mode Stuff
264
+						case SDLK_SPACE:
265
+							if(editmode)
266
+								feld.toggle();
267
+						break;
268
+						case SDLK_UP:
269
+							if(editmode && (!view3d || event.key.keysym.mod&KMOD_SHIFT))
270
+								feld.move(-1, 0);
271
+						break;
272
+						case SDLK_DOWN:
273
+							if(editmode && (!view3d || event.key.keysym.mod&KMOD_SHIFT))
274
+								feld.move(1, 0);
275
+						break;
276
+						case SDLK_RIGHT:
277
+							if(editmode && (!view3d || event.key.keysym.mod&KMOD_SHIFT))
278
+								feld.move(0, 1);
279
+						break;
280
+						case SDLK_LEFT:
281
+							if(editmode && (!view3d || event.key.keysym.mod&KMOD_SHIFT))
282
+								feld.move(0, -1);
283
+						break;
284
+						default:
285
+						
286
+						break;
287
+					}
288
+				break;
289
+				case SDL_VIDEORESIZE:
290
+					
291
+				break;
292
+			}
293
+		} else {
294
+			// Set scene stuff
295
+			lastframe = thisframe;
296
+			thisframe = SDL_GetTicks();
297
+			float sec = (thisframe-lastframe)/1000.0f;
298
+			if(view3d) {
299
+				Uint8 *keystate = SDL_GetKeyState(NULL);
300
+				if(!keystate[SDLK_LSHIFT]&&!keystate[SDLK_RSHIFT])
301
+					camera.handleKeys(sec);
302
+			}
303
+			
304
+			//Render scene
305
+			glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
306
+			glLoadIdentity();
307
+			
308
+			if(view3d)
309
+				camera.setCamera();
310
+			
311
+			glLightfv(GL_LIGHT1, GL_POSITION, light_position);
312
+// 			glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
313
+			
314
+			feld.render(sec*(!paused));
315
+			
316
+
317
+			if(renderosd) {
318
+				if(view3d) {
319
+					glDisable(GL_LIGHTING);
320
+					fontengine.prepare2DbyPushingMatrix();
321
+				}
322
+				
323
+				std::stringstream txt;
324
+				
325
+				glEnable(GL_TEXTURE_2D);
326
+				if(paused)
327
+					fontengine.renderLine("   Paused...", width/2, 0, true);
328
+				if(editmode)
329
+					fontengine.renderLine("Edit mode", width/2, 21, true);
330
+				txt << "Sec/Build: ";
331
+				if(feld.getBuildSec()!=0.0f)
332
+					txt << feld.getBuildSec();
333
+				else
334
+					txt << "Off";
335
+				fontengine.renderLine(txt.str(), width/3, height-21, true);
336
+				txt.str("");
337
+				txt.clear();
338
+				txt << "Sec/Tick: " << feld.getTickSec();
339
+				fontengine.renderLine(txt.str(), width*2/3, height-21, true);
340
+				txt.str("");
341
+				txt << "Generation: " << feld.getGeneration();
342
+				if(!paused)
343
+					fontengine.renderLine(txt.str(), width/2, 0, true);
344
+				glDisable(GL_TEXTURE_2D);
345
+				
346
+				if(view3d) {
347
+					glEnable(GL_LIGHTING);
348
+					fontengine.regain3DbyPoppingMatrix();
349
+				}
350
+			}
351
+			
352
+			SDL_GL_SwapBuffers();
353
+			fpsman.delay();
354
+		}
355
+
356
+	}
357
+	return 0;
358
+}
359
+
360
+void setVideo(bool view3d, int width, int height) {
361
+	glViewport(0, 0, (GLsizei)width, (GLsizei)height);
362
+	
363
+	glMatrixMode(GL_PROJECTION);
364
+	glLoadIdentity();
365
+	if(view3d)
366
+		gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, 0.2f, 100.0f);
367
+	else
368
+		glOrtho(0, width, height, 0, -1, 1);
369
+	glMatrixMode(GL_MODELVIEW);
370
+}
371
+
372
+void displayUsage() {
373
+	std::cout << "Usage: GameOfLife [options]" << std::endl <<
374
+			std::endl <<
375
+			"Options: " << std:: endl <<
376
+			"  -fx, --field-x <w>       Set field width (Default: 20)" << std::endl <<
377
+			"  -fy, --field-y <h>       Set field height (Default: 20)" << std::endl <<
378
+			"  -t, --torus <1/0>        Enable/Disable Torus (Default: 1)" << std::endl <<
379
+			"  -l, --load <file>        Load field from textfile" << std::endl <<
380
+			"  -rd, --rand-dead <int>   Defines dead-cellratio for random fill (Default: 3)" << std::endl <<
381
+			"  -ra, --rand-alife <int>  Defines alife-cellratio for random fill (Default: 1)" << std::endl <<
382
+			"  -h, --help               Shows this help and exits" << std::endl <<
383
+			"  -v, --version            Displays versioninfo and exits" << std::endl <<
384
+			std::endl <<
385
+			"Read the readme.txt supplied with this program for further information" << std::endl;
386
+}

+ 27
- 0
readme.txt View File

@@ -0,0 +1,27 @@
1
+Conway's "Game of Life" in 2d/3d
2
+written by seba (www.seba-geek.de)
3
+
4
+ - Write a field-file
5
+	Begin with a header in the first line
6
+		<x (width)> <y (height)> <torus>
7
+	For example "21 21 1" would be a 21x21 field torus
8
+	
9
+	After the header follows the field-definition,
10
+	col => x-axis, row => y-axis
11
+		0 is a dead cell
12
+		1 is a living cell
13
+	
14
+	Look at the example.fld for further explaination
15
+	
16
+	
17
+ - Controls
18
+	t		Toggle 2d/3d mode
19
+	p		Pause
20
+	o		Toggle OSD
21
+	e		Toggle Edit-Mode
22
+			Move with shift + arrows
23
+			Toggle fieldstate withs Space
24
+			Clear field with shift + c
25
+	b		Toggle buildup of cells
26
+	-/+		In- or decrease speed
27
+	

+ 22
- 0
start.fld View File

@@ -0,0 +1,22 @@
1
+21 21 1
2
+000000000000000000000
3
+001000000000001000000
4
+011100000000011100000
5
+001000000000001000000
6
+000000000000000000000
7
+000000000000000000000
8
+000000000000000000000
9
+000000000111000000000
10
+000000000101000000000
11
+000000000101000000000
12
+000000000000000000000
13
+000000000101000000000
14
+000000000101000000000
15
+000000000111000000000
16
+000000000000000000000
17
+000000000000000000000
18
+001000000000000100000
19
+011100000000001110000
20
+001000000000000100000
21
+000000000000000000000
22
+000000000000000000000

+ 7
- 0
waves.fld View File

@@ -0,0 +1,7 @@
1
+20 6 1
2
+00000000010000000000
3
+00000000010000000000
4
+00000000010000000000
5
+00000000010000000000
6
+00000000010000000000
7
+00000000010000000000

Loading…
Cancel
Save