SDL2 common mistakes and how to avoid them
nullprogram.com
nullprogram.com
> Mistake 2: Including SDL2/SDL.h
The overarching mistake here is not using a proper build toolchain (build system and package manager). I especially take issue with this assertion:
> The correct SDL2 include is the following:
> #include "SDL.h"
Including headers with the library prefix (#include <SDL2/SDL.h>) is our only hope to have an inter-operable ecosystem of C/C++ libraries. Let me illustrate this with a concrete example. What the article suggests is having the header search path (i.e., -I option from sdl2-config) pointing to the location of the SDL.h header (say, /usr/include/SDL2). But there are other headers[1]in there. Thankfully, most of them have the SDL_ prefix (by all means an exception rather than the rule, when it comes to C/C++ libraries), but not all. For example, there is the generically-named begin_code.h header.
Imagine now that your project is using another library with the same arrangement which also contains a bunch of generically-named headers and one of them happened to be called begin_code.h. Now which one gets picked up depends on the order of the -I options that are passed to the compiler.
[1] https://packages.debian.org/stable/amd64/libsdl2-dev/filelis...
Of course, all of this is moot if you don't intend to use a system-provided SDL. If you bundle your own SDL and take care of that with your build system, you can do whatever you want. But if you intend to use a system-provided SDL, and you intend for your software to be somewhat portable, you must use '#include "SDL.h"'.
Now I will agree that it would've been better if SDL decided to make '#include <SDL2/SDL.h>' the canonical include path. And the fact that SDL pollutes your header search space with generically named header is terrible. SDL is not a very good library, it's a "bad citizen". But that's the world we live in. The only ones who can change it are the people behind SDL (and I certainly hope they do change it with a potential future SDL3).
Wow ok. This FAQ[0] is for windows but I guess the same applied for linux.
> A software renderer fallback is exactly the behavior you want!
Apart from the fact that I don't see this situation happening in the context of gaming, if it were to happen, I am not sure that this behavior is always desirable. If your game has a sufficiently complex render loop, a software renderer might be too slow and will generate support calls. You might want to be upfront with the user and fail with a meaningful error message especially if you claim the need of a GPU on your system as a minimal requirement. The user might have a GPU but badly configured on their system for example.
So the gist of this article is that SDL is a framework and if you commit to a framework you might has well commit fully. SDL having an API that goes beyond graphics, you might find the API you need for a particular purpose and not use the system one to keep you software as portable as possible.
The thing is, I see SDL as a way to get a graphical window as cheaply as possible. I don't necessarily see it as a framework. I actually have a deep, and probably irrational, mistrust of frameworks.
[0] https://wiki.libsdl.org/SDL2/FAQWindows#i_get_undefined_refe......
Edit: added a conclusion
If you want anything more than the very basics, there's always Godot or Unity.
There were apparently some efforts to move the wiki from one technology to another which somehow left it in a semi-broken limbo which hasn't been fixed for years.
It seems to be one of those technologies you get mostly taught by the seniors at your studio and they don't need no documentation cause they all got it in their heads.
That said, I haven't had a problem with the wiki being inaccurate, but a lot of things do seem to not be documented there.
Well, I disagree with this assertion, but only partially. You should use either pkg-config or the CMake package definition, depending on how your application is being built. sdl2-config is deprecated and won't be available in SDL3.
> My solution [for generating random numbers] has been to mix event timestamps into the random state: [...]
That's a good, old, and proven technique that is worth highlighting (I would use only the youngest 2-3 bits however).
I wonder though if the author is not shooting themselves in the foot long-term for things like games, where you want to be able to replay the random number stream to hunt down bugs, or for networked game clients that need to run the simulation deterministically and in lockstep.
I've been making all of these mistakes except 1 (using vsync).
My stuff works fine but I'm going to try rewrite my current project how this page suggests.
I think you have a choice to make between writing "a C application" which also uses SDL but probably only runs easily on computers, or writing "an SDL application" which can be easily ported to all weirdo SDL platforms without a complete standard library like maybe Vita or PSP or GP2x or RISC OS or BeOS or something obscure.
Say you're writing a game where you want to decouple the game logic from the render, so your game can be more easily ported to different output formats like ncurses or even a different graphics library like raylib. In that situation I think it would be an error to depend on any SDL library functions like `SDL_calloc`. Or if you did, you'd need to have your own function pointer set for everything you used, like a global ops struct of function pointers with `my_ops->calloc()` (which is how I'm handling cross-render implementation too).
The SDL set is also missing some things, like I need locale support for wchar.
Overall, I don't want to write "SDL C", I want to write C99.
Finally some clarity on:
Mistake 2: Including SDL2/SDL.h
This is like a virus and it turns out I've been consistently 'fixing it' the wrong way..if I have to read the source code for it. what prereq knowledge do I need if I’m from a web dev background trying to build graphical desktop apps with sdl2
But SDL is too low level to consider as a framework for GUI, it's basically a cross platform library for hardware (screen, graphics, input, audio) and event handling. You're still going to have to write everything yourself.
You should at least know C or C++, although there are ports to several languages, and several GUI frameworks like Dear ImGui and I think Pygame use SDL in the background.