hist.c
1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#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;
}