• Advertisement
Sign in to follow this  

GLUT keyboard movements

This topic is 3821 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

Hi Guys, I have a line defined as follows: #define X_CENT 0.0 /*centre of window*/ #define Y_CENT 0.0 #define LINE_LENGTH 0.5 /*length of square sides*/ glBegin(GL_LINE_LOOP); glVertex2f( X_CENT - LINE_LENGTH / 2, Y_CENT - LINE_LENGTH / 2); glVertex2f( X_CENT + LINE_LENGTH / 2, Y_CENT - LINE_LENGTH / 2); glEnd(); Now what I want to do is when the left arrow key is pressed on the keyboard, the line will move to the left, the more you press the key, the more to the left it'll move. This is what I have come up with so far: void SpecialKeyboardChars(unsigned char key, int x, int y) { switch(key) { case GLUT_KEY_LEFT: X_CENT = -0.1 glutPostRedisplay(); break; default: break; } } This doesnt work but its the best I could come up with. How can I make it work ?

Share this post


Link to post
Share on other sites
Advertisement
I'm guessing that instead of:


X_CENT = -0.1


you want


X_CENT -= 0.1;


to progressively reduce X_CENT while the key is being pressed.

However, I don't know anything about GLUT, but I'd guess that the SpecialKeyboardChars() function is probably only called on the keydown and keyup events.

Share this post


Link to post
Share on other sites
Thanks. I tried that, but I get the following error:

Error 2 error C2106: '-=' : left operand must be l-value

Share this post


Link to post
Share on other sites
Oh yeah, X_CENT is a define, not a variable. Sorry, didn't read that properly.

X_CENT and Y_CENT need to be variables, since you want to be able to change their values, so instead of #define X_CENT 0.0, you want:

float X_CENT=0.0f;

However...

I'm assuming you want the line to keep moving while the key is pressed and stop when the user releases the key. I've had a look at GLUT and it appears that as well as glutSpecialFunc(), there is also glutSpecialUpFunc() which registers a call back for when a key is released. Assuming we assign a function to this callback as well, your structure probably wants to look a bit more like (in simple terms):


bool LeftKeyPressed=false;
bool RightKeyPressed=false;

float LineX=0;

void MainLoop()
{
if(LeftKeyPressed) LineX-=0.1f;
if(RightKeyPressed) LineX+=0.1f;

RenderLineAt(X,100); // or whatever, use X to position line horizontally
}

void SpecialKeyFunc(int key,int x,int y)
{
if(key==GLUT_KEY_LEFT) LeftKeyPressed=true;
if(key==GLUT_KEY_RIGHT) RightKeyPressed=true;
}

void SpecialKeyUpFunc(int key,int x,int y) // tagged to glutSpecialUpFunc()
{
if(key==GLUT_KEY_LEFT) LeftKeyPressed=false;
if(key==GLUT_KEY_RIGHT) RightKeyPressed=false;
}


So the modification to the position of the line actually takes place in the main loop, before the render, and all that the keyboard callbacks do is update a couple of global bools that can tell the main loop whether or not a particular key is pressed.

A neater alternative, assuming the GLUT_KEY_* macros stay within a 0-256 value range, would be:


bool KeyPressed[256]={ false };

void SpecialKeyFunc(int key,int x,int y)
{
KeyPressed[Key]=true;
}

void SpecialKeyUpFunc(int key,int x,int y) // tagged to glutSpecialUpFunc()
{
KeyPressed[Key]=false;
}

void MainLoop()
{
if(KeyPressed[GLUT_KEY_UP]) X-=0.1f;
if(KeyPressed[GLUT_KEY_DOWN]) X+=0.1f; // etc
}


although I don't know enough about GLUT to say whether this would work. It is a simplified form of the way I respond to WM_KEYDOWN and WM_KEYUP messages in my games.

Hope all that was of some use.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement