Thread: SDL C++ Handle Mouse Motions and clicks?

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

    Question SDL C++ Handle Mouse Motions and clicks?

    Hello! I have a game that I'm trying to create but I don't really know how to handle mouse motions yet. I have read the LazyFoo.net lesson 9. And I have too searched around on Google but haven't find any good article for what I want to do

    But what I want is to check if the mouse is OVER the START button of the game and if the user has pressed the LEFT mouse button the game should START.

    This works when I am hovering over the area there I want the mouse to do something:

    Code:
       else if (event.type == SDL_MOUSEMOTION)
                {
                     if (event.motion.x == 0)
                       {
                           quit = true;
                       }
                }
    I do not know how to check this. The START and QUIT button is in the middle of the screen and I don't know how to position the mouse there either. Thanks for help!
    Last edited by DecoratorFawn82; 09-14-2013 at 01:00 AM.

  2. #2
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    What you probably want to do is create a class Region that's a rectangle, either using the top-left and lower-right coordinates to define the rectangular region, or the top-left and width, height. You'd have a member function isInRegion(unsigned x, unsigned y) and pass the current mouse position to that function which will return true if the given coordinate is within the region/rectangle. Buttons could then be a subclass of Region if they do more than a rectangle does.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    171
    So what you mean I have to do is a SDL_Rect that holds the x, y, w, h variables. And a class called Button and a constructor of Button that takes the x, y, w, h arguments. Am I right?

  4. #4
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Quote Originally Posted by DecoratorFawn82 View Post
    So what you mean I have to do is a SDL_Rect that holds the x, y, w, h variables. And a class called Button and a constructor of Button that takes the x, y, w, h arguments. Am I right?
    I don't think it needs to be an SDL_Rect, your own rectangle class would do

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

    Question

    Can you show me a bit of code how you mean. I'm not really understanding. Is it something like:

    Code:
    class Button
    {
        private:
        //The attributes of the button
        SDL_Rect box;
        
        //The part of the button sprite sheet that will be shown
        SDL_Rect* clip;
        
        public:
        //Initialize the variables
        Button( int x, int y, int w, int h );
        
        //Handles events and set the button's sprite region
        void handle_events();
        
        //Shows the button on the screen
        void show();
    };
    Like that from the LazyFoo tutorial? Or how do I do this. The only thing Í want to do is to say if the mouse is in position (On the start button) and the user has clicked the left mouse button the program should start the game.

    SDL C++ Handle Mouse Motions and clicks?-start_quit-png

  6. #6
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Ok, working with SDL_Rect, how would you answer the question is (mx, my) inside the rectangle? Where mx and my are the x and y position of the mouse.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to add mouse simulation clicks
    By whatnext in forum C Programming
    Replies: 6
    Last Post: 09-30-2011, 01:18 PM
  2. Monitor Mouse Clicks
    By mmarab in forum Windows Programming
    Replies: 10
    Last Post: 09-04-2007, 04:52 AM
  3. mouse clicks
    By chris1985 in forum C Programming
    Replies: 1
    Last Post: 01-14-2005, 04:19 PM
  4. Mouse Clicks
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 11-07-2001, 09:12 AM