View Single Post
Old 08-27-2009, 08:34 AM   #19
IXix
Human being with feelings
 
Join Date: Jan 2007
Location: mcr:uk
Posts: 3,450
Default Debugging Aid

Great thread! I can't contribute much but I'll re-post this (slightly improved) snippet here where more people might find it.

Just paste this at the bottom of your @gfx section to display a chunk of memory buffer and six general purpose debugging variables. You can set various parameters in the options bit at the top.

Code:
/*******************************************************************************
    General purpose debugging aid. Paste into the gfx section of your code.

    Variables:
    debug1...debug6      - Displayed at the top of the debug area.
    dOffset              - Index of first item of buffer data to display.

*******************************************************************************/

SHOWDEBUG = 1; //Set non-zero to enable the debug view

SHOWDEBUG ?
(    
    //Options
    dPrecision = 2;             //Decimal places for numbers
    dLeft = 5;                  //Debug window position in pixels
    dTop = 5;                   //Debug window position in pixels
    dRows = 5;                  //Row count for buffer data
    dCols = 6;                  //Column count for buffer data

    dRowHeight = 20;            //Row height offset for buffer data
    dColWidth = 80;             //Column width offset for buffer data
    dMarginX = 5;               //Left margin in pixels
    dMarginY = 5;               //Top margin in pixels
    dBufferY = 30;              //Vertical offset for buffer data display
    
    /* ------------------------------------------------------------- */

    dOld_x = gfx_x;
    dOld_y = gfx_y;
    dOld_a = gfx_a;
    dOld_r = gfx_r;
    dOld_g = gfx_g;
    dOld_b = gfx_b;
    dOld_mode = gfx_mode;

    gfx_a = 1;
    gfx_mode = 0;

    //Calculate debug window size
    dWidth = dCols * dColWidth + dMarginX;
    dHeight = dRows * dRowHeight + dBufferY;

    //Set background colour
    gfx_r=1;gfx_g=1;gfx_b=1;gfx_a=1;

    //Erase
    gfx_x = dLeft;
    gfx_y = dTop;
    gfx_rectto(dLeft + dWidth, dTop + dHeight);

    //Set text colour
    gfx_r=.7;gfx_g=0;gfx_b=.3;

    //Draw debug vars
    gfx_x = dLeft + dMarginX; gfx_y = dTop + dMarginY;
    gfx_drawNumber(debug1, dPrecision);

    gfx_x = dLeft + dMarginX + dColWidth; gfx_y = dTop + dMarginY;
    gfx_drawNumber(debug2, dPrecision);

    gfx_x = dLeft + dMarginX + dColWidth * 2; gfx_y = dTop + dMarginY;
    gfx_drawNumber(debug3, dPrecision);

    gfx_x = dLeft + dMarginX + dColWidth * 3; gfx_y = dTop + dMarginY;
    gfx_drawNumber(debug4, dPrecision);

    gfx_x = dLeft + dMarginX + dColWidth * 4; gfx_y = dTop + dMarginY;
    gfx_drawNumber(debug5, dPrecision);

    gfx_x = dLeft + dMarginX + dColWidth * 5; gfx_y = dTop + dMarginY;
    gfx_drawNumber(debug6, dPrecision);

    //Draw separator
    gfx_x = dLeft + dMarginX;
    gfx_y = dTop + dBufferY - dMarginY;
    gfx_lineto(dLeft + dWidth - dMarginX, gfx_y, 1);

    //Draw buffer data
    di = 0;
    dRow = 0;
    loop
    (
        dRows,
        dCol = 0;
        loop
        (
            dCols,
            gfx_x = dLeft + dMarginX + dCol * dColWidth;
            gfx_y = dTop + dMarginY + dBufferY + dRow * dRowHeight;
            gfx_drawNumber(dOffset[di], dPrecision);
            di += 1;
            dCol += 1;
        );
        dRow += 1;
    );

    //Restore previous state
    gfx_x = dOld_x;
    gfx_y = dOld_y;
    gfx_a = dOld_a;
    gfx_r = dOld_r;
    gfx_g = dOld_g;
    gfx_b = dOld_b;
    gfx_mode = dOld_mode;
);
You can use debug1...debug6 to report the value of a variable at specific points in your code, or they can be handy for tracing which parts of a conditional block are being executed. To use the buffer viewer, just set dOffset to the memory index you're interested in.

I've found it very helpful for dealing with my typically over-complicated scripts.

Last edited by IXix; 08-27-2009 at 08:45 AM. Reason: Added code to restore gfx variables to their original state after debug drawing is completed
IXix is offline   Reply With Quote