//--------------------------------------------------------------------------- #include #include #pragma hdrstop #include "TextureArrange.h" #include "Exception.h" //--------------------------------------------------------------------------- #pragma package(smart_init) //--------------------------------------------------------------------------- #define PATTERN 8 //--------------------------------------------------------------------------- // TTextureChoice //--------------------------------------------------------------------------- TPException __fastcall TTextureChoice::LoadFromFile(HANDLE fh) { DWORD dwRead; TPException ec = EC_NONE; if (!ReadFile(fh, &group1, sizeof(BYTE), &dwRead, NULL)) goto fail; if (!ReadFile(fh, &group2, sizeof(BYTE), &dwRead, NULL)) goto fail; if (!ReadFile(fh, group3, TNameLength, &dwRead, NULL)) goto fail; if (!ReadFile(fh, group4, TNameLength, &dwRead, NULL)) goto fail; if (!ReadFile(fh, &width, sizeof(short), &dwRead, NULL)) goto fail; if (!ReadFile(fh, &height, sizeof(short), &dwRead, NULL)) goto fail; if ((data = new Byte[width*height]) == NULL) { ec = EC_MEMORY_LACK; goto fail; } if (!ReadFile(fh, data, width*height, &dwRead, NULL)) goto fail; return EC_NONE; fail: if (data) { delete[] data; data = NULL; } if (ec == EC_NONE) ec = EC_FILE_NOT_READ; return ec; } //--------------------------------------------------------------------------- TPException __fastcall TTextureChoice::SaveToFile(HANDLE fh) { DWORD dwWrite; if (!WriteFile(fh, &group1, sizeof(BYTE), &dwWrite, NULL)) goto fail; if (!WriteFile(fh, &group2, sizeof(BYTE), &dwWrite, NULL)) goto fail; if (!WriteFile(fh, group3, TNameLength, &dwWrite, NULL)) goto fail; if (!WriteFile(fh, group4, TNameLength, &dwWrite, NULL)) goto fail; if (!WriteFile(fh, &width, sizeof(short), &dwWrite, NULL)) goto fail; if (!WriteFile(fh, &height, sizeof(short), &dwWrite, NULL)) goto fail; if (!WriteFile(fh, data, width*height, &dwWrite, NULL)) goto fail; return EC_NONE; fail: return EC_FILE_NOT_WRITE; } //--------------------------------------------------------------------------- // TTextureArrange //--------------------------------------------------------------------------- __fastcall TTextureArrange::TTextureArrange() { Array = new TList; ChoiceCount = 0; bFinish = false; bNormal = true; for (int i = 0; i < 9; i++) { Choice[i].data = NULL; Choice[i].width = Choice[i].height = 0; } for (int j = 0; j < 9; j++) { tm[j].map[0] = NULL; tm[j].map[1] = NULL; } } //--------------------------------------------------------------------------- __fastcall TTextureArrange::~TTextureArrange() { TTextureArray *texarray; for (int j = 0; j < 9; j++) { if (tm[j].map[0]) { HeapFree(GetProcessHeap(), 0, tm[j].map[0]); tm[j].map[0] = NULL; } if (tm[j].map[1]) { HeapFree(GetProcessHeap(), 0, tm[j].map[1]); tm[j].map[1] = NULL; } } for (int i = 0; i < 9; i++) { if (Choice[i].data) { HeapFree(GetProcessHeap(), 0, Choice[i].data); Choice[i].data = NULL; } } while(Array->Count) { texarray = (TTextureArray *) Array->Last(); Array->Remove(texarray); delete texarray; } delete Array; Array = NULL; } //--------------------------------------------------------------------------- void __fastcall TTextureArrange::InitChoiceBitData(int i, int w, int h, Byte *Bitmap) { Choice[i].width = w; Choice[i].height = h; Choice[i].data = NULL; Choice[i].data = (Byte *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, w*h); memcpy(Choice[i].data, Bitmap, w*h); if (Choice[i].data == NULL) { #ifdef TPDEBUG SHOWDEBUG; #endif } } //--------------------------------------------------------------------------- void __fastcall TTextureArrange::DeleteChoiceBitData(int i) { if (Choice[i].data) { HeapFree(GetProcessHeap(), 0, Choice[i].data); Choice[i].data = NULL; } } //--------------------------------------------------------------------------- void __fastcall TTextureArrange::AddArray(int c, int r) { TTextureArray *texarray = new TTextureArray; texarray->code = c; texarray->repeat = r; texarray->size = Choice[c-1].width * r; Array->Add(texarray); } //--------------------------------------------------------------------------- void __fastcall TTextureArrange::InsertArray(int i) { TTextureArray *texarray = new TTextureArray; texarray->code = 1; texarray->repeat = 0; texarray->size = 0; Array->Insert(i, texarray); } //--------------------------------------------------------------------------- void __fastcall TTextureArrange::RemoveArray(int i) { TTextureArray *texarray; texarray = (TTextureArray *) Array->Items[i]; Array->Remove(texarray); delete texarray; } //--------------------------------------------------------------------------- void __fastcall TTextureArrange::ClearArray() { TTextureArray *texarray; while(Array->Count) { texarray = (TTextureArray *) Array->Last(); Array->Remove(texarray); delete texarray; } } //--------------------------------------------------------------------------- TPException __fastcall TTextureArrange::LoadFromFile(HANDLE fh, TTexVersion &v) { DWORD dwRead; int i, j, n; BYTE c, group[4]; TTextureArray *ta = NULL; TPException ec = EC_NONE; if (v.Texpia=='P') { if (v.Method=='T' && v.Number>=200) { // Windows Version if (!ReadFile(fh, &ChoiceCount, sizeof(int), &dwRead, NULL)) goto fail; if (!ReadFile(fh, &c, sizeof(BYTE), &dwRead, NULL)) goto fail; if (v.Number>=240){ for (i=0, j=1; i<8; i++, j<<=1) { if (c&j) { if ((ec = Choice[i].LoadFromFile(fh)) != EC_NONE) goto fail; } } }else{ for (i=0, j=1; i<4; i++, j<<=1) { if (c&j) { if ((ec = Choice[i].LoadFromFile(fh)) != EC_NONE) goto fail; } } } } else { // DOS Version if (!ReadFile(fh, &c, sizeof(BYTE), &dwRead, NULL)) goto fail; ChoiceCount = c; for (i=0; i=240){ if ((ec = Choice[PATTERN].LoadFromFile(fh)) != EC_NONE) goto fail; if (!ReadFile(fh, &bNormal, sizeof(bool), &dwRead, NULL)) goto fail; if (bNormal) { if ((Array = new TList) == NULL) { ec = EC_MEMORY_LACK; goto fail; } if (!ReadFile(fh, &n, sizeof(int), &dwRead, NULL)) goto fail; for (i=0; iAdd(ta); ta = NULL; } } }else{ if ((Array = new TList) == NULL) { ec = EC_MEMORY_LACK; goto fail; } if (!ReadFile(fh, &n, sizeof(int), &dwRead, NULL)) goto fail; for (i=0; isize = Choice[ta->code-1].width*ta->repeat; //by qe size°¡ À߸ø ÀúÀåµÇ¾îÀÖ´Â ÆÄÀÏÀÌ À־..´Ù½Ã°è»ê Array->Add(ta); ta = NULL; } ArrayToPatternData(); bNormal = true; } bFinish = true; } return EC_NONE; fail: if (ta) delete ta; if (ec == EC_NONE) ec = EC_FILE_NOT_READ; return ec; } //--------------------------------------------------------------------------- TPException __fastcall TTextureArrange::SaveToFile(HANDLE fh) { DWORD dwWrite; int i, j, n; BYTE c; TTextureArray *ta; TPException ec = EC_NONE; if (!WriteFile(fh, &ChoiceCount, sizeof(int), &dwWrite, NULL)) goto fail; c = 0; for (i=0, j=1; i<8; i++, j<<=1) { if (Choice[i].data) c |= j; } if (!WriteFile(fh, &c, sizeof(BYTE), &dwWrite, NULL)) goto fail; for (i=0; i<9; i++) { if (Choice[i].data) { if ((ec = Choice[i].SaveToFile(fh)) != EC_NONE) goto fail; } } if(!WriteFile(fh, &bNormal, sizeof(bool), &dwWrite, NULL)) goto fail; if(bNormal){ n = Array->Count; if (!WriteFile(fh, &n, sizeof(int), &dwWrite, NULL)) goto fail; for (i=0; iItems[i]; if (!WriteFile(fh, ta, sizeof(TTextureArray), &dwWrite, NULL)) goto fail; } } return EC_NONE; fail: if (ec == EC_NONE) ec = EC_FILE_NOT_WRITE; return ec; } //--------------------------------------------------------------------------- bool __fastcall TTextureArrange::InitTexture() { Byte *sp, *dp; int c, x, y, limit, size; Byte *td; for (int i = 0; i < ChoiceCount; i++) { td = Choice[i].data; size = Choice[i].width * Choice[i].height; if ((tm[i].map[0] = (Byte *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size)) == NULL) goto fail; if ((tm[i].map[1] = (Byte *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size)) == NULL) goto fail; tm[i].cnt = 0; for (x = 0; x < Choice[i].width; x++) { for (c = 0; c < tm[i].cnt; c++) { sp = td + x; dp = tm[i].map[0] + c; for (y = 0; y < Choice[i].height; y++) { if (*sp != *dp) break; sp += Choice[i].width; dp += Choice[i].width; } if (y >= Choice[i].height) break; } if (c >= tm[i].cnt) { sp = td + x; dp = tm[i].map[0] + c; for (y = 0; y < Choice[i].height; y++) { *dp = *sp; sp += Choice[i].width; dp += Choice[i].width; } tm[i].cnt++; } limit = c * Choice[i].width + x; if (limit < size) { *(tm[i].map[1] + c*Choice[i].width + x) = 1; } } } return true; fail: ExitTexture(); return false; } //--------------------------------------------------------------------------- void __fastcall TTextureArrange::ExitTexture() { for (int i = 0; i < ChoiceCount; i++) { if (tm[i].map[0]) { HeapFree(GetProcessHeap(), 0, tm[i].map[0]); tm[i].map[0] = NULL; } if (tm[i].map[1]) { HeapFree(GetProcessHeap(), 0, tm[i].map[1]); tm[i].map[1] = NULL; } } } //--------------------------------------------------------------------------- int __fastcall TTextureArrange::GetLCM(int x, int y) /* ÃÖ¼Ò°ø¹è¼ö */ { int temp, a, b; if (x < 1) x = 1; if (y < 1) y = 1; a = x, b = y; while(a != b){ //ÃÖ´ë°ø¾à¼ö ±¸ÇÔ temp = min(a,b); a = max(a, b) - min(a, b); b = temp; } return x*y/a; //ÃÖ´ë °ø¹è¼ö = µÎ¼öÀÇ °ö/ÃÖ´ë°ø¾à¼ö } //--------------------------------------------------------------------------- void __fastcall TTextureArrange::ArrayToPatternData() { int h, i, size, code, dx, dy , x, y, Start; int PatternX = 0, PatternY = 0; Byte *DP, *PP; Start = 0; for(i = 0; i< Array->Count; i++){ code = ((TTextureArray *)Array->Items[i])->code -1; h = Choice[code].height; PatternX += ((TTextureArray *)Array->Items[i])->size; PatternY = GetLCM(h, PatternY); } DeleteChoiceBitData(PATTERN); Choice[PATTERN].width = PatternX; Choice[PATTERN].height = PatternY; Choice[PATTERN].data = NULL; Choice[PATTERN].data = (Byte *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, PatternX*PatternY); PP = Choice[PATTERN].data; for(i = 0; iCount; i++){ code = ((TTextureArray *)Array->Items[i])->code -1; size = ((TTextureArray *)Array->Items[i])->size; DP = Choice[code].data; for (y = 0, dy = 0; y < Choice[PATTERN].height; y++,dy++){ if(dy >= Choice[code].height) dy = 0; for(x = 0,dx = 0; x < size; x++,dx++){ if(dx >= Choice[code].width) dx = 0; *(PP + Choice[PATTERN].width*y + Start + x) = *(DP + Choice[code].width*dy + dx); } } Start = Start + size; } } //----------------------------------------------------------------------------