LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-21-2006, 07:47 PM   #1
Shadowalker
LQ Newbie
 
Registered: Oct 2005
Distribution: Suse 10
Posts: 18

Rep: Reputation: 0
mouse click and mouse xy


Hi all. I'm working on quake1 engine. I'm trying to get the mousexy in a video mode on a mouse click. In 800x600, it tells me the position of the mouse in terms of the pixel it's on. Quake has mouselook on/off, moves the view around. This is called every frame in glvidlinuxglx:

Code:
static void HandleEvents(void)
{
	XEvent event;
	qboolean dowarp = false;
	int mwx = vid.width/2;
	int mwy = vid.height/2;

	if (!dpy)
		return;

	while (XPending(dpy)) {
		XNextEvent(dpy, &event);

		switch (event.type)
		{
		case KeyPress:
		case KeyRelease:
			Key_Event(XLateKey(&event.xkey), event.type == KeyPress);
			break;

		case MotionNotify:
			if (mouse_active)
			{
#ifdef USE_XF86_DGA
				if (dgamouse)
				{
					mx += (event.xmotion.x + win_x) * 2;
					my += (event.xmotion.y + win_y) * 2;
				}
#else
				if (true)
				{
					int xoffset = ((int)event.xmotion.x - mwx);
					int yoffset = ((int)event.xmotion.y - mwy);
						
					if(xoffset != 0 || yoffset != 0) {
						
						mx += xoffset;
						my += yoffset;
							
						XSelectInput(dpy, win, 
						             X_MASK & ~PointerMotionMask);
						XWarpPointer(dpy, None, win, 0, 0, 0, 0,
						             mwx, mwy);
						XSelectInput(dpy, win, X_MASK);
					}
				}
#endif
				else
				{
					mx += ((int)event.xmotion.x - mwx) * 2;
					my += ((int)event.xmotion.y - mwy) * 2;
#if 0 //ndef USE_XF86_DGA
					mwx = event.xmotion.x;
					mwy = event.xmotion.y;
#endif
					if (mx || my)
						dowarp = true;
				}
			}
			break;

		/*case ButtonPress:
			b=-1;
			if (event.xbutton.button == 1)
				b = 0;
			else if (event.xbutton.button == 2)
				b = 2;
			else if (event.xbutton.button == 3)
				b = 1;
			if (b>=0)
				Key_Event(K_MOUSE1 + b, true);
			break;

		case ButtonRelease:
			b=-1;
			if (event.xbutton.button == 1)
				b = 0;
			else if (event.xbutton.button == 2)
				b = 2;
			else if (event.xbutton.button == 3)
				b = 1;
			if (b>=0)
				Key_Event(K_MOUSE1 + b, false);
			break;*/

		case ButtonPress:
			// mouse button pressed
			switch(event.xbutton.button)
			{
			case 1:
				Key_Event(K_MOUSE1, true);
				break;
			case 2:
				Key_Event(K_MOUSE3, true);
				break;
			case 3:
				Key_Event(K_MOUSE2, true);
				break;
			case 4:
				Key_Event(K_MWHEELUP, true);
				break;
			case 5:
				Key_Event(K_MWHEELDOWN, true);
				break;
			default:
				Con_Printf("HandleEvents: ButtonPress gave value %d, 1-12 expected\n", event.xbutton.button);
				break;
			}
			break;

		case ButtonRelease:
			// mouse button released
			switch(event.xbutton.button)
			{
			case 1:
				Key_Event(K_MOUSE1, false);
				break;
			case 2:
				Key_Event(K_MOUSE3, false);
				break;
			case 3:
				Key_Event(K_MOUSE2, false);
				break;
			case 4:
				Key_Event(K_MWHEELUP, false);
				break;
			case 5:
				Key_Event(K_MWHEELDOWN, false);
				break;
			default:
				Con_Printf("HandleEvents: ButtonRelease gave value %d, 1-12 expected\n", event.xbutton.button);
				break;
			}
			break;
//end

		case CreateNotify :
			win_x = event.xcreatewindow.x;
			win_y = event.xcreatewindow.y;
			break;

		case ConfigureNotify :
			win_x = event.xconfigure.x;
			win_y = event.xconfigure.y;
			break;
		}
	}

	if (dowarp) {
		/* move the mouse to the window center again */
		XWarpPointer(dpy, None, win, 0, 0, 0, 0, vid.width / 2, vid.height / 2);
	}

}

