Problem with OpenGL

Started by
2 comments, last by DoubleT 22 years, 4 months ago
I''m looking to get into some game programming. I know I''m starting a little late since I''m already a college student. I just finished my first class in computer graphic and we covered OpenGL extensively. In fact we even built our own ray tracer. But my problem lies in this. I''m working on my first game which is a Tetris clone. I decided to write it using just text characters and then use graphics. I''m up to the graphics point right now. My problem is with the main game loop. I don''t understand how to implement in conjunction with the GLUT glutMainLoop() function. This is not a Win32 program since I only use Linux so it''s like a console app if you''re using MSVC. Any help would be greatly appreciated. Double T Roy.Ens@ttu.edu
Double TRoy.Ens@ttu.edu
Advertisement
I think what you''re asking is how to implement a program in this awkward environment where GLUT calls your display callback or animate callback. Well, basicly you have to maintain a "state" for your program. Ignoring user input for now, say your tetris game just keeps dropping pieces down the screen, and they eventually fill up the screen and the game ends. There are two states in this case, one where the game is in play and one where the game has ended. So in your game loop, or GLUT callback in this case, you''d do something like:

int gameState; // this is a global

void animateCallback() {
if (gameState == GAME_PLAYING) {
movePieces();
}
else {
showGameOverScreen();
}
}

Now, there are other "states" in the game, like when the pieces are moving, you have to save their position and orientation, so the movePieces() function might look like this:

int x, y, angle; // these are globals

void movePieces() {
x += playerInputX; // x position is controlled by player
y += 1; // y always moves down
if (y > SCREEN_HEIGHT) { // actually if the piece stopped
currentPiece = rand(); // generate new piece
x = 0; y = 0; // reset coordinates
}
}

you also have some callbacks for the keyboard, so the function above will interact with the keyboard callback like this:

void keyboardCallback(int key) {
switch (key) {
case GLUT_LEFT:
playerInputX = -1;
break;
case GLUT_RIGHT:
playerInputX = 1;
break;
}
}

hope that helps..

--bart
Ok, that makes a lot of sense. I''ve tried implementing something similar and it''s starting to work now.

Double T
Roy.Ens@ttu.edu
Double TRoy.Ens@ttu.edu
Ok, that makes a lot of sense. I''ve tried implementing something similar and it''s starting to work now.

Double T
Roy.Ens@ttu.edu
Double TRoy.Ens@ttu.edu

This topic is closed to new replies.

Advertisement