PDA

View Full Version : keyboardfunc()



GMan
03-25-2006, 04:23 PM
I made Pong, but the problem is, that when I move the paddles, they don't move like they should. The rate at which they move is simmilar to typing. For example, when you hold down the 'a' key, an 'a' is printed to the screen, then there is a small delay, then more a's. That's how the paddles move.

How can I get rid of this delay, and just have the paddles move at a constant speed as soon as the key is pressed.

Ehsan Kamrani
03-25-2006, 05:37 PM
It's the problem of the function glutKeyboardFunc(). You can get better results with WM_KEYDOWN or WM_KEYUP.At least there are no delays in simple games. However for a commercial game, you should use from DirectInput.
-Ehsan-

Overmind
03-26-2006, 05:08 AM
You shouldn't move your paddles in the glutKeyboardFunc.

The usual way to do this is to have a boolean variable for each key. In the glutKeyboardFunc you set this flag to true, in the glutKeyboardUpFunc you set it to false.

Then, in the glutIdleFunc you move the paddle if the flag is set to true.

This archieves at least continuous motion. To get constant speed, you have to measure the time between two idle events and make the distance you move the paddle dependant on that.

Ther
03-27-2006, 02:09 PM
My solution:

.......
main.cc
........

FsKeyboard keyboard; // which is extern maybe
void windowKeyPressed (unsigned char key, int x, int y) { keyboard.keyPressed ( key ); }
void windowKeyReleased ( unsigned char key, int x, int y ) { keyboard.keyReleased ( key ); }
void windowSpecialUp ( int key, int x, int y ) { keyboard.keySpecialReleased ( key ); }
void windowSpecial (int key, int x, int y) { keyboard.keySpecialPressed ( key ); return; }
void windowMouse (int button, int state, int x, int y) { keyboard.mouseAction ( button, state, x, y ); return; }
void windowMotion ( int x, int y ) { keyboard.mouseMotion ( x,y ); return; }
void windowShape (int width, int height) { window_width = width; window_height = height; return; }

..........
keyboard.h
...........


enum FsSpecialKeys {
FS_MOUSE_LEFT = 256, FS_MOUSE_MIDDLE= 257, FS_MOUSE_RIGHT = 258, FS_KEY_LEFT = 259,
FS_KEY_RIGHT = 260, FS_KEY_UP = 261, FS_KEY_DOWN = 262, FS_KEY_F1 = 263,
FS_KEY_F2 = 264, FS_KEY_F3 = 267, FS_KEY_F4 = 268, FS_KEY_F5 = 269,
FS_KEY_F6 = 270, FS_KEY_F7 = 271, FS_KEY_F8 = 272, FS_KEY_F9 = 273,
FS_KEY_F10 = 274, FS_KEY_F11 = 275, FS_KEY_F12 = 276, FS_KEY_F13 = -1, //WE AR NOT MACHINTOS USERS!!!
FS_KEY_HOME = 277, FS_KEY_PAGEUP = 278, FS_KEY_PAGEDOWN= 279, FS_KEY_END = 280,
FS_KEY_INSERT = 281, FS_WHEEL_UP = 282, FS_WHEEL_DOWN = 283
};

extern int fsGlutCodes [];


//----------------
class FsKeyboard {
//----------------
private:
bool keys [ 512 ];
int mousex, mousey;
public:
FsKeyboard () { for ( int i=0; i< 512; i++ ) keys [i] = false; }

bool isKeyDown ( int i ) { return keys [i]; }

void keyPressed ( unsigned char key ) { keys [key] = true; }
void keyReleased ( unsigned char key ) { keys [key] = false; }

void keySpecialPressed ( int key ) {
for ( int i = 0; i < 25; i++ ) {
if ( fsGlutCodes [i] == key ) { keys [256+i] = true; }
}
}

void keySpecialReleased ( int key ) {
for ( int i = 0; i < 25; i++ ) {
if ( fsGlutCodes [i] == key ) { keys [256+i] = false; }
}
}
void mouseAction ( int button, int state, int x, int y ) {
switch ( button ) {
case 0: keys [ FS_MOUSE_LEFT ] = 1-state;
case 1: keys [ FS_MOUSE_MIDDLE ] = 1-state;
case 2: keys [ FS_MOUSE_RIGHT ] = 1-state;
case 3: keys [ FS_WHEEL_DOWN ] = 1-state;
case 4: keys [ FS_WHEEL_UP ] = 1-state;
}
return;
}

int getx () { return mousex; }
int gety () { return mousey; }

void mouseMotion (int x, int y) { mousex = x; mousey = y;}
void set ( void ) { for ( int i = 0; i < 512; i++ ) { keys [i] = false; } }
};

Ther
03-27-2006, 02:12 PM
and in the main function:

glutDisplayFunc( windowDraw ); //draw
glutReshapeFunc ( windowShape ); //window resize
glutIdleFunc( windowDraw ); //draw
glutKeyboardFunc ( windowKeyPressed ); //transformable keys on the keyboard
glutKeyboardUpFunc ( windowKeyReleased ); // key released
glutSpecialFunc( windowSpecial ); // not tranformable keys on the keyboard
glutSpecialUpFunc ( windowSpecialUp );
glutMouseFunc ( windowMouse ); // mouse click
glutPassiveMotionFunc ( windowMotion ); // motion, when button is not downand in the program you just ask for:

if ( keyboard.isKeyDown ('a') ) { ... }or

if ( keyboard.isKeyDown ('FS_KEY_UP') ) { ... }