WavyWaves Screen Saver with libCinder and C++11


After a looong time without blog posts, I’m dedicating this one to the release of the WavyWaves Screen Saver (binary and sources).

You can “preview” it on HTML5 version or watching this video.

I know that it is more of the same, but using different technologies. After creating the FlockDraw application with libCinder, I decided to refactor the old minified JavaScript version to C++ using OpenGL with libCinder. Also I used some C++11 and boost features.

After all  this post is intended to say more about the C++11 and boost’s  features than about the already-explained-old-version-of-WavyWaves.

As a programming junkie I got astonished by the productivity delivered by both. In WavyWaves lots of pointers to functions are used to control the movement of the particles, and everyone knows that the old fashioned way of creating dozens of functions and pointing them is a little bit boring, and then we have lambda functions!

The particles are simple points in space, with a directional vector for speed. The speed is calculated using an input, in this case related to the position of the particle. The basic example is:

So all my functions would take an float argument and return a float, but some functions must have access to external properties, as the time passed since the application start, the total number of particles or any other thing. In that case I would have to have a kind of variant function, or a function with lots of frequently unused parameters.

The lambda function helps to do that with the capture list, where you can reference external variables as in JavaScript closures.

Consider the following code:

So if we want to use a sin function to update our  particle, we just need to assign the function pointer to the callback variable (this code would be inserted at the class constructor):

But if we want to compose something based on the lifetime of the application? Lambda functions will deal with it:

The callback haves now the the lambda function that acts as method of the calling class (if you have any kind of trouble to understand the syntax, read this article), accessing all the variables of class MyClass.

Would be very boring to code the update functions as function pointers, passing everything as parameters. The lambda function allowed almost a direct translation of the code existing of my JavaScript source to C++ with few efforts.

C++11 have terrific features. Lambda functions are one of many, as the new syntax of “for” statement when using containers, the std::thread classes, “auto” types, function bindings… just to name a few.

The boost library haves lots of stuff, but I haven’t managed to have much experience with it. In my last projects I used the boost::filesystem library which is fantastic!

This lib used some creativity to implement the operators in a totally bizarre way (in conceptual view), but very easy to understand.

For instance, you want to open a file, which you have the name and the directory in different strings:

This kind of library makes the life of any programmer easier and much more enjoyable. It’s sad that lots of entrepreneurial projects do not allow the inclusion of libraries like boost, certainly the productivity of coding teams would improved and the redundant code avoided. If you have a lib that does it well, for free and it is tested, so you should use it!

But this is another point in the curve that may be discussed in a future article.

To download the WavyWaves Screen Saver for Windows, just follow this link.

As the usual, the sources are at GitHub.

You may also like...