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

Author Topic: [SOLVED]Efficient ways to draw large grids?  (Read 5631 times)

0 Members and 2 Guests are viewing this topic.

Elias Daler

  • Hero Member
  • *****
  • Posts: 599
    • View Profile
    • Blog
    • Email
[SOLVED]Efficient ways to draw large grids?
« on: May 25, 2017, 03:37:38 pm »
I have a level editor in my game and potentially maps may be pretty large. I want to draw grid overlay which shows borders for each tile. I realize that I must use VertexArray for grids, but as the map size gets large (e.g. more than 200x200 tiles), the size of VertexArray grows fast. And it doesn't make much sense to draw the entire grid when I only see some region of the map, so I would like to draw only the grid for tiles currently on the screen.

Any tips? :)
« Last Edit: May 26, 2017, 11:04:33 am by Elias Daler »
Tomb Painter, Re:creation dev (abandoned, doing other things) | edw.is | @EliasDaler

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Efficient ways to draw large grids?
« Reply #1 on: May 25, 2017, 05:02:28 pm »
Resize and recreate the vertex array for the borders that are in view.

How you decide which borders to draw can be the same decision for deciding which map cells to draw in a gridlike map.

e.g. If your map is showing (20,20) to (40,40) then only draw the borders in that range.

Which type of border? Single lines per edge? Use sf::PrimitiveType::Lines. Thick edges? Use sf::PrimitiveType::Quads or sf::PrimitiveType::Triangles.

I hope I understood correctly what you were asking.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Efficient ways to draw large grids?
« Reply #2 on: May 25, 2017, 07:03:47 pm »
Why don't you just draw a grid which has the same size as the window? You just have to apply a small offset to align it on your actual tiles, depending on the current view.
Laurent Gomila - SFML developer

Elias Daler

  • Hero Member
  • *****
  • Posts: 599
    • View Profile
    • Blog
    • Email
Re: Efficient ways to draw large grids?
« Reply #3 on: May 25, 2017, 09:58:52 pm »
The problem is that I can zoom in and out, so the number of cells I have to draw will change.
But thinking about it and what Hapax said (I forgot about sf::PrimitiveType::Lines, thanks!)... My monitor is 1920x1080. When each cell is only one pixel, only 810x590 cells will fit on the screen. If I wanted to draw them all, I'd have to store 2 * 810 + 2 * 590 = 2800 vertices, which isn't that bad! I'll see if drawing that many vertices is okay in every frame (don't want to reallocate arrays when zoom level changes).

But I think if I'll notice slowdowns, I can just have two arrays: for horizontal and for vertical lines. So when I only need to draw 30x40 grid, I can just draw the grid using part of preallocated array of maximum allowed size. :D
Tomb Painter, Re:creation dev (abandoned, doing other things) | edw.is | @EliasDaler

Elias Daler

  • Hero Member
  • *****
  • Posts: 599
    • View Profile
    • Blog
    • Email
Re: Efficient ways to draw large grids?
« Reply #4 on: May 26, 2017, 11:04:13 am »


Seems like 500x500 grid is enough to draw grids on 1920x1080 monitor (the second picture is a part of 1920x1080 screenshot). At some zoom value I just don't draw the grid as it doesn't make much sense.

Thanks for suggestions! (Especially the one about moving the grid with camera, I didn't think about that. I'm transforming sf::RenderStates, not moving the grid, of course)
Tomb Painter, Re:creation dev (abandoned, doing other things) | edw.is | @EliasDaler

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: [SOLVED]Efficient ways to draw large grids?
« Reply #5 on: May 28, 2017, 02:05:14 am »
Drawing the horizontal lines and the vertical lines separately will only result in an extra draw call. The same area is covered by both calls and the same number of vertices are still used. I'd say that all in one would be the better option.

If you're worried about reallocations, use a vector of vertices instead of a vertex array:
std::vector<sf::Vertex> vertices;
That way, you can reserve the size you need in advance. You can reserve and also "shrink to fit" (C++11) as needed.

Also, remember that the x position of the vertices for the horizontal lines can always be the edges of the window and similar for y position of vertical lines since your grid is constant. (originally, I thought you wanted each cell have the option of having its borders drawn)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*