summaryrefslogtreecommitdiff
path: root/context.h
blob: f59aade93d73b07913a54628ad3b1b46cddfb6fa (plain)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// Compiler version:
// 2.4.2 build 81 (GC MW 1.3.2)

// Compiler flags:
// -nodefaults -proc gekko -fp hard -O4 -use_lmw_stmw on -enum int -str reuse -rostr -sdata2 4 -lang=c++ -Cpp_exceptions off -RTTI off

// Typedefs.
typedef signed char s8;
typedef signed short int s16;
typedef signed long s32;
typedef signed long long int s64;
typedef unsigned char u8;
typedef unsigned short int u16;
typedef unsigned long u32;
typedef unsigned long long int u64;
typedef int BOOL;

typedef unsigned long size_t;
typedef long ptrdiff_t;

// Macros.
#define FALSE 0
#define TRUE 1

#define NULL 0
#if !defined(__cplusplus) || __cplusplus < 201103L
#ifndef nullptr
#define nullptr NULL
#endif
#endif
#define ATTRIBUTE_ALIGN(num) __attribute__((aligned(num)))

#define OBJECT_NAME(name) const char *name##_name = #name;
#define OBJECT_NAMES \
	o(TObject) \
	o(TMainTask)
#define TL_OBJECTS \
	o(tl_su, TL_SU) \
	TL_OBJECTS_AT_TL_00

#define TL_OBJECTS_AT_TL_00 \
	TL_00_OBJECTS \
	TL_02_OBJECTS \
	o(tl_particle, TL_PARTICLE) \
	TL_PARTICLE_MODEL_OBJECTS

#define TL_OBJECTS_AT_TL_00_WITHOUT_TL_PARTICLE \
	TL_00_OBJECTS \
	TL_02_OBJECTS \
	TL_PARTICLE_MODEL_OBJECTS

#define TL_OBJECTS_AT_TL_02 \
	TL_02_OBJECTS \
	o(tl_particle, TL_PARTICLE) \
	TL_PARTICLE_MODEL_OBJECTS

#define TL_00_OBJECTS \
	o(tl_00, TL_00) \
	o(tl_camera, TL_CAMERA) \
	o(tl_01, TL_01)

#define TL_02_OBJECTS \
	o(tl_02, TL_02) \
	o(tl_item_equip, TL_ITEM_EQUIP) \
	o(tl_03, TL_03) \
	o(tl_loc_start, TL_LOC_START) \
	o(tl_04, TL_04) \
	o(tl_05, TL_05) \
	o(tl_06, TL_06) \
	o(tl_07, TL_07) \
	o(tl_loc_end, TL_LOC_END) \
	o(tl_window, TL_WINDOW)

#define TL_PARTICLE_MODEL_OBJECTS \
	o(tl_particlemodel, TL_PARTICLEMODEL) \
	o(tl_radermap, TL_RADERMAP) \
	o(tl_clipout, TL_CLIPOUT) \
	o(tl_fade, TL_FADE) \
	o(tl_fadeafter, TL_FADEAFTER)

#define FOREACH_NODE(type, first, varname) for (type *varname = (type *)(first); varname != NULL; varname = (type *)(varname->next()))
#define FOREACH_NODE_NODECL(type, first, varname) for (varname = (type *)(first); varname != NULL; varname = (type *)(varname->next()))

#define FOREACH_NODE_MULTI_ITER(type, first, varname, ...) for (type *varname = (type *)(first); varname != NULL; varname = (type *)(varname->next()), __VA_ARGS__)
#define FOREACH_NODE_NODECL_MULTI_ITER(type, first, varname, ...) for (varname = (type *)(first); varname != NULL; varname = (type *)(varname->next()), __VA_ARGS__)

#define X_OR_Y_CHILD(flags, old_flags, one_prefix, zero_prefix, suffix)					\
	if (flags != old_flags) {									\
		TMainTask *child;									\
		u32 bit = 1;										\
		FOREACH_NODE_NODECL_MULTI_ITER(TMainTask, main_task.down(), child, bit <<= 1) {		\
			if (flags & bit) {								\
				child->one_prefix##_##suffix();						\
			} else {									\
				child->zero_prefix##_##suffix();					\
			}										\
		}											\
		old_flags = flags;									\
	}

