Every now and then I sit down and try out some features of some library, C++ itself or with SFML. Instead of letting those code fragments go to waste somewhere on my disk or get lost in the depth of the SFML forum, I decided to put them openly and centralized here on GitHub for everyone to see, share and learn from them.
Keep in mind that the code is provided as-is and isn't necessarily suited for your final product, as not a lot of thought was put into performance or code design.
zlib is a small and useful library which allows the compression and decompression of data. By applying some settings it's possible to let zlib decompress a gzip file, which this examples demonstrates by loading a file into memory and output the decompressed data to standard output.
One of the most common questions you'll run into on the SFML forum is how to center a sf::Text
object inside a shape, or more commonly, a button. Because sf::Text
aligns itself to the baseline
instead of the visual top/bottom, it tends to be more confusing that working with shapes and
sprites. In order to get it working properly, you have to take into account the LocalBounds
as
is shown in the given example.
This was originally created as a test for this thread (or was it this one?), but since I kind of liked the result I adjusted a few things and uploaded it.
Out of nowhere came suddenly all those requests (starting with this one) for subjective blending and I think, I could help a few people with my example on how to achieve what they wanted.
Although I can't find the original SFML thread about this, I found the StackOverflow question which was IIRC created before the forum thread. This effect can definitely give a nice ambiance, but it probably could need some retouching. Since the picture do not tell much: Starting off with one color the example automatically iterates through the whole RGB color spectrum in a smooth way.
I had once read a tutorial in connection with SFML on how to create some nice 'laser' effect. Unfortunately I don't remember which blog/site this was on, because he was using some texture moving which made everything look even better. While doing a search to possible find that tutorial again, I found another similar piece of code on the forum, adapted it for SFML 2 and made a nicer class out of it.
The other day in #learnprogramming on freenode someone was trying to implement a way to mix two audio files without actually playing them back. I wondered how something like that would work with SFML and thus went ahead an wrote a small example. A quick Google search on audio sample mixing brought me to this site and lead me to the current implementation.
I'm not sure if there ever existed a forum post or if the whole conversation happened on IRC, but someone wanted to make a 'scrolling' road and since I really liked that idea, I went ahead and implemented an example. The code here is particularly not that nice, so keep that in mind when adapting it.
One of the age old questions by game development beginners is along the lines of "How do I rotate
my player to face X?". This short example answers that question, by rotating a triangle
(sf::CircleShape
with three points) to wherever the mouse pointer is on the window.
This example answers the same question as the 'Rotating Triangle' one, but it uses a sf::VertexArray
in combination with a sf::Transformable
. It's a good foundation to see how one could start to
implement an entity class deriving from sf::Transformable
and sf::Drawable
.
Every month there's at least one question on simple collision detection testing the last 'bigger' one with in this forum thread, which lead to this example. I've even implemented some very simple physics, which will fail in most situations, but you can at least move around and jump.
Even though SFML has a tutorial and an example on sound recording, I wanted to have my own small demonstration of said feature with recording device selection and mono/stereo options.
All examples are under the zlib/png license and thus free to use. See the LICENSE file for more details.