Solved

Keyboard input for a simple game

Posted on 2003-11-28
1
2,620 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
No surround sound in games Windows 10 64bit. 12 168
Recommend a place where I can get free 3D objects 2 50
count8 challlenge 13 146
Is EDM my only option for trying music again? 4 34
Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I was…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

810 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