Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

DoubleT

Problem with OpenGL

This topic is 6062 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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..

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!