Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Arrays of vertex arrays?  (Read 3054 times)

0 Members and 1 Guest are viewing this topic.

CppToast

  • Newbie
  • *
  • Posts: 1
    • View Profile
Arrays of vertex arrays?
« 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?
« Last Edit: August 17, 2018, 08:20:49 pm by CppToast »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Arrays of vertex arrays?
« Reply #1 on: August 17, 2018, 08:57:37 pm »
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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

eishiya

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Arrays of vertex arrays?
« Reply #2 on: August 20, 2018, 12:47:52 am »
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.