Skip to main content
GameDev.net gamedev.net
🔒 Locked

managing input with glut

Started by 2xp Jul 6, 2004 at 2:43 AM 1 replies 1.7k views
Original Post
2xp
2xp
hi i want to manage my character movements with glut. most of the functions i use come from nehe's tutorials. however, i found out that the app cannot manage multiple keyboard strokes, meaning i can only use one button then another button. this is quite impractical beacause i want to create an action game. do i have to use another library ? or do i have to implement more developed functions? thanks for the help i develop on MACOSX with cocoa
okonomiyaki
okonomiyaki

Edit: ah, crap, I just realized that you are on the MACOSX. I was describing the windows message pump, but I'm sure there's something similar in macs.. Never developed on a mac.


This is fairly easy to do with the standard windows message pump. However, if I remember right, you skip creating all that with glut. I don't know much about glut myself, I've heard it's good to start with but you shouldn't use it for anything real. This might be a limitation of glut. If so, look into creating the window yourself (tutorials on nehe.gamedev.net). At one point you need to declare a message pump for windows (usually WndProc). Within this function, there is a case statement where you can catch windows events, such as key downs. This will fire whenever a key is pressed, even if other keys are pressed. You can get the key information from the wparam parameter of the function. It might look something like this.

switch(message) {
case WM_KEYDOWN:
keys[wParam] = 1;
return 0;
case WM_KEYUP:
keys[wParam] = 0;
return 0;
}

Now you have an array filled with keystrokes that signify all of the keys currently being pressed.

This might be useful to you, might not. You might not be willing to give up glut. I'd recommend looking around, you can look up SDL as well if you want to skip creating the window yourself. SDL does window creation for you, but gives you access to the same kind of message pump.
MtK
MtK
Glut >=3.7 provides four functions

void keyboardFunc(unsigned char key, int x, int y)
void specialFunc(int key, int x, int y)

void keyboardUpFunc(unsigned char key, int x, int y)
void specialUpFunc(int key, int x, int y)

The first two get called when a key/special key is pressed, the others when it's released.

Just do something like that

bool keyPressed[255];...void keyboardFunc(unsigned char key, int x, int y) {  keyPressed[ key ] = true;}void specialFunc(int key, int x, int y) {  keyPressed[ key ] = true;}void keyboardUpFunc(unsigned char key, int x, int y) {  keyPressed[ key ] = true;}void specialUpFunc(int key, int x, int y) {  keyPressed[ key ] = true;}void getInput() { //this may be a timer function that gets called every now and then  if( keyPressed[ 's' ] ) {    shoot();  }}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.