SFML community forums
Help => Graphics => Topic started by: CppToast on August 17, 2018, 07:17:22 pm
-
I am currently working on a 2.5D engine in C++ using the SFML library. I quickly learnt how to use SFML, but I found no mentions of this anywhere, not in the documentation nor on this forum, so I'm asking here: is it possible to have arrays of vertex arrays (more specifically: quads)? I need this in order to place lots of walls in a map and process them in a loop.
I've already tried this:
VertexArray wall_draw[4](Quads,4);
which when compiled returned this:
main.cpp: In function ‘int main()’:
main.cpp:159:37: warning: expression list treated as compound expression in initializer [-fpermissive]
VertexArray wall_draw[4](Quads,4);
^
main.cpp:159:37: warning: array must be initialized with a brace-enclosed initializer [-fpermissive]
main.cpp:159:37: error: conversion from ‘int’ to non-scalar type ‘sf::VertexArray’ requested
How do I create an array like this? If it's impossible to do it this way, what else can I try?
-
How to create and initialize an array of some type is quite unrelated to SFML and a rather generic C++ problem. You'll find countless answers on StackOverflow and similar.
As for your code, the point of a VertexArray is, to have a collection of vertices that can be drawn all at once. By using multiple vertex arrays, you defeat that point and you'd be better off with just an vector of sprites.
Alternatively you may want to store a pack of four vertices separately and merge them shortly before rendering into one vertex array.
-
The usual way to do what you're trying to do is a single VertexArray, where every set of four vertices is a quad. So, the vertices at index 0~3 are your first quad, the vertices at index 4~7 are your second quad, and so on.
To access the Nth quad within the array, you'd use N*4+i, where i is the index of the corner of the quad that you want (i ranges from 0 to 3). N is multiplied by 4 because there are four vertices per quad. Using this, you could write some helper functions or a wrapper to give you easier reads/writes at a given quad index N.