#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; } }}