Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: RectangleShape.getPosition always returns 0 for value  (Read 1935 times)

0 Members and 4 Guests are viewing this topic.

Merlin_Foffi

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
RectangleShape.getPosition always returns 0 for value
« on: February 03, 2024, 10:39:15 pm »
I wrote this struct to handle menus for a title screen, but anytime i call the hover function it never worked, and when i added the test prints to the console, it always printed 0 for the Box Pos X and Box Pos Y
and i don't know why. I thought about call by value mistakes but this is not even a passed object, it's for the struct itself. Also when i call .move() or reset the position inside of the hover function it still reports a 0,
but visibly changes the box when the windows get redrawn. Thanks in advance

struct Menu
{
    sf::Text displayName;
    MenuType type;
    sf::RectangleShape box;

    bool isHovered(sf::Vector2i mPos)
    {
        printf("Mouse Pos X: %d \n", mPos.x);
        printf("Mouse Pos Y: %d \n", mPos.y);
        printf("Box Pos X: %d \n", box.getPosition().x);
        printf("Box Pos Y: %d \n", box.getPosition().y);
        // check if mouse.x is in range of box
        bool xIn = mPos.x >= box.getPosition().x && mPos.x <= box.getPosition().x + box.getSize().x;
       
        // check if mouse.y is in range of box
        bool yIn = mPos.y >= box.getPosition().y && mPos.y <= box.getPosition().y + box.getSize().y;

        // only true if both are true
        return xIn && yIn;
    }
};
 

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: RectangleShape.getPosition always returns 0 for value
« Reply #1 on: February 04, 2024, 01:03:56 pm »
There is no code here that actually alters "box" so it's not clear as to if there's an issue elsewhere.

Could printf be an issue here?

Consider adding brackets to your bool assignments as it's not perfectly clear the intention of the code.

SFML has in-built rectangle testing:
bool isIn = box.getGlobalBounds().contains(mPos);

I notice that mPos is integers so it's likely that this is a pixel location (mouse, probably?). Remember to convert this position to the view before using in world co-ordinates:
sf::Vector2f position(window.mapPixelToCoords(mPos));
« Last Edit: February 04, 2024, 01:07:37 pm by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Merlin_Foffi

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: RectangleShape.getPosition always returns 0 for value
« Reply #2 on: February 04, 2024, 01:38:31 pm »
There is no code here that actually alters "box" so it's not clear as to if there's an issue elsewhere.

Could printf be an issue here?

Consider adding brackets to your bool assignments as it's not perfectly clear the intention of the code.

SFML has in-built rectangle testing:
bool isIn = box.getGlobalBounds().contains(mPos);

I notice that mPos is integers so it's likely that this is a pixel location (mouse, probably?). Remember to convert this position to the view before using in world co-ordinates:
sf::Vector2f position(window.mapPixelToCoords(mPos));

The code the alters the box's position would be that:

void TITLE::addMenu(const char* displayName, MenuType menuType)
{
    sf::Font font;
    font.loadFromFile(DEFAULT_FONT);

    // create and set up new menu
    Menu* menu = new Menu();

    // set type
    menu->type = menuType;  

    // set up display name
    menu->displayName.setString(displayName);
    menu->displayName.setFont(font);
    menu->displayName.setCharacterSize(12);
    menu->displayName.setFillColor(HIGHLIGHT_COLOR);

    // set up box to be next to title
    menu->box.setFillColor(DEFAULT_COLOR);
    menu->box.setSize(sf::Vector2f(150, 75));
    menu->box.setPosition(0, 0);

    // only set Menu if it doesn&#39;t exist
    if(!containsMenu(menuType))
    {
        for(int i = 0; i < MenuTypeCount; i++)
        {
            if(menus[i] == nullptr)
            {
                menu->box.move(600, 100 + (110 * (i)));    // set box&#39;s position + offset
                menus[i] = menu;
                break;
            }
        }
    }
}
 

also, i did the changes you pointed out. I tested it and the conversion didn't change the value of the coords.
Thank you tho for pointing out the printf();. I used std::cout and it did correctly report the coords of the boxes,
but for my mouse positions i had:
- Mouse X: 0
- Mouse Y: 2100

with conversion and without.
Also, all this is on a Mac.. i forgot to point that out before.
I'm not sure if there are extra settings for the window i have to set up if i'm on a Mac, but it seems odd to me that it reports the mouse position as (0, 2100) in the up left corner, which normally would be (0, 0)

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: RectangleShape.getPosition always returns 0 for value
« Reply #3 on: February 04, 2024, 09:25:20 pm »
It's not the issue you are asking about, but in your addMenu function you are creating a temporary font variable, loading a font with it and setting the font on the text object.
The issue is setFont doesn't store the font in the text object, it just stores a pointer to the font. The font itself must be kept alive, otherwise the result is undefined behavior.

Merlin_Foffi

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: RectangleShape.getPosition always returns 0 for value
« Reply #4 on: February 04, 2024, 09:47:17 pm »
It's not the issue you are asking about, but in your addMenu function you are creating a temporary font variable, loading a font with it and setting the font on the text object.
The issue is setFont doesn't store the font in the text object, it just stores a pointer to the font. The font itself must be kept alive, otherwise the result is undefined behavior.


Thanks for pointing that out. But i did notice that before, and i was planning on inserting a field into the TITLE class that holds the font object as long as the TITLE is alive (which is also as long as the Menus are alive), i just didn't do it yet, because i wanted to fix my highlighting issue first. Still, many thanks for the reminding me again

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: RectangleShape.getPosition always returns 0 for value
« Reply #5 on: February 07, 2024, 04:37:53 pm »
I'm not sure if there are extra settings for the window i have to set up if i'm on a Mac, but it seems odd to me that it reports the mouse position as (0, 2100) in the up left corner, which normally would be (0, 0)

How are you receiving these mouse co-ordinates? Events (check that you're using the right part of the union) or real-time values (remember to add the window as a parameter to make it local co-ordinates).

I mention the local co-ordinates even though I believe all Mac apps are technically full screen (I think... right?) because it might be affected by a multiple monitor set-up.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*