Thread: SDL: How to handle mouse motion input?

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171

    Question SDL: How to handle mouse motion input?

    Hi! I'm a C++ programmer and I am using SDL 2.0. But I don't know how to handle mouse input. Do you think you can explain of how to do that? Or give me a link that explains that really great? (I'm sorry if this is a dumb question to post here)


    I know how to handle a mouse button that presses down for a sec.

    Code:
            if (event.type == SDL_MOUSEBUTTONDOWN)
            {
                if (event.button.button == SDL_BUTTON_LEFT)
                {
                     // Do something . . .
                }
            }
    But not how to handle the mouse input of what the mouse is hovering over and that.


    Like in a start menu of a game. I want a button for example to appear blue when I hover over it and when I click on it. It should start the game for example.


    Please help!

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Something like this then?

    SDL_MouseMotionEvent

  3. #3
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171
    Yes, But in SDL 2.0. What I want is a easy tutorial of how to handle mouse motion input in SDL 2.0. But however thanks for the link!

    And if you know how to handle SDL_MouseMotionEvent. Do you think you can give me a code example?
    Last edited by DecoratorFawn82; 10-19-2013 at 03:30 PM.

  4. #4
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Idk about 2.0 compliance but here's a Lazy Foo tutorial : Lazy Foo' Productions

    I hope this helps. Honestly, I don't code with SDL though I have experimented with it in the past and I enjoy it greatly. SDL's so... so awesome

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    SDL Wiki has links to documentation and tutorials for SDL2.0.

    SDL_MouseMotionEvent (which is a struct within the SDL_Event union) can be found in the documentation.

    Presumably it is used something like this:
    Code:
    if (event.type == SDL_MOUSEMOTION)
    {
        if (event.motion.state & SDL_BUTTON_LMASK)
        {
            // mouse moved with left button down
        }
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171
    Yeah, That code works if you want to hold down a mouse button and draw the mouse during you're holding down the left mouse button. But I want so I can hover over a button and just click on it. And when I have clicked on it. It should start the game.

    Like in minecraft: You hover the mouse over "Single Player". You click on it and it brings you to your saved worlds. You select a world and click start game

  7. #7
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    Just check the lazy foo pages, good tutorials. You basically just need to have an events loop for the mouse and then on the various events check if the pointer position is over the boundaries of your various buttons etc. Its not too difficult at all. Then you can do stuff like flip the buton image to one that is 'highlighted' or whatever you fancy. Just practice by loading an sdl surface and displaying a button image on it and then make it do stuff according to your mouse events, all that kind of stuff is on the lazy foo tutorials.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SDL C++ Handle Mouse Motions and clicks?
    By DecoratorFawn82 in forum C++ Programming
    Replies: 5
    Last Post: 09-14-2013, 04:36 AM
  2. Handle raw mouse movement?
    By TriKri in forum C Programming
    Replies: 4
    Last Post: 05-03-2010, 09:36 AM
  3. I'm lost with mouse motion OpenGL
    By javalurnin in forum C Programming
    Replies: 1
    Last Post: 12-18-2009, 01:15 PM
  4. how to handle keyboard and mouse events for all windows
    By manav-II in forum Windows Programming
    Replies: 3
    Last Post: 08-25-2008, 09:03 AM
  5. How to Handle heavy input files (~500MB)?
    By gibsosmat in forum Windows Programming
    Replies: 14
    Last Post: 11-26-2007, 10:51 PM