hist.c 1.75 KB

#include <assert.h>

#include "hist.h"
#include "UI.h"


void
Hist_Init (Hist *pHist, s32 size, s32 max) 
{
    int i;
    
    /* We cannot create a hist that is larger than what is allocated. */
    assert (size <= kHistSize);
    
    pHist->fIndex = 0;
    pHist->fSize = size;         /* get number of longs in the buffer */
    pHist->fMax = max;
    
    for (i=0; i<pHist->fSize; i++)
        pHist->fData[i] = 0;
}


void
Hist_Draw (Hist *pHist, Rect *pRect, Gfx **ppGfx) 
{
    u16 shortcolor;
    u16 histHeight;
    s32 col;
    s32 index;
    u32 val;
    s32 i;    

    gDPPipeSync ((*ppGfx)++);
    gDPSetCycleType ((*ppGfx)++, G_CYC_FILL);
    gDPSetRenderMode ((*ppGfx)++, G_RM_OPA_SURF, G_RM_OPA_SURF2);

    shortcolor = GPACK_RGBA5551(82, 97, 197, 1);
    gDPSetFillColor((*ppGfx)++, shortcolor << 16 | shortcolor);

    histHeight = pRect->bottom - pRect->top;
    index = pHist->fIndex;

    for (i=0; i<pHist->fSize; i++)
    {
        if (--index < 0)
            index = pHist->fSize - 1;

        assert(index >= 0 && index < pHist->fSize);

        /* Convert the histogram data into screen units and limit val to the screen bounds. */
        val = (pHist->fData[index] * histHeight)/pHist->fMax;
        if (val > histHeight)
            val = histHeight;

        /* To display a bar graph, draw a hist value every other pixel (i*2). */
	col = pRect->left + 2*i;

        gDPFillRectangle ((*ppGfx)++, col, pRect->bottom-val, col, pRect->bottom); 

	/* Work-around for an failed assertion bug! */
        gDPPipeSync ((*ppGfx)++);
    }
}



void
Hist_SetValue (Hist *pHist, s32 value) 
{
    assert(pHist->fIndex < pHist->fSize);
    
    pHist->fData[pHist->fIndex++] = value;
    
    if (pHist->fIndex >= pHist->fSize)
        pHist->fIndex = 0;    
}