Jump to content
  • Advertisement
Sign in to follow this  
huckdogg

DirectInput Crashes Program

This topic is 4402 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, I didn't know whether to ask this question in this forum or in the DirectX forum, but I figured this was more of a beginners question, so here we go. I am writing a small DirectX based game that uses Direct Input to handle the the mouse and keyboard. My problem is that when I try to run the game, I get a standard Windows crash dialog box and the program exits. I have narrowed the problem down to one line in my mouse initialization function, but as I am new to using Direct Input, I do not know why this particular line of code would be making my game crash. Here is the function: LPDIRECTINPUT8 dinput; LPDIRECTINPUTDEVICE8 dimouse; LPDIRECTINPUTDEVICE8 dikeyboard; DIMOUSESTATE mouse_state; //keyboard input char keys[256]; int Init_Mouse(HWND hWnd) { //set the data format for mouse input //this is the line causing the crash HRESULT result = dimouse->SetDataFormat(&c_dfDIMouse); if (result != DI_OK) return 0; //Added the next line to see where the program was crashing MessageBox(hWnd, "Error", "Error", MB_OK); //set the cooperative level //this will also hide the mouse pointer result = dimouse->SetCooperativeLevel(hWnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND); if (result != DI_OK) return 0; //acquire the mouse result = dimouse->Acquire(); if (result != DI_OK) return 0; //clean return return 1; } Here is the function call: //initialize the mouse if (!Init_Mouse(hWnd)) { MessageBox(hWnd, "Error initializing the mouse", "Error", MB_OK); return 0; } The line causing the problem: HRESULT result = dimouse->SetDataFormat(&c_dfDIMouse); What about this line of code would cause a crash each and every time that I run the game? This is code that I got directly from some examples that I was using to learn Direct Input. Thank you to anyone who might be able to help. I also have one more question. In the call to SetCooperativeLevel(), what are the best settings for a windowed game in which the mouse pointer needs to be visible? I am not sure whether to use EXCLUSIVE/NONEXCLUSIVE and FOREGROUND/BACKGROUND (I am guessing FOREGROUND on that one). Thanks for any help on this one, too! Brian Huckaby

Share this post


Link to post
Share on other sites
Advertisement
What error are you receiving? Please specify this whenever asking questions. Without more information, I can only speculate you forgot to call DirectInput8Create() or DirectInputCreateEx().

To get the error, use DXGetErrorString(). Here's an example:

#include <dxerr.h>//with your includes

if (result != DI_OK){
MessageBox(NULL,DXGetErrorString(result),"Error",MB_OK);
return 0;
}


As for exclusivity, there's no set rules. I personally capture the mouse into exclusive mode on a click, then allow it to be released with a key on the keyboard. If you do want an exclusive mode in a window, it's probably smart to allow someone to lose exclusivity when they want. You should lose the mouse when a system key is pressed on a non-exclusive keyboard, or you could write a menu option or bind a key or whatever else.

Share this post


Link to post
Share on other sites
Did you obtain dimouse from somewhere? Right now there is only the declaration LPDIRECTINPUTDEVICE8 which is an interface pointer. And i don't see where you assign any value to it.

You have to ask DirectInput to fill the pointer for you.

Share this post


Link to post
Share on other sites
Very sorry, I am not always sure what I need to include to provide all the information that others will need to help me.

Anyway, I did figure out my problem, with your help, richardurich. I had a function that called DirectInput8Create(), but the function wasn't being called anywhere in my code. Of course, I didn't have a clue, but thank you for making me realize my mistake! Thank you for responding, too, Endurion.

Share this post


Link to post
Share on other sites
I decided to ask this question in my own thread so that I didn't keep cluttering the Beginners forum.

The program that I mentioned above is still giving me problems. The code compiles and links with no errors, and the .exe runs with no crashes (so far). My problem is that even after adding the code for Direct Input, mouse and keyboard events are not being read by the game. If I try to print out the mouse coordinates in a message box, I always get 0. I also have the program set to exit when the escape key is pressed.

This works:

//code fragment from dxinput.cpp file
#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)

//code fragment from game.cpp file
if (KEYDOWN(VK_ESCAPE))
PostMessage(hWnd, WM_DESTROY, 0, 0);

This does not:

//code fragment from dxinput.cpp file
char keys[256];

void Poll_Keyboard()
{
dikeyboard->GetDeviceState(sizeof(keys), (LPVOID)&keys);
}

int Key_Down(int key)
{
return (keys[key] & 0x80);
}

//code fragment from game.cpp file
if (Key_Down(DIK_ESCAPE))
PostMessage(hWnd, WM_DESTROY, 0, 0);

All of the code for my dxinput.cpp file came from a tutorial in a book, so I am fairly certain that I have all of the necessary functions to handle simple keyboard and mouse input. So my question is this: With all of the Direct Input framework in place and no compiler errors, what could be the problem that is causing my mouse and keyboard to not be handled by Direct Input? I am open to any suggestions. I know that what I have provided isn't nearly enough to give a definitive answer to my problem, but I didn't know how to ask without posting all of my code. I will post it if I need to. Thanks to anyone who might be able to suggest a possible solution to my problem. Let me know what other code I might need to post to narrow down the problem.

Brian

Share this post


Link to post
Share on other sites
Any time you call a DirectX function, you should check the return value to see if it has reported an error code. Each function is documented thoroughly on MSDN, and details the meaning of each error code. The best place to start would be to do error checking on all of your DirectInput calls and see if you can isolate an error result.

If none of your calls are returning errors, the problem is likely in your DirectInput setup code, which you'll need to post for inspection.

Share this post


Link to post
Share on other sites
Quote:
Original post by ApochPiQ
Any time you call a DirectX function, you should check the return value to see if it has reported an error code. Each function is documented thoroughly on MSDN, and details the meaning of each error code. The best place to start would be to do error checking on all of your DirectInput calls and see if you can isolate an error result.

If none of your calls are returning errors, the problem is likely in your DirectInput setup code, which you'll need to post for inspection.


Thank you, ApochPiQ. I thought all of my DirectInput function calls were being checked, but your post made me take a second look. And quess what? All but 2 functions were being checked, and those were my Poll_Mouse() and Poll_Keyboard() functions. I checked those and found my error. I was calling Acquire() twice by accident on both of the keyboard and mouse.

Thank you for your help!

[Edited by - huckdogg on July 30, 2006 9:23:30 PM]

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!