From 553086dca7ca2edf9d9d21324848a89f53879f10 Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Mon, 22 Oct 2018 01:35:03 +0200 Subject: [PATCH] Format graphics related files --- Source/capture.cpp | 264 ++++++++++++++-------------- Source/dx.cpp | 417 ++++++++++++++++++++++----------------------- Source/dx.h | 4 +- Source/palette.cpp | 404 +++++++++++++++++++++---------------------- 4 files changed, 520 insertions(+), 569 deletions(-) diff --git a/Source/capture.cpp b/Source/capture.cpp index 8e9e4ac8d..26f4a155a 100644 --- a/Source/capture.cpp +++ b/Source/capture.cpp @@ -4,170 +4,154 @@ void __cdecl CaptureScreen() { - PALETTEENTRY palette[256]; - char FileName[MAX_PATH]; - - HANDLE hObject = CaptureFile(FileName); - if ( hObject != INVALID_HANDLE_VALUE) - { - DrawAndBlit(); - lpDDPalette->GetEntries(0, 0, 256, palette); - RedPalette(palette); - - j_lock_buf_priv(2); - bool success = CaptureHdr(hObject, 640, 480); - if (success) - { - success = CapturePix(hObject, 640, 480, 768, (BYTE*)gpBuffer->row[0].pixels); - if (success) - { - success = CapturePal(hObject, palette); - } - } - j_unlock_buf_priv(2); - CloseHandle(hObject); - - if (!success) - DeleteFile(FileName); - - Sleep(300); - lpDDPalette->SetEntries(0, 0, 256, palette); - } + PALETTEENTRY palette[256]; + char FileName[MAX_PATH]; + + HANDLE hObject = CaptureFile(FileName); + if (hObject != INVALID_HANDLE_VALUE) { + DrawAndBlit(); + lpDDPalette->GetEntries(0, 0, 256, palette); + RedPalette(palette); + + j_lock_buf_priv(2); + bool success = CaptureHdr(hObject, 640, 480); + if (success) { + success = CapturePix(hObject, 640, 480, 768, (BYTE *)gpBuffer->row[0].pixels); + if (success) { + success = CapturePal(hObject, palette); + } + } + j_unlock_buf_priv(2); + CloseHandle(hObject); + + if (!success) + DeleteFile(FileName); + + Sleep(300); + lpDDPalette->SetEntries(0, 0, 256, palette); + } } bool __fastcall CaptureHdr(HANDLE hFile, short width, short height) { - 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 = width; - Buffer.numColorPlanes = 1; - Buffer.bytesPerScanLine = width; - - DWORD lpNumBytes; - return WriteFile(hFile, &Buffer, sizeof(Buffer), &lpNumBytes, NULL) && lpNumBytes == sizeof(Buffer); + 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 = width; + Buffer.numColorPlanes = 1; + Buffer.bytesPerScanLine = width; + + DWORD lpNumBytes; + return WriteFile(hFile, &Buffer, sizeof(Buffer), &lpNumBytes, NULL) && lpNumBytes == sizeof(Buffer); } bool __fastcall CapturePal(HANDLE hFile, PALETTEENTRY *palette) { - char *v3; - char Buffer[769]; - - Buffer[0] = 12; - v3 = &Buffer[1]; - for (int i = 256; i != 0; --i) - { - v3[0] = palette->peRed; - v3[1] = palette->peGreen; - v3[2] = palette->peBlue; - - palette++; - v3 += 3; - } - - DWORD lpNumBytes; - return WriteFile(hFile, Buffer, sizeof(Buffer), &lpNumBytes, NULL) && lpNumBytes == sizeof(Buffer); + char *v3; + char Buffer[769]; + + Buffer[0] = 12; + v3 = &Buffer[1]; + for (int i = 256; i != 0; --i) { + v3[0] = palette->peRed; + v3[1] = palette->peGreen; + v3[2] = palette->peBlue; + + palette++; + v3 += 3; + } + + DWORD lpNumBytes; + return WriteFile(hFile, Buffer, sizeof(Buffer), &lpNumBytes, NULL) && lpNumBytes == sizeof(Buffer); } bool __fastcall CapturePix(HANDLE hFile, WORD width, WORD height, WORD stride, BYTE *pixels) { - int writeSize; - DWORD lpNumBytes; - - BYTE *pBuffer = (BYTE *)DiabloAllocPtr(2 * width); - do - { - if ( !height ) - { - mem_free_dbg(pBuffer); - return 1; - } - height--; - BYTE *pBufferEnd = CaptureEnc(pixels, pBuffer, width); - pixels += stride; - writeSize = pBufferEnd - pBuffer; - } - while (WriteFile(hFile, pBuffer, writeSize, &lpNumBytes, 0) && lpNumBytes == writeSize); - return 0; + int writeSize; + DWORD lpNumBytes; + + BYTE *pBuffer = (BYTE *)DiabloAllocPtr(2 * width); + do { + if (!height) { + mem_free_dbg(pBuffer); + return 1; + } + height--; + BYTE *pBufferEnd = CaptureEnc(pixels, pBuffer, width); + pixels += stride; + writeSize = pBufferEnd - pBuffer; + } while (WriteFile(hFile, pBuffer, writeSize, &lpNumBytes, 0) && lpNumBytes == writeSize); + return 0; } BYTE *__fastcall CaptureEnc(BYTE *src, BYTE *dst, int width) { - do - { - BYTE rlePixel = *src++; - --width; - - int rleLength = 1; - while (rlePixel == *src) - { - if (rleLength >= 63) - break; - if (!width) - break; - ++rleLength; - - --width; - ++src; - } - - if (rlePixel > 0xBF || rleLength > 1) - { - *dst++ = rleLength | 0xC0; - } - *dst++ = rlePixel; - } while (width); - return dst; + do { + BYTE rlePixel = *src++; + --width; + + int rleLength = 1; + while (rlePixel == *src) { + if (rleLength >= 63) + break; + if (!width) + break; + ++rleLength; + + --width; + ++src; + } + + if (rlePixel > 0xBF || rleLength > 1) { + *dst++ = rleLength | 0xC0; + } + *dst++ = rlePixel; + } while (width); + return dst; } HANDLE __fastcall CaptureFile(char *dst_path) { - bool num_used[100] = { false }; - - _finddata_t finder; - int hFind = _findfirst("screen??.PCX", &finder); - if (hFind != -1) - { - do - { - if (isdigit(finder.name[6]) && isdigit(finder.name[7])) - { - num_used[10 * (finder.name[6] - '0') + (finder.name[7] - '0')] = true; - } - } - while (_findnext(hFind, &finder) == 0); - } - - int free_num = 0; - while (num_used[free_num]) - { - ++free_num; - if (free_num >= 100) - return INVALID_HANDLE_VALUE; - } - - sprintf(dst_path, "screen%02d.PCX", free_num); - return CreateFile(dst_path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); + bool num_used[100] = { false }; + + _finddata_t finder; + int hFind = _findfirst("screen??.PCX", &finder); + if (hFind != -1) { + do { + if (isdigit(finder.name[6]) && isdigit(finder.name[7])) { + num_used[10 * (finder.name[6] - '0') + (finder.name[7] - '0')] = true; + } + } while (_findnext(hFind, &finder) == 0); + } + + int free_num = 0; + while (num_used[free_num]) { + ++free_num; + if (free_num >= 100) + return INVALID_HANDLE_VALUE; + } + + sprintf(dst_path, "screen%02d.PCX", free_num); + return CreateFile(dst_path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); } void __fastcall RedPalette(PALETTEENTRY *pal) { - PALETTEENTRY red[256]; + PALETTEENTRY red[256]; - for(int i = 0; i < 256; i++) - { - red[i].peRed = pal[i].peRed; - red[i].peGreen = 0; - red[i].peBlue = 0; - red[i].peFlags = 0; - } + for (int i = 0; i < 256; i++) { + red[i].peRed = pal[i].peRed; + red[i].peGreen = 0; + red[i].peBlue = 0; + red[i].peFlags = 0; + } - lpDDPalette->SetEntries(0, 0, 256, red); + lpDDPalette->SetEntries(0, 0, 256, red); } diff --git a/Source/dx.cpp b/Source/dx.cpp index dafc97549..e3b2025ca 100644 --- a/Source/dx.cpp +++ b/Source/dx.cpp @@ -14,285 +14,276 @@ IDirectDrawSurface *lpDDSPrimary; int locktbl[256]; #endif static CRITICAL_SECTION sgMemCrit; -char gbBackBuf; // weak -char gbEmulate; // weak +char gbBackBuf; // weak +char gbEmulate; // weak HMODULE ghDiabMod; // idb int dx_inf = 0x7F800000; // weak -struct dx_cpp_init_1 -{ - dx_cpp_init_1() - { - dx_cpp_init_value = dx_inf; - } +struct dx_cpp_init_1 { + dx_cpp_init_1() + { + dx_cpp_init_value = dx_inf; + } } _dx_cpp_init_1; // 47A464: using guessed type int dx_inf; // 52A514: using guessed type int dx_cpp_init_value; -struct dx_cpp_init_2 -{ - dx_cpp_init_2() - { - dx_init_mutex(); - dx_cleanup_mutex_atexit(); - } +struct dx_cpp_init_2 { + dx_cpp_init_2() + { + dx_init_mutex(); + dx_cleanup_mutex_atexit(); + } } _dx_cpp_init_2; void __cdecl dx_init_mutex() { - InitializeCriticalSection(&sgMemCrit); + InitializeCriticalSection(&sgMemCrit); } void __cdecl dx_cleanup_mutex_atexit() { - atexit(dx_cleanup_mutex); + atexit(dx_cleanup_mutex); } void __cdecl dx_cleanup_mutex() { - DeleteCriticalSection(&sgMemCrit); + DeleteCriticalSection(&sgMemCrit); } void __fastcall dx_init(HWND hWnd) { - HWND v1; // esi - GUID *v2; // ecx - int v3; // eax - int v4; // eax - //int v5; // ecx - int v6; // edi - int v7; // eax - int v8; // eax - HWND hWnda; // [esp+1Ch] [ebp-4h] - - v1 = hWnd; - hWnda = hWnd; - SetFocus(hWnd); - ShowWindow(v1, SW_SHOWNORMAL); - v2 = NULL; - if ( gbEmulate ) - v2 = (GUID *)DDCREATE_EMULATIONONLY; - v3 = dx_DirectDrawCreate(v2, &lpDDInterface, NULL); - if ( v3 ) - ErrDlg(IDD_DIALOG1, v3, "C:\\Src\\Diablo\\Source\\dx.cpp", 149); - fullscreen = 1; - v4 = lpDDInterface->SetCooperativeLevel(v1, DDSCL_EXCLUSIVE|DDSCL_ALLOWREBOOT|DDSCL_FULLSCREEN); - if ( v4 == DDERR_EXCLUSIVEMODEALREADYSET ) - { - MI_Dummy(0); // v5 - } - else if ( v4 ) - { - ErrDlg(IDD_DIALOG1, v4, "C:\\Src\\Diablo\\Source\\dx.cpp", 170); - } - if ( lpDDInterface->SetDisplayMode(640, 480, 8) ) - { - v6 = GetSystemMetrics(SM_CXSCREEN); - v7 = GetSystemMetrics(SM_CYSCREEN); - v8 = lpDDInterface->SetDisplayMode(v6, v7, 8); - if ( v8 ) - ErrDlg(IDD_DIALOG1, v8, "C:\\Src\\Diablo\\Source\\dx.cpp", 183); - } - dx_create_primary_surface(); - palette_init(); - GdiSetBatchLimit(1); - dx_create_back_buffer(); - SDrawManualInitialize(hWnda, lpDDInterface, lpDDSPrimary, 0, 0, lpDDSBackBuf, lpDDPalette, 0); + HWND v1; // esi + GUID *v2; // ecx + int v3; // eax + int v4; // eax + //int v5; // ecx + int v6; // edi + int v7; // eax + int v8; // eax + HWND hWnda; // [esp+1Ch] [ebp-4h] + + v1 = hWnd; + hWnda = hWnd; + SetFocus(hWnd); + ShowWindow(v1, SW_SHOWNORMAL); + v2 = NULL; + if (gbEmulate) + v2 = (GUID *)DDCREATE_EMULATIONONLY; + v3 = dx_DirectDrawCreate(v2, &lpDDInterface, NULL); + if (v3) + ErrDlg(IDD_DIALOG1, v3, "C:\\Src\\Diablo\\Source\\dx.cpp", 149); + fullscreen = 1; + v4 = lpDDInterface->SetCooperativeLevel(v1, DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT | DDSCL_FULLSCREEN); + if (v4 == DDERR_EXCLUSIVEMODEALREADYSET) { + MI_Dummy(0); // v5 + } else if (v4) { + ErrDlg(IDD_DIALOG1, v4, "C:\\Src\\Diablo\\Source\\dx.cpp", 170); + } + if (lpDDInterface->SetDisplayMode(640, 480, 8)) { + v6 = GetSystemMetrics(SM_CXSCREEN); + v7 = GetSystemMetrics(SM_CYSCREEN); + v8 = lpDDInterface->SetDisplayMode(v6, v7, 8); + if (v8) + ErrDlg(IDD_DIALOG1, v8, "C:\\Src\\Diablo\\Source\\dx.cpp", 183); + } + dx_create_primary_surface(); + palette_init(); + GdiSetBatchLimit(1); + dx_create_back_buffer(); + SDrawManualInitialize(hWnda, lpDDInterface, lpDDSPrimary, 0, 0, lpDDSBackBuf, lpDDPalette, 0); } // 52A549: using guessed type char gbEmulate; void __cdecl dx_create_back_buffer() { - DDSCAPS caps; - HRESULT error_code = lpDDSPrimary->GetCaps(&caps); - if ( error_code != DD_OK ) - DDErrMsg(error_code, 59, "C:\\Src\\Diablo\\Source\\dx.cpp"); - - DDSURFACEDESC ddsd; - if ( gbBackBuf == NULL ) { - ddsd.dwSize = sizeof(ddsd); - error_code = lpDDSPrimary->Lock(NULL, &ddsd, DDLOCK_WRITEONLY|DDLOCK_WAIT, NULL); - if ( error_code == DD_OK ) { - lpDDSPrimary->Unlock(NULL); - sgpBackBuf = (Screen *)DiabloAllocPtr(sizeof(Screen)); - return; - } - if ( error_code != DDERR_CANTLOCKSURFACE ) - ErrDlg(IDD_DIALOG1, error_code, "C:\\Src\\Diablo\\Source\\dx.cpp", 81); - } - - memset(&ddsd, 0, sizeof(ddsd)); - ddsd.dwWidth = 768; - ddsd.lPitch = 768; - ddsd.dwSize = sizeof(ddsd); - ddsd.dwFlags = DDSD_PIXELFORMAT|DDSD_PITCH|DDSD_WIDTH|DDSD_HEIGHT|DDSD_CAPS; - ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY|DDSCAPS_OFFSCREENPLAIN; - ddsd.dwHeight = 656; - ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat); - error_code = lpDDSPrimary->GetPixelFormat(&ddsd.ddpfPixelFormat); - if ( error_code != DD_OK ) - ErrDlg(IDD_DIALOG1, error_code, "C:\\Src\\Diablo\\Source\\dx.cpp", 94); - error_code = lpDDInterface->CreateSurface(&ddsd, &lpDDSBackBuf, NULL); - if ( error_code != DD_OK ) - ErrDlg(IDD_DIALOG1, error_code, "C:\\Src\\Diablo\\Source\\dx.cpp", 96); + DDSCAPS caps; + HRESULT error_code = lpDDSPrimary->GetCaps(&caps); + if (error_code != DD_OK) + DDErrMsg(error_code, 59, "C:\\Src\\Diablo\\Source\\dx.cpp"); + + DDSURFACEDESC ddsd; + if (gbBackBuf == NULL) { + ddsd.dwSize = sizeof(ddsd); + error_code = lpDDSPrimary->Lock(NULL, &ddsd, DDLOCK_WRITEONLY | DDLOCK_WAIT, NULL); + if (error_code == DD_OK) { + lpDDSPrimary->Unlock(NULL); + sgpBackBuf = (Screen *)DiabloAllocPtr(sizeof(Screen)); + return; + } + if (error_code != DDERR_CANTLOCKSURFACE) + ErrDlg(IDD_DIALOG1, error_code, "C:\\Src\\Diablo\\Source\\dx.cpp", 81); + } + + memset(&ddsd, 0, sizeof(ddsd)); + ddsd.dwWidth = 768; + ddsd.lPitch = 768; + ddsd.dwSize = sizeof(ddsd); + ddsd.dwFlags = DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CAPS; + ddsd.ddsCaps.dwCaps = DDSCAPS_SYSTEMMEMORY | DDSCAPS_OFFSCREENPLAIN; + ddsd.dwHeight = 656; + ddsd.ddpfPixelFormat.dwSize = sizeof(ddsd.ddpfPixelFormat); + error_code = lpDDSPrimary->GetPixelFormat(&ddsd.ddpfPixelFormat); + if (error_code != DD_OK) + ErrDlg(IDD_DIALOG1, error_code, "C:\\Src\\Diablo\\Source\\dx.cpp", 94); + error_code = lpDDInterface->CreateSurface(&ddsd, &lpDDSBackBuf, NULL); + if (error_code != DD_OK) + ErrDlg(IDD_DIALOG1, error_code, "C:\\Src\\Diablo\\Source\\dx.cpp", 96); } // 52A548: using guessed type char gbBackBuf; void __cdecl dx_create_primary_surface() { - DDSURFACEDESC ddsd; - memset(&ddsd, 0, sizeof(ddsd)); - ddsd.dwSize = sizeof(ddsd); - ddsd.dwFlags = DDSD_CAPS; - ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; - HRESULT error_code = lpDDInterface->CreateSurface(&ddsd, &lpDDSPrimary, NULL); - if ( error_code != DD_OK ) - ErrDlg(IDD_DIALOG1, error_code, "C:\\Src\\Diablo\\Source\\dx.cpp", 109); + DDSURFACEDESC ddsd; + memset(&ddsd, 0, sizeof(ddsd)); + ddsd.dwSize = sizeof(ddsd); + ddsd.dwFlags = DDSD_CAPS; + ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE; + HRESULT error_code = lpDDInterface->CreateSurface(&ddsd, &lpDDSPrimary, NULL); + if (error_code != DD_OK) + ErrDlg(IDD_DIALOG1, error_code, "C:\\Src\\Diablo\\Source\\dx.cpp", 109); } HRESULT __fastcall dx_DirectDrawCreate(LPGUID guid, LPDIRECTDRAW *lplpDD, LPUNKNOWN pUnkOuter) { - if (ghDiabMod == NULL) { - ghDiabMod = LoadLibrary("ddraw.dll"); - if (ghDiabMod == NULL) { - ErrDlg(IDD_DIALOG4, GetLastError(), "C:\\Src\\Diablo\\Source\\dx.cpp", 122); - } - } - - HRESULT(WINAPI * DirectDrawCreate) - (LPGUID lpGuid, LPDIRECTDRAW * lplpDD, LPUNKNOWN pUnkOuter); - DirectDrawCreate = (HRESULT(WINAPI *)(LPGUID, LPDIRECTDRAW *, LPUNKNOWN))GetProcAddress(ghDiabMod, "DirectDrawCreate"); - if (DirectDrawCreate == NULL) { - ErrDlg(IDD_DIALOG4, GetLastError(), "C:\\Src\\Diablo\\Source\\dx.cpp", 127); - } - return DirectDrawCreate(guid, lplpDD, pUnkOuter); + if (ghDiabMod == NULL) { + ghDiabMod = LoadLibrary("ddraw.dll"); + if (ghDiabMod == NULL) { + ErrDlg(IDD_DIALOG4, GetLastError(), "C:\\Src\\Diablo\\Source\\dx.cpp", 122); + } + } + + HRESULT(WINAPI * DirectDrawCreate) + (LPGUID lpGuid, LPDIRECTDRAW * lplpDD, LPUNKNOWN pUnkOuter); + DirectDrawCreate = (HRESULT(WINAPI *)(LPGUID, LPDIRECTDRAW *, LPUNKNOWN))GetProcAddress(ghDiabMod, "DirectDrawCreate"); + if (DirectDrawCreate == NULL) { + ErrDlg(IDD_DIALOG4, GetLastError(), "C:\\Src\\Diablo\\Source\\dx.cpp", 127); + } + return DirectDrawCreate(guid, lplpDD, pUnkOuter); } -void __fastcall j_lock_buf_priv(BYTE idx) { +void __fastcall j_lock_buf_priv(BYTE idx) +{ #ifdef _DEBUG - ++locktbl[idx]; + ++locktbl[idx]; #endif - lock_buf_priv(); + lock_buf_priv(); } void __cdecl lock_buf_priv() { - EnterCriticalSection(&sgMemCrit); - if ( sgpBackBuf != NULL ) { - gpBuffer = sgpBackBuf; - sgdwLockCount++; - return; - } - - if ( lpDDSBackBuf == NULL ) { - Sleep(20000); - TermMsg("lock_buf_priv"); - sgdwLockCount++; - return; - } - - if ( sgdwLockCount != 0 ) { - sgdwLockCount++; - return; - } - DDSURFACEDESC ddsd; - ddsd.dwSize = sizeof(ddsd); - HRESULT error_code = lpDDSBackBuf->Lock(NULL, &ddsd, DDLOCK_WAIT, NULL); - if ( error_code != DD_OK ) - DDErrMsg(error_code, 235, "C:\\Src\\Diablo\\Source\\dx.cpp"); - - gpBufEnd += (int)ddsd.lpSurface; - gpBuffer = (Screen *)ddsd.lpSurface; - sgdwLockCount++; + EnterCriticalSection(&sgMemCrit); + if (sgpBackBuf != NULL) { + gpBuffer = sgpBackBuf; + sgdwLockCount++; + return; + } + + if (lpDDSBackBuf == NULL) { + Sleep(20000); + TermMsg("lock_buf_priv"); + sgdwLockCount++; + return; + } + + if (sgdwLockCount != 0) { + sgdwLockCount++; + return; + } + DDSURFACEDESC ddsd; + ddsd.dwSize = sizeof(ddsd); + HRESULT error_code = lpDDSBackBuf->Lock(NULL, &ddsd, DDLOCK_WAIT, NULL); + if (error_code != DD_OK) + DDErrMsg(error_code, 235, "C:\\Src\\Diablo\\Source\\dx.cpp"); + + gpBufEnd += (int)ddsd.lpSurface; + gpBuffer = (Screen *)ddsd.lpSurface; + sgdwLockCount++; } -void __fastcall j_unlock_buf_priv(BYTE idx) { +void __fastcall j_unlock_buf_priv(BYTE idx) +{ #ifdef _DEBUG - if ( !locktbl[idx] ) - TermMsg("Draw lock underflow: 0x%x", idx); - --locktbl[idx]; + if (!locktbl[idx]) + TermMsg("Draw lock underflow: 0x%x", idx); + --locktbl[idx]; #endif - unlock_buf_priv(); + unlock_buf_priv(); } void __cdecl unlock_buf_priv() { - if ( sgdwLockCount == 0 ) - TermMsg("draw main unlock error"); - if ( !gpBuffer ) - TermMsg("draw consistency error"); - - sgdwLockCount--; - if ( sgdwLockCount == 0 ) { - gpBufEnd -= (int)gpBuffer; - gpBuffer = NULL; - if ( sgpBackBuf == NULL ) { - HRESULT error_code = lpDDSBackBuf->Unlock(NULL); - if ( error_code != DD_OK ) - DDErrMsg(error_code, 273, "C:\\Src\\Diablo\\Source\\dx.cpp"); - } - } - LeaveCriticalSection(&sgMemCrit); + if (sgdwLockCount == 0) + TermMsg("draw main unlock error"); + if (!gpBuffer) + TermMsg("draw consistency error"); + + sgdwLockCount--; + if (sgdwLockCount == 0) { + gpBufEnd -= (int)gpBuffer; + gpBuffer = NULL; + if (sgpBackBuf == NULL) { + HRESULT error_code = lpDDSBackBuf->Unlock(NULL); + if (error_code != DD_OK) + DDErrMsg(error_code, 273, "C:\\Src\\Diablo\\Source\\dx.cpp"); + } + } + LeaveCriticalSection(&sgMemCrit); } void __cdecl dx_cleanup() { - Screen *v0; // ecx - - if ( ghMainWnd ) - ShowWindow(ghMainWnd, SW_HIDE); - SDrawDestroy(); - EnterCriticalSection(&sgMemCrit); - if ( sgpBackBuf != NULL ) { - v0 = sgpBackBuf; - sgpBackBuf = 0; - mem_free_dbg(v0); - } - else if ( lpDDSBackBuf != NULL ) - { - lpDDSBackBuf->Release(); - lpDDSBackBuf = NULL; - } - sgdwLockCount = 0; - gpBuffer = 0; - LeaveCriticalSection(&sgMemCrit); - if ( lpDDSPrimary ) - { - lpDDSPrimary->Release(); - lpDDSPrimary = 0; - } - if ( lpDDPalette ) - { - lpDDPalette->Release(); - lpDDPalette = 0; - } - if ( lpDDInterface ) - { - lpDDInterface->Release(); - lpDDInterface = 0; - } + Screen *v0; // ecx + + if (ghMainWnd) + ShowWindow(ghMainWnd, SW_HIDE); + SDrawDestroy(); + EnterCriticalSection(&sgMemCrit); + if (sgpBackBuf != NULL) { + v0 = sgpBackBuf; + sgpBackBuf = 0; + mem_free_dbg(v0); + } else if (lpDDSBackBuf != NULL) { + lpDDSBackBuf->Release(); + lpDDSBackBuf = NULL; + } + sgdwLockCount = 0; + gpBuffer = 0; + LeaveCriticalSection(&sgMemCrit); + if (lpDDSPrimary) { + lpDDSPrimary->Release(); + lpDDSPrimary = 0; + } + if (lpDDPalette) { + lpDDPalette->Release(); + lpDDPalette = 0; + } + if (lpDDInterface) { + lpDDInterface->Release(); + lpDDInterface = 0; + } } void __cdecl dx_reinit() { - EnterCriticalSection(&sgMemCrit); - ClearCursor(); - int lockCount = sgdwLockCount; + EnterCriticalSection(&sgMemCrit); + ClearCursor(); + int lockCount = sgdwLockCount; - while ( sgdwLockCount != 0 ) - unlock_buf_priv(); + while (sgdwLockCount != 0) + unlock_buf_priv(); - dx_cleanup(); + dx_cleanup(); - drawpanflag = 255; + drawpanflag = 255; - dx_init(ghMainWnd); + dx_init(ghMainWnd); - while (lockCount != 0) { - lock_buf_priv(); - lockCount--; - } + while (lockCount != 0) { + lock_buf_priv(); + lockCount--; + } - LeaveCriticalSection(&sgMemCrit); + LeaveCriticalSection(&sgMemCrit); } diff --git a/Source/dx.h b/Source/dx.h index e8ad24627..bd19ceb6b 100644 --- a/Source/dx.h +++ b/Source/dx.h @@ -10,8 +10,8 @@ extern int sgdwLockCount; extern Screen *gpBuffer; extern IDirectDrawSurface *lpDDSBackBuf; extern IDirectDrawSurface *lpDDSPrimary; -extern char gbBackBuf; // weak -extern char gbEmulate; // weak +extern char gbBackBuf; // weak +extern char gbEmulate; // weak extern HMODULE ghDiabMod; // idb void __cdecl dx_cpp_init_1(); diff --git a/Source/palette.cpp b/Source/palette.cpp index a2186067c..d25f45708 100644 --- a/Source/palette.cpp +++ b/Source/palette.cpp @@ -16,306 +16,282 @@ int gamma_correction = 100; // idb BOOL color_cycling_enabled = TRUE; bool sgbFadedIn = 1; -struct palette_cpp_init -{ - palette_cpp_init() - { - palette_cpp_init_value = palette_inf; - } +struct palette_cpp_init { + palette_cpp_init() + { + palette_cpp_init_value = palette_inf; + } } _palette_cpp_init; // 47F16C: using guessed type int palette_inf; // 67DBCC: using guessed type int palette_cpp_init_value; void __cdecl SaveGamma() { - SRegSaveValue("Diablo", "Gamma Correction", 0, gamma_correction); - SRegSaveValue("Diablo", "Color Cycling", FALSE, color_cycling_enabled); + SRegSaveValue("Diablo", "Gamma Correction", 0, gamma_correction); + SRegSaveValue("Diablo", "Color Cycling", FALSE, color_cycling_enabled); } void __cdecl palette_init() { - int v0; // eax - int v1; // eax - - LoadGamma(); - memcpy(system_palette, orig_palette, 0x400u); - LoadSysPal(); - v0 = lpDDInterface->CreatePalette(DDPCAPS_ALLOW256|DDPCAPS_8BIT, system_palette, &lpDDPalette, NULL); - if ( v0 ) - ErrDlg(IDD_DIALOG8, v0, "C:\\Src\\Diablo\\Source\\PALETTE.CPP", 143); - v1 = lpDDSPrimary->SetPalette(lpDDPalette); - if ( v1 ) - ErrDlg(IDD_DIALOG8, v1, "C:\\Src\\Diablo\\Source\\PALETTE.CPP", 146); + int v0; // eax + int v1; // eax + + LoadGamma(); + memcpy(system_palette, orig_palette, 0x400u); + LoadSysPal(); + v0 = lpDDInterface->CreatePalette(DDPCAPS_ALLOW256 | DDPCAPS_8BIT, system_palette, &lpDDPalette, NULL); + if (v0) + ErrDlg(IDD_DIALOG8, v0, "C:\\Src\\Diablo\\Source\\PALETTE.CPP", 143); + v1 = lpDDSPrimary->SetPalette(lpDDPalette); + if (v1) + ErrDlg(IDD_DIALOG8, v1, "C:\\Src\\Diablo\\Source\\PALETTE.CPP", 146); } void __cdecl LoadGamma() { - int v3; // eax - int value; // [esp+8h] [ebp-4h] - - value = gamma_correction; - if ( !SRegLoadValue("Diablo", "Gamma Correction", 0, &value) ) - value = 100; - if ( value >= 30 ) - { - if ( value > 100 ) - value = 100; - } - else - { - value = 30; - } - gamma_correction = value - value % 5; - if ( SRegLoadValue("Diablo", "Color Cycling", 0, &value) ) - v3 = value; - else - v3 = TRUE; - color_cycling_enabled = v3; + int v3; // eax + int value; // [esp+8h] [ebp-4h] + + value = gamma_correction; + if (!SRegLoadValue("Diablo", "Gamma Correction", 0, &value)) + value = 100; + if (value >= 30) { + if (value > 100) + value = 100; + } else { + value = 30; + } + gamma_correction = value - value % 5; + if (SRegLoadValue("Diablo", "Color Cycling", 0, &value)) + v3 = value; + else + v3 = TRUE; + color_cycling_enabled = v3; } void __cdecl LoadSysPal() { - HDC hDC; // ebx - int i; // ecx - int iStartIndex; // edi - - for(i = 0; i < 256; i++) - system_palette[i].peFlags = PC_NOCOLLAPSE|PC_RESERVED; - - if ( !fullscreen ) - { - hDC = GetDC(NULL); - gdwPalEntries = GetDeviceCaps(hDC, NUMRESERVED) / 2; - GetSystemPaletteEntries(hDC, 0, gdwPalEntries, system_palette); - for ( i = 0; i < gdwPalEntries; i++ ) - system_palette[i].peFlags = 0; - iStartIndex = 256 - gdwPalEntries; - GetSystemPaletteEntries(hDC, iStartIndex, gdwPalEntries, &system_palette[iStartIndex]); - if ( iStartIndex < 256 ) - { - for(i = iStartIndex; i < 256; i++) - system_palette[i].peFlags = 0; - } - ReleaseDC(NULL, hDC); - } + HDC hDC; // ebx + int i; // ecx + int iStartIndex; // edi + + for (i = 0; i < 256; i++) + system_palette[i].peFlags = PC_NOCOLLAPSE | PC_RESERVED; + + if (!fullscreen) { + hDC = GetDC(NULL); + gdwPalEntries = GetDeviceCaps(hDC, NUMRESERVED) / 2; + GetSystemPaletteEntries(hDC, 0, gdwPalEntries, system_palette); + for (i = 0; i < gdwPalEntries; i++) + system_palette[i].peFlags = 0; + iStartIndex = 256 - gdwPalEntries; + GetSystemPaletteEntries(hDC, iStartIndex, gdwPalEntries, &system_palette[iStartIndex]); + if (iStartIndex < 256) { + for (i = iStartIndex; i < 256; i++) + system_palette[i].peFlags = 0; + } + ReleaseDC(NULL, hDC); + } } void __fastcall LoadPalette(char *pszFileName) { - int i; // eax - char PalData[256][3]; // [esp+0h] [ebp-304h] - void *pBuf; // [esp+300h] [ebp-4h] - - WOpenFile(pszFileName, &pBuf, 0); - WReadFile(pBuf, (char *)PalData, 768); - WCloseFile(pBuf); - - for(i = 0; i < 256; i++) - { - orig_palette[i].peFlags = 0; - orig_palette[i].peRed = PalData[i][0]; - orig_palette[i].peGreen = PalData[i][1]; - orig_palette[i].peBlue = PalData[i][2]; - } + int i; // eax + char PalData[256][3]; // [esp+0h] [ebp-304h] + void *pBuf; // [esp+300h] [ebp-4h] + + WOpenFile(pszFileName, &pBuf, 0); + WReadFile(pBuf, (char *)PalData, 768); + WCloseFile(pBuf); + + for (i = 0; i < 256; i++) { + orig_palette[i].peFlags = 0; + orig_palette[i].peRed = PalData[i][0]; + orig_palette[i].peGreen = PalData[i][1]; + orig_palette[i].peBlue = PalData[i][2]; + } } void __fastcall LoadRndLvlPal(int l) { - char *pszPal; // ecx - char szTemp[260]; // [esp+4h] [ebp-104h] - - if ( l ) - { - sprintf(szTemp, "Levels\\L%iData\\L%i_%i.PAL", l, l, random(0, 4) + 1); - pszPal = szTemp; - } - else - { - pszPal = "Levels\\TownData\\Town.pal"; - } - LoadPalette(pszPal); + char *pszPal; // ecx + char szTemp[260]; // [esp+4h] [ebp-104h] + + if (l) { + sprintf(szTemp, "Levels\\L%iData\\L%i_%i.PAL", l, l, random(0, 4) + 1); + pszPal = szTemp; + } else { + pszPal = "Levels\\TownData\\Town.pal"; + } + LoadPalette(pszPal); } void __cdecl ResetPal() { - if ( !lpDDSPrimary - || lpDDSPrimary->IsLost() != DDERR_SURFACELOST - || !lpDDSPrimary->Restore() ) - { - SDrawRealizePalette(); - } + if (!lpDDSPrimary + || lpDDSPrimary->IsLost() != DDERR_SURFACELOST + || !lpDDSPrimary->Restore()) { + SDrawRealizePalette(); + } } void __cdecl IncreaseGamma() { - if ( gamma_correction < 100 ) - { - gamma_correction += 5; - if ( gamma_correction > 100 ) - gamma_correction = 100; - ApplyGamma(system_palette, logical_palette, 256); - palette_update(); - } + if (gamma_correction < 100) { + gamma_correction += 5; + if (gamma_correction > 100) + gamma_correction = 100; + ApplyGamma(system_palette, logical_palette, 256); + palette_update(); + } } void __cdecl palette_update() { - int v0; // ecx - int v1; // eax - - if ( lpDDPalette ) - { - v0 = 0; - v1 = 256; - if ( !fullscreen ) - { - v0 = gdwPalEntries; - v1 = 2 * (128 - gdwPalEntries); - } - SDrawUpdatePalette(v0, v1, &system_palette[v0], 0); - } + int v0; // ecx + int v1; // eax + + if (lpDDPalette) { + v0 = 0; + v1 = 256; + if (!fullscreen) { + v0 = gdwPalEntries; + v1 = 2 * (128 - gdwPalEntries); + } + SDrawUpdatePalette(v0, v1, &system_palette[v0], 0); + } } void __fastcall ApplyGamma(PALETTEENTRY *dst, PALETTEENTRY *src, int n) { - PALETTEENTRY *v3; // edi - PALETTEENTRY *v4; // esi - double v5; // [esp+18h] [ebp-Ch] - - v3 = src; - v4 = dst; - v5 = (double)gamma_correction * 0.01; - if ( n > 0 ) - { - do - { - v4->peRed = pow(v3->peRed * 0.00390625, v5) * 256.0; - v4->peGreen = pow(v3->peGreen * 0.00390625, v5) * 256.0; - v4->peBlue = pow(v3->peBlue * 0.00390625, v5) * 256.0; - ++v4; - ++v3; - --n; - } - while ( n ); - } + PALETTEENTRY *v3; // edi + PALETTEENTRY *v4; // esi + double v5; // [esp+18h] [ebp-Ch] + + v3 = src; + v4 = dst; + v5 = (double)gamma_correction * 0.01; + if (n > 0) { + do { + v4->peRed = pow(v3->peRed * 0.00390625, v5) * 256.0; + v4->peGreen = pow(v3->peGreen * 0.00390625, v5) * 256.0; + v4->peBlue = pow(v3->peBlue * 0.00390625, v5) * 256.0; + ++v4; + ++v3; + --n; + } while (n); + } } void __cdecl DecreaseGamma() { - if ( gamma_correction > 30 ) - { - gamma_correction -= 5; - if ( gamma_correction < 30 ) - gamma_correction = 30; - ApplyGamma(system_palette, logical_palette, 256); - palette_update(); - } + if (gamma_correction > 30) { + gamma_correction -= 5; + if (gamma_correction < 30) + gamma_correction = 30; + ApplyGamma(system_palette, logical_palette, 256); + palette_update(); + } } int __fastcall UpdateGamma(int gamma) { - if ( gamma ) - { - gamma_correction = 130 - gamma; - ApplyGamma(system_palette, logical_palette, 256); - palette_update(); - } - return 130 - gamma_correction; + if (gamma) { + gamma_correction = 130 - gamma; + ApplyGamma(system_palette, logical_palette, 256); + palette_update(); + } + return 130 - gamma_correction; } void __cdecl BlackPalette() { - SetFadeLevel(0); + SetFadeLevel(0); } void __fastcall SetFadeLevel(int fadeval) { - int i; // eax - - if ( lpDDInterface ) - { - for(i = 0; i < 255; i++) - { - system_palette[i].peRed = (fadeval * logical_palette[i].peRed) >> 8; - system_palette[i].peGreen = (fadeval * logical_palette[i].peGreen) >> 8; - system_palette[i].peBlue = (fadeval * logical_palette[i].peBlue) >> 8; - } - Sleep(3); - lpDDInterface->WaitForVerticalBlank(DDWAITVB_BLOCKBEGIN, NULL); - palette_update(); - } + int i; // eax + + if (lpDDInterface) { + for (i = 0; i < 255; i++) { + system_palette[i].peRed = (fadeval * logical_palette[i].peRed) >> 8; + system_palette[i].peGreen = (fadeval * logical_palette[i].peGreen) >> 8; + system_palette[i].peBlue = (fadeval * logical_palette[i].peBlue) >> 8; + } + Sleep(3); + lpDDInterface->WaitForVerticalBlank(DDWAITVB_BLOCKBEGIN, NULL); + palette_update(); + } } void __fastcall PaletteFadeIn(int fr) { - int i; // ebp + int i; // ebp - ApplyGamma(logical_palette, orig_palette, 256); + ApplyGamma(logical_palette, orig_palette, 256); - for(i = 0; i < 256; i += fr) - SetFadeLevel(i); + for (i = 0; i < 256; i += fr) + SetFadeLevel(i); - SetFadeLevel(256); - memcpy(logical_palette, orig_palette, 0x400u); - sgbFadedIn = 1; + SetFadeLevel(256); + memcpy(logical_palette, orig_palette, 0x400u); + sgbFadedIn = 1; } void __fastcall PaletteFadeOut(int fr) { - int i; // esi + int i; // esi - if ( sgbFadedIn ) - { - for(i = 256; i > 0; i -= fr) - SetFadeLevel(i); + if (sgbFadedIn) { + for (i = 256; i > 0; i -= fr) + SetFadeLevel(i); - SetFadeLevel(0); - sgbFadedIn = 0; - } + SetFadeLevel(0); + sgbFadedIn = 0; + } } void __cdecl palette_update_caves() { - BYTE v0; // cx - signed int v1; // esi - signed int v2; // eax - BYTE v4; // [esp+6h] [ebp-2h] - BYTE v5; - - v0 = system_palette[1].peRed; - v5 = system_palette[1].peGreen; - v4 = system_palette[1].peBlue; - v1 = 1; - do - { - v2 = v1++; - system_palette[v2].peRed = system_palette[v2 + 1].peRed; - system_palette[v2].peGreen = system_palette[v2 + 1].peGreen; - system_palette[v2].peBlue = system_palette[v2 + 1].peBlue; - } - while ( v1 < 31 ); - system_palette[v1].peRed = v0; - system_palette[v1].peGreen = v5; - system_palette[v1].peBlue = v4; - palette_update(); + BYTE v0; // cx + signed int v1; // esi + signed int v2; // eax + BYTE v4; // [esp+6h] [ebp-2h] + BYTE v5; + + v0 = system_palette[1].peRed; + v5 = system_palette[1].peGreen; + v4 = system_palette[1].peBlue; + v1 = 1; + do { + v2 = v1++; + system_palette[v2].peRed = system_palette[v2 + 1].peRed; + system_palette[v2].peGreen = system_palette[v2 + 1].peGreen; + system_palette[v2].peBlue = system_palette[v2 + 1].peBlue; + } while (v1 < 31); + system_palette[v1].peRed = v0; + system_palette[v1].peGreen = v5; + system_palette[v1].peBlue = v4; + palette_update(); } void __fastcall palette_update_quest_palette(int n) { - int i; // eax + int i; // eax - for ( i = 32 - n; i >= 0; --i ) - logical_palette[i] = orig_palette[i]; - ApplyGamma(system_palette, logical_palette, 32); - palette_update(); + for (i = 32 - n; i >= 0; --i) + logical_palette[i] = orig_palette[i]; + ApplyGamma(system_palette, logical_palette, 32); + palette_update(); } bool __cdecl palette_get_colour_cycling() { - return color_cycling_enabled; + return color_cycling_enabled; } void __fastcall palette_set_color_cycling(bool enabled) { - color_cycling_enabled = enabled; + color_cycling_enabled = enabled; }