【问题标题】:SFML - Cannot modify object in windowSFML - 无法修改窗口中的对象
【发布时间】:2016-12-26 17:38:21
【问题描述】:

总的来说,我是 SFML 和 C++ 的新手。 我正在为一个学校项目构建康威人生游戏。

作为我的网格的基础对象,我有 Squares(作为基础是 sf::RectangleShape)。 网格是std::vector<std::vector<Square>>(Square 向量的向量)。

我现在要做的是在将光标悬停在正方形上时更改正方形的颜色。 我的做法(见下面的代码):

  1. 当我移动光标时,我得到了光标的位置
  2. 我遍历我的矩阵并检查每个正方形是否光标的位置在正方形的边界内。
  3. 找到光标下的方块后,我想改变它的颜色(.setFillColor(...))。

第 3 点不起作用我不明白为什么。这是下面代码中的函数“mouseHover”。 你能看出哪里不对吗?

int main()
{
    int width;
    int height;
    int numberSquareWidth = 15;
    int numberSquareHeight = 15;
    int squareSize = 20;
    int offset = 1;
    initializeWindowGridValues(width, height, numberSquareWidth, numberSquareHeight, squareSize, offset);

    sf::RenderWindow window(sf::VideoMode(width, height), "Stephan's Game Of Life!");
    std::vector<std::vector<Square>> matrix(numberSquareWidth, std::vector<Square>(numberSquareHeight));
    initiatlizeGrid(numberSquareWidth, numberSquareHeight, squareSize, offset, matrix);

    sf::Vector2i cursorPos;
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();

            if (event.type == sf::Event::MouseMoved)
            {
                cursorPos = sf::Mouse::getPosition(window);
            }
        }
        mouseHover(matrix, cursorPos, squareSize, offset); // Problem here

        window.clear(sf::Color::Black);
        drawGrid(window, matrix);
        window.display();
    }
    return 0;
}

void mouseHover(std::vector<std::vector<Square>> &matrix, const sf::Vector2i &cursorPos, const int &squareSize, const int &offset)
{
    int rowIndex = floor((cursorPos.x - (offset * floor(cursorPos.x / squareSize))) / squareSize);
    int colIndex = floor((cursorPos.y - (offset * floor(cursorPos.y / squareSize))) / squareSize);
    matrix[rowIndex][colIndex].setFillColor(sf::Color::Blue); // This does not happen.
}

void drawGrid(sf::RenderWindow &window, const std::vector<std::vector<Square>> &matrix)
{
    for (const auto &vector : matrix)
    {
        for (const auto &square : vector)
        {
            window.draw(square);
        }
    }
}

void initiatlizeGrid(const int &numberSquareWidth, const int &numberSquareHeight, const int &squareSize, const int &offset, std::vector<std::vector<Square>> &matrix)
{
    int previousY = 0;
    for (size_t row = 0; row < numberSquareWidth; row++)
    {
        int offsetv;
        if (row == 0)
        {
            offsetv = 0;
        }
        else
        {
            offsetv = offset;
        }
        int previousX = 0;
        for (size_t col = 0; col < numberSquareHeight; col++)
        {
            int offseth;
            if (col == 0)
            {
                offseth = 0;
            }
            else
            {
                offseth = offset;
            }
            Square square(squareSize);
            square.setPosition(previousX + offseth, previousY + offsetv);
            matrix[row][col] = square;
            previousX = previousX + squareSize + offseth;
        }
        previousY = previousY + squareSize + offsetv;
    }
}

void initializeWindowGridValues(int &width, int &height, int &nuberSquareWidth, int &nuberSquareHeight, int &squareSize, const int &offset)
{
    if (nuberSquareWidth * squareSize + offset * (nuberSquareWidth - 1) > 1800 || nuberSquareHeight * squareSize + offset * (nuberSquareHeight - 1) > 900)
    {
        nuberSquareWidth = floor(1600 / squareSize);
        nuberSquareHeight = floor(900 / squareSize);
        width = ceil(nuberSquareWidth * squareSize + offset * (nuberSquareWidth - 1));
        height = ceil(nuberSquareHeight * squareSize + offset * (nuberSquareHeight - 1));
    }
    else if (nuberSquareWidth < 5 || nuberSquareHeight < 5)
    {
        nuberSquareWidth = 5;
        nuberSquareHeight = 5;
        width = ceil(nuberSquareWidth * squareSize + offset * (nuberSquareWidth - 1));
        height = ceil(nuberSquareHeight * squareSize + offset * (nuberSquareHeight - 1));
    }
    else
    {
        width = ceil(nuberSquareWidth * squareSize + offset * (nuberSquareWidth - 1));
        height = ceil(nuberSquareHeight * squareSize + offset * (nuberSquareHeight - 1));
    }
}

【问题讨论】:

  • 碰撞逻辑对我有效,你能显示initializegrid()和initializeWindowGridValues()
  • @LorenceHernandez 我添加了整个 main()。请注意,我更改了mouseover() 的实现:我计算矩阵中正方形的索引,而不是遍历向量。 initializeWindowGridValues() 确保窗口足够大以显示所有网格,并在出现极端值时为其设置最大和最小尺寸。 initializegrid() 用具有适当位置值的正方形填充我的正方形向量向量。

标签: c++ vector sfml


【解决方案1】:

问题是我既继承了RectangleShape(我的正方形是矩形形状),又使RectangleShape(正方形)成为myclass 的成员。当我修改数据时,我修改了类成员变量,但在 draw 方法中我绘制了另一个矩形。

我删除了班级成员RectangleShape 并从这里继续。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-18
    • 1970-01-01
    • 2013-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多