#17362 - LOst? - Sat Mar 06, 2004 11:18 pm
I've seen ASSERT's in some game code. It looks like some debug output and may be very informative?
How do I create an ASSERT function/macro?
What does it do?
How do it work?
What's good with it?
#17363 - sajiimori - Sat Mar 06, 2004 11:30 pm
It's not a standard thing. The C standard library has assert(), but that's not appropriate for GBA because it calls abort() if the assertion fails. Anyway, if you know how to write macros, you can write one to do whatever you want when it fails.
Example:
Code: |
#define my_assert(x) \
if(!(x)) { \
my_debug_output("assertion failed at %d: %s\n", \
__LINE__, #x); \
}
|
(The stringize syntax is just #x right?)
#17365 - Miked0801 - Sat Mar 06, 2004 11:37 pm
Learn to love ASSERT - it is your best friend.
What it does is stop game execution and hopefully report which .c module and line number the error occured on along with optional useful text.
You use asserts to make checks that certain vars/parameters are within expected ranges. Here's an example:
void PlotPixel(u32 x, u32 y, u8 color)
{
ASSERT(x < 240);
ASSERT(y < 160);
// Do draw code here
}
Now if ever this function receives out of bounds data, you'll know about it immediately and will be able to fix it. Other common locations for asserts include:
the default: section of switch statements
anything function that takes a pointer as an argument can be checked to see if the argument is a valid GBA pointer (and depending, not NULL)
in memory management code to make sure that you aren't corrupting mem blocks (writing past the ends of arrays)
anywhere else where you want to make sure data is valid. A good, carefully thought out assert will save you more time than you can possibly imagine.
void ASSERT(BOOL value)
{
if(!value)
{
write out debug info.
while(special escape input routine is false)
{
}
}
}
Keep in mind that asserts do slow game execution and should be disabled when making a "Gold" build.
#17368 - LOst? - Sat Mar 06, 2004 11:46 pm
So how do I get the line number where the assert was called? I don't know that much about compilers.
#17375 - dagamer34 - Sun Mar 07, 2004 1:02 am
__FILE__ - this tells you what file the assert occured in
__LINE__ - this tells you what line the error occured on
_________________
Little kids and Playstation 2's don't mix. :(
#17379 - LOst? - Sun Mar 07, 2004 2:24 am
dagamer34 wrote: |
__FILE__ - this tells you what file the assert occured in
__LINE__ - this tells you what line the error occured on |
Thanks! This will make my exception handlers much better ^_^