Engine Overview

Purpose of SHOW_DEBUG_HELPERS

What's the purpose of the SHOW_DEBUG_HELPERS definition in Globals.h?

Abstract art representing computer programming

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:

  1. Easier debugging: We can include additional error checking and logging in debug builds.
  2. Performance optimization: We can remove these checks in release builds for better performance.
  3. 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.

This Question is from the Lesson:

Engine Overview

An introduction to the generic engine classes we'll use to create the game

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Engine Overview

An introduction to the generic engine classes we'll use to create the game

3D art representing computer programming
Part of the course:

Building Minesweeper with C++ and SDL2

Apply what we learned to build an interactive, portfolio-ready capstone project using C++ and the SDL2 library

Free, unlimited access

This course includes:

  • 37 Lessons
  • 100+ Code Samples
  • 92% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved