2016-12-26 3 views
0

SFMLおよびC++の新機能です。 私は学校プロジェクトのためにConwayの人生ゲームを構築しています。SFML - ウィンドウ内のオブジェクトを変更できません

私のグリッドのベースオブジェクトとして、私はスクエアを持っています(ベースはsf::RectangleShapeです)。 グリッドはstd::vector<std::vector<Square>>(ベクトルの2乗のベクトル)です。

私が現時点でやっているのは、カーソルでカーソルを移動すると、正方形の色を変えることです。 それを行うための私の方法(以下のコードを参照してください):

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

コリジョンロジックが動作します。initializegrid()とinitializeWindowGridValues()を表示できますか? –

+0

@LorenceHernandez私はmain()全体を追加しました。私は 'mouseover()'の実装を変更することに注意してください:私は、ベクトルの繰り返しではなく、行列の正方形のインデックスを計算します。 'initializeWindowGridValues()'は、すべてのグリッドを表示するのに十分な大きさのウィンドウを作成し、極端な値の場合に最大サイズと最小サイズを設定します。 'initializegrid()'は、自ベクトルのベクトルを四角で適切な位置の値で埋めます。 –

答えて

0

問題は、私は両方のRectangleShapeを継承した(私の四角がRectangleShape IS)とRectangleShape(四角)はMyClassのメンバーをしたということでした。私がデータを変更するとき、私はクラスメンバ変数を変更しますが、描画メソッドの中で私は他の矩形を描画します。

クラスメンバRectangleShapeを削除し、ここから続行します。

関連する問題