c++ - Manipulating sfml Vertex Array -
i doing research on sfml vertex array functions.based on tutorial i've been introduced basic implementation , wanting add it. unfortunately relatively new @ oop , appreciate adding this. output generates checkerboard pattern using sprite grid.
my goal connect grid-floor tiles using pathfinding algorithm(recursive bactracker) generate path.
the rest of part instantiated in main.cpp:
//load texture our background vertex array texture texturebackground; texturebackground.loadfromfile("graphics/background_sheet.png");
once in game loop as:
//pass vertex array reference createbackground function int tilesize = createbackground(background, arena);
and in draw scene:
window.draw(background, &texturebackground); #include "stdafx.h" #include <sfml/graphics.hpp> #include "zarena.h" int createbackground(vertexarray& rva, intrect arena) { // rva doing background (in main function) // how big 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; // type of primitive using? rva.setprimitivetype(quads); // set size of vertex array rva.resize(worldwidth * worldheight * verts_in_quad); // start @ beginning of vertex array int currentvertex = 0; (int w = 0; w < worldwidth; w++) { (int h = 0; h < worldheight; h++) { // position each vertex in 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 position in texture draw 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 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 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 next vertices currentvertex = currentvertex + verts_in_quad; } } return tile_size; }
Comments
Post a Comment