【问题标题】:Manipulating sfml Vertex Array操作 sfml 顶点数组
【发布时间】:2017-09-10 22:07:57
【问题描述】:

我正在研究 sfml Vertex Array 函数。根据本教程,我已经了解了一个基本实现,我想添加它。不幸的是,我在 OOP 方面相对较新,希望能对此有所帮助。 输出使用精灵网格生成棋盘状图案。

我的目标是使用寻路算法(递归 bactracker)连接网格地板砖以生成路径。

这部分的其余部分在 main.cpp 中实例化:

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

在游戏循环中作为:

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

【问题讨论】:

  • 你不应该混合逻辑和渲染部分。因此,顶点数组应该与路径算法的工作方式无关。
  • 我对这个库比较陌生,因此我尽我所知使用该库。所以现在我正在寻找我的方法的替代方案。我的问题是我正在努力以满足这些组件的方式构建图块引擎。任何指针将不胜感激。

标签: c++ algorithm sfml


【解决方案1】:

据我所知,您正在生成您的图块动态。如果你想创建类似可步行空间的东西,你应该先生成你的瓦片地图,然后根据生成的内容进行绘制。

也许矫枉过正你的问题,有几种方法可以generate random maps满足特定的限制。

当你完成选择后,你可以像你一样简单地画画,但不是

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

应该有类似的东西

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

使用这种方法,您必须将 tilemap 传递给您的渲染方法,或者更好的是,创建一个覆盖 draw() 方法的 TileMap 类

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-12
    • 1970-01-01
    • 2012-08-05
    • 1970-01-01
    • 2011-08-10
    • 1970-01-01
    • 2015-01-08
    相关资源
    最近更新 更多