diff --git a/Source/capture.cpp b/Source/capture.cpp index 34002df77..f46a7de64 100644 --- a/Source/capture.cpp +++ b/Source/capture.cpp @@ -4,194 +4,164 @@ void __cdecl CaptureScreen() { - int v4; // edi - PALETTEENTRY palette[256]; // [esp+0h] [ebp-508h] - char FileName[260]; // [esp+400h] [ebp-108h] - HANDLE hObject; // [esp+504h] [ebp-4h] + PALETTEENTRY palette[256]; + char FileName[MAX_PATH]; - hObject = CaptureFile(FileName); - if ( hObject != (HANDLE)-1 ) + HANDLE hObject = CaptureFile(FileName); + if ( hObject != INVALID_HANDLE_VALUE) { DrawAndBlit(); lpDDPalette->GetEntries(0, 0, 256, palette); RedPalette(palette); + dx_lock_mutex(); - v4 = CaptureHdr(hObject, 640, 480); - if ( v4 ) + bool success = CaptureHdr(hObject, 640, 480); + if (success) { - v4 = CapturePix(hObject, 640, 480, 768, gpBuffer->row[0].pixels); - if ( v4 ) + success = CapturePix(hObject, 640, 480, 768, (BYTE*)gpBuffer->row[0].pixels); + if (success) { - v4 = CapturePal(hObject, palette); + success = CapturePal(hObject, palette); } } dx_unlock_mutex(); CloseHandle(hObject); - if ( !v4 ) + + if (!success) DeleteFileA(FileName); + Sleep(300); lpDDPalette->SetEntries(0, 0, 256, palette); } } -// 40311B: could not find valid save-restore pair for ebx -// 40311B: could not find valid save-restore pair for edi -// 40311B: could not find valid save-restore pair for esi -bool __fastcall CaptureHdr(HANDLE hFile, short width, int height) +bool __fastcall CaptureHdr(HANDLE hFile, short width, short height) { - short v3; // si - HANDLE v4; // ebx - PCXHeader Buffer; // [esp+Ch] [ebp-84h] - DWORD lpNumBytes; // [esp+8Ch] [ebp-4h] - - v3 = width; - v4 = hFile; - memset(&Buffer, 0, 0x80u); - Buffer.xmax = v3 - 1; + PCXHeader Buffer; + memset(&Buffer, 0, sizeof(Buffer)); + + Buffer.xmax = width - 1; Buffer.vertRes = height; Buffer.manufacturer = 10; Buffer.version = 5; Buffer.encoding = 1; Buffer.bitsPerPixel = 8; Buffer.ymax = height - 1; - Buffer.horzRes = v3; + Buffer.horzRes = width; Buffer.numColorPlanes = 1; - Buffer.bytesPerScanLine = v3; - return WriteFile(v4, &Buffer, 0x80u, &lpNumBytes, NULL) && lpNumBytes == 128; + Buffer.bytesPerScanLine = width; + + DWORD lpNumBytes; + return WriteFile(hFile, &Buffer, sizeof(Buffer), &lpNumBytes, NULL) && lpNumBytes == sizeof(Buffer); } bool __fastcall CapturePal(HANDLE hFile, PALETTEENTRY *palette) { - BYTE *v2; // eax - char *v3; // esi - signed int v4; // edx - char Buffer[772]; // [esp+8h] [ebp-308h] - DWORD lpNumBytes; // [esp+30Ch] [ebp-4h] + char *v3; + char Buffer[769]; - v2 = &palette->peBlue; Buffer[0] = 12; - v3 = &Buffer[2]; - v4 = 256; - do + v3 = &Buffer[1]; + for (int i = 256; i != 0; --i) { - *(v3 - 1) = *(v2 - 2); - *v3 = *(v2 - 1); - v3[1] = *v2; - v2 += 4; + v3[0] = palette->peRed; + v3[1] = palette->peGreen; + v3[2] = palette->peBlue; + + palette++; v3 += 3; - --v4; } - while ( v4 ); - return WriteFile(hFile, Buffer, 0x301u, &lpNumBytes, 0) && lpNumBytes == 769; + + DWORD lpNumBytes; + return WriteFile(hFile, Buffer, sizeof(Buffer), &lpNumBytes, NULL) && lpNumBytes == sizeof(Buffer); } -bool __fastcall CapturePix(HANDLE hFile, short width, short height, short stride, char *pixels) +bool __fastcall CapturePix(HANDLE hFile, WORD width, WORD height, WORD stride, BYTE *pixels) { - int v5; // esi - char *v6; // edi - char *v7; // eax - int v8; // ebx - DWORD lpNumBytes; // [esp+Ch] [ebp-8h] - HANDLE hFilea; // [esp+10h] [ebp-4h] - - v5 = (unsigned short)width; - hFilea = hFile; - v6 = (char *)DiabloAllocPtr(2 * (unsigned short)width); + int writeSize; + DWORD lpNumBytes; + + BYTE *pBuffer = (BYTE *)DiabloAllocPtr(2 * width); do { if ( !height ) { - mem_free_dbg(v6); + mem_free_dbg(pBuffer); return 1; } - *(_DWORD *)&height = height + 0xFFFF; - v7 = CaptureEnc(pixels, v6, v5); - pixels += (unsigned short)stride; - v8 = v7 - v6; + height--; + BYTE *pBufferEnd = CaptureEnc(pixels, pBuffer, width); + pixels += stride; + writeSize = pBufferEnd - pBuffer; } - while ( WriteFile(hFilea, v6, v7 - v6, &lpNumBytes, 0) && lpNumBytes == v8 ); + while (WriteFile(hFile, pBuffer, writeSize, &lpNumBytes, 0) && lpNumBytes == writeSize); return 0; } -char *__fastcall CaptureEnc(char *src, char *dst, int width) +BYTE *__fastcall CaptureEnc(BYTE *src, BYTE *dst, int width) { - int v3; // esi - char v4; // bl - signed int v5; // eax - - v3 = width; do { - v4 = *src++; - v5 = 1; - --v3; - if ( v4 == *src ) + BYTE rlePixel = *src++; + --width; + + int rleLength = 1; + while (rlePixel == *src) { - do - { - if ( v5 >= 63 ) - break; - if ( !v3 ) - break; - ++v5; - --v3; - ++src; - } - while ( v4 == *src ); - if ( v5 > 1 ) - goto LABEL_13; + if (rleLength >= 63) + break; + if (!width) + break; + ++rleLength; + + --width; + ++src; } - if ( (unsigned char)v4 > 0xBFu ) -LABEL_13: - *dst++ = v5 | 0xC0; - *dst++ = v4; - } - while ( v3 ); + + if (rlePixel > 0xBF || rleLength > 1) + { + *dst++ = rleLength | 0xC0; + } + *dst++ = rlePixel; + } while (width); return dst; } HANDLE __fastcall CaptureFile(char *dst_path) { - char *v1; // edi - __int32 v2; // esi - int v3; // eax - int v5; // [esp-4h] [ebp-18Ch] - struct _finddata_t v6; // [esp+Ch] [ebp-17Ch] - char v7[100]; // [esp+124h] [ebp-64h] - - v1 = dst_path; - memset(v7, 0, 0x64u); - v2 = _findfirst("screen??.PCX", &v6); - if ( v2 != -1 ) + bool num_used[100] = { false }; + + _finddata_t finder; + int hFind = _findfirst("screen??.PCX", &finder); + if (hFind != -1) { do { - if ( isdigit(v6.name[6]) ) + if (isdigit(finder.name[6]) && isdigit(finder.name[7])) { - if ( isdigit(v6.name[7]) ) - v7[10 * v6.name[6] - 528 + v6.name[7]] = 1; + num_used[10 * (finder.name[6] - '0') + (finder.name[7] - '0')] = true; } } - while ( !_findnext(v2, &v6) ); + while (_findnext(hFind, &finder) == 0); } - v3 = 0; - while ( v7[v3] ) + + int free_num = 0; + while (num_used[free_num]) { - if ( ++v3 >= 100 ) - return (HANDLE)-1; + ++free_num; + if (free_num >= 100) + return INVALID_HANDLE_VALUE; } - v5 = v3; - sprintf(v1, "screen%02d.PCX", v3); - return CreateFileA(v1, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + + sprintf(dst_path, "screen%02d.PCX", free_num); + return CreateFileA(dst_path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); } -// 4033A8: using guessed type char var_64[100]; void __fastcall RedPalette(PALETTEENTRY *pal) { - int i; // eax - PALETTEENTRY red[256]; // [esp+Ch] [ebp-400h] + PALETTEENTRY red[256]; - for(i = 0; i < 256; i++) + for(int i = 0; i < 256; i++) { red[i].peRed = pal[i].peRed; red[i].peGreen = 0; diff --git a/Source/capture.h b/Source/capture.h index 3f3b6e8c3..4c82cb4b6 100644 --- a/Source/capture.h +++ b/Source/capture.h @@ -3,10 +3,10 @@ #define __CAPTURE_H__ void __cdecl CaptureScreen(); -bool __fastcall CaptureHdr(HANDLE hFile, short width, int height); +bool __fastcall CaptureHdr(HANDLE hFile, short width, short height); bool __fastcall CapturePal(HANDLE hFile, PALETTEENTRY *palette); -bool __fastcall CapturePix(HANDLE hFile, short width, short height, short stride, char *pixels); -char *__fastcall CaptureEnc(char *src, char *dst, int width); +bool __fastcall CapturePix(HANDLE hFile, WORD width, WORD height, WORD stride, BYTE *pixels); +BYTE *__fastcall CaptureEnc(BYTE *src, BYTE *dst, int width); HANDLE __fastcall CaptureFile(char *dst_path); void __fastcall RedPalette(PALETTEENTRY *pal);