tags:

views:

88

answers:

1

I'm trying to figure out a transparent solution for debug halts that repeatedly get hit in my game.

For a trivial example; say I have a halt in my renderer that tells me when I'm trying to use a NULL material. My renderer handles this fine but I still want to know what I'm doing wrong. This halt will hit every frame now unless I manually disable it.

This is the code id like to turn into a macro (or something else thats as transparent as porssible)

#define HALT(errorMsg) printf(errorMsg);__asm { int 3 };
satic bool hitOnce = false;
if (!hitOnce)
{
    hitOnce = true;
    HALT("its all gone wrong!")
}

The idea i had, was to make a macro that created this code, with a unique bool variable each time. The problem Ive hit so far is that I cannot increment numbers to at compile time to generate unique static bools for each HALT_ONCE.

+6  A: 

anything wrong with this?

#define HALT_ONCE(err_msg) \
do { \
    static bool hitOnce = false; \
    if (!hitOnce) { \
        hitOnce = true; \
        printf(err_msg); \
        __asm { int 3 }; \
    } \
} while(0)

Then you can just do this in your code:

HALT_ONCE("its all gone wrong!");

The do/while creates its own scope which makes hitOnce only exist for a very short time. I think this will prevent it from conflicting with other hitOnce variables created by this macro.

Evan Teran
Great thanks, i see now how i was thinking about the "unique" part of the problem the wrong way round.
Nathan Ross Powell
I understand why the curly braces are needed for the scope, but is the do/while part really necessary?
Brian
@Brian: It's necessary if you want constructs like `if (condition) HALT_ONCE(""); else do_something_else();` to parse correctly.
ephemient