NSMBW-Decomp
A decompilation of New Super Mario Bros. Wii
Loading...
Searching...
No Matches
g3d_rescommon.h
1#ifndef NW4R_G3D_RES_RES_COMMON_H
2#define NW4R_G3D_RES_RES_COMMON_H
3#include <nw4r/types_nw4r.h>
4
5#include <nw4r/ut.h>
6
7#include <revolution/GX.h>
8
9/******************************************************************************
10 *
11 * Macros
12 *
13 ******************************************************************************/
14
15/**
16 * Define ResName pascal string for file resource groups.
17 */
18#define NW4R_G3D_RESFILE_NAME_DEF(VAR, STR) \
19 nw4r::g3d::ResNameData27 ResNameData_##VAR ALIGN(32) = {sizeof(STR) - 1, \
20 STR}
21
22/**
23 * Similar to "ofs_to_obj" but accounting for the additional -4 offset.
24 * Debug builds show this behavior was not achieved through a function.
25 */
26#define NW4R_G3D_OFS_TO_RESNAME(BASE, OFS) \
27 nw4r::g3d::ResName((char*)(BASE) + (OFS) - sizeof(ulong))
28
29/**
30 * Define common functions for resource classes.
31 * @note Hides ResCommon::ref, why did they do this???
32 */
33#define NW4R_G3D_RESOURCE_FUNC_DEF(T) \
34 NW4R_G3D_RESOURCE_FUNC_DEF_IMPL(T, T##Data)
35#define NW4R_G3D_RESOURCE_FUNC_DEF_EX(TCLS, TDATA) \
36 NW4R_G3D_RESOURCE_FUNC_DEF_IMPL(TCLS, TDATA)
37
38#define NW4R_G3D_RESOURCE_FUNC_DEF_IMPL(TCLS, TDATA) \
39 explicit TCLS(void* pData = NULL) : nw4r::g3d::ResCommon<TDATA>(pData) {} \
40 \
41 TDATA& ref() { \
42 return *ptr(); \
43 } \
44 \
45 const TDATA& ref() const { \
46 return *ptr(); \
47 } \
48 \
49 bool operator==(const TCLS& rOther) const { \
50 return ptr() == rOther.ptr(); \
51 } \
52 \
53 bool operator!=(const TCLS& rOther) const { \
54 return ptr() != rOther.ptr(); \
55 }
56
57namespace nw4r {
58namespace g3d {
59
60/******************************************************************************
61 *
62 * Common resource wrapper
63 *
64 ******************************************************************************/
65template <typename T> class ResCommon {
66public:
67 explicit ResCommon(void* pData) : mpData(static_cast<T*>(pData)) {}
68
69 explicit ResCommon(const void* pData)
70 : mpData(static_cast<const T*>(pData)) {}
71
72 bool IsValid() const {
73 return mpData != NULL;
74 }
75
76 T* ptr() {
77 return mpData;
78 }
79 const T* ptr() const {
80 return mpData;
81 }
82
83 T& ref() {
84 return *mpData;
85 }
86 const T& ref() const {
87 return *mpData;
88 }
89
90 template <typename T> T* ofs_to_ptr_raw(s32 ofs) {
91 return reinterpret_cast<T*>((char*)mpData + ofs);
92 }
93 template <typename T> const T* ofs_to_ptr_raw(s32 ofs) const {
94 return reinterpret_cast<const T*>((char*)mpData + ofs);
95 }
96
97 template <typename T> T* ofs_to_ptr(s32 ofs) {
98 u8* pPtr = reinterpret_cast<u8*>(mpData);
99
100 if (ofs != 0) {
101 return reinterpret_cast<T*>(pPtr + ofs);
102 }
103
104 return NULL;
105 }
106 template <typename T> const T* ofs_to_ptr(s32 ofs) const {
107 const u8* pPtr = reinterpret_cast<const u8*>(mpData);
108
109 if (ofs != 0) {
110 return reinterpret_cast<const T*>(pPtr + ofs);
111 }
112
113 return NULL;
114 }
115
116 template <typename T> T ofs_to_obj(s32 ofs) {
117 u8* pPtr = reinterpret_cast<u8*>(mpData);
118
119 if (ofs != 0) {
120 return T(pPtr + ofs);
121 }
122
123 return T(NULL);
124 }
125 template <typename T> const T ofs_to_obj(s32 ofs) const {
126 const u8* pPtr = reinterpret_cast<const u8*>(mpData);
127
128 if (ofs != 0) {
129 return T(const_cast<u8*>(pPtr + ofs));
130 }
131
132 return T(NULL);
133 }
134
135private:
136 T* mpData;
137};
138
139/**
140 * Header for resource data structures.
141 */
143 char kind[4]; // at 0x0
144 ulong size; // at 0x4
145};
146
147/**
148 * Name for file resource groups.
149 */
151 ulong len; // at 0x0
152 char str[32 - sizeof(ulong)]; // at 0x4
153};
154
155/******************************************************************************
156 *
157 * Named resource
158 *
159 ******************************************************************************/
161 ulong len; // at 0x0
162 char str[4]; // at 0x4
163};
164
165class ResName : public ResCommon<const ResNameData> {
166public:
167 explicit ResName(const void* pData) : ResCommon(pData) {}
168
169 ulong GetLength() const {
170 return ref().len;
171 }
172
173 const char* GetName() const {
174 return ref().str;
175 }
176
177 bool operator==(const ResName rhs) const;
178};
179
180/******************************************************************************
181 *
182 * Generic display list
183 *
184 ******************************************************************************/
186 ulong bufSize; // at 0x0
187 ulong cmdSize; // at 0x4
188 s32 toDL; // at 0x8
189};
190
191class ResTagDL : public ResCommon<ResTagDLData> {
192public:
193 NW4R_G3D_RESOURCE_FUNC_DEF(ResTagDL);
194
195 ulong GetBufSize() const {
196 return ref().bufSize;
197 }
198
199 ulong GetCmdSize() const {
200 return ref().cmdSize;
201 }
202
203 u8* GetDL() {
204 return const_cast<u8*>(ofs_to_ptr<u8>(ref().toDL));
205 }
206 const u8* GetDL() const {
207 return ofs_to_ptr<u8>(ref().toDL);
208 }
209};
210
211/******************************************************************************
212 *
213 * Model bytecode
214 *
215 ******************************************************************************/
216namespace ResByteCodeData {
217
218enum OpCode {
219 NOOP, // No operation
220 END, // End of bytecode
221 CALC, // Calculate matrix
222 WEIGHT, // Apply weighting
223 DRAW, // Draw polygon
224 EVPMTX, // Envelope matrix
225 MTXDUP // Duplicate matrix
226};
227
228// CALC opcode layout
230 u8 opcode; // at 0x0
231 u8 nodeIdHi; // at 0x1
232 u8 nodeIdLo; // at 0x2
233 u8 parentMtxIdHi; // at 0x1
234 u8 parentMtxIdLo; // at 0x2
235};
236
237// WEIGHT opcode layout
239 u8 opcode; // at 0x0
240 u8 tgtIdHi; // at 0x1
241 u8 tgtIdLo; // at 0x2
242 u8 numBlendMtx; // at 0x3
243};
244// WEIGHT opcode layout - weighting entry
246 u8 mtxIdHi; // at 0x0
247 u8 mtxIdLo; // at 0x1
248 u8 fWeight0; // at 0x2
249 u8 fWeight1; // at 0x3
250 u8 fWeight2; // at 0x4
251 u8 fWeight3; // at 0x5
252};
253
254// DRAW opcode layout
256 u8 opcode; // at 0x0
257 u8 matIdHi; // at 0x3
258 u8 matIdLo; // at 0x4
259 u8 shpIdHi; // at 0x1
260 u8 shpIdLo; // at 0x2
261 u8 nodeIdHi; // at 0x5
262 u8 nodeIdLo; // at 0x6
263 u8 priority; // at 0x7
264};
265
266// EVPMTX opcode layout
268 u8 opcode; // at 0x0
269 u8 mtxIdHi; // at 0x1
270 u8 mtxIdLo; // at 0x2
271 u8 nodeIdHi; // at 0x1
272 u8 nodeIdLo; // at 0x2
273};
274
275// MTXDUP opcode layout
277 u8 opcode; // at 0x0
278 u8 toMtxIdHi; // at 0x1
279 u8 toMtxIdLo; // at 0x2
280 u8 fromMtxIdHi; // at 0x1
281 u8 fromMtxIdLo; // at 0x2
282};
283
284} // namespace ResByteCodeData
285
286namespace detail {
287
288/******************************************************************************
289 *
290 * Primitive read/write
291 *
292 ******************************************************************************/
293inline u8 ResRead_u8(const u8* pPtr) {
294 return *pPtr;
295}
296
297inline ulong ResRead_ulong(const u8* pPtr) {
298 ulong value = ResRead_u8(pPtr++) << 24;
299 value |= ResRead_u8(pPtr++) << 16;
300 value |= ResRead_u8(pPtr++) << 8;
301 value |= ResRead_u8(pPtr++) << 0;
302 return value;
303}
304
305inline void ResWrite_u8(u8* pPtr, u8 data) {
306 *pPtr = data;
307}
308
309inline void ResWrite_u16(u8* pPtr, u16 data) {
310 ResWrite_u8(pPtr++, data >> 8);
311 ResWrite_u8(pPtr++, data >> 0);
312}
313
314inline void ResWrite_ulong(u8* pPtr, ulong data) {
315 ResWrite_u8(pPtr++, data >> 24);
316 ResWrite_u8(pPtr++, data >> 16);
317 ResWrite_u8(pPtr++, data >> 8);
318 ResWrite_u8(pPtr++, data >> 0);
319}
320
321/******************************************************************************
322 *
323 * GX Blitting Processor (BP)
324 *
325 ******************************************************************************/
326inline void ResReadBPCmd(const u8* pPtr, ulong* pOut) {
327 // Skip over FIFO command byte
328 *pOut = ResRead_ulong(pPtr + 1);
329}
330
331void ResWriteBPCmd(u8* pPtr, ulong reg);
332void ResWriteBPCmd(u8* pPtr, ulong reg, ulong mask);
333void ResWriteSSMask(u8* pPtr, ulong value);
334
335/******************************************************************************
336 *
337 * GX Command Processor (CP)
338 *
339 ******************************************************************************/
340inline void ResReadCPCmd(const u8* pPtr, ulong* pOut) {
341 // Skip over FIFO command byte + addr byte
342 *pOut = ResRead_ulong(pPtr + 2);
343}
344
345void ResWriteCPCmd(u8* pPtr, u8 addr, ulong value);
346
347/******************************************************************************
348 *
349 * GX Transform Unit (XF)
350 *
351 ******************************************************************************/
352inline void ResReadXFCmd(const u8* pPtr, ulong* pOut) {
353 // Skip over FIFO command byte + size short + addr short
354 *pOut = ResRead_ulong(pPtr + 5);
355}
356
357void ResWriteXFCmd(u8* pPtr, u16 addr, ulong value);
358
359/******************************************************************************
360 *
361 * Utility functions
362 *
363 ******************************************************************************/
364inline GXColor GetRGBA(u8 r, u8 g, u8 b, u8 a) {
365 return (GXColor){r, g, b, a};
366}
367inline GXColorS10 GetRGBAS10(s16 r, s16 g, s16 b, s16 a) {
368 return (GXColorS10){r, g, b, a};
369}
370inline GXColorS10 GetRGBAS10(const nw4r::ut::Color &col) {
371 return (GXColorS10){col.r, col.g, col.b, col.a};
372}
373
374} // namespace detail
375} // namespace g3d
376} // namespace nw4r
377
378#endif
3D graphics drawing library.
Definition g3d_3dsmax.h:10