#define SET_OR_CLEAR_CHILD_FLAGS(flags, old_flags, flag_bit) \
	X_OR_Y_CHILD(flags, old_flags, set_flag, clear_flag, flag_bit)

#define DISALLOW_OR_ALLOW_CHILD(flags, old_flags, flag_name) \
	X_OR_Y_CHILD(flags, old_flags, allow, disallow, flag_name)


// Class forward.
class THeap;
class TObject;
class TMainTask;

// Const defs.
static const int tl_object_count = 20;

// Extern defs.
#define o(name) extern const char *name##_name;
OBJECT_NAMES
#undef o

#define o(var, name) extern const char *var##_name;
TL_OBJECTS
#undef o

#define o(var, name) extern TObject *var;
TL_OBJECTS
#undef o

extern THeap *obj_heap;
extern THeap *alt_heap;
extern TMainTask main_task;
extern TObject global_obj1;
extern TObject global_obj2;

// Variable defs.
u32 some_main_task_flag = 1;
u32 update_flags;
u32 old_update_flags;
u32 render_flags;
u32 old_render_flags;
u32 render_shadow_flags;
u32 old_render_shadow_flags;
u32 some_id_805c6f74;

// func defs.
extern void heap_xfree(void *ptr);
extern void *heap_xmalloc(size_t size);

extern void camera_stuff();

extern void set_depth_buffer_settings_1();
extern void set_depth_buffer_settings_2();
extern void set_depth_buffer_settings_3();
extern void set_depth_buffer_settings_id(u32 id);
extern void save_depth_buffer_settings();
extern void restore_depth_buffer_settings();

extern void func_80083a00();
extern void func_80141618();
extern void set_some_id(u32 id);
extern void func_803e11e8(u32 arg1);
extern void func_803e11f0();

// Enum defs.
enum object_flags {
	NONE = 0,
	QUEUE_DESTRUCTION = 1,
	CHILD_QUEUE_DESTRUCTION = 2,
	BIT_2 = 4,
	BIT_3 = 8,
	DISALLOW_UPDATE = 0x0F,
	DISALLOW_RENDER = 0x10,
	DISALLOW_DESTRUCTION = 0x20,
	DISALLOW_RENDER_SHADOWS = 0x100,
	BIT_9 = 0x200,
	BIT_10 = 0x400,
	BIT_11 = 0x800,
	BIT_12 = 0x1000,
	BIT_13 = 0x2000,
	BIT_14 = 0x4000,
	BIT_15 = 0x8000,
	ALL_BITS = 0xFFFF
};

// Inline operator defs.
static inline object_flags operator^(object_flags a, object_flags b) { return static_cast<object_flags>(static_cast<u16>(a) ^ static_cast<u16>(b)); };
static inline object_flags operator&(object_flags a, object_flags b) { return static_cast<object_flags>(static_cast<u16>(a) & static_cast<u16>(b)); };
static inline object_flags operator|(object_flags a, object_flags b) { return static_cast<object_flags>(static_cast<u16>(a) | static_cast<u16>(b)); };
static inline object_flags operator~(object_flags a) { return static_cast<object_flags>(~static_cast<u16>(a)); }
static inline void operator^=(object_flags &a, object_flags b) { a = a ^ b; };
static inline void operator&=(object_flags &a, object_flags b) { a = a & b; };
static inline void operator|=(object_flags &a, object_flags b) { a = a | b; };

// Class defs.
class THeap {
public:
	struct heap_node {
		heap_node *next;
		size_t remaining_size;
	};
	heap_node *heap_nodes;
	size_t mbr_0x04;
	size_t align;
	size_t mbr_0x0C;
	size_t mbr_0x10;
public:
	THeap(size_t size, int align);
	~THeap();
	void *operator new(size_t size) { return heap_xmalloc(size); };
	void operator delete(void *ptr) { heap_xfree(ptr); };
	void *heap_alloc(size_t size);
	void *heap_zalloc(size_t size);
	void heap_free(void *ptr);
};

