I'm trying to run a small piece of code, just to get familier with VertexArray:
#include <iostream>
#include <SFML/Graphics.hpp>
using namespace std;
#define CELL_SIZE 32
void fillVertexArray(sf::VertexArray& va, int num_of_squares_x, int num_of_squares_y){
for (int i = 0; i < num_of_squares_y; i++){
for (int j = 0; j < num_of_squares_x; j++){
va.append(sf::Vector2f((float)j * CELL_SIZE, (float)i * CELL_SIZE));
va.append(sf::Vector2f((float)(j + 1) * CELL_SIZE, (float)i));
va.append(sf::Vector2f((float)(j + 1) * CELL_SIZE, (float)(i + 1) * CELL_SIZE));
va.append(sf::Vector2f((float)j, (float)(i + 1) * CELL_SIZE));
}
}
}
void printLogTime(sf::Clock& code_timer, bool reset){
if (reset) code_timer.restart();
else std::cout << code_timer.getElapsedTime().asMilliseconds() << std::endl;
}
int main() {
sf::VertexArray va(sf::PrimitiveType::Quads);
fillVertexArray(va, 32, 32);
sf::RenderWindow window(sf::VideoMode(1440, 810), "SFML test", sf::Style::Default);
float zoom = 1;
sf::View view = window.getDefaultView();
sf::Clock code_timer;
while(true){
sf::Event evnt;
while (window.pollEvent(evnt)){
switch(evnt.type){
case sf::Event::Closed:
return -1;
case sf::Event::MouseWheelScrolled:
// 'zoom()' is by a factor. a number greater than 1 means zoom-out; a number smaller than 1 means zoom-in.
if (evnt.mouseWheelScroll.delta <= -1) { // Scroll down - zoom-out
zoom = std::min(2.0, zoom + 0.1); // By using 'min' with '2', we set it as a lower limit.
}
else if (evnt.mouseWheelScroll.delta >= 1) { // Scroll up - zoom-in
zoom = std::max(0.5, zoom - 0.1); // By using 'max' with '0.5', we set it as an upper limit.
}
// We use 'setSize()' here to reset our view (by setting it to the default view's size).
// Why? Because, as we've said, 'zoom()' is by a factor. So if we zoomed twice we'd be multiplying instead of adding.
// For that we reset the view and then apply the zoom on it.
view.setSize(window.getDefaultView().getSize()); // Reset the size
view.zoom(zoom);
window.setView(view);
break;
}
}
window.clear();
window.draw(va);
printLogTime(code_timer, true);
window.display();
printLogTime(code_timer, false);
}
return 0;
}
But when logging I get that every window.display() call takes about ~40 milliseconds.
When deleting the 'window.draw(va)' line, it drops down to 0 milliseconds (rounding).
For comparison, in another project where there's a LOT of shapes on screen, it's also about 0 milliseconds, so I don't understand the sudden spike here, considering VertexArray should be very efficient