ASSERT(), VERIFY() and TRACE() for non-MFC Applications
This article explains how to create your own ASSERT(), VERIFY(), and TRACE() macros for non-MFC applications.
When it comes to game development under C++, few people choose to use MFC. Still, I find the ASSERT(), VERIFY() and TRACE() macros useful. So I thought to write my own versions that work for any kind of project for Windows platforms.
A few reminders:
- ASSERT() is supposed to evaluate its parameter, and if this is zero, to break the execution. In release mode, assert should expand to nothing.
- VERIFY() is very similar to ASSERT(), except that in Release mode, it is supposed to expand to its parameter.
- ASSERT() should be used with expressions that do not include any function call. For expressions that include a function call, you should use VERIFY(), so the function call is preserved in release mode.
- TRACE() is the counterpart of printf(), except that it prints to the debug window. In Release mode, TRACE() also should expand to nothing.
- None of the three macros imply any runtime penalty in release mode. The macros distinguish between debug and release mode by the pre-defined _DEBUG macro. This is specific to Microsoft Visual C++. If you are using some other compiler you might have to use some appropriate macro.
Since it makes no sense to re-invent the wheel, I peeked into the MFC code and build my macros similarly. For ASSERT() and VERIFY() I left the fancy "Debug assertion failed..." dialog boxes aside, and simply implemented a plain vanilla breakpoint.
There are two files needed to support ASSERT(), VERIFY and TRACE(): debug.h and debug.cpp. You should include debug.h in some main header of your project. It does not pollute recurrent inclusions, since it does not include any file itself. You also should add debug.cpp to the source files of your project.
Here they are:
// file debug.h
#ifndef __DEBUG_H__
#define __DEBUG_H__
#ifdef _DEBUG
void _trace(char *fmt, ...);
#define ASSERT(x) {if(!(x)) _asm{int 0x03}}
#define VERIFY(x) {if(!(x)) _asm{int 0x03}}
#else
#define ASSERT(x)
#define VERIFY(x) x
#endif
#ifdef _DEBUG
#define TRACE _trace
#else
inline void _trace(LPCTSTR fmt, ...) { }
#define TRACE 1 ? (void)0 : _trace
#endif
#endif // __DEBUG_H__
//file debug.cpp
#ifdef _DEBUG
#include <windows.h>
#include <cstdarg>
#include <cstdio>
void _trace(char *fmt, ...)
{
char out[1024];
va_list body;
va_start(body, fmt);
vsprintf(out, fmt, body);
va_end(body);
OutputDebugString(out);
}
#endif <stdio.h><stdarg.h><windows.h></windows.h></stdarg.h></stdio.h> Related Tutorials
Balancing Game Development and Creative Direction in Indie Production
A practical look at how indie developers can balance creative direction with hands-on game development. This article co…
My Unreal Engine Development Process: From Core Idea to Playable Build
A practical overview of my Unreal Engine development process, covering how I move from a core game idea to a playable b…
Introducing LaneGraph: The Ultimate Road Network Solution for Unity
Discover the power of LaneGraph, a lightweight and flexible lane-based navigation system for Unity. LaneGraph makes it…
Retargeting Mixamo Characters with Root Motion In Unreal Engine 5.4.
I have always found Retargeting Mixamo Characters To have Root Motion is a serious lengthy Tast, Recently I stumbled up…
How To Make A SIMPLE Main Menu In Unity
In this tutorial for unity, i go over how to make a simple main menu for unity, it's an unlisted video because i do not…
Guide to Gameplay Balance
A perspective on competitive gameplay balance, from a background of "shooter" sandbox design.
Discussion
More from Gabriel Fleseriu
Comparing Shadow Mapping Techniques with Shadow Explorer
New Incentives and a Whole New Platform From The Intel AppUp developer program
The final installment in this series on Intel's AppUp program details additional incentives and opportunities for devel…
The Game Maker
This book excerpt contains the first chapter of the book that introduces beginning game developers to the Game Maker pr…
Discussion