0
\$\begingroup\$

I'm trying to create a clickable object. I wrote a pseudo code to try it. When I add a new member to object it works fine, that is when I hold the mouse cursor on the rectangle determined it returns true even it's moved around. But once the rectangle moves out of the visible area and returns back to the visible area, the if statement return false when I hold the cursor on the rectangle determined(I'm using an int variable for camera effect.). I'm using SDL 2.0.3.

This is the loop:

multiset<IDrawable*, Drawable_compare> objects;

for(auto i = objects.begin(); i != objects.end(); ++i)
{
  if((*i)->Collision(*MouseX, *MouseY) == true)
  {
    (*i)->Update();
    cout<<"true"<<endl;
  }
  else
  {
    cout<<"false"<<endl;
  }
}

These are the base and derived classes:

class IDrawable
{
public:
  IDrawable();
  virtual ~IDrawable(void) = 0;

  virtual bool Collision(int Mouse_X, int Mouse_Y) = 0;
};

.

class Object1: public IDrawable
{
public:

  Sprite *Object1 = new Sprite( 
    sdl_set->GetRenderer(),
    "data/Trees/Tree1.png", 
    X_pos, Y_pos, 
    100, 200, 
    CamX, CamY, 
    SelectionRect(0, 0, 100, 200) );

  bool Collision(int Mouse_X, int Mouse_Y)
  {
    return (Object1->MouseOver(&Mouse_X, &Mouse_Y));
  }
};

This is the sprite class:

class Sprite
{
public:

  void CSprite::Update()
  {
    SelectionRectangle.SetX(rect1.x + *CamX);
    SelectionRectangle.SetY(rect1.y + *CamY);
  }

  bool CSprite::MouseOver(int *MouseX, int *MouseY)
  {
    return !(
       ( (SelectionRectangle.GetRectangle().x + SelectionRectangle.GetRectangle().w ) 
          < *MouseX ) 
    || ( *MouseX < SelectionRectangle.GetRectangle().x )
    || ( ( SelectionRectangle.GetRectangle().y + SelectionRectangle.GetRectangle().h ) 
          < *MouseY )
    || ( *MouseY < SelectionRectangle.GetRectangle().y) );
  }
};

This is the class to determine a selectable area:

class SelectionRect
{
public:

  SelectionRect(int x, int y, int w, int h);
  ~SelectionRect(void);

  SDL_Rect GetRectangle() { return SelectRect; }

  void SetX(int x) { SelectRect.x = x + OffsetX; }  
  void SetY(int y) { SelectRect.y = y + OffsetY; }

  void SelectionRect::SetRectangle(int x, int y, int w, int h);

};

What might be causing this? How can I correct my code?

Thanks.

Edit : I did some trial and error work and found out that when I use the alternative MouseOver() function no problem occurs. What do you think the reason is?

My Draw() function for objects:

void CSprite::Draw()
{
    Camera1.x = rect1.x + *CamX;
    Camera1.y = rect1.y + *CamY;

    SDL_RenderCopy(renderer,image, NULL, &Camera1);

}

Alternative MouseOver function:

bool CSprite::MouseOver(int *MouseX, int *MouseY)
{
        cout<<"Called"<<endl;

        return !(Camera1.x + Camera1.w < *MouseX || *MouseX < Camera1.x || Camera1.y + Camera1.h < *MouseY || *MouseY < Camera1.y);
    }
\$\endgroup\$
  • \$\begingroup\$ Is the object still in the container of drawable objects? Can you verify that it is still being iterated over and the Collision member function is being called? I wonder if the object leaving the screen causes some logic somewhere else to prevent the object from being added to the drawables container. \$\endgroup\$ – Connor Hollis Aug 11 '15 at 21:56
  • \$\begingroup\$ Object still exists and the Collision is called but it returns false. The object is rendered as I said, but when it returns to the focus window the Collision returns false. \$\endgroup\$ – bfkjohns Aug 12 '15 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.