NSMBW-Decomp
A decompilation of New Super Mario Bros. Wii
Loading...
Searching...
No Matches
anm_tex_pat.cpp
1#include <game/mLib/m_3d.hpp>
2#include <game/mLib/m_heap.hpp>
3
4size_t m3d::anmTexPat_c::child_c::heapCost(nw4r::g3d::ResMdl mdl, nw4r::g3d::ResAnmTexPat anmTexPat, bool calcAligned) {
5 size_t size = 0;
6 nw4r::g3d::AnmObjTexPatRes::Construct(nullptr, &size, anmTexPat, mdl, false);
7 if (calcAligned) {
8 size = nw4r::ut::RoundUp(mHeap::frmHeapCost(size, 0x20), 0x20);
9 }
10 return size;
11}
12
13bool m3d::anmTexPat_c::child_c::create(nw4r::g3d::ResMdl mdl, nw4r::g3d::ResAnmTexPat anmTexPat, mAllocator_c *allocator, size_t *objSize) {
14 if (allocator == nullptr) {
15 allocator = m3d::internal::l_allocator_p;
16 }
17
18 size_t size;
19 if (objSize == nullptr) {
20 objSize = &size;
21 }
22
23 *objSize = heapCost(mdl, anmTexPat, false);
24 if (!createAllocator(allocator, objSize)) {
25 return false;
26 }
27
28 mpObj = nw4r::g3d::AnmObjTexPatRes::Construct(&mAllocator, nullptr, anmTexPat, mdl, false);
29 if (!mpObj->Bind(mdl)) {
30 remove();
31 return false;
32 }
33
34 setFrmCtrlDefault(anmTexPat, PLAYMODE_INHERIT);
35 return true;
36}
37
38void m3d::anmTexPat_c::child_c::setAnm(m3d::bmdl_c &mdl, nw4r::g3d::ResAnmTexPat anmTexPat, m3d::playMode_e playMode) {
39 releaseAnm();
40 mpObj = nw4r::g3d::AnmObjTexPatRes::Construct(&mAllocator, nullptr, anmTexPat, mdl.getResMdl(), false);
41 mpObj->Bind(mdl.getResMdl());
42 setFrmCtrlDefault(anmTexPat, playMode);
43}
44
45void m3d::anmTexPat_c::child_c::releaseAnm() {
46 if (mpObj == nullptr) {
47 return;
48 }
49 mpObj->Release();
50 mpHeap->free(MEM_FRMHEAP_FREE_ALL);
51 mpObj = nullptr;
52}
53
54void m3d::anmTexPat_c::child_c::setFrmCtrlDefault(nw4r::g3d::ResAnmTexPat &anmTexPat, m3d::playMode_e playMode) {
55 if (playMode == PLAYMODE_INHERIT) {
56 playMode = (anmTexPat.p->mAnimateType == nw4r::g3d::ANM_POLICY_ONCE) ? FORWARD_ONCE : FORWARD_LOOP;
57 }
58 fanm_c::set(anmTexPat.getDuration(), playMode, 1.0f, -1.0f);
59}
60
61size_t m3d::anmTexPat_c::heapCost(nw4r::g3d::ResMdl mdl, nw4r::g3d::ResAnmTexPat anmTexPat, long count, bool calcAligned) {
62 size_t size = 0;
63 nw4r::g3d::AnmObjTexPatOverride::Construct(nullptr, &size, mdl, count);
64 size += nw4r::ut::RoundUp(count * sizeof(child_c), 0x20);
65 size += nw4r::ut::RoundUp(child_c::heapCost(mdl, anmTexPat, true), 0x20) * count;
66 if (calcAligned) {
67 size = nw4r::ut::RoundUp(mHeap::frmHeapCost(size, 0x20), 0x20);
68 }
69 return size;
70}
71
72bool m3d::anmTexPat_c::create(nw4r::g3d::ResMdl mdl, nw4r::g3d::ResAnmTexPat anmTexPat, mAllocator_c *allocator, size_t *objSize, long count) {
73 if (allocator == nullptr) {
74 allocator = m3d::internal::l_allocator_p;
75 }
76
77 size_t size = 0;
78 if (objSize == nullptr) {
79 objSize = &size;
80 }
81
82 *objSize = heapCost(mdl, anmTexPat, count, false);
83 if (!createAllocator(allocator, objSize)) {
84 return false;
85 }
86
87 mpObj = nw4r::g3d::AnmObjTexPatOverride::Construct(&mAllocator, nullptr, mdl, count);
88 children = (m3d::anmTexPat_c::child_c *) MEMAllocFromAllocator(&mAllocator, nw4r::ut::RoundUp(count * sizeof(child_c), 0x20));
89
90 nw4r::g3d::AnmObjTexPatOverride *texPatOverride = nw4r::g3d::G3dObj::DynamicCast<nw4r::g3d::AnmObjTexPatOverride>(mpObj);
91
92 child_c *child = &children[0];
93 for (int i = 0; i < count; i++) {
94 new(child) child_c();
95 if (!child->create(mdl, anmTexPat, &mAllocator, nullptr)) {
96 mHeap::destroyFrmHeap(mpHeap);
97 return false;
98 }
99 if (i == 0) {
100 nw4r::g3d::AnmObjTexPatRes *texPatRes = nw4r::g3d::G3dObj::DynamicCast<nw4r::g3d::AnmObjTexPatRes>(child->getObj());
101 texPatOverride->Attach(i, texPatRes);
102 } else {
103 child->releaseAnm();
104 }
105 child++;
106 }
107 return true;
108}
109
110m3d::anmTexPat_c::~anmTexPat_c() {
111 anmTexPat_c::remove();
112}
113
114void m3d::anmTexPat_c::remove() {
115 nw4r::g3d::AnmObjTexPatOverride *texPat = nw4r::g3d::G3dObj::DynamicCast<nw4r::g3d::AnmObjTexPatOverride>(mpObj);
116 if (texPat != nullptr && children != nullptr) {
117 int count = texPat->getCount();
118 for (int i = 0; i < count; i++) {
119 children[i].remove();
120 }
121 children = nullptr;
122 }
123 banm_c::remove();
124}
125
126void m3d::anmTexPat_c::setAnm(m3d::bmdl_c &mdl, nw4r::g3d::ResAnmTexPat anmTexPat, long idx, m3d::playMode_e playMode) {
127 nw4r::g3d::AnmObjTexPatOverride *texPat = nw4r::g3d::G3dObj::DynamicCast<nw4r::g3d::AnmObjTexPatOverride>(mpObj);
128 texPat->Detach(idx);
129 children[idx].setAnm(mdl, anmTexPat, playMode);
130 nw4r::g3d::AnmObjTexPatRes *texPatRes = nw4r::g3d::G3dObj::DynamicCast<nw4r::g3d::AnmObjTexPatRes>(children[idx].getObj());
131 texPat->Attach(idx, texPatRes);
132}
133
134void m3d::anmTexPat_c::releaseAnm(long idx) {
135 nw4r::g3d::AnmObjTexPatOverride *texPat = nw4r::g3d::G3dObj::DynamicCast<nw4r::g3d::AnmObjTexPatOverride>(mpObj);
136 texPat->Detach(idx);
137 children[idx].releaseAnm();
138}
139
140void m3d::anmTexPat_c::play() {
141 nw4r::g3d::AnmObjTexPatOverride *texPat = nw4r::g3d::G3dObj::DynamicCast<nw4r::g3d::AnmObjTexPatOverride>(mpObj);
142 int count = texPat->getCount();
143 for (int i = 0; i < count; i++) {
144 play(i);
145 }
146}
147
148void m3d::anmTexPat_c::play(long idx) {
149 if (children[idx].IsBound()) {
150 children[idx].play();
151 }
152}
153
154float m3d::anmTexPat_c::getFrame(long idx) const {
155 return children[idx].getFrame();
156}
157
158void m3d::anmTexPat_c::setFrame(float frame, long idx) {
159 children[idx].setFrame(frame);
160}
161
162float m3d::anmTexPat_c::getRate(long idx) const {
163 return children[idx].getRate();
164}
165
166void m3d::anmTexPat_c::setRate(float rate, long idx) {
167 children[idx].setRate(rate);
168}
169
170bool m3d::anmTexPat_c::isStop(long idx) const {
171 return children[idx].isStop();
172}
173
174bool m3d::anmTexPat_c::checkFrame(float frame, long idx) const {
175 return children[idx].checkFrame(frame);
176}
177
178void m3d::anmTexPat_c::setPlayMode(m3d::playMode_e mode, long idx) {
179 children[idx].mPlayMode = mode;
180}
181
182float m3d::anmTexPat_c::getFrameMax(long idx) const {
183 return children[idx].mFrameMax;
184}
@ MEM_FRMHEAP_FREE_ALL
Deallocates all memory blocks from the heap.
Definition MEMFrmHeap.h:13
void set(float duration, m3d::playMode_e playMode, float updateRate, float startFrame)
Starts the animation with the given parameters.
Definition fanm.cpp:51
playMode_e
Definition banm.hpp:7
@ FORWARD_ONCE
Play the animation forward once.
Definition banm.hpp:9
@ FORWARD_LOOP
Play the animation forward in a loop.
Definition banm.hpp:8
@ PLAYMODE_INHERIT
Use the play mode of the parent.
Definition banm.hpp:12
T RoundUp(T x, u32 base)
Rounds x up to a multiple of base.
Definition inlines.hpp:21