简体   繁体   English

在SDL / C ++中将鼠标悬停不起作用

[英]Mouse Over in SDL/C++ not working

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. 但是它不会返回true,所以我似乎无法正常工作。

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: 我正在尝试使用的IsIn函数...在我的主循环中,我有:

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. SDL_Quit可以正常工作,但是当我将鼠标悬停在btn_quit按钮上时,case语句之后的if语句似乎无法返回true。 Any ideas why this might be ? 任何想法为什么会这样?

Because btn_quit has no width or height, and therefore you can never be inside it's bounds. 由于btn_quit没有宽度或高度,因此您永远不能处于其边界内。

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 . 您的检查将失败,因为您的鼠标位置永远不能为>x && <x+0>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.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM