r/sfml • • 14d ago

Expecting primary expression error

I keep getting the error error: expected primary expression before 'mouse' and error: expected primary expression before 'rect'

Can someone assist me on this because I am lost

CONTEXT

I made a boolean function that detects if a point is colliding with a rectangle and in my main function when button is pressed and mouse is colliding with rectangle than the console needs to output string

bool pointRectCollision(Mouse mouse, RectangleShape rect){
      if(mouse.getPosition().x > int(rect.getPosition().x) && mouse.getPosition.x() < int(rect.getPosition().x + rect.getSize().x) && mouse.getPosition().y > int(rect.getPosition().y) && mouse.getPosition.y() < int(rect.getPosition().y + rect.getSize().y)){
           return true;
      }

      else {
          return false;
       }

}

int main(){

  // GAME LOOP
  if (mouse.isButtonPressed(Button::Left) && pointRectCollision(mouse, pauseBtn)){
        cout << "COLLISION" << endl;
    }


  return 0;
}

UPDATE

I was able to fix the problem. I had to fix the syntax of how I was declaring mouse and also nest my conditional statement for detecting collision.

// function to detect point-rectangle collision
bool pointRectCollision(Vector2i mousePos, RectangleShape &rect){ 
  if(mousePos.x > int(rect.getPosition().x) && mousePos.x < int(rect.getPosition().x + rect.getSize().x) && mousePos.y > int(rect.getPosition().y) && mousePos.y < int(rect.getPosition().y + rect.getSize().y)){
      return true;
}

  else {
    return false;
  }

}

int main(){
  // GAME LOOP

  // MOUSE
  if (Mouse::isButtonPressed(Mouse::Button::Left)){
    if(pointRectCollision(Mouse::getPosition(window), pauseBtn)){
      cout << "collision" << endl;
    }

   }

}
3 Upvotes

6 comments sorted by

1

u/thedaian 14d ago

Is the error on the line with the  pointRectCollision(

If so, you're probably declaring it before you have using namespace sf;

Which is another reason to avoid using namespace sf; and just typing out sf::

1

u/TheEyebal 14d ago

I updated the code but right now it is not detecting the collision. It seems like my mouse is detecting my entire computer resolution and not the coordinates within the window

https://github.com/mikkimat81539/Do_It_Fast/blob/testBranch/src/pause.cpp

3

u/thedaian 14d ago

You need to pass the window object into the get position function to get the coordinates relative to the window: https://www.sfml-dev.org/documentation/3.1.0/namespacesf_1_1Mouse.html#a88e3e03774b60576ec48a2301f4f57f9

Since it's a function, I'd just pass the coordinates in as a vector2i instead of adding the window as a parameter to your function. Or switch to using events for this.

2

u/TheEyebal 14d ago edited 14d ago

oh that worked

if (Mouse::isButtonPressed(Mouse::Button::Left)){
    if(pointRectCollision(Mouse::getPosition(window), pauseBtn)){
        cout << "collision" << endl;
    }
}

1

u/landmvx 14d ago

Another Option ist to use the Rects contains method, to detect if the point is within the rectangle: https://www.sfml-dev.org/documentation/3.1.0/classsf_1_1Rect.html

1

u/TheEyebal 13d ago

Ok I will look at that. I am new at SFML and was trying to find something that contains the button