void IN_MouseMove (usercmd_t *cmd)
{
	if (!mouse_avail)
		return;

	if (m_filter->value)
	{
		mx = (mx + old_mouse_x) * 0.5;
		my = (my + old_mouse_y) * 0.5;
	}
	old_mouse_x = mx;
	old_mouse_y = my;

	mx *= sensitivity->value;
	my *= sensitivity->value;

// add mouse X/Y movement to cmd
	if ( (in_strafe.state & 1) || (lookstrafe->value && ((in_mlook.state & 1) ^ ((int)m_look->value & 1)) ))	// 2001-12-16 M_LOOK cvar by Heffo/Maddes
		cmd->sidemove += m_side->value * mx;
	else
		cl.viewangles[YAW] -= m_yaw->value * mx;

	if ((in_mlook.state & 1) ^ ((int)m_look->value & 1))	// 2001-12-16 M_LOOK cvar by Heffo/Maddes
		V_StopPitchDrift ();

	if ( ((in_mlook.state & 1) ^ ((int)m_look->value & 1)) && !(in_strafe.state & 1))	// 2001-12-16 M_LOOK cvar by Heffo/Maddes
	{
		cl.viewangles[PITCH] += m_pitch->value * my;
		if (cl.viewangles[PITCH] > 80)
			cl.viewangles[PITCH] = 80;
		if (cl.viewangles[PITCH] < -70)
			cl.viewangles[PITCH] = -70;
	}
	else
	{
		if ((in_strafe.state & 1) && noclip_anglehack)
			cmd->upmove -= m_forward->value * my;
		else
			cmd->forwardmove -= m_forward->value * my;
	}
	mx = my = 0;
}
I tried to cut down the first to something more manageable so I could understand it.

Code:
void setmousexy (void)
{
	XEvent event;

	if (!dpy)
		return;

	while (XPending(dpy))
	{
		XNextEvent(dpy, &event);

		if (event.type == MotionNotify)
		{
			mousex = (int)event.xmotion.x;
			mousey = (int)event.xmotion.y;
		}
	}
}
Two problems, one there is a delay between the click and and getting the xy. The mousexy is offset right and down. That increases as you go from X=0/Y=0 to vid.width/vid.height.

On a menu screen this is checked:

Code:
//hydr_customhud from hyperdrive quake1 engine
		case MENU_HUD_ARMOR_ICON:
			if (keydown[K_MOUSE1])
			{
				sbar_armor_iconx->value= mousex;
				Cvar_SetValue (sbar_armor_iconx, sbar_armor_iconx->value);
				sbar_armor_icony->value= mousey;
				Cvar_SetValue (sbar_armor_icony, sbar_armor_icony->value);
				m_changesound = true;
			}
			break;
I still have to do this for windows. The cvar usage is working fine. It's mouse access that's the problem. Need help!

as

Last edited by Shadowalker; 07-21-2006 at 07:52 PM.
 
  


Reply

Tags
mouse



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
mouse: single-click becomes double-click kinzlaw Linux - Hardware 2 08-24-2005 07:55 PM
Weird mouse click in counterstrike 1.6 AM1SHFURN1TURE Linux - Games 2 10-12-2004 11:42 AM
mouse unwanted double-click Darshbot Debian 1 10-10-2004 09:02 PM
X freezes on mouse click. bruno buys Debian 3 09-23-2004 02:45 AM
MS Explorer Mouse scroll and right click cmsustud19 Linux - Newbie 1 03-26-2004 07:18 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration