2017-09-10 3 views
0

Je fais des recherches sur les fonctions sfml Vertex Array. Basé sur ce tutoriel, j'ai été initié à une implémentation de base et je souhaite y ajouter. Malheureusement, je suis relativement nouveau à l'OOP et j'apprécierais toute aide supplémentaire à cela. La sortie génère un motif de type damier à l'aide d'une grille d'images-objets.Manipulation de sfml Vertex Array

Mon objectif est de connecter les tuiles de plancher de grille en utilisant un algorithme pathfinding (bactracker récursif) pour générer un chemin.

le reste de cette partie est instancié dans le main.cpp:

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

une fois dans la boucle de jeu comme:

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

et enfin sur la scène du tirage:

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

Vous ne devez pas mélanger des parties de logique et de rendu. En tant que tel, les tableaux de vertex n'ont rien à voir avec le fonctionnement de votre algorithme de chemin. – Lukas

+0

Je suis relativement nouveau dans cette bibliothèque et, en tant que tel, j'utilise la bibliothèque au meilleur de ma connaissance. Maintenant, je cherche des alternatives à mon approche. Mon problème est que j'ai du mal à construire le moteur de tuiles. pour satisfaire ces composants. Tous les pointeurs seraient appréciés. –

Répondre

0

Pour autant que je vois, vous générez vos carreaux "à la volée". Si vous voulez créer quelque chose comme un espace piéton, vous devez d'abord générer votre carte de tuiles, puis la dessiner en fonction du contenu généré.

Peut-être surpeuplement votre question, il y a plusieurs façons generate random maps satisfaisant des contraintes spécifiques.

Lorsque vous avez le choix fait, vous pouvez simplement dessiner comme vous le faites, mais au lieu de

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

devrait avoir quelque chose avec cette approche comme

// 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; 

vous devez passer tilemap à votre méthode de rendu ou, mieux encore, création d'une classe TileMap surchargée draw() méthode