Sign in to follow this  
Kim2

Mouse pos stuck but registers clicks

Recommended Posts

Hi guys, I am trying to use directInput for my mouse control but I am having some problems. The mouse functions are executing and I can get the mouse button clicks but the movement of the cursor (which is stored in the struct variable mouse_state.Xl/Yl/Zl) does not change from 0. I have uploaded my project, it is here: http://www.kimgadecki.com/code/Newton Example.rar I am just a beginner so it is quite small and simple. Someone with programming experience, especially in directx, could probably look at it briefly and say "you forgot to include this" or "this doesn't go there" (or whatever the problem is) quite easily. Thank you for your time

Share this post


Link to post
Share on other sites
DirectInput won't give you absolute coords of the mouse, so you have to calculate them and keep track of them yourself. Your better off using standard windows messaging for mouse and keyboard and keep DirectInput for joysticks.

Share this post


Link to post
Share on other sites
I'd be happy with any value except 0! ;) (unless the mouse is at the middle of the screen)

Calculating the position can't be that hard, wouldn't it just be something like

xPos += xPos + mouse_state.lX

Please someone take a look at the code if you can, it'd only take a minute

Share this post


Link to post
Share on other sites
A couple of things:

1) You're reinitialising DirectInput every frame:


// Init game
Game.Init();

// Create scene
Game.Prepare();

// Main Loop
while(Game.device->run())
{
Game.InitDInput();
Game.MainLoop();
}

// Clean up memory
Game.Finish();



Move Game.InitDInput(); outside of your game loop, or move it into your Game::Init() function.

2) The HWND you're passing into DInput is uninitialised. You have
HWND hWnd;
in game.cpp, but it's never given a value, or is it ever used. The HWND you pass into DInput must be a "valid top-level window handle that belongs to the process". In other words, you need to retrieve the HWND from whatever API you're using to create the window, before passing it into DirectInput. I'm actually surprised it doesn't crash.

EDIT: Oh, and don't retrieve background access and hide the cursor. It means that your program stays in focus. I couldn't alt tab, couldn't access task manager with the mouse, and I had to close your program by hitting [alt]+[f4].

Share this post


Link to post
Share on other sites
Thanks for your reply, sc4freak.

A couple of things.

I have no doubt that you are correct but when I moved the init directInput call all input (including the keyboard) ceased to work.

I'm not sure what hwnd should refer to here because irrlicht is managing the window creation.

Share this post


Link to post
Share on other sites
Well, reinitialising DirectInput *just because it works* is certianly not an option. It's quite a serious memory leak; you're recreating the device every single frame and not releasing it. After maybe an hour, your program will probably crash because it sucked up every last megabyte of memory available.

As I said, you'll need to get the HWND from the API you're using to create the window. In this case, you'll need to read irrlicht's documentation to find out how to get the HWND from it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this