summaryrefslogtreecommitdiff
path: root/src/T2DLL/UT2BkgndInfo.cpp
blob: 14e62aae9811247b7e58c05a453c9af2df86cd25 (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
#include "UT2BkgndInfo.h"

/*static*/ BkgndInfo* UT2BkgndInfo::SetupBkgndInfo(const RECT& rect, int z) {
	BkgndInfo *bkgndInfoPtr = NULL;

    int size;
	int vRange = (rect.bottom - rect.top) / 2;
	int hRange = (rect.right - rect.left) / 8;
	int zRange = z / 2;

	if (vRange > 0 && hRange > 0) {
		size = sizeof(BkgndInfo) + sizeof(void *) * (vRange - 1);
		bkgndInfoPtr = (BkgndInfo *) malloc(size);
		memset(bkgndInfoPtr, 0, size);

		bkgndInfoPtr->vRange = vRange;
		bkgndInfoPtr->hRange = hRange;
		bkgndInfoPtr->zRange = zRange;

		for (int i = 0; i < vRange && bkgndInfoPtr; i++) {
			size = sizeof(unsigned int) * hRange;
			bkgndInfoPtr->arrays[i] = (unsigned int *) malloc(size);
			memset(bkgndInfoPtr->arrays[i], 0, size);
			if (!bkgndInfoPtr->arrays[i])
				DisposeBkgndInfo(bkgndInfoPtr);
		}
	}

	return bkgndInfoPtr;
}

/*static*/ void UT2BkgndInfo::DisposeBkgndInfo(BkgndInfo*& info) {
	if (info) {
		for (int i = 0; i < info->vRange; i++) {
			if (info->arrays[i])
				free(info->arrays[i]);
		}

		free(info);
		info = NULL;
	}
}

/*static*/ unsigned int UT2BkgndInfo::GetBkgndInfo(BkgndInfo* const inBkgndInfoPtr, int inV, int inH) {
	unsigned int result = 0;

	if (!inBkgndInfoPtr)
		return 0;
	if (inV < 0 || inV >= inBkgndInfoPtr->vRange)
		return 0;
	if (inH < 0 || inH >= inBkgndInfoPtr->hRange)
		return 0;

	unsigned int value = inBkgndInfoPtr->arrays[inV][inH];
	value &= 0xFFFF;
	result = value;
	return result;
}

/*static*/ void UT2BkgndInfo::UnitToBkgndRect(const RECT& inRect, RECT& outRect) {
	outRect.top = inRect.top / 2;
	outRect.left = inRect.left / 8;
	outRect.bottom = inRect.bottom / 2;
	outRect.right = inRect.right / 8;

	if ((inRect.bottom % 2) != 0)
		outRect.bottom++;
	if ((inRect.right % 8) != 0)
		outRect.right++;
}

/*static*/ void UT2BkgndInfo::BkgndToUnitRect(const RECT& inRect, RECT& outRect) {
	outRect.top = inRect.top * 2;
	outRect.left = inRect.left * 8;
	outRect.bottom = inRect.bottom * 2;
	outRect.right = inRect.right * 8;
}

/*static*/ void UT2BkgndInfo::GetOffBkgndRect(int offset, RECT& rect) {
	SetRect(&rect, 0, 0, 8, 2);
	OffsetRect(&rect, 0, offset * 2);
}

/*static*/ void UT2BkgndInfo::ReplaceID(const BkgndInfo* info, unsigned int a, int b) {
	for (int v = 0; v < info->vRange; v++) {
		for (int h = 0; h < info->hRange; h++) {
			unsigned int *p = &info->arrays[v][h];
			if ((*p & 0xFFFF) >= a)
				*p += b;
		}
	}}