Direct Input Mouse Problem

Started by
6 comments, last by Peon 23 years, 2 months ago
Here''s the problem: I''m using if (mousestate.rgbButtons[0]) to test if the user is left clicking. This works fine in my Setup() function that runs at first. However, I copied the SAME test into my GameMain and found that what follows the if statement will run WITHOUT clicking! Yet, the same statement works fine earlier. WHY?! I''m about to tear my hair out! > Thanks, hehe. Peon
Peon
Advertisement
To correctly deturmine if the button is up use this code (pascal)
mousestate.rgbButtons[0] and $80 = $80; 

Remember to update the mouse state every frame or so
In C++, that translates to

inline bool MouseButtonDown(int iButton){   return (g_MouseState.rgbButtons[iButton] & 0x80) != 0;} 


-Domenic-
Geek^n

Edited by - Domenic on January 28, 2001 9:19:00 AM
-Domenic-Geek^n
Wow....

I spent several hours, pretty much destroyed the organization of my code only to find such a simple solution

Anyway, thanks for the help. Works like a charm now!

Peon
Peon
Er...I THOUGHT it worked, I'm still having problems though.

Basically what happens I click once on my setup screen, and it thinks the button is always down. I did a little test to test whether or not the button is down:

      if (mousestate.rgbButtons[0] &0x80){buttonState = 1;}else{buttonState = 0;}      


(I have the above test in my GameMain() function, and it runs every loop)

I added a part to my main function to test if buttonState was equal to 0 (and it should). buttonState was = to 0 in the Setup() function, all was good. But much to my surprise, the buttonState was always equal to 1 in GameMain(), even when I wasn't holding down the button. I've tried everything I can think of, it still doesn't work.

Maybe I'm just implementing the prior advice wrong, I dunno. Please help if you have any ideas

Peon




Edited by - Peon on January 28, 2001 1:10:27 PM

Edited by - Peon on January 28, 2001 1:11:27 PM
Peon
UPDATE:

After testing for a sucessful click in Setup(), I added a Sleep(500) so that I could make SURE the mouse button was down. So I clicked an option when prompted by Setup() and left the mouse there. Sure enough, the test in GameMain() STILL thought the button was down!

Now here's the strange part. I clicked the option, quickly moved the mouse away (because I was testing if a mouse click was in a given rectangle), and then GameMain ran. GameMain recognized that the button was not down. Huh? Now I'm really confused. If even one wants to take a look at the code, say so and I'll email ASAP.

In summary... GameMain() seems to think the button is down at first, but not later on.

Anyway, it's kinda long (and messy after all this hashing of possible solutions) but here's GameMain()...
  int Game_Main(void *parms = NULL, int num_parms = 0){	if (mousestate.rgbButtons[0] & 0x80 && buttonState != 1)	{		buttonState = 1;	}	else	{		buttonState = 0;	}	//get data from mouse again (do i even need this?)	lpdimouse->GetDeviceState(sizeof(DIMOUSESTATE),(LPVOID)&mousestate);// for now test if user is hitting ESC and send WM_CLOSEif (KEYDOWN(VK_ESCAPE))   SendMessage(main_window_handle,WM_CLOSE,0,0);//GetDClpddsprimary->GetDC(&xdc);//select green pen to draw gridSelectObject(xdc,gray_pen);//draw the grid of 5 by 5 pixel sections so it's easier to drawfor (int i = 1; i < (xDim*5+5); i= i+5)		// In need to add 5 to drw the final line{	MoveToEx(xdc, i,40, NULL);	LineTo(xdc, i, (40+yDim*5));}for (i = 1; i < (yDim*5+5); i= i+5) {	MoveToEx(xdc, 1,i+39, NULL);	LineTo(xdc, (1+xDim*5), i+39);}SetTextColor(xdc, RGB(255,255,255));		//white text with transparent backgroundSetBkMode(xdc, TRANSPARENT);//write various things to the screenTextOut(xdc,0,0,"SpriteEd v0.1", strlen("SpriteEd v0.1"));TextOut(xdc, 110,40,"Preview:", strlen("Preview"));TextOut(xdc, 215,20, "New",strlen("New"));TextOut(xdc, 215,40,"Load",strlen("Load"));//debugsprintf(buffer, "%d, %d, %d, %d, %d", xDim, yDim, MouseX, MouseY, buttonState);TextOut(xdc, 200,150,buffer,strlen(buffer));//end debugRECT frame = {130,65,130+xDim,65+yDim};			//holds frame that surrounds preview boxFrameRect(xdc, &frame, white_brush);//ReleaseDClpddsprimary->ReleaseDC(xdc);lpddsprimary->Lock(NULL, &ddsd, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT, NULL);MouseX += mousestate.lX;MouseY += mousestate.lY;if (buttonState == 1 && RectClick(0,70,50,90)) //rect click tests if input is in given rectangle.  This paticular rectangle is also the rectangle tested in Setup(){	MessageBox(main_window_handle, "how?", " ", MB_OK);	//convert screen coordinates into array coordinates and assign color	//MouseInput();		//check arrays and draw appropriate colors	//DrawSprite();}//unlock surface!   last time I forgot, GDI didn't work >:(lpddsprimary->Unlock(NULL);return(1);} // end Game_Main  


[/source]

Thanks,
Peon



Edited by - Peon on January 28, 2001 1:38:22 PM [/source]

Edited by - Peon on January 28, 2001 1:39:11 PM
Peon
What you need to do is move the lpdimouse->GetDeviceState(sizeof(DIMOUSESTATE),(LPVOID)&mousestate); statement up before the if statement. Right now, since you click the button in Startup() and don''t update the state until after the if statement, the button will still be down in Game_Main(). Sorry if that''s a bit confusing, I need sleep

-Greg
Nope it was perfect, Greg I''m SURE it works now, hehe.

Thanks for the help all.
Peon

This topic is closed to new replies.

Advertisement