class TObject {
private:
	void _delete_children() {
		while (m_down != NULL) {
			delete m_down;
		}
	};
	void add_parent(TObject *parent, bool set_parent) {
		if (set_parent) {
			m_up = parent;
		}
		TObject *child;
		if (parent == NULL) {
			m_prev = this;
			m_next = NULL;
			return;
		}
		child = parent->m_down;
		if (child != NULL) {
			m_prev = child->m_next;
			m_next = NULL;
			child->m_prev->m_next = this;
			child->m_prev = this;
		} else {
			m_prev = this;
			parent->m_down = this;
			m_next = NULL;
		}
	};
	void remove_parent() {
		if (m_up != NULL) {
			if (m_prev == this) {
				m_up->m_down = NULL;
			} else if (m_up->m_down == this) {
				m_up->m_down = m_next;
				m_prev->m_next = NULL;
				if (m_next != NULL) {
					m_next->m_prev = m_prev;
				}
			} else {
				m_prev->m_next = m_next;
				if (m_next != NULL) {
					m_next->m_prev = m_prev;
				} else {
					m_up->m_down->m_prev = m_prev;
				}
			}
		}
	};

	void set_flags(object_flags flags) {
		m_flags |= flags;
	}

	void clear_flags(object_flags flags) {
		m_flags_u16 &= ~static_cast<u16>(flags);
	}

	void toggle_flags(object_flags flags) {
		m_flags ^= flags;
	}

	u32 get_flags(object_flags flags) {
		return m_flags & flags;
	};


	const char *m_name;
	union {
		object_flags m_flags;
		u16 m_flags_u16;
	};
	u16 m_id;
	TObject *m_prev;
	TObject *m_next;
	TObject *m_up;
	TObject *m_down;
public:
	void disallow_rendering_shadows();
	void allow_rendering_shadows();
	void disallow_rendering();
	void allow_rendering();
	void toggle_flag_3();
	void set_flag_3();
	void clear_flag_3();
	void queue_destruction();
	void set_flag_9();
	u32 get_flag_9();
	void clear_flag_9();

	TObject(TObject *parent = NULL);
	virtual ~TObject();

	void *operator new (size_t size) { return alloc(size); };
	void operator delete(void *ptr) { free(ptr); };

	const char *name() { return m_name; };
	object_flags flags() { return m_flags; };
	u16 flags_u16() { return m_flags_u16; };
	u16 id() { return m_id; };
	TObject *prev() { return m_prev; };
	TObject *next() { return m_next; };
	TObject *up() { return m_up; };
	TObject *down() { return m_down; };

	void set_name(const char *name) { m_name = name; };
	void set_obj_flags(object_flags flags) { m_flags = flags; };
	void set_flags_u16(u16 flags) { m_flags_u16 = flags; };
	void set_id(u16 id) { m_id = id; };
	void set_prev(TObject *node) { m_prev = node; };
	void set_next(TObject *node) { m_next = node; };
	void set_up(TObject *node) { m_up = node; };
	void set_down(TObject *node) { m_down = node; };

	void delete_children();
	void queue_destruction_for_each_node();
	void run_tasks();
	void render_nodes();
	void render_shadows_for_each_node();
	void render_nodes2();
	void empty_func();
	void set_parent(TObject *parent);

	virtual void run_task();
	virtual void render();
	virtual void render_shadows();

	void empty_func2();
	void log(const char *str);
	int get_node_count();
	bool all_parents_unqueued_for_destruction();
	static void *alloc(size_t size);
	static void free(void *ptr);
	bool toggle_flag_9_if_flag_10_is_clear();
};

class TMainTask : public TObject {
public:
	u32 task_flags;
	u32 mbr_0x20;
	u32 mbr_0x24;
public:
	TMainTask();
	void unused_render_func();
	void render_objects();
	void render_ui();
	void render_particle_effects();
	void render_effects();
	void render_geometry();
	void func_80228bbc();
	void func_80228c44(s32 arg0);
	void func_80228dbc();
	void render_clipout_and_fade();
	void init_main_task();

	virtual ~TMainTask();
	virtual void run_task();
	virtual void render();
	virtual void render_shadows();
private:
	void set_task_flags(u32 flags) {
		this->task_flags |= flags;
	}

	void clear_task_flags(u32 flags) {
		this->task_flags &= ~flags;
	}

	u32 get_task_flags(u32 flags) {
		return this->task_flags & flags;
	};
};