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);
}
}
]