
//===========================================================================
//
// Name:			bb_cg_hud.c
//===========================================================================

#include "cg_local.h"

#include "bb_cg_hud.h"
/*
==================
BB_HUD_DrawStringExt
a modified version of CG_DrawStringExt
 
Draws a multi-colored string with an outline, optionally forcing
to a fixed color.
 
Coordinates are at 640 by 480 virtual resolution
==================
*/
void BB_HUD_DrawStringExt (int x, int y, const char *string,
                           const float *setColor, qboolean forceColor,
                           qboolean outline, int charWidth, int charHeight,
                           int maxChars) {
    vec4_t		color;
    const char	*s;
    int			xx;
    int			cnt;

    if (maxChars <= 0)
        maxChars = 32767; // do them all!

    // draw the outline
    if (outline) {
        color[0] = color[1] = color[2] = 0;
        color[3] = setColor[3];
        trap_R_SetColor( color );
        s = string;
        xx = x;
        cnt = 0;
        while (*s && cnt < maxChars) {
            if (Q_IsColorString(s)) {
                s += 2;
                continue;
            }

            CG_DrawChar (xx - 1, y - 1, charWidth+2, charHeight+2, *s);
            cnt++;
            xx += charWidth;
            s++;
        }
    }

    // draw the colored text
    s = string;
    xx = x;
    cnt = 0;
    trap_R_SetColor (setColor);
    while (*s && cnt < maxChars) {
        if ( Q_IsColorString( s ) ) {
            if (!forceColor) {
                memcpy(color,g_color_table[ColorIndex(*(s+1))],sizeof (color));
                color[3] = setColor[3];
                trap_R_SetColor (color);
            }

            s += 2;
            continue;
        }

        CG_DrawChar( xx, y, charWidth, charHeight, *s );
        xx += charWidth;
        cnt++;
        s++;
    }
    trap_R_SetColor( NULL );
}


/*
===================
BB_HUD_DrawWeaponSelect
 
a modified version of CG_DrawWeaponSelect
===================
*/
void BB_HUD_DrawWeaponBar (void) {
    int		ammo;
    int		i, iconheight, iconwidth;
    int		bits;
    int		count;
    int		x, y;
    vec4_t	acolor;

    // don't display if dead
    if (cg.predictedPlayerState.stats[STAT_HEALTH] <= 0)
        return;

    // count the number of weapons owned
    bits = cg.snap->ps.stats[ STAT_WEAPONS ];
    count = 0;
    for (i = 1 ; i < 16; i++) {
        if (bits & ( 1 << i ))
            count++;
    }

    iconheight = iconwidth = 3 * TINYCHAR_WIDTH;
    x = 320 - count * (iconwidth / 2);

    for (i = 1 ; i < 16 ; i++) {
        if (!(bits & (1 << i)))
            continue;

        CG_RegisterWeapon (i);

        y = 425 - TINYCHAR_HEIGHT;
        ammo = cg.predictedPlayerState.ammo[i];
        // Don't draw negative ammo
        if (ammo >= 0) {
            char str[10];
            Com_sprintf (str, sizeof(str), "%i", ammo);
            // draw the text
            acolor[0] = 1.0;
            acolor[1] = acolor[2] = ammo < 10 ? 0.3 : 1.0;
            acolor[3] = 1.0;
            BB_HUD_DrawStringExt((int)(x+((int)TINYCHAR_WIDTH*((3.0-strlen(str))/2.0))), y,
                                 (const char *)str, (const float *)acolor, qfalse, qtrue, (int)TINYCHAR_WIDTH,
                                 (int)TINYCHAR_HEIGHT, 3);
        }
		
        // draw selection marker
        if (i == cg.predictedPlayerState.weapon) {
            CG_DrawPic (x-2, y-3, iconwidth+4, iconheight+TINYCHAR_HEIGHT+5,
                        cgs.media.selectShader);
        }

        // draw weapon icon
        y += TINYCHAR_HEIGHT;
        CG_DrawPic (x, y, iconwidth, iconheight, cg_weapons[i].weaponIcon);

        // no ammo cross on top
        if (!cg.snap->ps.ammo[i])
            CG_DrawPic (x, y, iconheight, iconwidth, cgs.media.noammoShader);

        x += iconwidth + 4;
    }

    trap_R_SetColor (NULL);
} //BB_HUD_DrawWeaponBar ()


