Question: im coding a tic tac toe game in open gl, im not done yet but when I try to run the code a black window

im coding a tic tac toe game in open gl, im not done yet but when I try to run the code a black window pops up but nothing is going on how come its just a black screen?

#include

#include

#include

#include

#include

#include

using namespace std;

int matrix [3][3]; // board for the gameplay of tic tac toe we are using a 3x3 matrix board

int turn; // indicates whose turn it is going to be

bool gameover; // is the game over would you like to end the game

int result;// the ending result of the game

void begin()

{

turn=1;

for(int i=0;i<3;i++) // setting up the board and clearing the matrix we start at 1, basically clearing our matrix

{

for(int j=0;j<3;j++)

matrix[i][j]=0;

}

}

void drawxo() // setting up our X and O if it is 1 then we want to use x and if it is 2 then 0

{

for(int i = 0; i <= 2; i++)

{

for(int j = 0; j <= 2; j++)

{

if(matrix[i][j] == 1) //if it is 1 then draw x

{

glBegin(GL_LINES);

glVertex2f(50 + j * 100 - 25, 100 + i * 100 - 25);

glVertex2f(50 + j * 100 + 25, 100 + i * 100 + 25);

glVertex2f(50 + j * 100 - 25, 100 + i * 100 + 25);

glVertex2f(50 + j * 100 + 25, 100 + i * 100 - 25);

glEnd();

}

else if(matrix[i][j] == 2) //if it is 2 then draw o

{

glBegin(GL_LINE_LOOP);

glVertex2f(50 + j * 100 - 25, 100 + i * 100 - 25);

glVertex2f(50 + j * 100 - 25, 100 + i * 100 + 25);

glVertex2f(50 + j * 100 + 25, 100 + i * 100 + 25);

glVertex2f(50 + j * 100 + 25, 100 + i * 100 - 25);

glEnd();

}

}

}

}

void DrawString(void *font,const char s[],float x,float y)

{

unsigned int i;

glRasterPos2f(x,y);

for(i=0;i

{

glutBitmapCharacter(font,s[i]);

}

}

void drawLines()

{

glBegin(GL_LINES);

glColor3f(0, 0, 0);

//2 vertical lines

glVertex2f(100, 50);

glVertex2f(100, 340);

glVertex2f(200, 340);

glVertex2f(200, 50);

//2 horizontal lines

glVertex2f(0, 150);

glVertex2f(300, 150);

glVertex2f(0, 250);

glVertex2f(300, 250);

glEnd();

}

void display()

{

glClear(GL_COLOR_BUFFER_BIT);

glClearColor(1, 1, 1, 1);

glColor3f(0, 0, 0);

if(turn == 1)

DrawString(GLUT_BITMAP_HELVETICA_18, "Player1's turn", 100, 30);

drawxo();

drawLines();

glutSwapBuffers();

}

void KeyPress(unsigned char key, int x, int y)

{

switch (key)

{

case 27: // the escape key to exit the program

exit (0);

break;

case 'y':

if (gameover = true) {

gameover = false;

begin();

}

break;

case 'n' :

if (gameover = true) {

exit(0);

}

break;

}

}

int main(int argc, char **argv)

{

begin();

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);

glutInitWindowPosition(550,200);

glutInitWindowSize(300,350);

glutCreateWindow("Tic tac toe");

glutDisplayFunc(display);

glutKeyboardFunc(KeyPress);

glutIdleFunc(display);

glutMainLoop();

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!