/* GameOfLife - Conway's Game of Life in 3D
*
* Copyright (c) 2008 by Sebastian Lohff, seba@seba-geek.de
* http://www.seba-geek.de
*
* This file is part of GameOfLife.
*
* GameOfLife is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* GameOfLife is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GameOfLife. If not, see .
*/
#include "gameoflife.h"
GameOfLife::GameOfLife(int _x, int _y) {
init();
x = _x;
y = _y;
allocate();
}
GameOfLife::GameOfLife(std::string file) {
init();
load(file);
}
GameOfLife::GameOfLife(int _x, int _y, int r_life, int r_dead) {
init();
fillRandom(_x, _y, r_life, r_dead);
}
void GameOfLife::init() {
view3d = true;
feld = 0;
x = y = 0;
generation = 0;
thickness = 0.5f;
cellwidth = 1.0f;
torus = true;
fullcelluse = true;
radius = 0.3f;
height = 1.0f;
parts = 12;
sectobuild = 0.91f;
b_secdone = 0.0f;
secpertick = 1.0f;
t_secdone = 0.0f;
editmode = false;
// Calc cylinder sinvals
sinval = new float[parts];
cosval = new float[parts];
for(int i=0; i> x;
data >> y;
data >> torus;
if(x<=0 || y <=0)
return false;
allocate();
int j=0;
getline(data, tmp); // Remove \n
while(getline(data, tmp) && j=0 && _x=0 && _y0)
setpos.y = ((int)setpos.y+y+1)%y;
else if(up<0)
setpos.y = ((int)setpos.y+y-1)%y;
if(right>0)
setpos.x = ((int)setpos.x+x+1)%x;
else if(right<0)
setpos.x = ((int)setpos.x+x-1)%x;
}
void GameOfLife::toggle() {
if(feld[(int)setpos.x][(int)setpos.y]==born||feld[(int)setpos.x][(int)setpos.y]==alife) {
feld[(int)setpos.x][(int)setpos.y] = dead;
} else {
feld[(int)setpos.x][(int)setpos.y] = alife;
}
}
void GameOfLife::render(float sec) {
if(!feld)
return;
// Cylinder-building-timer
if(b_secdone<=sectobuild) {
b_secdone += sec;
if(b_secdone>=sectobuild) {
lockStates();
}
}
// Tick-timer
t_secdone += sec;
if(t_secdone>=secpertick) {
tick();
t_secdone = 0.0f;
b_secdone = 0.0f;
}
if(view3d) {
renderBrett();
for(int j=0; jw /(float)x;
float hperp = screen->h /(float)y;
float border;
glPushMatrix();
if(wperpsecpertick)
lockStates();
int near;
int x1, y1;
for(int i=0; i=x || b>=y)))
continue;
if(torus) {
x1 = (a+x)%x;
y1 = (b+y)%y;
} else {
x1 = a;
y1 = b;
}
if(feld[x1][y1]==alife || (fullcelluse && (feld[x1][y1]==dies)))
near++;
}
}
if(feld[i][j]==alife) {
if(near!=2 && near!=3)
feld[i][j] = dies;
} else if(feld[i][j]==dead) {
if(near==3)
feld[i][j] = born;
}
}
}
if(sectobuild<0.1f)
lockStates();
generation++;
}
void GameOfLife::clear() {
for(int i=0; i