September 01, 2019

Faster vertex paint with SFML

In a previous blogpost it was explained, that SFML is not fast enough for drawing 2000 objects at the same time to the screen. After consulting some online forums the identified bottleneck is the drawing routine of SFML. It can be improved by using a so called vertexarray. The idea is first to store the particles into an array, and then blit the complete array to the screen. In the example screenshot, around 2000 robots are painted at the same time and the amount of cpu consumption is at 5.6 % which is much better than the 45% in the previous example.





According to the documentation a vertex is equal to a single point on the screen. For drawing a larger triangle, three dots are needed. The creating of new triangle is located in the add() function within the sourcecode. First, all the triangles are created, and secondly the entire array is painted to the screen. This seems to be a very fast alternative over drawing each primitive by it's own.

Let us try out, if the technique scales up to larger amount of robots. Suppose the swarm contains of 10000 robots which are displayed at the same time with 40 fps. On a standard notebook the simulation needs only 15% of the CPU ressources. It seems, that the idea of using a vertex array is suitable for large amount of objects on the screen.



Perhaps one word about the update method for the robots. A naive approach would be to put every robot in a separate thread. Since the advent of node.js and non-blocking event-driven programming it's known, that creating thousands of threads in a single program is not the best idea. So I've decided to imitate the idea of node.js and there is only one thread which is the game loop. And each robot is updated with event driven programming. The event is that the robot has reached a goal. Then a new goal is defined. This allows to move all the robots separate but only one thread is needed.

At the end a screenshot of a normal situation which is taken place in most Real time strategy games. On the screen 200 robots are shown which are moving with smooth 40 fps. The overall cpu consumption is 3%, which means, that the application can run 24/7 in the background. What can't be visualized in a screenshot is, that all the robots in the screens are moving. That means there is a lot of action going on.