2017-09-10 17 views
0

私はsfml Vertex Array関数について研究しています。このチュートリアルに基づいて、私は基本的な実装を紹介し、追加したいと思います。残念ながら、私はOOPで比較的新しいので、これを追加する助けを感謝します。 出力はスプライトグリッドを使用してチェッカーボードのようなパターンを生成します。sfml Vertex Arrayを操作する

私の目標は、経路発見アルゴリズム(再帰的なバクトラッカー)を使って格子床タイルを接続してパスを生成することです。

   //pass the vertex array by reference to the createBackground function 
      int tileSize = createBackground(background, arena); 

、最終的にはドローシーンで:

 window.draw(background, &textureBackground); 

#include "stdafx.h" 
#include <SFML/Graphics.hpp> 
#include "zArena.h" 

int createBackground(VertexArray& rVA, IntRect arena) 
{ 
    // Anything we do to rVA we are actually doing to background (in the main function) 

    // How big is each tile/texture 
    const int TILE_SIZE = 50; 
    const int TILE_TYPES = 3; 
    const int VERTS_IN_QUAD = 4; 

    int worldWidth = arena.width/TILE_SIZE; 
    int worldHeight = arena.height/TILE_SIZE; 

    // What type of primitive are we using? 
    rVA.setPrimitiveType(Quads); 

    // Set the size of the vertex array 
    rVA.resize(worldWidth * worldHeight * VERTS_IN_QUAD); 

    // Start at the beginning of the vertex array 
    int currentVertex = 0; 

    for (int w = 0; w < worldWidth; w++) 
    { 
     for (int h = 0; h < worldHeight; h++) 
     { 
      // Position each vertex in the current quad 
      rVA[currentVertex + 0].position = Vector2f(w * TILE_SIZE, h * TILE_SIZE); 
      rVA[currentVertex + 1].position = Vector2f((w * TILE_SIZE) + TILE_SIZE, h * TILE_SIZE); 
      rVA[currentVertex + 2].position = Vector2f((w * TILE_SIZE) + TILE_SIZE, (h * TILE_SIZE) + TILE_SIZE); 
      rVA[currentVertex + 3].position = Vector2f((w * TILE_SIZE), (h * TILE_SIZE) + TILE_SIZE); 

      // Define the position in the Texture to draw for current quad 
      // Either mud, stone, grass or wall 
      //if (h == 0 || h == worldHeight - 1 || w == 0 || w == worldWidth - 1) 
      if ((h % 2 !=0)&& (w % 2 != 0)) 
      { 
       // Use the wall texture 
       rVA[currentVertex + 0].texCoords = Vector2f(0, 0 + TILE_TYPES * TILE_SIZE); 
       rVA[currentVertex + 1].texCoords = Vector2f(TILE_SIZE, 0 + TILE_TYPES * TILE_SIZE); 
       rVA[currentVertex + 2].texCoords = Vector2f(TILE_SIZE, TILE_SIZE + TILE_TYPES * TILE_SIZE); 
       rVA[currentVertex + 3].texCoords = Vector2f(0, TILE_SIZE + TILE_TYPES * TILE_SIZE); 
      } 
      else 
      { 

       // Use a random floor texture 
       srand((int)time(0) + h * w - h); 
       int mOrG = (rand() % TILE_TYPES); 
       int verticalOffset = mOrG * TILE_SIZE; 
       //int verticalOffset = 0; 

       rVA[currentVertex + 0].texCoords = Vector2f(0, 0 + verticalOffset); 
       rVA[currentVertex + 1].texCoords = Vector2f(TILE_SIZE, 0 + verticalOffset); 
       rVA[currentVertex + 2].texCoords = Vector2f(TILE_SIZE, TILE_SIZE + verticalOffset); 
       rVA[currentVertex + 3].texCoords = Vector2f(0, TILE_SIZE + verticalOffset); 

      } 

      // Position ready for the next for vertices 
      currentVertex = currentVertex + VERTS_IN_QUAD; 
     } 
    } 

    return TILE_SIZE; 
} 
一度としてゲームループ内

//load the texture for our background vertex array 
Texture textureBackground; 
textureBackground.loadFromFile("graphics/background_sheet.png"); 

この部分の残りの部分はmain.cppにしてインスタンス化されます

+0

ロジックとレンダリングパーツを混在させないでください。そのため、頂点配列はパスアルゴリズムの仕組みとは関係がありません。 – Lukas

+0

私はこのライブラリには比較的新しいので、私は自分の知る限りでライブラリを使用しています。今私は私のアプローチの代替案を探しています。私の問題は、私がタイルエンジンをある意味で構築しようとしていることですこれらのコンポーネントを満たすために。すべてのポインタが評価されるだろう。 –

答えて

0

私が見ている限り、あなたはタイルを「オンザフライで」生成しています。歩くことのできるスペースのようなものを作成したい場合は、最初にタイルマップを生成し、生成されたコンテンツに基づいて描画する必要があります。

あなたの質問を過剰にしているかもしれませんが、generate random mapsにはいくつかの制約があります。

あなたが選択が行われていた場合、あなたがそうであるように、そしてあなたは、単に描くことができますが、代わりに、このアプローチで

// Select texture rect based on generated tilemap 
      int mOrG = tilemap[w][h]; // Or tilemap[h * worldWidth + w] if you do it as unidimensional array 
      int verticalOffset = mOrG * TILE_SIZE; 

のようなものを持っている必要があり

// Use a random floor texture 
      srand((int)time(0) + h * w - h); 
      int mOrG = (rand() % TILE_TYPES); 
      int verticalOffset = mOrG * TILE_SIZE; 

あなたにタイルマップ渡す必要がありますメソッドをレンダリングするか、それ以上にも、TileMapクラスをオーバーライドして作成するメソッド