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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Merlin_Foffi

Pages: [1]
1
Graphics / Re: RectangleShape.getPosition always returns 0 for value
« 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

2
Graphics / Re: RectangleShape.getPosition always returns 0 for value
« 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'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)

3
Graphics / 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;
    }
};
 

Pages: [1]
anything