Solved

Keyboard input for a simple game

Posted on 2003-11-28
1
2,619 Views
Last Modified: 2013-11-12
Hello. For a class project I am doing a simple 3D game using C++ and OpenGl.

So far, I am happy with the progress. It basically works the way I expect. As of now, I can use the "K","L" keys to move a paddle across the across the screen. The problem I am having is when the game is actually started, the user pushes "S" to start the game, it goes into a while loop and doesn't exit until the ball goes out of play.

This works fine, but while the loop is executing, the keyboard function doesn't work, therefore it doesn't last long as I cannot move the paddle.

It seems like after the loop exits, it stored all of the keyboard inputs, because then the paddle is drawn in the correct position. Its just not working while the loop is active.

Is there a way to temporarily break from the loop so as to check the keyboard function? If not, I need to find a way to force the system to check for keystrokes every run through the loop.

Here is some code if this helps.

//<<<<<<<<<<<<<<<<<<<<<<<< myKeyboard >>>>>>>>>>>>>>>>>>>>>>
void myKeyboard(unsigned char key, int x, int y)
{
  switch(key)
  {
  case 'l': movePlayer( 0.3); break; //slide paddle right
  case 'k': movePlayer(-0.3); break; // slide paddle left
  case 's': startGame();      break; // start the game
  case 'r': resetGame();      break; // reset the game

  }
  glutPostRedisplay(); // draw it again
}

//<<<<<<<<<<<<<<<<<<<<<Game Loop>>>>>>>>>>>>>>>>>>
while (game_state == PLAYING && player.lives > 0)
   {
     //I think i need to call Move Paddle here, but i am not sure how.
         
     // Check to see if ball is falling off bottom of table
     BallOut();
     
     //Check Paddle-Ball Collision
     Check_Paddle_Ball_Collision();
     
     //Check Brick-Ball Collision
....
0
Comment
Question by:bbaisley
1 Comment
 

Accepted Solution

by:
DeathCheese earned 50 total points
ID: 9853611
It sounds like you are starving the window's message queue with your while(PLAYING) loop.  What's probably happening is glut isn't able to check the message queue until the ball goes out of play and you leave the function containing the while(PLAYING) loop.  Then it  checks the message queue, getting all the keyboard input and calling your keyboard function.

If you aren't already, register the idle callback function and put your game logic (no rendering) in it.  The idle function is automatically called by glut every frame, I believe.  Then you can have code that might look like this:

void IdleFunc(...)
{
    switch(game_state)
    {
        ...
        // any other game states
        ...
        case PLAYING:
        {
            // BallOut, Collision, etc
        }
        ....
    }
}

That way, your code will be run just once per frame, and then glut can continue and do what it needs to do, checking the message queue and gathering keyboard input among other things.

Hope that helped!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Life . . . (received from a friend) 14 137
nearTen java challenge 8 99
How are Tech stock earnings calculated? 2 66
Graphics Card 2GB Versus 4GB. 7 93
Article by: ErwindeVries
Released on the 7th of September this real time strategy game sets a new bar for any real time strategy game. R.U.S.E. is a game made by Eugen Systems and published by Ubisoft and released for the PC, Playstation 3 and the Xbox 360. The setting o…
Programming  of  forecasting lottery numbers for self use  on Turbo C++ is done by use of random number generator code by a program algorithm logic. Random numbers usually generates numbers between definite interval. Therefore, when entering the c…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now