According to the documentation, it does not deallocate the memory, so I don't think this can be problematic.
Right, I forgot that clear() on a
std::vector doesn't deallocate memory.
The following code gives me ~1875 FPS in release mode and 1700 FPS in debug mode. Again if you don't change the vertices, you can construct the vertex array in the beginning.
#include <SFML/Graphics.hpp>
#include <sstream>
#include <iostream>
class FPS
{
private:
unsigned int Frame;
unsigned int Fps;
sf::Clock FpsClock;
public:
FPS();
void update();
const unsigned int getFPS();
};
//==================================
FPS::FPS()
{
Frame = 0;
Fps = 0;
FpsClock.restart();
}
//==================================
void FPS::update()
{
if (FpsClock.getElapsedTime().asSeconds() >= 1)
{
Fps = Frame;
Frame = 0;
FpsClock.restart();
}
Frame++;
}
//==================================
const unsigned int FPS::getFPS()
{
return Fps;
}
//==================================
int main()
{
sf::RenderWindow MainWindow;
sf::ContextSettings ContextSetter(0,0,0);
const int DEFAULT_SCREEN_WIDTH = 800;
const int DEFAULT_SCREEN_HEIGHT = 600;
MainWindow.create(sf::VideoMode(DEFAULT_SCREEN_WIDTH, DEFAULT_SCREEN_HEIGHT, 32), "Primitive Drawing Test", sf::Style::Default, ContextSetter);
sf::Event Event;
const int CELL_SIZE = 10;
int NumOfHorizontalCells = DEFAULT_SCREEN_WIDTH / CELL_SIZE;
int NumOfVerticalCells = DEFAULT_SCREEN_HEIGHT / CELL_SIZE;
sf::VertexArray VertArray(sf::Quads, 4*NumOfHorizontalCells*NumOfVerticalCells);
FPS Fps;
bool Quit = false;
while (Quit != true)
{
Fps.update();
std::stringstream ss;
ss << Fps.getFPS();
MainWindow.setTitle("Primitive Drawing Test -- FPS: " + ss.str());
while (MainWindow.pollEvent(Event))
{
if (Event.type == sf::Event::Closed)
{
Quit = true;
}
}
for (int y = 0; y < NumOfVerticalCells; ++y)
for (int x = 0; x < NumOfHorizontalCells; ++x)
{
const float xLoc = static_cast<float>(x * CELL_SIZE);
const float yLoc = static_cast<float>(y * CELL_SIZE);
// top left
VertArray[4*((y*NumOfHorizontalCells)+x)].position = sf::Vector2f(xLoc, yLoc);
VertArray[4*((y*NumOfHorizontalCells)+x)].color = sf::Color::Black;
// top right
VertArray[4*((y*NumOfHorizontalCells)+x)+1].position = sf::Vector2f(xLoc + CELL_SIZE, yLoc);
VertArray[4*((y*NumOfHorizontalCells)+x)+1].color = sf::Color::Black;
// bottom right
VertArray[4*((y*NumOfHorizontalCells)+x)+2].position = sf::Vector2f(xLoc + CELL_SIZE, yLoc + CELL_SIZE);
VertArray[4*((y*NumOfHorizontalCells)+x)+2].color = sf::Color::Black;
// bottom left
VertArray[4*((y*NumOfHorizontalCells)+x)+3].position = sf::Vector2f(xLoc, yLoc + CELL_SIZE);
VertArray[4*((y*NumOfHorizontalCells)+x)+3].color = sf::Color::Black;
}
MainWindow.clear(sf::Color(60, 150, 90));
MainWindow.draw(VertArray);
MainWindow.display();
}
}
With the code here, you'll create the initial array with the wanted size and every frame, iterate over the grid and set the position and the color.
If you'd now want to increase the size, you could do that separately by simply calling resize and set the new values for the column/row count. Also since the position of the vertices only changes with the grid size, you don't have to reset the position every frame, but rather just on grid size change.
First of all, thanks for the response, that makes a good deal of sense. Part of the reason I didn't keep a stored VertexArray with all the points on it is that the very array wasn't a fixed size, the program had an 'infinite grid', which was really just a series of 800x600 boards (well, I suppose 880 x 660 with the spacing) that could be allocated / deallocated anywhere needed.
You still shouldn't be recreating the whole vertex array every time, but rather use what you have and change it once the grid is supposed to change size. Constantly recreating everything, just because it
may get increased is not very good for the performance.