diff --git a/src/main.c b/src/main.c index 9bd070e..95c9d2f 100644 --- a/src/main.c +++ b/src/main.c @@ -36,9 +36,7 @@ void mouse_lock(); void mouse_unlock(); /* from screenshot.c */ -#ifdef HAVE_LIBPNG BOOL screenshot(struct IDirectDrawSurfaceImpl *); -#endif IDirectDrawImpl *ddraw = NULL; @@ -761,7 +759,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) return 0; } } -#ifdef HAVE_LIBPNG + if(wParam == VK_CONTROL || wParam == ddraw->screenshotKey) { if(GetAsyncKeyState(VK_CONTROL) & 0x8000 && GetAsyncKeyState(ddraw->screenshotKey) & 0x8000) @@ -770,16 +768,13 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) return 0; } } -#endif break; -#ifdef HAVE_LIBPNG case WM_KEYUP: if (wParam == VK_SNAPSHOT) screenshot(ddraw->primary); break; -#endif /* button up messages reactivate cursor lock */ diff --git a/src/screenshot.c b/src/screenshot.c index 774ee1c..0b43ffa 100644 --- a/src/screenshot.c +++ b/src/screenshot.c @@ -13,9 +13,6 @@ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ - -#ifdef HAVE_LIBPNG - #include #include #include @@ -24,6 +21,102 @@ #include "palette.h" #include "surface.h" +#ifndef HAVE_LIBPNG + +//https://docs.microsoft.com/en-us/windows/desktop/gdi/storing-an-image + +BOOL screenshot(struct IDirectDrawSurfaceImpl *src) +{ + HANDLE hf; // file handle + BITMAPFILEHEADER hdr; // bitmap file-header + PBITMAPINFOHEADER pbih; // bitmap info-header + LPBYTE lpBits; // memory pointer + DWORD dwTotal; // total count of bytes + DWORD cb; // incremental count of bytes + BYTE *hp; // byte pointer + DWORD dwTmp; + int i; + char title[128]; + char filename[128]; + char str_time[64]; + time_t t = time(NULL); + BOOL result = TRUE; + + strncpy(title, ddraw->title, sizeof(ddraw->title)); + + for (i = 0; ibmi; + lpBits = (LPBYTE)src->surface; + + if (!lpBits) + return FALSE; + + // Create the .BMP file. + hf = CreateFile(filename, + GENERIC_READ | GENERIC_WRITE, + (DWORD)0, + NULL, + CREATE_ALWAYS, + FILE_ATTRIBUTE_NORMAL, + (HANDLE)NULL); + + if (hf == INVALID_HANDLE_VALUE) + return FALSE; + + hdr.bfType = 0x4d42; // 0x42 = "B" 0x4d = "M" + // Compute the size of the entire file. + hdr.bfSize = (DWORD)(sizeof(BITMAPFILEHEADER) + + pbih->biSize + pbih->biClrUsed + * sizeof(RGBQUAD) + pbih->biSizeImage); + hdr.bfReserved1 = 0; + hdr.bfReserved2 = 0; + + // Compute the offset to the array of color indices. + hdr.bfOffBits = (DWORD) sizeof(BITMAPFILEHEADER) + + pbih->biSize + pbih->biClrUsed + * sizeof(RGBQUAD); + + // Copy the BITMAPFILEHEADER into the .BMP file. + if (!WriteFile(hf, (LPVOID)&hdr, sizeof(BITMAPFILEHEADER), + (LPDWORD)&dwTmp, NULL)) + { + result = FALSE; + } + + // Copy the BITMAPINFOHEADER and RGBQUAD array into the file. + if (!WriteFile(hf, (LPVOID)pbih, sizeof(BITMAPINFOHEADER) + + pbih->biClrUsed * sizeof(RGBQUAD), + (LPDWORD)&dwTmp, (NULL))) + result = FALSE; + + // Copy the array of color indices into the .BMP file. + dwTotal = cb = pbih->biSizeImage; + hp = lpBits; + if (!WriteFile(hf, (LPSTR)hp, (int)cb, (LPDWORD)&dwTmp, NULL)) + result = FALSE; + + // Close the .BMP file. + if (!CloseHandle(hf)) + result = FALSE; + + return result; +} + +#else + #include BOOL screenshot(struct IDirectDrawSurfaceImpl *src) diff --git a/src/surface.c b/src/surface.c index 37e261b..ccc53c8 100644 --- a/src/surface.c +++ b/src/surface.c @@ -560,6 +560,12 @@ HRESULT __stdcall ddraw_CreateSurface(IDirectDrawImpl *This, LPDDSURFACEDESC lpD Surface->bmi->bmiHeader.biBitCount = Surface->bpp; Surface->bmi->bmiHeader.biCompression = BI_RGB; + WORD cClrBits = (WORD)(Surface->bmi->bmiHeader.biPlanes * Surface->bmi->bmiHeader.biBitCount); + if (cClrBits < 24) + Surface->bmi->bmiHeader.biClrUsed = (1 << cClrBits); + + Surface->bmi->bmiHeader.biSizeImage = ((Surface->width * cClrBits + 31) & ~31) / 8 * Surface->height; + int i; for (i = 0; i < 256; i++) {