Sign in to follow this  
MARS_999

How to get Mousewheel event in SDL?

Recommended Posts

IIRC it counts as a mouse button event. From the docs:
Quote:

Structure Data
type SDL_MOUSEBUTTONDOWN or SDL_MOUSEBUTTONUP

button The mouse button index (SDL_BUTTON_LEFT, SDL_BUTTON_MIDDLE, SDL_BUTTON_RIGHT, SDL_BUTTON_WHEELUP, SDL_BUTTON_WHEELDOWN)

state SDL_PRESSED or SDL_RELEASED

x, y The X/Y coordinates of the mouse at press/release time


Would elaborate, but I have to go [smile]

HTH

Share this post


Link to post
Share on other sites
Thanks for the tip. Am I correct in assuming that with SDL_BUTTON_WHEELUP or down you can make the wheel movement amount whatever you want? As of now I just increment by a value and thats what I get... Windows mouse wheel movement is a bit different, where it goes something like so


if(directInput.getMouseMovementWheel())
gCameraHeight += (directInput.getMouseMovementWheel() / 120.0f * gameSetupData.mouseWheelSensitivity);

//So in SDL I am doing

case SDL_BUTTON_WHEELUP:
gCameraHeight += .005f;

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The code above (assuming you do the same for wheel down) would give incorrect results, as one button is followed by another. So you would get a mouse up then a mouse down or vice versa and you camera would never alter.

Share this post


Link to post
Share on other sites
Well if thats the case than how do you just get the input for a mouse wheel when its only moved up or down and only want to add or sub a value? Not sure why both should be called if the user only moves the wheel up than up should be called not down...

Share this post


Link to post
Share on other sites
Here is some old source I have from when I was just interested in whether the wheel was up or down and it is hackish due to the buffer size which could cause an overrun.

void SDL_input::capture_mousewheel()
{
int const num = 32;
SDL_Event events[num];
int count = SDL_PeepEvents(events, num, SDL_GETEVENT, SDL_EVENTMASK(SDL_MOUSEBUTTONDOWN));
//Mousewheel events are just that and are not states.
//When the mousewheel is moved a SDL_MOUSEBUTTONDOWN followed by a SDL_MOUSEBUTTONUP
//is generated so we need to remove these off the queue.
//at the current frame rate i get one (if any) per update.
//if(count >0)
// SYSTEM_LOG(FILENLINE,"number of mouse wheel down events is: %d",count);

for( int i = 0; i<count;++i)
{
if(events[i].button.button == SDL_BUTTON_WHEELUP)
{ m_mouse_current.wheel_up=true; }
else if(events[i].button.button == SDL_BUTTON_WHEELDOWN)
{ m_mouse_current.wheel_down=true; }
}
//get the release events of the queue
SDL_PeepEvents(events, num, SDL_GETEVENT, SDL_EVENTMASK(SDL_MOUSEBUTTONUP) );

}

Share this post


Link to post
Share on other sites
Would you like a copy of my mouse class? I was planning to implement mouse wheel sooner or later, so if you like ill do it now. Its nothing particularly fancy, but it works in the game engine im making right now.

Basicaly, you need a value for the amount of scrolling which has been done, preferably a float.
for each mouse wheel event, add or subtract the "speed per click" to this value.
do this once per frame and take the result once all events on the message buffer have been handled. I use a similar method for cursor movement, which gives better results than simply reading the last instance of SDL_MOUSEMOTION, which is what I did when I first started out with SDL.


Share this post


Link to post
Share on other sites
Quote:
Original post by speciesUnknown
Would you like a copy of my mouse class? I was planning to implement mouse wheel sooner or later, so if you like ill do it now. Its nothing particularly fancy, but it works in the game engine im making right now.

Basicaly, you need a value for the amount of scrolling which has been done, preferably a float.
for each mouse wheel event, add or subtract the "speed per click" to this value.
do this once per frame and take the result once all events on the message buffer have been handled. I use a similar method for cursor movement, which gives better results than simply reading the last instance of SDL_MOUSEMOTION, which is what I did when I first started out with SDL.


If you want to great, but this is what I am using now and seems to work fine....


case SDL_MOUSEBUTTONUP:
switch(event->button.button)
{
case SDL_BUTTON_WHEELUP:
gCameraHeight += .5f;
break;

case SDL_BUTTON_WHEELDOWN:
gCameraHeight -= .5f;
break;
}
break;



Thanks

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