tron@2186: /* $Id$ */ tron@2186: truelight@0: #include "stdafx.h" Darkvater@1891: #include "openttd.h" tron@1299: #include "debug.h" tron@2163: #include "functions.h" tron@1309: #include "strings.h" tron@507: #include "table/strings.h" truelight@0: #include "gfx.h" Darkvater@3329: #include "hal.h" truelight@0: #include "viewport.h" truelight@0: #include "player.h" tron@430: #include "screenshot.h" tron@2159: #include "variables.h" rubidium@4261: #include "date.h" rubidium@5838: #include "helpers.hpp" truelight@0: tron@2121: char _screenshot_format_name[8]; tron@2121: uint _num_screenshot_formats; tron@2121: uint _cur_screenshot_format; belugas@4184: ScreenshotType current_screenshot_type; tron@2121: celestar@9908: /* called by the ScreenShot proc to generate screenshot lines. */ tron@2120: typedef void ScreenshotCallback(void *userdata, Pixel *buf, uint y, uint pitch, uint n); tron@1991: typedef bool ScreenshotHandlerProc(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette); truelight@0: celestar@9895: struct ScreenshotFormat { truelight@0: const char *name; truelight@0: const char *extension; truelight@0: ScreenshotHandlerProc *proc; celestar@9895: }; truelight@0: truelight@0: //************************************************ truelight@0: //*** SCREENSHOT CODE FOR WINDOWS BITMAP (.BMP) truelight@0: //************************************************ truelight@923: #if defined(_MSC_VER) || defined(__WATCOMC__) truelight@0: #pragma pack(push, 1) truelight@0: #endif truelight@0: celestar@9895: struct BitmapFileHeader { truelight@0: uint16 type; truelight@0: uint32 size; truelight@0: uint32 reserved; truelight@0: uint32 off_bits; celestar@9895: } GCC_PACK; truelight@0: assert_compile(sizeof(BitmapFileHeader) == 14); truelight@0: truelight@923: #if defined(_MSC_VER) || defined(__WATCOMC__) truelight@0: #pragma pack(pop) truelight@0: #endif truelight@0: celestar@9895: struct BitmapInfoHeader { truelight@0: uint32 size; truelight@0: int32 width, height; truelight@0: uint16 planes, bitcount; truelight@0: uint32 compression, sizeimage, xpels, ypels, clrused, clrimp; celestar@9895: }; truelight@0: assert_compile(sizeof(BitmapInfoHeader) == 40); truelight@0: celestar@9895: struct RgbQuad { truelight@0: byte blue, green, red, reserved; celestar@9895: }; truelight@0: assert_compile(sizeof(RgbQuad) == 4); truelight@0: celestar@9908: /* generic .BMP writer */ tron@1991: static bool MakeBmpImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette) truelight@0: { truelight@0: BitmapFileHeader bfh; truelight@0: BitmapInfoHeader bih; darkvater@318: RgbQuad rq[256]; truelight@0: FILE *f; truelight@0: uint i, padw; truelight@0: uint n, maxlines; truelight@0: celestar@9908: /* only implemented for 8bit images so far. */ truelight@0: if (pixelformat != 8) truelight@0: return false; truelight@0: Darkvater@5167: f = fopen(name, "wb"); truelight@0: if (f == NULL) return false; truelight@0: celestar@9908: /* each scanline must be aligned on a 32bit boundary */ tron@2398: padw = ALIGN(w, 4); truelight@0: celestar@9908: /* setup the file header */ truelight@0: bfh.type = TO_LE16('MB'); truelight@0: bfh.size = TO_LE32(sizeof(bfh) + sizeof(bih) + sizeof(RgbQuad) * 256 + padw * h); truelight@0: bfh.reserved = 0; truelight@0: bfh.off_bits = TO_LE32(sizeof(bfh) + sizeof(bih) + sizeof(RgbQuad) * 256); truelight@0: celestar@9908: /* setup the info header */ truelight@0: bih.size = TO_LE32(sizeof(BitmapInfoHeader)); truelight@0: bih.width = TO_LE32(w); truelight@0: bih.height = TO_LE32(h); truelight@0: bih.planes = TO_LE16(1); truelight@0: bih.bitcount = TO_LE16(8); truelight@0: bih.compression = 0; truelight@0: bih.sizeimage = 0; truelight@0: bih.xpels = 0; truelight@0: bih.ypels = 0; truelight@0: bih.clrused = 0; truelight@0: bih.clrimp = 0; truelight@0: celestar@9908: /* convert the palette to the windows format */ tron@430: for (i = 0; i != 256; i++) { tron@1991: rq[i].red = palette[i].r; tron@1991: rq[i].green = palette[i].g; tron@1991: rq[i].blue = palette[i].b; truelight@0: rq[i].reserved = 0; truelight@0: } truelight@0: celestar@9908: /* write file header and info header and palette */ tron@430: if (fwrite(&bfh, sizeof(bfh), 1, f) != 1) return false; tron@430: if (fwrite(&bih, sizeof(bih), 1, f) != 1) return false; tron@430: if (fwrite(rq, sizeof(rq), 1, f) != 1) return false; truelight@0: celestar@9908: /* use by default 64k temp memory */ truelight@0: maxlines = clamp(65536 / padw, 16, 128); truelight@0: celestar@9908: /* now generate the bitmap bits */ KUDr@5860: Pixel *buff = MallocT(padw * maxlines); // by default generate 128 lines at a time. tron@430: if (buff == NULL) { tron@430: fclose(f); tron@430: return false; tron@430: } truelight@0: memset(buff, 0, padw * maxlines); // zero the buffer to have the padding bytes set to 0 truelight@0: celestar@9908: /* start at the bottom, since bitmaps are stored bottom up. */ truelight@0: do { celestar@9908: /* determine # lines */ truelight@0: n = min(h, maxlines); truelight@0: h -= n; truelight@193: celestar@9908: /* render the pixels */ truelight@0: callb(userdata, buff, h, padw, n); truelight@193: celestar@9908: /* write each line */ truelight@193: while (n) tron@430: if (fwrite(buff + (--n) * padw, padw, 1, f) != 1) { tron@430: free(buff); tron@430: fclose(f); tron@430: return false; tron@430: } tron@430: } while (h != 0); truelight@0: tron@430: free(buff); truelight@0: fclose(f); truelight@0: truelight@0: return true; truelight@0: } truelight@0: truelight@0: //******************************************************** truelight@0: //*** SCREENSHOT CODE FOR PORTABLE NETWORK GRAPHICS (.PNG) truelight@0: //******************************************************** truelight@0: #if defined(WITH_PNG) truelight@0: #include truelight@0: truelight@0: static void PNGAPI png_my_error(png_structp png_ptr, png_const_charp message) truelight@0: { Darkvater@5568: DEBUG(misc, 0, "[libpng] error: %s - %s", message, (char *)png_get_error_ptr(png_ptr)); truelight@0: longjmp(png_ptr->jmpbuf, 1); truelight@0: } truelight@0: truelight@0: static void PNGAPI png_my_warning(png_structp png_ptr, png_const_charp message) truelight@0: { Darkvater@5568: DEBUG(misc, 1, "[libpng] warning: %s - %s", message, (char *)png_get_error_ptr(png_ptr)); truelight@0: } truelight@0: tron@1991: static bool MakePNGImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette) truelight@0: { darkvater@318: png_color rq[256]; truelight@0: FILE *f; truelight@0: uint i, y, n; truelight@0: uint maxlines; truelight@0: png_structp png_ptr; truelight@0: png_infop info_ptr; truelight@0: celestar@9908: /* only implemented for 8bit images so far. */ truelight@0: if (pixelformat != 8) truelight@0: return false; truelight@0: Darkvater@5167: f = fopen(name, "wb"); truelight@0: if (f == NULL) return false; truelight@0: tron@430: png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (char *)name, png_my_error, png_my_warning); truelight@0: tron@430: if (png_ptr == NULL) { truelight@0: fclose(f); truelight@0: return false; truelight@0: } truelight@0: truelight@0: info_ptr = png_create_info_struct(png_ptr); tron@430: if (info_ptr == NULL) { truelight@0: png_destroy_write_struct(&png_ptr, (png_infopp)NULL); truelight@0: fclose(f); truelight@0: return false; truelight@0: } truelight@0: truelight@0: if (setjmp(png_jmpbuf(png_ptr))) { truelight@0: png_destroy_write_struct(&png_ptr, &info_ptr); truelight@0: fclose(f); truelight@0: return false; truelight@0: } truelight@0: truelight@0: png_init_io(png_ptr, f); truelight@193: truelight@0: png_set_filter(png_ptr, 0, PNG_FILTER_NONE); truelight@0: tron@430: png_set_IHDR(png_ptr, info_ptr, w, h, pixelformat, PNG_COLOR_TYPE_PALETTE, tron@430: PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); truelight@0: celestar@9908: /* convert the palette to the .PNG format. */ tron@1991: for (i = 0; i != 256; i++) { tron@1991: rq[i].red = palette[i].r; tron@1991: rq[i].green = palette[i].g; tron@1991: rq[i].blue = palette[i].b; truelight@0: } truelight@0: truelight@0: png_set_PLTE(png_ptr, info_ptr, rq, 256); truelight@0: png_write_info(png_ptr, info_ptr); truelight@0: png_set_flush(png_ptr, 512); truelight@0: celestar@9908: /* use by default 64k temp memory */ truelight@0: maxlines = clamp(65536 / w, 16, 128); truelight@0: celestar@9908: /* now generate the bitmap bits */ KUDr@5860: Pixel *buff = MallocT(w * maxlines); // by default generate 128 lines at a time. tron@430: if (buff == NULL) { tron@430: png_destroy_write_struct(&png_ptr, &info_ptr); tron@430: fclose(f); tron@430: return false; tron@430: } truelight@0: memset(buff, 0, w * maxlines); // zero the buffer to have the padding bytes set to 0 truelight@0: truelight@0: y = 0; truelight@0: do { celestar@9908: /* determine # lines to write */ truelight@0: n = min(h - y, maxlines); truelight@193: celestar@9908: /* render the pixels into the buffer */ truelight@0: callb(userdata, buff, y, w, n); truelight@0: y += n; truelight@0: celestar@9908: /* write them to png */ tron@430: for (i = 0; i != n; i++) truelight@0: png_write_row(png_ptr, buff + i * w); truelight@0: } while (y != h); truelight@0: truelight@0: png_write_end(png_ptr, info_ptr); truelight@0: png_destroy_write_struct(&png_ptr, &info_ptr); truelight@0: tron@430: free(buff); truelight@0: fclose(f); truelight@0: return true; truelight@0: } truelight@4300: #endif /* WITH_PNG */ truelight@0: truelight@0: truelight@0: //************************************************ truelight@0: //*** SCREENSHOT CODE FOR ZSOFT PAINTBRUSH (.PCX) truelight@0: //************************************************ truelight@0: celestar@9895: struct PcxHeader { truelight@0: byte manufacturer; truelight@0: byte version; truelight@0: byte rle; truelight@0: byte bpp; truelight@0: uint32 unused; truelight@0: uint16 xmax, ymax; truelight@0: uint16 hdpi, vdpi; truelight@0: byte pal_small[16*3]; truelight@0: byte reserved; truelight@0: byte planes; truelight@0: uint16 pitch; truelight@0: uint16 cpal; truelight@0: uint16 width; truelight@0: uint16 height; truelight@0: byte filler[54]; celestar@9895: }; truelight@0: assert_compile(sizeof(PcxHeader) == 128); truelight@0: tron@1991: static bool MakePCXImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette) truelight@0: { truelight@0: FILE *f; truelight@0: uint maxlines; truelight@0: uint y; truelight@0: PcxHeader pcx; peter1138@4019: bool success; truelight@0: truelight@0: if (pixelformat != 8 || w == 0) truelight@0: return false; truelight@0: Darkvater@5167: f = fopen(name, "wb"); truelight@0: if (f == NULL) return false; truelight@0: truelight@0: memset(&pcx, 0, sizeof(pcx)); truelight@193: celestar@9908: /* setup pcx header */ truelight@0: pcx.manufacturer = 10; truelight@0: pcx.version = 5; truelight@0: pcx.rle = 1; truelight@0: pcx.bpp = 8; tron@430: pcx.xmax = TO_LE16(w - 1); tron@430: pcx.ymax = TO_LE16(h - 1); truelight@0: pcx.hdpi = TO_LE16(320); truelight@0: pcx.vdpi = TO_LE16(320); truelight@0: truelight@0: pcx.planes = 1; truelight@0: pcx.cpal = TO_LE16(1); truelight@0: pcx.width = pcx.pitch = TO_LE16(w); truelight@0: pcx.height = TO_LE16(h); truelight@193: celestar@9908: /* write pcx header */ tron@430: if (fwrite(&pcx, sizeof(pcx), 1, f) != 1) { tron@430: fclose(f); tron@430: return false; tron@430: } truelight@0: celestar@9908: /* use by default 64k temp memory */ truelight@0: maxlines = clamp(65536 / w, 16, 128); truelight@0: celestar@9908: /* now generate the bitmap bits */ KUDr@5860: Pixel *buff = MallocT(w * maxlines); // by default generate 128 lines at a time. tron@430: if (buff == NULL) { tron@430: fclose(f); tron@430: return false; tron@430: } tron@430: memset(buff, 0, w * maxlines); // zero the buffer to have the padding bytes set to 0 truelight@0: truelight@0: y = 0; truelight@0: do { celestar@9908: /* determine # lines to write */ tron@430: uint n = min(h - y, maxlines); tron@430: uint i; truelight@193: celestar@9908: /* render the pixels into the buffer */ truelight@0: callb(userdata, buff, y, w, n); truelight@0: y += n; truelight@0: celestar@9908: /* write them to pcx */ tron@430: for (i = 0; i != n; i++) { tron@2062: const Pixel* bufp = buff + i * w; tron@2004: byte runchar = bufp[0]; tron@2004: uint runcount = 1; tron@2004: uint j; truelight@0: celestar@9908: /* for each pixel... */ tron@2004: for (j = 1; j < w; j++) { tron@2062: Pixel ch = bufp[j]; tron@2004: truelight@0: if (ch != runchar || runcount >= 0x3f) { tron@430: if (runcount > 1 || (runchar & 0xC0) == 0xC0) tron@430: if (fputc(0xC0 | runcount, f) == EOF) { tron@430: free(buff); tron@430: fclose(f); tron@430: return false; tron@430: } tron@430: if (fputc(runchar, f) == EOF) { tron@430: free(buff); tron@430: fclose(f); tron@430: return false; tron@430: } truelight@0: runcount = 0; truelight@0: runchar = ch; truelight@0: } truelight@0: runcount++; truelight@0: } truelight@0: celestar@9908: /* write remaining bytes.. */ tron@430: if (runcount > 1 || (runchar & 0xC0) == 0xC0) tron@430: if (fputc(0xC0 | runcount, f) == EOF) { tron@430: free(buff); tron@430: fclose(f); tron@430: return false; tron@430: } tron@430: if (fputc(runchar, f) == EOF) { tron@430: free(buff); tron@430: fclose(f); tron@430: return false; tron@430: } truelight@0: } truelight@0: } while (y != h); truelight@0: tron@430: free(buff); tron@430: celestar@9908: /* write 8-bit color palette */ tron@430: if (fputc(12, f) == EOF) { tron@430: fclose(f); tron@430: return false; tron@430: } tron@1991: peter1138@4019: if (sizeof(*palette) == 3) { peter1138@4019: success = fwrite(palette, 256 * sizeof(*palette), 1, f) == 1; peter1138@4019: } else { peter1138@4019: /* If the palette is word-aligned, copy it to a temporary byte array */ tron@4079: byte tmp[256 * 3]; peter1138@4019: uint i; tron@4079: peter1138@4019: for (i = 0; i < 256; i++) { peter1138@4019: tmp[i * 3 + 0] = palette[i].r; peter1138@4019: tmp[i * 3 + 1] = palette[i].g; peter1138@4019: tmp[i * 3 + 2] = palette[i].b; peter1138@4019: } tron@4079: success = fwrite(tmp, sizeof(tmp), 1, f) == 1; tron@430: } peter1138@4019: truelight@0: fclose(f); truelight@0: peter1138@4019: return success; truelight@0: } truelight@0: truelight@0: //************************************************ truelight@0: //*** GENERIC SCREENSHOT CODE truelight@0: //************************************************ truelight@0: truelight@0: static const ScreenshotFormat _screenshot_formats[] = { truelight@0: #if defined(WITH_PNG) truelight@0: {"PNG", "png", &MakePNGImage}, truelight@0: #endif matthijs@4360: {"BMP", "bmp", &MakeBmpImage}, truelight@0: {"PCX", "pcx", &MakePCXImage}, truelight@0: }; truelight@0: celestar@9895: void InitializeScreenshotFormats() truelight@0: { tron@430: int i, j; tron@430: for (i = 0, j = 0; i != lengthof(_screenshot_formats); i++) tron@430: if (!strcmp(_screenshot_format_name, _screenshot_formats[i].extension)) { tron@430: j = i; tron@430: break; tron@430: } truelight@0: _cur_screenshot_format = j; truelight@0: _num_screenshot_formats = lengthof(_screenshot_formats); belugas@4185: current_screenshot_type = SC_NONE; truelight@0: } truelight@0: truelight@0: const char *GetScreenshotFormatDesc(int i) truelight@0: { truelight@0: return _screenshot_formats[i].name; truelight@0: } truelight@0: truelight@0: void SetScreenshotFormat(int i) truelight@0: { truelight@0: _cur_screenshot_format = i; truelight@0: strcpy(_screenshot_format_name, _screenshot_formats[i].extension); truelight@0: } truelight@0: celestar@9908: /* screenshot generator that dumps the current video buffer */ tron@2062: static void CurrentScreenCallback(void *userdata, Pixel *buf, uint y, uint pitch, uint n) truelight@0: { tron@430: for (; n > 0; --n) { truelight@315: memcpy(buf, _screen.dst_ptr + y * _screen.pitch, _screen.width); truelight@315: ++y; truelight@315: buf += pitch; truelight@315: } truelight@0: } truelight@0: celestar@9908: /* generate a large piece of the world */ tron@2062: static void LargeWorldCallback(void *userdata, Pixel *buf, uint y, uint pitch, uint n) truelight@0: { tron@4522: ViewPort *vp = (ViewPort *)userdata; truelight@0: DrawPixelInfo dpi, *old_dpi; truelight@0: int wx, left; truelight@0: truelight@0: old_dpi = _cur_dpi; truelight@0: _cur_dpi = &dpi; truelight@0: truelight@0: dpi.dst_ptr = buf; truelight@0: dpi.height = n; truelight@0: dpi.width = vp->width; truelight@0: dpi.pitch = pitch; truelight@0: dpi.zoom = 0; truelight@0: dpi.left = 0; truelight@0: dpi.top = y; truelight@0: truelight@0: left = 0; truelight@0: while (vp->width - left != 0) { truelight@0: wx = min(vp->width - left, 1600); truelight@0: left += wx; truelight@193: truelight@193: ViewportDoDraw(vp, truelight@0: ((left - wx - vp->left) << vp->zoom) + vp->virtual_left, truelight@0: ((y - vp->top) << vp->zoom) + vp->virtual_top, truelight@0: ((left - vp->left) << vp->zoom) + vp->virtual_left, tron@430: (((y + n) - vp->top) << vp->zoom) + vp->virtual_top truelight@0: ); truelight@0: } truelight@0: truelight@0: _cur_dpi = old_dpi; truelight@0: } truelight@0: truelight@0: static char *MakeScreenshotName(const char *ext) truelight@0: { truelight@0: static char filename[256]; truelight@0: char *base; truelight@0: int serial; truelight@0: Darkvater@4848: if (_game_mode == GM_EDITOR || _game_mode == GM_MENU || _local_player == PLAYER_SPECTATOR) { truelight@0: sprintf(_screenshot_name, "screenshot"); truelight@0: } else { tron@2548: const Player* p = GetPlayer(_local_player); tron@534: SetDParam(0, p->name_1); tron@534: SetDParam(1, p->name_2); tron@534: SetDParam(2, _date); Darkvater@4912: GetString(_screenshot_name, STR_4004, lastof(_screenshot_name)); truelight@0: } truelight@0: truelight@0: base = strchr(_screenshot_name, 0); truelight@0: base[0] = '.'; strcpy(base + 1, ext); truelight@0: truelight@0: serial = 0; tron@430: for (;;) { Darkvater@5296: snprintf(filename, sizeof(filename), "%s%s", _paths.personal_dir, _screenshot_name); truelight@0: if (!FileExists(filename)) truelight@0: break; truelight@0: sprintf(base, " #%d.%s", ++serial, ext); truelight@0: } truelight@0: truelight@0: return filename; truelight@0: } truelight@0: belugas@4184: void SetScreenshotType(ScreenshotType t) belugas@4184: { belugas@4184: current_screenshot_type = t; belugas@4184: } belugas@4184: celestar@9895: bool IsScreenshotRequested() belugas@4184: { belugas@4184: return (current_screenshot_type != SC_NONE); belugas@4184: } belugas@4184: celestar@9895: static bool MakeSmallScreenshot() truelight@0: { truelight@0: const ScreenshotFormat *sf = _screenshot_formats + _cur_screenshot_format; truelight@193: return sf->proc(MakeScreenshotName(sf->extension), CurrentScreenCallback, NULL, _screen.width, _screen.height, 8, _cur_palette); truelight@0: } truelight@0: celestar@9895: static bool MakeWorldScreenshot() truelight@0: { truelight@0: ViewPort vp; truelight@0: const ScreenshotFormat *sf; truelight@0: belugas@4184: vp.zoom = 0; truelight@0: vp.left = 0; truelight@0: vp.top = 0; belugas@4184: vp.virtual_left = -(int)MapMaxX() * TILE_PIXELS; belugas@4184: vp.virtual_top = 0; belugas@4184: vp.virtual_width = (MapMaxX() + MapMaxY()) * TILE_PIXELS; belugas@4184: vp.width = vp.virtual_width; belugas@4184: vp.virtual_height = (MapMaxX() + MapMaxY()) * TILE_PIXELS >> 1; belugas@4184: vp.height = vp.virtual_height; truelight@0: truelight@0: sf = _screenshot_formats + _cur_screenshot_format; truelight@0: return sf->proc(MakeScreenshotName(sf->extension), LargeWorldCallback, &vp, vp.width, vp.height, 8, _cur_palette); truelight@0: } belugas@4184: celestar@9895: bool MakeScreenshot() belugas@4184: { belugas@4184: switch (current_screenshot_type) { belugas@4184: case SC_VIEWPORT: belugas@4184: UndrawMouseCursor(); belugas@5559: DrawDirtyBlocks(); belugas@4184: current_screenshot_type = SC_NONE; belugas@4184: return MakeSmallScreenshot(); belugas@4184: case SC_WORLD: belugas@4184: current_screenshot_type = SC_NONE; belugas@4184: return MakeWorldScreenshot(); belugas@4184: default: return false; belugas@4184: } belugas@4184: } belugas@4184: Darkvater@5568: Darkvater@5568: