Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8781405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:17:31+00:00 2026-06-13T20:17:31+00:00

I have a simple script where by i want the script to load a

  • 0

I have a simple script where by i want the script to load a second button when the mouse is hovering over the first initial button. But it won’t return true so i can’t seem to get it to work.

This is my class which checks the situation:

class Button
    {
    private:
        int m_x, m_y;            
        int m_width, m_height;

    public:
    Button(int x, int y, int width, int height)
    {
       m_x = x;
       m_y = y;
       m_width = width;
       m_height = height;

    }

    bool IsIn( int mouseX, int mouseY )
    {
        if (((mouseX > m_x) && (mouseX < m_x + m_width)) 
        && ((mouseY > m_y) && (mouseY < m_y + m_height ) ) ) {
            return true;
        } else {
            return false;
        }
    }

    void Render(SDL_Surface *source,SDL_Surface *destination)
    {
        SDL_Rect offset;
        offset.x = m_x;
        offset.y = m_y;
        offset.w = m_width;
        offset.h = m_height;

        source = IMG_Load("button.png");


        SDL_BlitSurface( source, NULL, destination, &offset );

    }
};

Its the IsIn function I am trying to get to work… in my main loop i have:

while(!quit){
while( SDL_PollEvent( &event ) )
        switch( event.type ){
            case SDL_QUIT: quit = true; break;
            case SDL_MOUSEMOTION: mouseX = event.motion.x; mouseY = event.motion.y; break;      
        }

 Button btn_quit(screen->w/2,screen->h/2,0,0);
 btn_quit.Render(menu,screen);

 if(btn_quit.IsIn(mouseX,mouseY)){

     Button btn_settings(screen->w/2,screen->h/2+70,0,0);
     btn_settings.Render(menu,screen);

 }

SDL_Quit works fine but i can’t seem to get the if statement after the case statement to return true when i hover the mouse over the btn_quit button. Any ideas why this might be ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T20:17:33+00:00Added an answer on June 13, 2026 at 8:17 pm

    Because btn_quit has no width or height, and therefore you can never be inside it’s bounds.

    Button btn_quit(screen->w/2,screen->h/2,0,0);
    

    You checks will fail because your mouse position can never be >x && <x+0 or >y && <y+0.

    Perhaps a better way would be to define the location of your button, and let get the dimensions from the loaded image?

    class Button
    {
      public:
        // Construct the button with an image name, and position on screen.
        // This is important, your existing code loaded the image each render.
        // That is not necessary, load it once when you construct the class.
        Button(std::string name, int x, int y) : img(IMG_Load(name)) 
        {
          if(img) // If the image didn't load, NULL is returned.
          {
            // Set the dimensions using the image width and height.
            offset.x = x; offset.y = y;
            offset.w = img.w; offset.h = img.h;
          }
          else { throw; } // Handle the fact that the image didn't load somehow.
        }     
        ~Button() { SDL_FreeSurface(img); } // Make sure you free your resources!
        void Render(SDL_Surface *destination)
        {
          // Simplified render call.
          SDL_BlitSurface(img, NULL, destination, &offset );
        }
        bool Contains(int x, int y)
        {
          if((x>offset.x) && (x<offset.x+offset.w)
          && (y>offset.y) && (y<offset.y+offset.h))
          return true; // If your button contains this point.
          return false;  // Otherwise false.
        }
      private:
        SDL_Rect offset; // Dimensions of the button.
        SDL_Surface* img; // The button image.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple jQuery script.. all I want it to do is
I have two simple script that enables/disables Cisco AnyConnect when I don't want it
I have a simple load more style script that works fine on the index
I have simple script with EventMachine, Fibers and faye require faye require em-synchrony require
I have a simple script of code that reads a PHP file and when
I have a simple script to check how many files a user has uploaded:
I have this simple script that I'm working on. I must admit, I'm totally
I have a simple script, which tries to kill an already running process. I
I have a simple script that does some search and replace. This is basically
I have a simple script that pulls about 100 million results, 1 million at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.