首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >操纵sfml顶点阵列

操纵sfml顶点阵列
EN

Stack Overflow用户
提问于 2017-09-10 22:07:57
回答 1查看 820关注 0票数 0

在本教程中,我正在对sfml顶点数组functions.Based进行研究,我已经介绍了一个基本实现,并希望将其添加到其中。不幸的是,我在面向对象程序设计( OOP )方面相对较新,希望得到任何帮助。输出使用sprite网格生成类似棋盘的模式。

我的目标是连接网格地砖,使用路径查找算法(递归杆菌追踪器)生成路径。

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

代码语言:javascript
复制
//load the texture for our background vertex array
Texture textureBackground;
textureBackground.loadFromFile("graphics/background_sheet.png");

一旦进入游戏循环,如下所示:

代码语言:javascript
复制
                //pass the vertex array by reference to the createBackground function
            int tileSize = createBackground(background, arena);

最后在抽奖现场:

代码语言:javascript
复制
        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;
}
EN

回答 1

Stack Overflow用户

发布于 2017-09-25 14:49:29

据我所见,你正在飞来飞去地生成你的瓷砖。如果您想要创建类似于可步行空间的东西,您应该首先生成您的平铺地图,然后根据生成的内容绘制它。

也许过多地解决了您的问题,有几种方法可以让generate random maps满足特定的约束条件。

当你有了选择的时候,你就可以像你做的那样画画,而不是

代码语言:javascript
复制
// Use a random floor texture
            srand((int)time(0) + h * w - h);
            int mOrG = (rand() % TILE_TYPES);
            int verticalOffset = mOrG * TILE_SIZE;

应该有这样的东西

代码语言:javascript
复制
// 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传递给您的呈现方法,或者更好的是创建一个重写TileMap方法的draw()类。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46146002

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档