Purpose of SHOW_DEBUG_HELPERS
What's the purpose of the SHOW_DEBUG_HELPERS
definition in Globals.h
?
The SHOW_DEBUG_HELPERS
definition in Globals.h
is a debugging tool that allows us to include or exclude certain code sections based on whether we're in debug or release mode. This is a common practice in game development and software engineering in general.
Here's how it works. First, we define SHOW_DEBUG_HELPERS
at the top of our Globals.h
file:
#define SHOW_DEBUG_HELPERS
Then, we use preprocessor directives to include or exclude code based on this definition:
#ifdef SHOW_DEBUG_HELPERS
inline void
CheckSDLError(const std::string &Msg) {
const char *error = SDL_GetError();
if (*error != '\0') {
std::cerr << Msg << " Error: " << error
<< '\n';
SDL_ClearError();
}
}
#endif
In this example, the CheckSDLError()
function is only defined when SHOW_DEBUG_HELPERS
is defined. This function helps us catch and print SDL errors during development.
We can use this helper function in our code like this:
#include <SDL.h>
#include "Globals.h"
int main() {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
#ifdef SHOW_DEBUG_HELPERS
Utils::CheckSDLError("SDL_Init");
#endif
return 1;
}
// Rest of the code...
}
The highlighted line will only be compiled when SHOW_DEBUG_HELPERS
is defined.
The main benefits of using SHOW_DEBUG_HELPERS
are:
- Easier debugging: We can include additional error checking and logging in debug builds.
- Performance optimization: We can remove these checks in release builds for better performance.
- Code cleanliness: We can keep debugging code in our source files without cluttering release builds.
To disable debug helpers, we simply comment out or remove the #define SHOW_DEBUG_HELPERS
line in Globals.h
. This is typically done when preparing a release build of the game.
Remember, while debugging helpers are invaluable during development, they should be used judiciously to avoid impacting the game's performance in release builds.
Engine Overview
An introduction to the generic engine classes we'll use to create the game