summaryrefslogtreecommitdiff
path: root/src/worldmap.cpp
blob: 97d1b738cee0fd0144e2afe91601a1dc5bf3d805 (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
/*
 * Newer Super Mario Bros. Wii
 * 2D World Maps
 *
 * Wish me luck. That's all I'm saying. --Treeki
 * Started: 03/06/2010; 11:52pm
 */

#include "worldmap.h"

/* Code */
void PaneSetAllVisible(void *pane, int flag, bool value) {
	//OSReport("%08X {\n", pane);
	if (value)
		((char*)pane)[0xBB] &= ~(1 << flag);
	else
		((char*)pane)[0xBB] |= (1 << flag);
	
	nw4rLinkListNode *firstNode = (nw4rLinkListNode*)(pane+0x14);
	nw4rLinkListNode *currentNode = (nw4rLinkListNode*)(firstNode->prev);
	
	while (currentNode != firstNode) {
		PaneSetAllVisible(((void*)currentNode) - 4, flag, value);
		currentNode = (nw4rLinkListNode*)currentNode->prev;
	}
	//OSReport("} %08X\n", pane);
}

void LayoutSetAllVisible(void *layout, int flag, bool value) {
	// get the rootpane from offset 0x10
	PaneSetAllVisible(*((void**)(layout+0x10)), flag, value);
}


void *LoadFile(FileHandle *handle, char *name) {
	int entryNum = DVDConvertPathToEntrynum(name);
	
	DVDHandle dvdhandle;
	if (!DVDFastOpen(entryNum, &dvdhandle)) {
		OSReport("Cannot open file %s\n", name);
		return 0;
	}
	
	handle->length = dvdhandle.length;
	handle->filePtr = EGG__Heap__alloc((handle->length+0x1F) & ~0x1F, 0x20, GetArchiveHeap());
	
	int ret = DVDReadPrio(&dvdhandle, handle->filePtr, (handle->length+0x1F) & ~0x1F, 0, 2);
	//OSReport("retval: %x\n", ret);
	
	DVDClose(&dvdhandle);
	
	return handle->filePtr;
}

bool FreeFile(FileHandle *handle) {
	if (!handle) return false;
	
	if (handle->filePtr)
		EGG__Heap__free(handle->filePtr, GetArchiveHeap());
	
	return true;
}

void d2DWorldMap_c__LoadLayout(d2DWorldMap_c *self, char *arcName, char *prefix, char *brlytName) {
	//char *arc = NSMBWLoadFile(&self->fileHandle, arcName, 0, *((void**)0x8042A72C));
	//char *arc = NSMBWLoadFile(&self->fileHandle, arcName, 0, 0);
	char *arc = (char*)LoadFile(&self->fileHandle, arcName);
	if (arc == 0) {
		OSReport("Can't load %s :(\n", arcName);
		while (true) { }
	}
	
	// allocate 0xC0 space for the ArcResourceAccessor
	// unsure what the actual size is, but we should be safe with that
	self->ResAcc = AllocFromGameHeap1(0xC0);
	nsmbw__ArcResourceAccessor__ArcResourceAccessor(self->ResAcc);
#ifdef REGION_NTSC
	*((void**)self->ResAcc) = (void*)0x802FE410; // replace vtable with NSMB's version
#endif
#ifdef REGION_PAL
	*((void**)self->ResAcc) = (void*)0x802FE710; // replace vtable with NSMB's version
#endif
	nsmbw__ArcResourceAccessor__Set(self->ResAcc, arc, prefix);
	
	char *brlyt = nsmbw__ArcResourceAccessor__GetResource(self->ResAcc, 0x626C7974, brlytName, 0);
	
	// allocate 0x20 space for the Layout
	self->Layout = AllocFromGameHeap1(0x20);
	lyt__Layout__Layout(self->Layout);
	lyt__Layout__Build(self->Layout, brlyt, self->ResAcc);
	
	// allocate 0x54 space for the DrawInfo
	self->DrawInfo = (nw4r__lyt__DrawInfo*)AllocFromGameHeap1(0x54);
	lyt__DrawInfo__DrawInfo(self->DrawInfo);
	
	// set up the anims
	self->BrlanArray = 0;
	self->BrlanCount = 0;
	
	//PSMTXTransApply(self->DrawInfo->matrix, self->DrawInfo->matrix, 304, 228, 0);
	
	/*self->DrawInfo->left = 0;
	self->DrawInfo->top = 0;
	self->DrawInfo->right = 608;
	self->DrawInfo->bottom = 456;*/
}

void d2DWorldMap_c__LoadAnims(d2DWorldMap_c *self, const char **brlanNames, int brlanCount) {
	//OSReport("Loading anims\n");
	void *buf = __nwa(brlanCount*0x1C + 0x10);
	//OSReport("Alloced buffer: %08X\n", buf);
	// function params are BrlanHandler_ctor, BrlanHandler_dtor
	self->BrlanArray = construct_new_array(buf, (void*)0x800C9150, (void*)0x800C91A0, 0x1C, brlanCount);
	//OSReport("Constructed array: %08X\n", self->BrlanArray);
	
	// huge hack coming up
	struct TrickClass_t {
		void *pad1;
		void *resacc;
		void *pad2;
		void *pad3;
		void *pad4;
		void *pad5;
		void *pad6;
		void *pad7;
	} TrickClass;
	TrickClass.resacc = self->ResAcc;
	//OSReport("Created TrickClass\n");
	
	for (int i = 0; i < brlanCount; i++) {
		//OSReport("b@%08X\n", self->BrlanArray+(0x1C*i));
		//OSReport("Loading brlan %d %s\n", i, brlanNames[i]);
		NSMBWBrlan__Load(self->BrlanArray+(0x1C*i), brlanNames[i], (void*)(&TrickClass), self->Layout, 1);
		//OSReport("Done loading brlan %d %s\n", i, brlanNames[i]);
		//OSReport("a@%08X\n", self->BrlanArray+(0x1C*i));
	}
	OSReport("Done loading brlans\n");
	
	/*char *brlan = nsmbw__ArcResourceAccessor__GetResource(self->ResAcc, 0x616e696d, brlanName, 0);
	OSReport("grabbed brlan %s to %08X\n", brlanName, brlan);
	
	// call CreateAnimTransform
	CreateAnimTransformFunc CreateAnimTransform = (*((CreateAnimTransformFunc**)self->Layout))[5];
	void *atf = CreateAnimTransform(self->Layout, brlan, self->ResAcc);
	
	// call BindAnimation
	BindAnimationFunc BindAnimation = (*((BindAnimationFunc**)self->Layout))[7];
	BindAnimation(self->Layout, atf);
	
	return atf;*/
}

void d2DWorldMap_c__SetAnimationEnable(d2DWorldMap_c *self, void *anim, bool val) {
	SetAnimationEnableFunc SetAnimationEnable = (*((SetAnimationEnableFunc**)self->Layout))[11];
	SetAnimationEnable(self->Layout, anim, val);
}


void d2DWorldMap_c__FreeLayout(d2DWorldMap_c *self) {
	if (self->BrlanArray) {
		for (int i = 0; i < self->BrlanCount; i++) {
			NSMBWBrlan__Free(self->BrlanArray+(0x1C*i));
		}
		
		// function param is BrlanHandler_dtor
		DeleteArray(self->BrlanArray, (void*)0x800C91A0);
	}
	
	lyt__Layout__dt(self->Layout, 0);
	nsmbw__ArcResourceAccessor__dt(self->ResAcc, 0);
	lyt__DrawInfo__dt(self->DrawInfo, 0);
	
	FreeFromGameHeap1(self->ResAcc);
	FreeFromGameHeap1(self->Layout);
	FreeFromGameHeap1(self->DrawInfo);
	
	//NSMBWFreeFile(&self->fileHandle);
	FreeFile(&self->fileHandle);
}



const char *bannerStart = "banner_start.brlan";
const char *bannerLoop = "banner_loop.brlan";

bool d2DWorldMap_c__onCreate(d2DWorldMap_c *self) {
	OSReport("Starting\n");
	//d2DWorldMap_c__LoadLayout(self, testArc, "arc", "messageWindow_02.brlyt");
	d2DWorldMap_c__LoadLayout(self, "/NewerRes/wmap.arc", "arc", "banner.brlyt");
	OSReport("Loaded\n");
	//d2DWorldMap_c__LoadLayout(self, "/NewerRes/wmap.arc", "arc", "openingTitle_EU_00.brlyt");
	//d2DWorldMap_c__LoadLayout(self, "/NewerRes/wmap.arc", "arc", "pauseMenu_16.brlyt");
	//self->StartAnim = d2DWorldMap_c__LoadAnim(self, "banner_start.brlan");
	//OSReport("startanim: %X\n", self->StartAnim);
	//self->LoopAnim = d2DWorldMap_c__LoadAnim(self, "banner_loop.brlan");
	
	const char* anims[2];
	anims[0] = bannerStart;
	anims[1] = bannerLoop;
	OSReport("Set anims\n");
	
	d2DWorldMap_c__LoadAnims(self, anims, 2);
	OSReport("Loaded anims\n");
	OSReport("layout is at %8X\n", self->Layout); // 815e7450
	OSReport("Enabled anims\n");
	d2DWorldMap_c__SetAnimationEnable(self, (void*)0x815ED040, true);
	
	//LayoutMakeAllVisible(self->Layout, true);
	
	/*bool (*idk)(int) = (bool(*)(int))0x800B0DB0;
	idk(0);
	
	bool (*idk2)(int*) = (bool(*)(int*))0x800E4940;
	int blah = 0xFF;
	idk2(&blah);*/
	
	return true;
}

bool d2DWorldMap_c__onDelete(d2DWorldMap_c *self) {
	//d2DWorldMap_c__FreeLayout(self);
	return true;
}

bool d2DWorldMap_c__onExecute(d2DWorldMap_c *self) {
	// call Animate()
	AnimateFunc Animate = (*((AnimateFunc**)self->Layout))[14];
	Animate(self->Layout, true);
	return true;
}

bool d2DWorldMap_c__onDraw(d2DWorldMap_c *self) {
	MTX44 matrix;
	MTXOrtho(matrix, 228, -228, -304, 304, 0, 1);
	GXSetProjection(matrix, GX_ORTHOGRAPHIC);
	
	// call CalculateMtx()
	CalculateMtxFunc CalculateMtx = (*((CalculateMtxFunc**)self->Layout))[12];
	CalculateMtx(self->Layout, self->DrawInfo);
	// call Draw()
	DrawFunc Draw = (*((DrawFunc**)self->Layout))[13];
	Draw(self->Layout, self->DrawInfo);
	return true;
}