1#include "framework/seadProcessMeterBar.h"
2#include "basis/seadRawPrint.h"
3#include "framework/seadProcessMeter.h"
4
5namespace sead
6{
7ProcessMeterBarBase::ProcessMeterBarBase(ProcessMeterBarBase::Section* sections, s32 num_sections,
8 const SafeString& name, const Color4f& color)
9 : INamable(name), mColor(color)
10{
11 mSectionList(0).setBuffer(size: num_sections, bufferptr: sections);
12 _88(0) = 0;
13 mSectionList(1).setBuffer(size: num_sections, bufferptr: sections + num_sections);
14 _88(1) = 0;
15}
16
17ProcessMeterBarBase::~ProcessMeterBarBase()
18{
19 if (mParent)
20 mParent->detachProcessMeterBar(bar: this);
21}
22
23void ProcessMeterBarBase::measureBegin()
24{
25 if (mEnabled)
26 measureBeginImpl_(start_time: TickTime(), color: mColor);
27}
28
29void ProcessMeterBarBase::measureBegin(const TickTime& start_time)
30{
31 if (mEnabled)
32 measureBeginImpl_(start_time, color: mColor);
33}
34
35void ProcessMeterBarBase::measureBegin(const Color4f& color)
36{
37 if (mEnabled)
38 measureBeginImpl_(start_time: TickTime(), color);
39}
40
41void ProcessMeterBarBase::measureBegin(const TickTime& start_time, const Color4f& color)
42{
43 if (mEnabled)
44 measureBeginImpl_(start_time, color);
45}
46
47void ProcessMeterBarBase::measureEnd()
48{
49 if (mEnabled)
50 measureEndImpl_(end_time: TickTime());
51}
52
53void ProcessMeterBarBase::measureEnd(const TickTime& end_time)
54{
55 if (mEnabled)
56 measureEndImpl_(end_time);
57}
58
59const ProcessMeterBarBase::Section* ProcessMeterBarBase::getLastFirstBegin() const
60{
61 return mSectionList[1 - mActiveBufferIdx].get(idx: 0);
62}
63
64TickSpan ProcessMeterBarBase::getLastTotalSpan() const
65{
66 TickSpan total = 0;
67 for (s32 i = 0; i < _88[1 - mActiveBufferIdx]; ++i)
68 {
69 if (mSectionList[1 - mActiveBufferIdx].get(idx: i)->parent == -1)
70 total += mSectionList[1 - mActiveBufferIdx].get(idx: i)->span;
71 }
72 return total;
73}
74
75void ProcessMeterBarBase::onEndFrame()
76{
77 SEAD_ASSERT(mTopSection == -1);
78 SEAD_ASSERT(mOverNum == 0);
79 mActiveBufferIdx = 1 - mActiveBufferIdx;
80 mTopSection = -1;
81 mOverNum = 0;
82 _88[mActiveBufferIdx] = 0;
83 mEnabled = mParent != nullptr;
84}
85
86void ProcessMeterBarBase::setParentProcessMeter(ProcessMeter* parent)
87{
88 SEAD_ASSERT(mParent == nullptr || parent == nullptr);
89 mParent = parent;
90}
91
92void ProcessMeterBarBase::measureBeginImpl_(const TickTime& start_time, Color4f color)
93{
94 addSection_(time: start_time, color, parent: mTopSection);
95}
96
97void ProcessMeterBarBase::measureEndImpl_(const TickTime& end_time)
98{
99 TickTime new_time = end_time;
100 mTicks[mActiveBufferIdx] = new_time;
101
102 if (mOverNum > 0)
103 {
104 --mOverNum;
105 }
106 else
107 {
108 TickTime t = getCurSection_(idx: mTopSection)->time;
109 if (new_time.diff(other: t).toS64() < 0)
110 new_time = t;
111
112 SEAD_ASSERT_MSG(mTopSection >= 0, "Unmatching measureBegin / measureEnd.");
113 endSection_(idx: mTopSection, time: new_time);
114 mTopSection = getCurSection_(idx: mTopSection)->parent;
115 }
116}
117
118// NON_MATCHING: some stores are paired
119void ProcessMeterBarBase::addSection_(const TickTime& time, Color4f color, s32 parent)
120{
121 if (_88[mActiveBufferIdx] >= mSectionList[0].getSize())
122 {
123 ++mOverNum;
124 }
125 else
126 {
127 Section* sec = getCurSection_(idx: _88[mActiveBufferIdx]);
128 sec->time = time;
129 sec->span = -1;
130 sec->color = color;
131 sec->parent = parent;
132
133 mTopSection = _88[mActiveBufferIdx];
134 ++_88[mActiveBufferIdx];
135 }
136}
137
138ProcessMeterBarBase::Section* ProcessMeterBarBase::getCurSection_(s32 idx)
139{
140 SEAD_ASSERT(idx >= 0 && idx < mSectionList[0].getSize());
141 return mSectionList[mActiveBufferIdx].get(idx);
142}
143
144void ProcessMeterBarBase::endSection_(s32 idx, const TickTime& time)
145{
146 SEAD_ASSERT(idx >= 0 && idx < mSectionList[0].getSize());
147 Section* sec = getCurSection_(idx);
148 SEAD_ASSERT(sec->span.toS64() == -1);
149 sec->span = time.diff(other: sec->time);
150}
151} // namespace sead
152