From aa69dbbb3e55631e69bf05f696f5746402a2a080 Mon Sep 17 00:00:00 2001 From: Toni Spets Date: Sat, 20 Nov 2010 21:08:17 +0200 Subject: [PATCH] Initial screenshot support for paletted surfaces --- Makefile | 2 +- main.c | 11 +++++++ screenshot.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 screenshot.c diff --git a/Makefile b/Makefile index 2c78107..8274c1e 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ all: - i586-mingw32msvc-gcc -Wall -Wl,--enable-stdcall-fixup -shared -s -o ddraw.dll main.c mouse.c palette.c surface.c clipper.c render.c ddraw.def -lgdi32 -lopengl32 + i586-mingw32msvc-gcc -DHAVE_LIBPNG -Iinclude -Wall -Wl,--enable-stdcall-fixup -shared -s -o ddraw.dll main.c mouse.c palette.c surface.c clipper.c render.c screenshot.c lib/libpng14.a lib/libz.a ddraw.def -lgdi32 -lopengl32 clean: rm -f ddraw.dll diff --git a/main.c b/main.c index b6050a0..1733615 100644 --- a/main.c +++ b/main.c @@ -29,6 +29,11 @@ void mouse_init(HWND); void mouse_lock(); void mouse_unlock(); +/* from screenshot.c */ +#ifdef HAVE_LIBPNG +BOOL screenshot(struct IDirectDrawSurfaceImpl *); +#endif + IDirectDrawImpl *ddraw = NULL; DWORD WINAPI render_main(void); @@ -296,6 +301,12 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) mouse_unlock(); } } +#ifdef HAVE_LIBPNG + else if(wParam == VK_SNAPSHOT) + { + screenshot(ddraw->primary); + } +#endif break; case WM_LBUTTONDOWN: case WM_RBUTTONDOWN: diff --git a/screenshot.c b/screenshot.c new file mode 100644 index 0000000..a67892e --- /dev/null +++ b/screenshot.c @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2010 Toni Spets + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * 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 +#include "ddraw.h" + +#include "palette.h" +#include "surface.h" + +#include + +BOOL screenshot(struct IDirectDrawSurfaceImpl *src) +{ + int i; + FILE *fh; + + png_structp png_ptr; + png_infop info_ptr; + png_bytep *row_pointers; + png_color palette[256]; + + char filename[64]; + time_t t = time(NULL); + strftime(filename, 64, "screenshot-%Y-%m-%d-%H_%M_%S.png", localtime(&t)); + + if(!src || !src->palette) + { + return FALSE; + } + + if( !(fh = fopen(filename, "wb")) ) + { + return FALSE; + } + + png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); + if(!png_ptr) + { + return FALSE; + } + + info_ptr = png_create_info_struct(png_ptr); + + for(i=0;i<256;i++) { + palette[i].red = src->palette->data_bgr[i]; + palette[i].green = src->palette->data_bgr[i] >> 8; + palette[i].blue = src->palette->data_bgr[i] >> 16; + } + + setjmp(png_jmpbuf(png_ptr)); + + png_init_io(png_ptr, fh); + + png_set_IHDR(png_ptr, info_ptr, src->width, src->height, 8, PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); + png_set_PLTE(png_ptr, info_ptr, (png_colorp)&palette, 256); + + row_pointers = (png_bytep *)png_malloc(png_ptr, sizeof(png_bytep) * src->height); + + for(i=0;iheight;i++) { + row_pointers[i] = src->surface + (src->width * i); + } + + png_set_rows(png_ptr, info_ptr, row_pointers); + + png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL); + + fclose(fh); + + return TRUE; +} + +#endif