Because of my last two classes, I had a very convenient axis that was quite customizable and any movement wasn't so "choppy." What ever more could I need. I start to move the camera around and realize something. well I realize two things. first camera movement still is choppy. and lastly I can only work one button at a time. I think to myself how lame is that. Going back to XNA I first wonder if I can emulate how they handle keyboard input. I come to the conclusion that I cannot emulate it exactly because when you use XNA everytime you update your scene you have to query the system and get the state of the entire keyboard and see if specific keys are pressed. I couldn't easily do that. So I decided to take a look at what i know. In terms of keys I know 2 things. when the key was pressed and when the key was let go. And I'd like to make the assumption that all the time inbetween the two that the key is down.
I'm realeasing two versions of this class because both glut and wxWidgets handles mouse stuff slightly different. now lets take a look at how to use these classes.
First just like with anything you must include the proper header file:
after that you must create an instance of the class
after this is where it gets a bit tricky. at this point you have to set up the events.
wxWidgets:
BEGIN_EVENT_TABLE(tGLCanvas, wxGLCanvas)
EVT_KEY_DOWN(tGLCanvas::OnKeyDown)
EVT_KEY_UP(tGLCanvas::OnKeyUp)
END_EVENT_TABLE()
glut:
glutKeyboardFunc(KeyboardState);
glutSpecialFunc(specialKeyboardState);
glutKeyboardUpFunc(KeyboardUpState);
glutSpecialUpFunc(SpecialKUpState);
now inside each individual event you must tell the input class what key was pressed/released
wxWidgets:
void tGLCanvas::OnKeyUp(wxKeyEvent &event)
{
input.KeyboardUpState(event.GetKeyCode());
}
void tGLCanvas::OnKeyDown(wxKeyEvent &event)
{
input.KeyboardState(event.GetKeyCode());
}
glut:
void MouseState(int button,int state, int x, int y)
{
input.MouseState(button,state,x,y);
}
void MouseMotion(int x, int y)
{
input.MouseState(x,y);
}
void KeyboardState(unsigned char key,int x, int y)
{
if(menuState == GLUT_MENU_NOT_IN_USE)
input.KeyboardState(key);
}
void specialKeyboardState(int key, int x, int y)
{
if(menuState == GLUT_MENU_NOT_IN_USE)
input.KeyboardState(key);
}
In the glut version I check to see if the menus are in use so that it doesn't register the keyboard states. In wxWidgets I don't need to worry about it because I check when a charecter is entered if it is already registered.
next, only in glut, you need to turn off the keyboard repeat mode:
glutIgnoreKeyRepeat(GLUT_DEVICE_IGNORE_KEY_REPEAT);
and last in your Timer function you can see what keys are currently pressed:
wxWidgets:
if(input.isKeyDown('F'))
{
...
}
if(input.isKeyDown(WXK_LEFT))
{
...
}
glut:
if(input.isKeyDown('f'))
{
...
}
if(input.isKeyDown(GLUT_KEY_LEFT))
{
...
}
The rest of this library is going to fucus on glut only. and it deals with Mouse input. first you want to set up your mouse functions.
glutMouseFunc(MouseState);
glutMotionFunc(MouseMotion);
glutPassiveMotionFunc(MouseMotion);
next inside of each mouse function you want to update the tInput class with the mouses current state
void MouseState(int button,int state, int x, int y)
{
input.MouseState(button,state,x,y);
}
void MouseMotion(int x, int y)
{
input.MouseState(x,y);
}
now if you want to check if a certant button is down you call:
if(input.isButtonDown(GLUT_LEFT_BUTTON))
{
...
}
if you want to get the last change that the mouse had you would call:
elevation += input.GetChangeY() * timer.GetElapsedTime()* Anglemove;
azumuth += input.GetChangeX() * timer.GetElapsedTime()*Anglemove;
note on the above code the GetChangeX and GetChangeY only gets you the change once then the class returns 0 every time after that.
if you want to just get the current x and y position you would call this:
x = input.GetMouseX();
y = input.GetMouseY();
source code(glut):
tInput.h (3.93 kb)
tInput.cpp (1.85 kb)
source code(wxWidgets):
tInput.h (626.00 bytes)
tInput.cpp (1.41 kb)