Jump to content
  • Advertisement
Sign in to follow this  
Hexed

SDL Mouse Input

This topic is 4866 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

Hey I'm writing a 3D/FPS game engine, and I'm having this one annoying problem with mouse input handling. The game loop calls ProcessMouse() every frame to get mouse input via SDL_GetRelativeMouseState(), and it uses these values return to increase/decrease rotation/pitch. This works pretty well, but the issue is that when the program starts, the rotation/pitch seems to be somewhat random. When I disable the mouse input handling this is not an issue. Here's the applicable C source: Init:
SDL_WM_GrabInput(SDL_GRAB_ON);			// Take focus
SDL_ShowCursor(SDL_DISABLE);			// Hide mouse cursor
SDL_GetRelativeMouseState (NULL, NULL);		// Reset relative position

Per frame:
int ProcessMouse (void)
{
	int mx, my;
	Uint8 state;
	float sensitivity = 0.25;

	state = SDL_GetRelativeMouseState (&mx, &my);

	if (state & SDL_BUTTON (1)) { // Left mouse button pressed }
	if (state & SDL_BUTTON (2)) { // Middle mouse button pressed }
	if (state & SDL_BUTTON (3)) { // Right mouse button pressed }

	// Rotate camera based on mouse movement
	player01.angle.y -= mx * sensitivity;
	player01.angle.x -= my * sensitivity;

	// Clamp pitch value
	if (player01.angle.x < -90) { player01.angle.x = -90; }
	if (player01.angle.x > 90)	{ player01.angle.x = 90; }

	return (0);
}

Share this post


Link to post
Share on other sites
Advertisement
SDL_GetRelativeMouseState (&mx, &my) - this gives rather arbitrary values for the first time the program runs (I don't know what it subtracts current position from). Anyway, you could (but this is a very shabby workaround) do this:

bool first_time_running = true;

while (!done) // your game loop
{
// do stuff
if (!first_time_running)
{
// process mouse events
}

first_time_running = false;
/* first time has been run! you should get correct
mouse values now */
}

Share this post


Link to post
Share on other sites
Hey thanks for the reply! I was beginning to think no one on this forum knew what a mouse was. ;)

Anyway, yes this sounds like it could possibly work. I'm not a big fan of workarounds/hacks though, so I'm going to look into using SDL_GetMouseState() to calculate my own relative positions. Maybe that will work. Later.

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!