From cb2382285f12219167948cb55f9b480b56848640 Mon Sep 17 00:00:00 2001 From: terminx Date: Tue, 21 Jun 2016 00:33:19 +0000 Subject: [PATCH] Separate screen capture code from engine.c into screenshot.c git-svn-id: https://svn.eduke32.com/eduke32@5791 1a8010ca-5511-0410-912e-c29ae57300e0 --- polymer/eduke32/Makefile | 1 + polymer/eduke32/Makefile.msvc | 3 +- polymer/eduke32/build/Makefile.deps | 3 +- polymer/eduke32/build/src/engine.c | 298 ----------------------- polymer/eduke32/build/src/screenshot.c | 310 ++++++++++++++++++++++++ polymer/eduke32/eduke32.vcxproj | 1 + polymer/eduke32/eduke32.vcxproj.filters | 3 + 7 files changed, 319 insertions(+), 300 deletions(-) create mode 100644 polymer/eduke32/build/src/screenshot.c diff --git a/polymer/eduke32/Makefile b/polymer/eduke32/Makefile index aa1057b71..dc855bb15 100644 --- a/polymer/eduke32/Makefile +++ b/polymer/eduke32/Makefile @@ -62,6 +62,7 @@ ENGINE_OBJS = \ xxhash \ md4 \ colmatch \ + screenshot ENGINE_EDITOR_OBJS = \ build \ diff --git a/polymer/eduke32/Makefile.msvc b/polymer/eduke32/Makefile.msvc index 0ff69f86e..729411076 100644 --- a/polymer/eduke32/Makefile.msvc +++ b/polymer/eduke32/Makefile.msvc @@ -159,7 +159,8 @@ ENGINE_OBJS= \ $(ENGINE_OBJ)\scriptfile.$o \ $(ENGINE_OBJ)\mutex.$o \ $(ENGINE_OBJ)\winbits.$o \ - $(ENGINE_OBJ)\xxhash.$o + $(ENGINE_OBJ)\xxhash.$o \ + $(ENGINE_OBJ)\screenshot.$o ENGINE_EDITOR_OBJS=$(ENGINE_OBJ)\build.$o \ $(ENGINE_OBJ)\startwin.editor.$o \ diff --git a/polymer/eduke32/build/Makefile.deps b/polymer/eduke32/build/Makefile.deps index 0d7144c42..c94b85b3d 100644 --- a/polymer/eduke32/build/Makefile.deps +++ b/polymer/eduke32/build/Makefile.deps @@ -10,7 +10,8 @@ $(ENGINE_OBJ)/config.$o: $(ENGINE_SRC)/config.c $(ENGINE_INC)/compat.h $(ENGINE_ $(ENGINE_OBJ)/crc32.$o: $(ENGINE_SRC)/crc32.c $(ENGINE_INC)/crc32.h $(ENGINE_OBJ)/defs.$o: $(ENGINE_SRC)/defs.c $(ENGINE_INC)/build.h $(ENGINE_INC)/buildtypes.h $(ENGINE_INC)/baselayer.h $(ENGINE_INC)/scriptfile.h $(ENGINE_INC)/compat.h $(ENGINE_OBJ)/engine.$o: $(ENGINE_SRC)/engine.c $(ENGINE_INC)/compat.h $(ENGINE_INC)/build.h $(ENGINE_INC)/buildtypes.h $(ENGINE_INC)/pragmas.h $(ENGINE_INC)/cache1d.h $(ENGINE_INC)/a.h $(ENGINE_INC)/osd.h $(ENGINE_INC)/baselayer.h $(ENGINE_SRC)/engine_priv.h $(ENGINE_SRC)/engine_oldmap.h $(ENGINE_INC)/polymost.h $(ENGINE_INC)/hightile.h $(ENGINE_INC)/mdsprite.h $(ENGINE_INC)/polymer.h -$(ENGINE_OBJ)/2d.$o: $(ENGINE_SRC)/2d.c $(ENGINE_INC)/2d.h +$(ENGINE_OBJ)/2d.$o: $(ENGINE_SRC)/2d.c +$(ENGINE_OBJ)/screenshot.$o: $(ENGINE_SRC)/screenshot.c $(ENGINE_OBJ)/hash.$o: $(ENGINE_SRC)/hash.c $(ENGINE_INC)/hash.h $(ENGINE_OBJ)/palette.$o: $(ENGINE_SRC)/palette.c $(ENGINE_INC)/palette.h $(ENGINE_OBJ)/polymost.$o: $(ENGINE_SRC)/polymost.c $(ENGINE_INC)/lz4.h $(ENGINE_INC)/compat.h $(ENGINE_INC)/build.h $(ENGINE_INC)/buildtypes.h $(ENGINE_SRC)/engine_priv.h $(ENGINE_INC)/polymost.h $(ENGINE_INC)/hightile.h $(ENGINE_INC)/mdsprite.h $(ENGINE_INC)/texcache.h diff --git a/polymer/eduke32/build/src/engine.c b/polymer/eduke32/build/src/engine.c index 7ba94f264..7f67eee6d 100644 --- a/polymer/eduke32/build/src/engine.c +++ b/polymer/eduke32/build/src/engine.c @@ -2304,7 +2304,6 @@ static int32_t permhead = 0, permtail = 0; EDUKE32_STATIC_ASSERT(MAXWALLSB < INT16_MAX); int16_t numscans, numbunches; static int16_t numhits; -int16_t capturecount = 0; uint8_t vgapal16[4*256] = { @@ -15814,303 +15813,6 @@ void printext256(int32_t xpos, int32_t ypos, int16_t col, int16_t backcol, const } -// -// screencapture -// - -static int32_t screencapture_common1(char *fn, const char *ext, BFILE** filptr) -{ - int32_t i; - - do // JBF 2004022: So we don't overwrite existing screenshots - { - if (capturecount > 9999) return -1; - - i = Bstrrchr(fn,'.')-fn-4; - fn[i++] = ((capturecount/1000)%10)+48; - fn[i++] = ((capturecount/100)%10)+48; - fn[i++] = ((capturecount/10)%10)+48; - fn[i++] = (capturecount%10)+48; - i++; - Bstrcpy(&fn[i], ext); - - if ((*filptr = Bfopen(fn,"rb")) == NULL) break; - Bfclose(*filptr); - capturecount++; - } - while (1); - - *filptr = Bfopen(fn,"wb"); - if (*filptr == NULL) - return -1; - - return 0; -} - -#ifdef USE_LIBPNG -// PNG screenshots -- adapted from libpng example.c -static int32_t screencapture_png(const char *filename, char inverseit, const char *versionstr) -{ - int32_t i; - BFILE *fp; -# ifdef USE_OPENGL -# define HICOLOR (getrendermode() >= REND_POLYMOST && in3dmode()) -# else -# define HICOLOR 0 -# endif - png_structp png_ptr; - png_infop info_ptr; - png_colorp palette = NULL; - png_textp text = NULL; - - png_bytep buf = NULL; - png_bytepp rowptrs = NULL; - - char fn[32]; // careful... - - Bstrcpy(fn, filename); - i = screencapture_common1(fn, "png", &fp); - if (i) - return i; - - /* Create and initialize the png_struct with default error handling. */ - png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); - if (png_ptr == NULL) - { - Bfclose(fp); - return -1; - } - - /* Allocate/initialize the image information data. */ - info_ptr = png_create_info_struct(png_ptr); - if (info_ptr == NULL) - { - Bfclose(fp); - png_destroy_write_struct(&png_ptr, NULL); - return -1; - } - - /* Set error handling. */ - if (setjmp(png_jmpbuf(png_ptr))) - { - /* If we get here, we had a problem writing the file */ - Bfclose(fp); - if (palette) png_free(png_ptr, palette); - if (text) png_free(png_ptr, text); - if (buf) png_free(png_ptr, buf); - if (rowptrs) png_free(png_ptr, rowptrs); - png_destroy_write_struct(&png_ptr, &info_ptr); - - return -1; - } - - png_init_io(png_ptr, fp); - - // initialize various info fields from here on - png_set_IHDR(png_ptr, info_ptr, xdim, ydim, 8, - HICOLOR ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_PALETTE, - PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); - - if (HICOLOR && editstatus==0) - png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, PNG_FILTER_VALUE_NONE); - - if (!HICOLOR) - #if (PNG_LIBPNG_VER > 10599) - palette = (png_colorp)png_malloc(png_ptr, 256*sizeof(png_color)); - #else - palette = (png_colorp)png_malloc(png_ptr, 256*png_sizeof(png_color)); - #endif - - if (palette) - { - for (i=0; i<256; i++) - { - palette[i].red = inverseit ? 255-curpalettefaded[i].r : curpalettefaded[i].r; - palette[i].green = inverseit ? 255-curpalettefaded[i].g : curpalettefaded[i].g; - palette[i].blue = inverseit ? 255-curpalettefaded[i].b : curpalettefaded[i].b; - } - - png_set_PLTE(png_ptr, info_ptr, palette, 256); - } - -// png_set_gAMA(png_ptr, info_ptr, vid_gamma); // 1.0/vid_gamma ? -// png_set_sRGB(png_ptr, info_ptr, PNG_sRGB_INTENT_SATURATION); // hm... - - #if (PNG_LIBPNG_VER > 10599) - text = (png_textp)png_malloc(png_ptr, 2*sizeof(png_text)); - #else - text = (png_textp)png_malloc(png_ptr, 2*png_sizeof(png_text)); - #endif - text[0].compression = PNG_TEXT_COMPRESSION_NONE; - text[0].key = Bstrdup("Title"); - text[0].text = Bstrdup((editstatus ? "Mapster32 screenshot" : "EDuke32 screenshot")); - - text[1].compression = PNG_TEXT_COMPRESSION_NONE; - text[1].key = Bstrdup("Software"); - text[1].text = Bstrdup(versionstr); - png_set_text(png_ptr, info_ptr, text, 2); - - // get/set the pixel data - begindrawing(); //{{{ - if (palette) - { - buf = (png_bytep)png_malloc(png_ptr, bytesperline*ydim); - Bmemcpy(buf, (char *)frameplace, bytesperline*ydim); - } -# ifdef USE_OPENGL - else - { - buf = (png_bytep)png_malloc(png_ptr, xdim*ydim*3); - bglReadPixels(0,0,xdim,ydim,GL_RGB,GL_UNSIGNED_BYTE,buf); - } -# endif - enddrawing(); //}}} - - rowptrs = (png_bytepp)png_malloc(png_ptr, ydim*sizeof(png_bytep)); - - if (!palette) - { - for (i=0; i= REND_POLYMOST && in3dmode()) - { - head[1] = 0; // no colourmap - head[2] = 2; // uncompressed truecolour - head[3] = 0; // (low) first colourmap index - head[4] = 0; // (high) first colourmap index - head[5] = 0; // (low) number colourmap entries - head[6] = 0; // (high) number colourmap entries - head[7] = 0; // colourmap entry size - head[16] = 24; // 24 bits per pixel - } -# endif - - head[12] = xdim & 0xff; - head[13] = (xdim >> 8) & 0xff; - head[14] = ydim & 0xff; - head[15] = (ydim >> 8) & 0xff; - - Bfwrite(head, 18, 1, fil); - - begindrawing(); //{{{ - ptr = (char *)frameplace; - - // palette first -# ifdef USE_OPENGL - if (getrendermode() < REND_POLYMOST || (getrendermode() >= REND_POLYMOST && !in3dmode())) -# endif - { - //getpalette(0,256,palette); - for (i=0; i<256; i++) - { - Bfputc(inverseit ? 255-curpalettefaded[i].b : curpalettefaded[i].b, fil); // b - Bfputc(inverseit ? 255-curpalettefaded[i].g : curpalettefaded[i].g, fil); // g - Bfputc(inverseit ? 255-curpalettefaded[i].r : curpalettefaded[i].r, fil); // r - } - } - -# ifdef USE_OPENGL - if (getrendermode() >= REND_POLYMOST && in3dmode()) - { - char c; - // 24bit - inversebuf = (char *)Xmalloc(xdim*ydim*3); - - bglReadPixels(0,0,xdim,ydim,GL_RGB,GL_UNSIGNED_BYTE,inversebuf); - j = xdim*ydim*3; - for (i=0; i=0; i--) - Bfwrite(ptr+i*bytesperline, xdim, 1, fil); - } - - enddrawing(); //}}} - - Bfclose(fil); - OSD_Printf("Saved screenshot to %s\n", fn); - Bfree(fn); - capturecount++; - return(0); -} -# endif - - -int32_t screencapture(const char *filename, char inverseit, const char *versionstr) -{ -#ifndef USE_LIBPNG - UNREFERENCED_PARAMETER(versionstr); - return screencapture_tga(filename,inverseit); -#else - return screencapture_png(filename,inverseit,versionstr); -#endif -} - - #ifdef POLYMER static void PolymerProcessModels(void) { diff --git a/polymer/eduke32/build/src/screenshot.c b/polymer/eduke32/build/src/screenshot.c new file mode 100644 index 000000000..e6abdc59e --- /dev/null +++ b/polymer/eduke32/build/src/screenshot.c @@ -0,0 +1,310 @@ + +#if (PNG_LIBPNG_VER > 10599) +# include +#endif + +#include "compat.h" +#include "build.h" +#include "editor.h" + +#ifdef USE_LIBPNG +//# include +# include +#endif + +int16_t capturecount = 0; + +// +// screencapture +// + +static int32_t screencapture_common1(char *fn, const char *ext, BFILE** filptr) +{ + int32_t i; + + do // JBF 2004022: So we don't overwrite existing screenshots + { + if (capturecount > 9999) return -1; + + i = Bstrrchr(fn, '.')-fn-4; + fn[i++] = ((capturecount/1000)%10)+48; + fn[i++] = ((capturecount/100)%10)+48; + fn[i++] = ((capturecount/10)%10)+48; + fn[i++] = (capturecount%10)+48; + i++; + Bstrcpy(&fn[i], ext); + + if ((*filptr = Bfopen(fn, "rb")) == NULL) break; + Bfclose(*filptr); + capturecount++; + } while (1); + + *filptr = Bfopen(fn, "wb"); + if (*filptr == NULL) + return -1; + + return 0; +} + +#ifdef USE_LIBPNG +// PNG screenshots -- adapted from libpng example.c +static int32_t screencapture_png(const char *filename, char inverseit, const char *versionstr) +{ + int32_t i; + BFILE *fp; +# ifdef USE_OPENGL +# define HICOLOR (getrendermode() >= REND_POLYMOST && in3dmode()) +# else +# define HICOLOR 0 +# endif + png_structp png_ptr; + png_infop info_ptr; + png_colorp palette = NULL; + png_textp text = NULL; + + png_bytep buf = NULL; + png_bytepp rowptrs = NULL; + + char fn[32]; // careful... + + Bstrcpy(fn, filename); + i = screencapture_common1(fn, "png", &fp); + if (i) + return i; + + /* Create and initialize the png_struct with default error handling. */ + png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); + if (png_ptr == NULL) + { + Bfclose(fp); + return -1; + } + + /* Allocate/initialize the image information data. */ + info_ptr = png_create_info_struct(png_ptr); + if (info_ptr == NULL) + { + Bfclose(fp); + png_destroy_write_struct(&png_ptr, NULL); + return -1; + } + + /* Set error handling. */ + if (setjmp(png_jmpbuf(png_ptr))) + { + /* If we get here, we had a problem writing the file */ + Bfclose(fp); + if (palette) png_free(png_ptr, palette); + if (text) png_free(png_ptr, text); + if (buf) png_free(png_ptr, buf); + if (rowptrs) png_free(png_ptr, rowptrs); + png_destroy_write_struct(&png_ptr, &info_ptr); + + return -1; + } + + png_init_io(png_ptr, fp); + + // initialize various info fields from here on + png_set_IHDR(png_ptr, info_ptr, xdim, ydim, 8, + HICOLOR ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_PALETTE, + PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); + + if (HICOLOR && editstatus==0) + png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, PNG_FILTER_VALUE_NONE); + + if (!HICOLOR) +#if (PNG_LIBPNG_VER > 10599) + palette = (png_colorp) png_malloc(png_ptr, 256*sizeof(png_color)); +#else + palette = (png_colorp) png_malloc(png_ptr, 256*png_sizeof(png_color)); +#endif + + if (palette) + { + for (i=0; i<256; i++) + { + palette[i].red = inverseit ? 255-curpalettefaded[i].r : curpalettefaded[i].r; + palette[i].green = inverseit ? 255-curpalettefaded[i].g : curpalettefaded[i].g; + palette[i].blue = inverseit ? 255-curpalettefaded[i].b : curpalettefaded[i].b; + } + + png_set_PLTE(png_ptr, info_ptr, palette, 256); + } + + // png_set_gAMA(png_ptr, info_ptr, vid_gamma); // 1.0/vid_gamma ? + // png_set_sRGB(png_ptr, info_ptr, PNG_sRGB_INTENT_SATURATION); // hm... + +#if (PNG_LIBPNG_VER > 10599) + text = (png_textp) png_malloc(png_ptr, 2*sizeof(png_text)); +#else + text = (png_textp) png_malloc(png_ptr, 2*png_sizeof(png_text)); +#endif + text[0].compression = PNG_TEXT_COMPRESSION_NONE; + text[0].key = Bstrdup("Title"); + text[0].text = Bstrdup((editstatus ? "Mapster32 screenshot" : "EDuke32 screenshot")); + + text[1].compression = PNG_TEXT_COMPRESSION_NONE; + text[1].key = Bstrdup("Software"); + text[1].text = Bstrdup(versionstr); + png_set_text(png_ptr, info_ptr, text, 2); + + // get/set the pixel data + begindrawing(); //{{{ + if (palette) + { + buf = (png_bytep) png_malloc(png_ptr, bytesperline*ydim); + Bmemcpy(buf, (char *) frameplace, bytesperline*ydim); + } +# ifdef USE_OPENGL + else + { + buf = (png_bytep) png_malloc(png_ptr, xdim*ydim*3); + bglReadPixels(0, 0, xdim, ydim, GL_RGB, GL_UNSIGNED_BYTE, buf); + } +# endif + enddrawing(); //}}} + + rowptrs = (png_bytepp) png_malloc(png_ptr, ydim*sizeof(png_bytep)); + + if (!palette) + { + for (i=0; i= REND_POLYMOST && in3dmode()) + { + head[1] = 0; // no colourmap + head[2] = 2; // uncompressed truecolour + head[3] = 0; // (low) first colourmap index + head[4] = 0; // (high) first colourmap index + head[5] = 0; // (low) number colourmap entries + head[6] = 0; // (high) number colourmap entries + head[7] = 0; // colourmap entry size + head[16] = 24; // 24 bits per pixel + } +# endif + + head[12] = xdim & 0xff; + head[13] = (xdim >> 8) & 0xff; + head[14] = ydim & 0xff; + head[15] = (ydim >> 8) & 0xff; + + Bfwrite(head, 18, 1, fil); + + begindrawing(); //{{{ + ptr = (char *) frameplace; + + // palette first +# ifdef USE_OPENGL + if (getrendermode() < REND_POLYMOST || (getrendermode() >= REND_POLYMOST && !in3dmode())) +# endif + { + //getpalette(0,256,palette); + for (i=0; i<256; i++) + { + Bfputc(inverseit ? 255-curpalettefaded[i].b : curpalettefaded[i].b, fil); // b + Bfputc(inverseit ? 255-curpalettefaded[i].g : curpalettefaded[i].g, fil); // g + Bfputc(inverseit ? 255-curpalettefaded[i].r : curpalettefaded[i].r, fil); // r + } + } + +# ifdef USE_OPENGL + if (getrendermode() >= REND_POLYMOST && in3dmode()) + { + char c; + // 24bit + inversebuf = (char *) Xmalloc(xdim*ydim*3); + + bglReadPixels(0, 0, xdim, ydim, GL_RGB, GL_UNSIGNED_BYTE, inversebuf); + j = xdim*ydim*3; + for (i=0; i=0; i--) + Bfwrite(ptr+i*bytesperline, xdim, 1, fil); + } + + enddrawing(); //}}} + + Bfclose(fil); + OSD_Printf("Saved screenshot to %s\n", fn); + Bfree(fn); + capturecount++; + return(0); +} +# endif + + +int32_t screencapture(const char *filename, char inverseit, const char *versionstr) +{ +#ifndef USE_LIBPNG + UNREFERENCED_PARAMETER(versionstr); + return screencapture_tga(filename, inverseit); +#else + return screencapture_png(filename, inverseit, versionstr); +#endif +} diff --git a/polymer/eduke32/eduke32.vcxproj b/polymer/eduke32/eduke32.vcxproj index c4fa8887a..a34bc4436 100644 --- a/polymer/eduke32/eduke32.vcxproj +++ b/polymer/eduke32/eduke32.vcxproj @@ -284,6 +284,7 @@ + diff --git a/polymer/eduke32/eduke32.vcxproj.filters b/polymer/eduke32/eduke32.vcxproj.filters index d42626a17..a68d72a20 100644 --- a/polymer/eduke32/eduke32.vcxproj.filters +++ b/polymer/eduke32/eduke32.vcxproj.filters @@ -944,6 +944,9 @@ build\source + + build\source +