#include <windows.h>#include <GL/gl.h>#include <GL/glu.h>#include <GL/glut.h>#include <vector>#define White 0,0,0 ///define colors that can be easily passed to drawing function#define Black 255,255,255#define Green 0,255,0bool Setup=true;static void resize(int width, int height) { const float ar = (float) width / (float) height; glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(0, width, height, 0, -1, 2); gluOrtho2D(0, width, height, 0); glMatrixMode(GL_MODELVIEW); glLoadIdentity() ;}struct Cell { bool TBF; bool alive;};void KillCell(std::vector<std::vector<Cell> >& Buffer, short x, short y) { Buffer[x][y].alive = false;//it's dead}void BirthCell(std::vector<std::vector<Cell> >& Buffer, short x, short y) { Buffer[x][y].alive = true;//it's alive}void boxColor(short x,short y,short w,short h,unsigned char r,unsigned char g,unsigned char b) { glPushMatrix(); glColor3b(r,g,b); glBegin(GL_QUADS); glVertex2i(x,y); glVertex2i(x,y+h); glVertex2i(x+w,y+h); glVertex2i(x+w,y); glEnd(); glPopMatrix();}void lineColor(short x1,short y1,short x2,short y2,unsigned char r,unsigned char g,unsigned char b) { glPushMatrix(); glColor3b(r,g,b); glBegin(GL_LINES); glVertex2i(x1,y1); glVertex2i(x2,y2); glEnd(); glPopMatrix();}void RenderCells(std::vector<std::vector<Cell> >& Buffer) { short CW=800,CH=600; short sizex = Buffer.size(); short sizey = Buffer[0].size(); for (short i=0;i<sizex;++i) { for (short j=0;j<sizey;++j) { if (Buffer[j].alive==true) { boxColor(i*CW,j*CH,i*CW+CW,j*CH+CH,Black); } else boxColor(i*CW,j*CH,i*CW+CW,j*CH+CH,White); } } ///draw all the lins for (short i=0;i<sizex;++i) /**call lineColro here*/; for (short i=0;i<sizey;++i) /**call lineColro here*/;}void InitCellBuffer(std::vector<std::vector<Cell> >& Buffer, short nx, short ny, bool live) { /************************ * initlize all cells * * to some state * ************************/ for (short i=0;i<nx;++i) { std::vector<Cell> TempV; for (short j=0;j<ny;++j) { Cell TempC; TempC.alive = live;///choose witch state to initlize them to TempC.TBF = false; ///we dont want them to be fliped just yet TempV.push_back(TempC); } Buffer.push_back(TempV); }}void CheckCells(std::vector<std::vector<Cell> >& Buffer) { short sizex = Buffer.size(); short sizey = Buffer[0].size(); for (short i=0;i<sizex;++i) { for (short j=0;j<sizey;++j) { char alive=0; char dead=0; /************************** * how many live or dead * * adjacent cells * **************************/ if(i>0&&j>0&&i<sizex-1&&j<sizey-1) { if (Buffer[j+1].alive) ++alive; else ++dead; if (Buffer[i-1][j].alive) ++alive; else ++dead; if (Buffer[j-1].alive) ++alive; else ++dead; if (Buffer[i+1][j+1].alive) ++alive; else ++dead; if (Buffer[i+1][j-1].alive) ++alive; else ++dead; if (Buffer[i-1][j+1].alive) ++alive; else ++dead; if (Buffer[i-1][j-1].alive) ++alive; else ++dead; if (Buffer[i+1][j].alive) ++alive; else ++dead; } if(i==0&&j==0) { if (Buffer[j+1].alive) ++alive; else ++dead; if (Buffer[i+1][j+1].alive) ++alive; else ++dead; if (Buffer[i+1][j].alive) ++alive; else ++dead; } if(i==0&&j==sizey-1) { if (Buffer[j-1].alive) ++alive; else ++dead; if (Buffer[i+1][j].alive) ++alive; else ++dead; if (Buffer[j-1].alive) ++alive; else ++dead; } if(i==sizex-1&&j==sizey-1) { if (Buffer[i-1][j-1].alive) ++alive; else ++dead; if (Buffer[i-1][j].alive) ++alive; else ++dead; if (Buffer[j-1].alive) ++alive; else ++dead; } if(i==sizex-1&&j==0) { if (Buffer[i-1][j+1].alive) ++alive; else ++dead; if (Buffer[i-1][j].alive) ++alive; else ++dead; if (Buffer[j+1].alive) ++alive; else ++dead; } if(i>0&&i<sizex-1&&j==0) { if (Buffer[i+1][j].alive) ++alive; else ++dead; if (Buffer[i-1][j].alive) ++alive; else ++dead; if (Buffer[i-1][j+1].alive) ++alive; else ++dead; if (Buffer[j+1].alive) ++alive; else ++dead; if (Buffer[i+1][j+1].alive) ++alive; else ++dead; } if(i>0&&i<sizex-1&&j==sizey-1) { if (Buffer[i-1][j].alive) ++alive; else ++dead; if (Buffer[i+1][j].alive) ++alive; else ++dead; if (Buffer[i-1][j-1].alive) ++alive; else ++dead; if (Buffer[i+1][j-1].alive) ++alive; else ++dead; if (Buffer[j-1].alive) ++alive; else ++dead; } if(i==sizex-1&&j>0&&j<sizey-1) { if (Buffer[i-1][j+1].alive) ++alive; else ++dead; if (Buffer[i-1][j-1].alive) ++alive; else ++dead; if (Buffer[j+1].alive) ++alive; else ++dead; if (Buffer[i-1][j].alive) ++alive; else ++dead; if (Buffer[j-1].alive) ++alive; else ++dead; } if(i==0&&j>0&&j<sizey-1) { if (Buffer[j+1].alive) ++alive; else ++dead; if (Buffer[j-1].alive) ++alive; else ++dead; if (Buffer[i+1][j+1].alive) ++alive; else ++dead; if (Buffer[i+1][j-1].alive) ++alive; else ++dead; if (Buffer[i+1][j].alive) ++alive; else ++dead; } /************************** * ? to be fliped or not * **************************/ if (Buffer[j].alive&&alive<2) { Buffer[j].TBF = true; } if (Buffer[j].alive&&alive>3) { Buffer[j].TBF = true; } if (!Buffer[j].alive&&alive==3) { Buffer[j].TBF = true; } } }}void ClearCells(std::vector<std::vector<Cell> >& Buffer) { short sizex = Buffer.size(); short sizey = Buffer[0].size(); for (short i=0;i<sizex;++i) { for (short j=0;j<sizey;++j) { Buffer[j].alive = false; Buffer[j].TBF = false; } }}void FlipCells(std::vector<std::vector<Cell> >& Buffer) { short sizex = Buffer.size(); short sizey = Buffer[0].size(); for (short i=0;i<sizex;++i) { for (short j=0;j<sizey;++j) { if (Buffer[j].TBF) { Buffer[j].alive = !Buffer[j].alive; Buffer[j].TBF = false; } } }}static void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); boxColor(100,500,50,50,Black); lineColor(0,0,800,600,Green); glutSwapBuffers();}static void key(unsigned char key, int x, int y) { switch (key) { case 27: case 'q': ///qiut break; case 32: Setup = !Setup; break; } glutPostRedisplay();}static void idle(void) { glutPostRedisplay();}int main(int argc, char *argv[]) { glutInit(&argc, argv); glutInitWindowSize(800,600); glutInitWindowPosition(800,500); glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE); glutCreateWindow("Game Of Life"); glutReshapeFunc(resize); glutDisplayFunc(display); glutKeyboardFunc(key); glutIdleFunc(idle); glClearColor(1,1,1,1); glutMainLoop(); return EXIT_SUCCESS;}
in fact nothing is coloring, whats up with that?