Migrating to SDL2
May 30, 2018
Hey, what's up? My name is Henrik Chukhran aka "Neuton Mouse" and this is my blog. I'm planning to post here about game development, music production, sound design, gaming in general and other related stuff. Most of these posts will deal with my own projects but sometimes there will be exceptions. Feel free to subscribe to my newsletter or follow me on one of the social network pages placed below here.
May 30, 2018
Hello everyone. It’s been nearly a month since my last post. I had material for at least two entries but hesitated to post them because I was in a middle of very intensive game development process. This time I went far beyond simple optimization and decided to completely change multimedia library for performance reasons. I’m happy to inform that it was pretty successful move despite the numerous obstacles encountered on its way.
The very first thought of transferring my game from Allegro 5 to whatever is better came to me right after publishing last Smintheus Beta release. Before jumping into final lap the game had to experience so called transitional stage. That stage was meant to solve performance issues game as well as add couple of minor features and fix itzy-bitzy inconveniences.
It had pretty simple cause – old laptop of mine had some performance spikes at certain moments and that urged me to rethink about optimization. The game was still playable but experience was way too apart from smooth. Thus I started optimization process by eliminating draw calls in the game. Suddenly, all enemy sprites were compressed into one atlas texture. Outcome was little but it was noticeable on old laptop. However, such escapade reached its ultimate conclusion with a realization of Allegro 5 rendering limits.
Allegro did drawing on spot, resulting tons of calls at the end and there were no available options to do batch call. I don’t mean to rant over the allegro library, it’s actually quite good and stable library, but my game grown bigger than expected. Search engine quickly prompted SDL2 as well as SDL_gpu. It didn’t took that much to run simple tests and see the difference - I was shocked to find out that Allegro 5 runs with ~6000 fps while having 800x600 black screen without unnecessary code whereas SDL2/SDL_gpu proudly demonstrated the maximum number of 9999. Both used OpenGL. Seriously, what the hell? Tried to switch rendering to DirectX but for some reason allegro showed even less frames than it would ith OpenGL. Oh well, I didn’t had desire to dive into role of detective and find what’s been bugging it, although frankly speaking I tried, so migration to SDL2/SDL_gpu started without hesitation.
Oh boy… I gotta tell you that took some time to complete. There were a lot of issues and I’m not talking about compiler errors, which were nearly zero, but more of logical/visual ones. Most of them were simply a nuisance and fixing just took time without causing much of headache. But there were those sinister ones that drained me completely. For example, SDL_gpu treated textures with alpha channel quite differently than anything else out there including allegro. First of all, it rendered alpha values differently and secondly, it had and still has some kind of twisted alpha channel discarding meaning the lower alpha is the more it ignores. But the issue was nothing comparing to to the visual trash artifacts it caused on some images. Black magic! I posted the issue on SDL_gpu’s github page but since then… no answers. Unfruitful attempts to solve it lead me to the though of abandoning migration idea. That was really sad…
Fortunately, one of my sandbox sessions ended with a total win! Solution is to create an empty buffer texture, draw problematic alpha texture on top of it and then render buffer. Simply as that, although it doesn’t solve that strange alpha value discarding issue. Latter was solved manually by boosting alpha transparency by 3-6 times of those problematic glow textures.
Next there was this frustration with blending. Multiply blending was really problematic. Default multiply blending preset was not good for Smintheus so I had to adjust it a bit. Tried doing it through shaders by using glsl-blend but eventually found that doing naively is much better. The main problem with was that it rendered textures with different color, more prone to desaturated colors. Strangely, to solve this one needs to draw multiply textures inside the buffer with a cleared color of (255,255,255,0). Going with all zeroes will result different colors. It does sound easy, right? You can’t imagine how much hair I had to pluck to get there :)
Light glows with overlay blending had problems as well. They weren’t vivid enough despite the nearly high alpha density. To deal with it I have to write little chunk of code with tons of magic numbers so that light would imitate those present in Allegro version. Magic numbers were picked in real-time thanks to ImGui library.
Ending of all visual issues was finished with me getting drunk on the day when last ToDo entry was crossed. It felt good, really good. These issues made me mentally exhausted so I had to celebrate such achievement to get back on tracks although usually I don’t allow myself for vacation until certain development phase ends.
Lastly, I had to deal with other troublemakers such as surprisingly different joystick behavior, SDL2 refused to work with my cheap-ass joystick even with that well featured controller database of theirs, inability to load fonts with different width and height, different time calculation, crazy shader rendering order, indexed png crap, strange image targeting shenanigans, app crashes without telling much and tiresome transformation of premultiplied alpha values all over the game code…
And what for you may ask? Well, for the 2x-3x rendering performance of course! That’s actually great result and totally worth it. Game runs smoothly on my prehistoric laptop and it doesn’t have unstable fps even on my rocket-fueled PC. It’s a pure win! I did a test run and found couple of things that needs to be fixed but I wouldn’t count them as serious or unworthy of having full stop over SDL2/SDL_gpu migration...
Before all this started, I had a vacation from a game development. Two of my childhood friends arrived from their business trips for a short moment and we went countryside for a “picnic”. It was brief moment but it surely energized me and inspired to dive into gamedev crunch for next 3 weeks. As always, I find nature to be inspiring and renovating. Most likely I’ll take a vacation trip soon for same purpose, even though game is nearly finished there is still lot of work to do
You can keep in touch with me by subscribing to mailing list. One in a while you'll get one of these:
Merry Christmas everyone! I wrote a new spacesynth track specially dedicated to the new volume of album series called...View post