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

Author Topic: Optimization of hexagon-grid  (Read 1954 times)

0 Members and 1 Guest are viewing this topic.

JensTheNewbie

  • Newbie
  • *
  • Posts: 4
    • View Profile
Optimization of hexagon-grid
« on: March 10, 2024, 02:25:27 am »
Hello, I am very new to using SFML so i hope you can have patience with me. At the moment i am trying to make a grid of hexagons so the grrid will look like the one from the old games Fallout 1 and 2.
I was trying to make a 30x30 grid but the framerate cannot get over 60 even though i have set the limit to 600.
I am using for-loops to calculate the positions of each corner and when displaying the grid.
The GPU is barely used which i think is bad since i think it would make my program be faster if i used it.
I was therefore wondering if there is a way i can either optimize how i draw the grid such that if i made the grid to 200x200 wouldn't be a problem either or if there is a way i could use the GPU to make calculations faster.
Parrallel processing is also a way i could increase the speed so if you have some tips or a guide i would be really happy to hear about it.

Below is some of my code. I hope it is displayed correctly. Below is the class of each tile of the grid and how i display it
[
#pragma once
#include <SFML/Graphics.hpp>
#include <vector>
#include <iostream>
class Tile{
private:
   double PI = 3.14159265; // just PI
   
public:// i need to access position, Neighbors, wall or not
   //variables for construction
   double midx = 200; //it was 200 before
   double midy = 200; // it was 200 before
   double r_1 = 15;
   double r_2 = 9;
   double radius;
   double angle = 0;
   double construcangle = 18;
   double sx;
   double sy;
   
   //placement of the tile
   int xRow;
   int yRow;
   double displace=0;

   Tile(int x, int y) {
      xRow = x;
      yRow = y;
     
      // Second row has to be displaced
      if (yRow % 2 == 0) {
         // std::cout << "should be displaced" << std::endl; // to see if displace works
         displace = r_1 * cos(PI * 18 / 180.0);
      }
      //placement of the middle of the hexagon compared to its coordinate in grid
      midx = midx + displace + xRow * 2 * r_1*cos(PI*construcangle / 180);
      midy = midy + yRow * (r_1 * 2 * sin(PI*construcangle / 180) + (r_2 - cos(PI*(90 - construcangle) / 180)*r_1));
   }
   Tile() {}// just for overloading

   void display(int x, int y, int z, sf::RenderWindow& );

};

]

[code=cpp][
#include "Tile.h"
#include <SFML/Graphics.hpp>

void Tile::display(int x, int y, int z, sf::RenderWindow& window) {
   /*
   creates the hexagon
   */

   sf::ConvexShape hexagon;
   hexagon.setPointCount(6);
   hexagon.setFillColor(sf::Color(x, y, z));

   for (int k = 0; k < 6; k++) {
      if (k == 0) {
         angle = PI * construcangle / 180.0;
         radius = r_1;
      }
      else if (k == 1) {
         angle = angle + (PI * (90 - construcangle) / 180);
         radius = r_2;
      }
      else if (k == 2) {
         angle = angle + ((PI * (90 - construcangle)) / 180.0);
         radius = r_1;
      }
      else if (k == 3) {
         angle = angle + (PI * (construcangle * 2) / 180.0);
         radius = r_1;
      }
      else if (k == 4) {
         angle = angle + (PI * (90 - construcangle) / 180.0);
         radius = r_2;
      }
      else if (k == 5) {
         angle = angle + (PI * (90 - construcangle) / 180.0);
         radius = r_1;
      }

      sx = midx + cos(angle) * radius;
      sy = midy + sin(angle) * radius;

      hexagon.setPoint(k, sf::Vector2f(float(sx), float(sy)));
   }
   hexagon.setOutlineColor(sf::Color::Blue); //sets color of outline
   hexagon.setOutlineThickness(1.f); //sets the thickness
   window.draw(hexagon);
}
]

[code=cpp][
for (int i = 0; i < cols; i++) {
      for (int j = 0; j < rows; j++) {
         grid[i][j].display(255, 0, 0, *this->window);
      }
   }
]

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Optimization of hexagon-grid
« Reply #1 on: March 11, 2024, 08:55:30 am »
There are multiple ways you can improve the code.

One simple step would be to not recalculate the shape for every hexagon and each frame.
Assuming the hexagon don't change in size, you'd could calculate the shape points once and then just calculate the necessary translation (movement) to the next position.

You could also store a copy of the ConvexShape in a vector or other container.

And lastly, you could use a VertexArray to handle the vertex data instead of many ConvexShapes, which would cut down on the draw calls overall.

You can find out what best to tackle by using a profiler to see where your bottlenecks are.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Optimization of hexagon-grid
« Reply #2 on: March 12, 2024, 09:41:18 pm »
You should note that setting frame rate limit only caps the highest possible rate so it won't go above that rate. It can't force a slow rate to be quicker, for example.
If you are also using v-sync, it could be limiting to your display's refresh rate. It's generally recommended to only use v-sync or a frame rate limit, not both.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

JensTheNewbie

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Optimization of hexagon-grid
« Reply #3 on: March 13, 2024, 11:16:40 pm »
Thank you for sharing your thoughts. The idea where the shape was saved really helped. I went from having an 45 fps to 245 fps ish so thank you.
However, now i have the problem that when i move the position of the points of the hexagon the framerate drops back to 45 because it is labor intensive.
Is there a way to move a shape without moving each point?
« Last Edit: March 13, 2024, 11:30:15 pm by JensTheNewbie »

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Optimization of hexagon-grid
« Reply #4 on: March 14, 2024, 08:18:52 am »
The points of a shape are relative to the shape's position (0,0 by default). You can move a shape just like a sprite: call setPosition on it.

JensTheNewbie

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Optimization of hexagon-grid
« Reply #5 on: March 26, 2024, 10:52:08 pm »
I have fixed it so thank you very much for the help everyone!