PDA

View Full Version : DirectInput



Vlasko
05-16-2003, 01:24 PM
I want to use DirectInput in my program.Where should I use the GetDeviceState function?I red somewhere it should be in rendering function-will it not slow rendering ?

Vlasko
05-20-2003, 06:32 AM
I ran into a serous problem with implementing directinput into GLUT.Is there a way to get HWND for glut window ?or is there an effective keyboard presses reader(like when i push more than one key)?I don't want to switch to win32-window creating functions look very ugly:-)

shinpaughp
05-20-2003, 08:55 AM
WIthin your glutKeyboardFunc use an array to keep track of state of keys by setting to true. There is another function glutKeyboardUpFunc which you could use to set the pressed key to false. Then you check the array within some other part of your code to determine state of keys.Your glut version must be 3.7 or greater.

Obli
05-20-2003, 08:59 AM
Sorry for the late mail, the opengl.org server was down!

About your first post.
You don't really need to check for new input every frame (not inside the render func) however, it does not make sense to me having in somewhere other.
I personally check for input on a per-frame basis (not in the render func, but immediatly before). It feels very good and does not seems to hurt performance i.e. I think I won't look here for a performance increase.

About the second post.
Probably glut does not allow you to pull out the HWND directly. You may be able to do it in other ways but I think you cannot (someone with this experience will tell you, I never had this kind of problem).
I heard the message-based input management is not so bad. I never tried this out, it's just a rumor. Someone should confirm this. Myself, I am actually using it to manage keyboard letters when in "text mode"...

"I don't want to switch to win32-window creating functions look very ugly:-)"
I actually think you can get the best by switching to pure win32 and porting what is necessary however, there's now two things:
1- Not use win32. Some people actually thinks you can do wonderful things using SDL or GLUT and they can effectively save you **lot** of time so you should check'em out and see if you feel ok with them.
2- Go for win32. Yes, it's bad and you'll be bored however looks like some functionalities can be done only in pure win32 mode (force feedback, advanced audio effects).

marcus256
05-21-2003, 03:24 AM
Hi!

I think you should have a serious look at GLFW (http://hem.passagen.se/opengl/glfw) . It's much neater than win32 programming, and has way better input support than GLUT. Plus: really-really portable.

> I heard the message-based input management is not so bad

GLFW uses message based input. It has gotten me quite far (actually, I think it's as good as DI - if not better in some cases).

Keyboard input:

- Unicode character input for text input needs (where supported - otherwise 8-bit ISO Latin 1 - handled transparently by GLFW)
- Keystroke information (unaffected by modifiers)
- Modifer information (shift, alt, ctrl - even left/right versions)
- Functional keys (F1-F25, cursors, page up/down etc)
- Keypad keys (in the keystroke interface, there is a difference between '1' and KP_1, for instance)
- Easy to use glfwGetKey() function which queries physical key state
- Works under all Windows versions (even NT4) and any compiler you can think of (not true for DirectInput)

Mouse input:

- Standard window-relative coordinates
- Hidden cursor mode (infinite range - similar to "raw mouse input")
- Mouse wheel


Joystick input:

- 16 sticks supported
- infinite number of buttons
- infinite number of axes
- Works under Windows, Linux and AmigaOS (Mac OS X to come, I hope)

(No force feedback though)


About win32 programming: It is a major pain in the ... to get your win32 code tested and working as you expect it to. There are sooo many problems and incompabilities between Windows versions etc, so you need special version dependent code paths and ugly work arounds to make your program behave the way you want it to, and equally on different Windows platforms. It has taken me years just to get GLFW working (and there are still some problems left to solve - e.g. why don't my SetForegroundWindow tricks work when GLFW is used from VisualBasic, but it's perfectly OK from any other program/compiler?).


[This message has been edited by marcus256 (edited 05-21-2003).]

Vlasko
05-21-2003, 10:16 AM
I wanted to use arrays for input but it doesn't work correctly.How did you ment with them shinpaughp?
I've done it this way

bool sipky[4];
void keyup(int key , int x , int y)//for glutSpecialUpFunc
{
switch(key)
{
case GLUT_KEY_UP :
sipky[0]=false;
break;
case GLUT_KEY_DOWN :
sipky[1]=false;
break;
case GLUT_KEY_LEFT :
sipky[2]=false;
break;
case GLUT_KEY_RIGHT :
sipky[3]=false;
break;
default: break;
}
CheckKeys();
}

void special(int key , int x , int y)//for glutSpecialFunc
{
switch(key)
{
case GLUT_KEY_UP :
sipky[0]=true;
break;
case GLUT_KEY_DOWN :
sipky[1]=true;
break;
case GLUT_KEY_LEFT :
sipky[2]=true;
break;
case GLUT_KEY_RIGHT :
sipky[3]=true;
break;
default: break;
}
CheckKeys();
}

void CheckKeys()
{
if(sipky[0])
my_position.z+=0.01f;
if(sipky[1])
my_position.z-=0.01f;
if(sipky[2])
my_position.x-=0.1f;
if(sipky[3])
my_position.x+=0.1f;
glutPostRedisplay();
}

marcus256
05-21-2003, 09:47 PM
You don't call CheckKeys() from the callback functios (then you could just as well have done the key checking in the callbacks). The thing is that the callbacks are only called when key state changes, but you want to use the key information during every frame.

What you want to do is to call CheckKeys() during every frame (from your main drawing function, for instance).

marcus256
05-21-2003, 10:04 PM
void CheckKeys()
{
if(sipky[0])
my_position.z+=0.01f;
if(sipky[1])
my_position.z-=0.01f;
if(sipky[2])
my_position.x-=0.1f;
if(sipky[3])
my_position.x+=0.1f;
glutPostRedisplay();
}


Skip glutPostRedisplay() - you should call it from your Draw()/Idle() function(s) instead.

Also, 0.1f is not a good figure, since the frame duration may vary from frame to frame and from system to system. You need to put the frame delta time in the expression:

my_position.x += 1.0f * dt;

If dt is the frame duration in seconds, this means that your position will change 1.0 units per second (i.e. the speed is 1.0 units/s).

With GLUT, you can use glutGet( GLUT_ELAPSED_TIME ), which returns a time in milliseconds. Unfortunately its resoltuion is about 10-50 ms under Windows (meaning that you can get jerky animation, and possibly dt = 0.0, when the rendering is faster than the timer resolution), but you have no alternative under GLUT (other than to use Win32 coding - e.g. QueryPerformanceCounter). Here is an example:




// Global variables
float t, dt;

// Main rendering function
void MyMainFunction( void )
{
float t_new;

// Update frame time and delta time
t_new = 0.001f * glutGet( GLUT_ELAPSED_TIME );
dt = t_new - t;
t = t_new;

// Update position
CheckKeys();

// Draw...
// ...

glutPostRedisplay();
}


Again, I would like to recommend GLFW, since it's timer usually has a resolution better than one microsecond (about 10,000-1,000,000 times better resolution than the GLUT timer).

Vlasko
05-22-2003, 06:52 AM
I don't feel ready for another toolkit right now(has the GLFW same structure as GLUT ?) but i will consider it probably later- I still think I'm total beginner to OpenGL(stuck between 19-25 nehe's lesson:-))But thanks for help it is working good now