This is the first of a series of short articles I plan to post here. The posts will come in the form of tips (or reminders) intended for game developers. These tips will hopefully help the reader avoid common frustrating situations when developing a game.
Let’s start with one that haunts me for years!
Graphics context state leaking will make a project very hard to maintain. So find a way to eliminate the leaks at the beginning, when you initially design your engine.
At first this might seem like overengineering, since your first few lines of rendering code will go smoothly without leak management. But during the project’s lifetime there will be lots of things to add and remove here and there. Every time you try to add a new feature in graphics, you are going to break something else. And then the nightmare starts … or the detective story if you will 🙂 Try to solve the mystery of a lost state in a couple of thousands lines of code …
- Create a function SetDefaultGraphicsState() and call it whenever you enter a new function that draws something and/or
- Create a couple of Save/LoadGraphicsState() functions
These should be enough to guarantee a consistent state in your rendering context. You could also design these functions to be smart and check if it’s needed or not to do an API call. For example if they need to disable blending, they could first check if blending was already disabled and if it was, avoid the function call.
But I suspect a good driver might also do this check before communicating with the graphics hardware anyway.