SFML - 无法在窗口中修改对象

时间:2016-12-26 17:38:21

标签: c++ vector sfml

我是SFML和C ++的新手。 我正在为学校项目建立康威的生活游戏。

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

我现在要做的是当我用光标悬停在方格上时改变方块的颜色。 我这样做的方式(见下面的代码):

  1. 移动时我得到光标的位置
  2. 我遍历我的矩阵并检查每个方格是否光标的位置在方形边界内。
  3. 当找到光标下的方块时,我想更改其颜色(.setFillColor(...))。
  4. 第3点不起作用我无法理解原因。这是函数&#34; mouseHover&#34;在下面的代码中。 你能看出出了什么问题吗?

    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));
        }
    }
    

1 个答案:

答案 0 :(得分:0)

问题在于我都继承了RectangleShape(我的方形IS A RectangleShape)并使RectangleShape(方形)成为myclass的成员。当我修改数据时,我修改了类成员变量,但在draw方法中我绘制了另一个矩形。

我删除了班级成员RectangleShape并从此处继续。