2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file screenshot.cpp The creation of screenshots! */
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2005-06-02 19:30:21 +00:00
|
|
|
#include "openttd.h"
|
2008-08-31 10:50:05 +00:00
|
|
|
#include "fileio_func.h"
|
2008-01-09 09:57:48 +00:00
|
|
|
#include "viewport_func.h"
|
|
|
|
#include "gfx_func.h"
|
2004-11-15 19:25:59 +00:00
|
|
|
#include "screenshot.h"
|
2007-06-17 20:30:28 +00:00
|
|
|
#include "blitter/factory.hpp"
|
2007-12-23 10:56:02 +00:00
|
|
|
#include "zoom_func.h"
|
2007-12-25 13:28:09 +00:00
|
|
|
#include "core/endian_func.hpp"
|
2007-12-26 11:45:43 +00:00
|
|
|
#include "map_func.h"
|
2009-01-04 15:32:25 +00:00
|
|
|
#include "saveload/saveload.h"
|
2008-09-30 20:51:04 +00:00
|
|
|
#include "company_func.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
|
2005-07-19 06:47:07 +00:00
|
|
|
char _screenshot_format_name[8];
|
|
|
|
uint _num_screenshot_formats;
|
|
|
|
uint _cur_screenshot_format;
|
2009-02-25 21:45:14 +00:00
|
|
|
char _screenshot_name[128];
|
2009-11-01 18:08:58 +00:00
|
|
|
static ScreenshotType _screenshot_type;
|
2005-07-19 06:47:07 +00:00
|
|
|
|
2007-04-04 01:35:16 +00:00
|
|
|
/* called by the ScreenShot proc to generate screenshot lines. */
|
2007-06-12 20:24:12 +00:00
|
|
|
typedef void ScreenshotCallback(void *userdata, void *buf, uint y, uint pitch, uint n);
|
2009-11-14 15:28:57 +00:00
|
|
|
typedef bool ScreenshotHandlerProc(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-07 12:11:48 +00:00
|
|
|
struct ScreenshotFormat {
|
2004-08-09 17:04:08 +00:00
|
|
|
const char *name;
|
|
|
|
const char *extension;
|
|
|
|
ScreenshotHandlerProc *proc;
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
/*************************************************
|
|
|
|
**** SCREENSHOT CODE FOR WINDOWS BITMAP (.BMP)
|
|
|
|
*************************************************/
|
2005-01-07 09:28:16 +00:00
|
|
|
#if defined(_MSC_VER) || defined(__WATCOMC__)
|
2004-08-09 17:04:08 +00:00
|
|
|
#pragma pack(push, 1)
|
|
|
|
#endif
|
|
|
|
|
2009-11-01 20:54:21 +00:00
|
|
|
/** BMP File Header (stored in little endian) */
|
2007-03-07 12:11:48 +00:00
|
|
|
struct BitmapFileHeader {
|
2004-08-09 17:04:08 +00:00
|
|
|
uint16 type;
|
|
|
|
uint32 size;
|
|
|
|
uint32 reserved;
|
|
|
|
uint32 off_bits;
|
2007-03-07 12:11:48 +00:00
|
|
|
} GCC_PACK;
|
2004-08-09 17:04:08 +00:00
|
|
|
assert_compile(sizeof(BitmapFileHeader) == 14);
|
|
|
|
|
2005-01-07 09:28:16 +00:00
|
|
|
#if defined(_MSC_VER) || defined(__WATCOMC__)
|
2004-08-09 17:04:08 +00:00
|
|
|
#pragma pack(pop)
|
|
|
|
#endif
|
|
|
|
|
2009-11-01 20:54:21 +00:00
|
|
|
/** BMP Info Header (stored in little endian) */
|
2007-03-07 12:11:48 +00:00
|
|
|
struct BitmapInfoHeader {
|
2004-08-09 17:04:08 +00:00
|
|
|
uint32 size;
|
|
|
|
int32 width, height;
|
|
|
|
uint16 planes, bitcount;
|
|
|
|
uint32 compression, sizeimage, xpels, ypels, clrused, clrimp;
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2004-08-09 17:04:08 +00:00
|
|
|
assert_compile(sizeof(BitmapInfoHeader) == 40);
|
|
|
|
|
2009-11-01 20:54:21 +00:00
|
|
|
/** Format of palette data in BMP header */
|
2007-03-07 12:11:48 +00:00
|
|
|
struct RgbQuad {
|
2004-08-09 17:04:08 +00:00
|
|
|
byte blue, green, red, reserved;
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2004-08-09 17:04:08 +00:00
|
|
|
assert_compile(sizeof(RgbQuad) == 4);
|
|
|
|
|
2009-11-01 20:54:21 +00:00
|
|
|
/** Pixel data in 24bpp BMP */
|
|
|
|
struct RgbTriplet {
|
|
|
|
byte b, g, r;
|
|
|
|
};
|
|
|
|
assert_compile(sizeof(RgbTriplet) == 3);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generic .BMP writer
|
|
|
|
* @param name file name including extension
|
|
|
|
* @param callb callback used for gathering rendered image
|
|
|
|
* @param userdata parameters forwarded to #callb
|
|
|
|
* @param w width in pixels
|
|
|
|
* @param h height in pixels
|
|
|
|
* @param pixelformat bits per pixel
|
2009-11-13 17:40:21 +00:00
|
|
|
* @param palette colour palette (for 8bpp mode)
|
2009-11-01 20:54:21 +00:00
|
|
|
* @return was everything ok?
|
|
|
|
*/
|
2009-11-14 15:31:06 +00:00
|
|
|
static bool MakeBMPImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-11-01 20:54:21 +00:00
|
|
|
uint bpp; // bytes per pixel
|
|
|
|
switch (pixelformat) {
|
|
|
|
case 8: bpp = 1; break;
|
|
|
|
/* 32bpp mode is saved as 24bpp BMP */
|
|
|
|
case 32: bpp = 3; break;
|
|
|
|
/* Only implemented for 8bit and 32bit images so far */
|
|
|
|
default: return false;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-11-01 20:54:21 +00:00
|
|
|
FILE *f = fopen(name, "wb");
|
2004-08-09 17:04:08 +00:00
|
|
|
if (f == NULL) return false;
|
|
|
|
|
2009-11-01 20:54:21 +00:00
|
|
|
/* Each scanline must be aligned on a 32bit boundary */
|
|
|
|
uint bytewidth = Align(w * bpp, 4); // bytes per line in file
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-11-01 20:54:21 +00:00
|
|
|
/* Size of palette. Only present for 8bpp mode */
|
|
|
|
uint pal_size = pixelformat == 8 ? sizeof(RgbQuad) * 256 : 0;
|
2007-06-12 20:24:12 +00:00
|
|
|
|
2009-11-01 20:54:21 +00:00
|
|
|
/* Setup the file header */
|
|
|
|
BitmapFileHeader bfh;
|
2004-08-09 17:04:08 +00:00
|
|
|
bfh.type = TO_LE16('MB');
|
2009-11-01 20:54:21 +00:00
|
|
|
bfh.size = TO_LE32(sizeof(BitmapFileHeader) + sizeof(BitmapInfoHeader) + pal_size + bytewidth * h);
|
2004-08-09 17:04:08 +00:00
|
|
|
bfh.reserved = 0;
|
2009-11-01 20:54:21 +00:00
|
|
|
bfh.off_bits = TO_LE32(sizeof(BitmapFileHeader) + sizeof(BitmapInfoHeader) + pal_size);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-11-01 20:54:21 +00:00
|
|
|
/* Setup the info header */
|
|
|
|
BitmapInfoHeader bih;
|
2004-08-09 17:04:08 +00:00
|
|
|
bih.size = TO_LE32(sizeof(BitmapInfoHeader));
|
|
|
|
bih.width = TO_LE32(w);
|
|
|
|
bih.height = TO_LE32(h);
|
|
|
|
bih.planes = TO_LE16(1);
|
2009-11-01 20:54:21 +00:00
|
|
|
bih.bitcount = TO_LE16(bpp * 8);
|
2004-08-09 17:04:08 +00:00
|
|
|
bih.compression = 0;
|
|
|
|
bih.sizeimage = 0;
|
|
|
|
bih.xpels = 0;
|
|
|
|
bih.ypels = 0;
|
|
|
|
bih.clrused = 0;
|
|
|
|
bih.clrimp = 0;
|
|
|
|
|
2009-11-01 20:54:21 +00:00
|
|
|
/* Write file header and info header */
|
|
|
|
if (fwrite(&bfh, sizeof(bfh), 1, f) != 1 || fwrite(&bih, sizeof(bih), 1, f) != 1) {
|
|
|
|
fclose(f);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-06-12 20:24:12 +00:00
|
|
|
if (pixelformat == 8) {
|
2009-11-01 20:54:21 +00:00
|
|
|
/* Convert the palette to the windows format */
|
|
|
|
RgbQuad rq[256];
|
|
|
|
for (uint i = 0; i < 256; i++) {
|
2007-06-12 20:24:12 +00:00
|
|
|
rq[i].red = palette[i].r;
|
|
|
|
rq[i].green = palette[i].g;
|
|
|
|
rq[i].blue = palette[i].b;
|
|
|
|
rq[i].reserved = 0;
|
|
|
|
}
|
2009-11-01 20:54:21 +00:00
|
|
|
/* Write the palette */
|
|
|
|
if (fwrite(rq, sizeof(rq), 1, f) != 1) {
|
|
|
|
fclose(f);
|
|
|
|
return false;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-11-01 20:54:21 +00:00
|
|
|
/* Try to use 64k of memory, store between 16 and 128 lines */
|
|
|
|
uint maxlines = Clamp(65536 / (w * pixelformat / 8), 16, 128); // number of lines per iteration
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-11-01 20:54:21 +00:00
|
|
|
uint8 *buff = MallocT<uint8>(maxlines * w * pixelformat / 8); // buffer which is rendered to
|
|
|
|
uint8 *line = AllocaM(uint8, bytewidth); // one line, stored to file
|
|
|
|
memset(line, 0, bytewidth);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2009-11-01 20:54:21 +00:00
|
|
|
/* Start at the bottom, since bitmaps are stored bottom up */
|
2004-08-09 17:04:08 +00:00
|
|
|
do {
|
2009-11-01 20:54:21 +00:00
|
|
|
uint n = min(h, maxlines);
|
2004-08-09 17:04:08 +00:00
|
|
|
h -= n;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2009-11-01 20:54:21 +00:00
|
|
|
/* Render the pixels */
|
|
|
|
callb(userdata, buff, h, w, n);
|
|
|
|
|
|
|
|
/* Write each line */
|
|
|
|
while (n-- != 0) {
|
|
|
|
if (pixelformat == 8) {
|
|
|
|
/* Move to 'line', leave last few pixels in line zeroed */
|
|
|
|
memcpy(line, buff + n * w, w);
|
|
|
|
} else {
|
|
|
|
/* Convert from 'native' 32bpp to BMP-like 24bpp.
|
|
|
|
* Works for both big and little endian machines */
|
|
|
|
Colour *src = ((Colour *)buff) + n * w;
|
|
|
|
RgbTriplet *dst = (RgbTriplet *)line;
|
|
|
|
for (uint i = 0; i < w; i++) {
|
|
|
|
dst[i].r = src[i].r;
|
|
|
|
dst[i].g = src[i].g;
|
|
|
|
dst[i].b = src[i].b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Write to file */
|
|
|
|
if (fwrite(line, bytewidth, 1, f) != 1) {
|
2004-11-15 19:25:59 +00:00
|
|
|
free(buff);
|
|
|
|
fclose(f);
|
|
|
|
return false;
|
|
|
|
}
|
2009-11-01 20:54:21 +00:00
|
|
|
}
|
2004-11-15 19:25:59 +00:00
|
|
|
} while (h != 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-11-15 19:25:59 +00:00
|
|
|
free(buff);
|
2004-08-09 17:04:08 +00:00
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
/*********************************************************
|
|
|
|
**** SCREENSHOT CODE FOR PORTABLE NETWORK GRAPHICS (.PNG)
|
|
|
|
*********************************************************/
|
2004-08-09 17:04:08 +00:00
|
|
|
#if defined(WITH_PNG)
|
|
|
|
#include <png.h>
|
|
|
|
|
|
|
|
static void PNGAPI png_my_error(png_structp png_ptr, png_const_charp message)
|
|
|
|
{
|
2009-11-14 15:28:57 +00:00
|
|
|
DEBUG(misc, 0, "[libpng] error: %s - %s", message, (const char *)png_get_error_ptr(png_ptr));
|
2004-08-09 17:04:08 +00:00
|
|
|
longjmp(png_ptr->jmpbuf, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void PNGAPI png_my_warning(png_structp png_ptr, png_const_charp message)
|
|
|
|
{
|
2009-11-14 15:28:57 +00:00
|
|
|
DEBUG(misc, 1, "[libpng] warning: %s - %s", message, (const char *)png_get_error_ptr(png_ptr));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-11-14 15:28:57 +00:00
|
|
|
static bool MakePNGImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2004-09-27 12:36:59 +00:00
|
|
|
png_color rq[256];
|
2004-08-09 17:04:08 +00:00
|
|
|
FILE *f;
|
|
|
|
uint i, y, n;
|
|
|
|
uint maxlines;
|
2007-06-12 20:24:12 +00:00
|
|
|
uint bpp = pixelformat / 8;
|
2004-08-09 17:04:08 +00:00
|
|
|
png_structp png_ptr;
|
|
|
|
png_infop info_ptr;
|
|
|
|
|
2007-06-12 20:24:12 +00:00
|
|
|
/* only implemented for 8bit and 32bit images so far. */
|
|
|
|
if (pixelformat != 8 && pixelformat != 32) return false;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2006-11-28 14:42:31 +00:00
|
|
|
f = fopen(name, "wb");
|
2004-08-09 17:04:08 +00:00
|
|
|
if (f == NULL) return false;
|
|
|
|
|
2009-11-14 15:28:57 +00:00
|
|
|
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (void *)name, png_my_error, png_my_warning);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2004-11-15 19:25:59 +00:00
|
|
|
if (png_ptr == NULL) {
|
2004-08-09 17:04:08 +00:00
|
|
|
fclose(f);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
info_ptr = png_create_info_struct(png_ptr);
|
2004-11-15 19:25:59 +00:00
|
|
|
if (info_ptr == NULL) {
|
2004-08-09 17:04:08 +00:00
|
|
|
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
|
|
|
|
fclose(f);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setjmp(png_jmpbuf(png_ptr))) {
|
|
|
|
png_destroy_write_struct(&png_ptr, &info_ptr);
|
|
|
|
fclose(f);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
png_init_io(png_ptr, f);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
png_set_filter(png_ptr, 0, PNG_FILTER_NONE);
|
|
|
|
|
2007-06-12 20:24:12 +00:00
|
|
|
png_set_IHDR(png_ptr, info_ptr, w, h, 8, pixelformat == 8 ? PNG_COLOR_TYPE_PALETTE : PNG_COLOR_TYPE_RGB,
|
2004-11-15 19:25:59 +00:00
|
|
|
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-06-12 20:24:12 +00:00
|
|
|
if (pixelformat == 8) {
|
|
|
|
/* convert the palette to the .PNG format. */
|
|
|
|
for (i = 0; i != 256; i++) {
|
|
|
|
rq[i].red = palette[i].r;
|
|
|
|
rq[i].green = palette[i].g;
|
|
|
|
rq[i].blue = palette[i].b;
|
|
|
|
}
|
|
|
|
|
|
|
|
png_set_PLTE(png_ptr, info_ptr, rq, 256);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
png_write_info(png_ptr, info_ptr);
|
|
|
|
png_set_flush(png_ptr, 512);
|
|
|
|
|
2007-06-12 20:24:12 +00:00
|
|
|
if (pixelformat == 32) {
|
|
|
|
png_color_8 sig_bit;
|
|
|
|
|
2009-02-09 02:57:15 +00:00
|
|
|
/* Save exact colour/alpha resolution */
|
2007-06-12 20:24:12 +00:00
|
|
|
sig_bit.alpha = 0;
|
|
|
|
sig_bit.blue = 8;
|
|
|
|
sig_bit.green = 8;
|
|
|
|
sig_bit.red = 8;
|
|
|
|
sig_bit.gray = 8;
|
|
|
|
png_set_sBIT(png_ptr, info_ptr, &sig_bit);
|
|
|
|
|
2008-06-17 19:38:00 +00:00
|
|
|
#if TTD_ENDIAN == TTD_LITTLE_ENDIAN
|
2007-06-12 20:24:12 +00:00
|
|
|
png_set_bgr(png_ptr);
|
|
|
|
png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
|
|
|
|
#else
|
|
|
|
png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
|
2008-06-17 19:38:00 +00:00
|
|
|
#endif /* TTD_ENDIAN == TTD_LITTLE_ENDIAN */
|
2007-06-12 20:24:12 +00:00
|
|
|
}
|
|
|
|
|
2007-04-04 01:35:16 +00:00
|
|
|
/* use by default 64k temp memory */
|
2007-11-19 18:38:10 +00:00
|
|
|
maxlines = Clamp(65536 / w, 16, 128);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-04 01:35:16 +00:00
|
|
|
/* now generate the bitmap bits */
|
2009-02-24 21:32:23 +00:00
|
|
|
void *buff = CallocT<uint8>(w * maxlines * bpp); // by default generate 128 lines at a time.
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
y = 0;
|
|
|
|
do {
|
2007-04-04 01:35:16 +00:00
|
|
|
/* determine # lines to write */
|
2004-08-09 17:04:08 +00:00
|
|
|
n = min(h - y, maxlines);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-04-04 01:35:16 +00:00
|
|
|
/* render the pixels into the buffer */
|
2004-08-09 17:04:08 +00:00
|
|
|
callb(userdata, buff, y, w, n);
|
|
|
|
y += n;
|
|
|
|
|
2007-04-04 01:35:16 +00:00
|
|
|
/* write them to png */
|
2004-11-15 19:25:59 +00:00
|
|
|
for (i = 0; i != n; i++)
|
2007-06-12 20:24:12 +00:00
|
|
|
png_write_row(png_ptr, (png_bytep)buff + i * w * bpp);
|
2004-08-09 17:04:08 +00:00
|
|
|
} while (y != h);
|
|
|
|
|
|
|
|
png_write_end(png_ptr, info_ptr);
|
|
|
|
png_destroy_write_struct(&png_ptr, &info_ptr);
|
|
|
|
|
2004-11-15 19:25:59 +00:00
|
|
|
free(buff);
|
2004-08-09 17:04:08 +00:00
|
|
|
fclose(f);
|
|
|
|
return true;
|
|
|
|
}
|
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
|
|
|
#endif /* WITH_PNG */
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
/*************************************************
|
|
|
|
**** SCREENSHOT CODE FOR ZSOFT PAINTBRUSH (.PCX)
|
|
|
|
*************************************************/
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-07 12:11:48 +00:00
|
|
|
struct PcxHeader {
|
2004-08-09 17:04:08 +00:00
|
|
|
byte manufacturer;
|
|
|
|
byte version;
|
|
|
|
byte rle;
|
|
|
|
byte bpp;
|
|
|
|
uint32 unused;
|
|
|
|
uint16 xmax, ymax;
|
|
|
|
uint16 hdpi, vdpi;
|
2007-04-18 22:10:36 +00:00
|
|
|
byte pal_small[16 * 3];
|
2004-08-09 17:04:08 +00:00
|
|
|
byte reserved;
|
|
|
|
byte planes;
|
|
|
|
uint16 pitch;
|
|
|
|
uint16 cpal;
|
|
|
|
uint16 width;
|
|
|
|
uint16 height;
|
|
|
|
byte filler[54];
|
2007-03-07 12:11:48 +00:00
|
|
|
};
|
2004-08-09 17:04:08 +00:00
|
|
|
assert_compile(sizeof(PcxHeader) == 128);
|
|
|
|
|
2009-11-14 15:28:57 +00:00
|
|
|
static bool MakePCXImage(const char *name, ScreenshotCallback *callb, void *userdata, uint w, uint h, int pixelformat, const Colour *palette)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
uint maxlines;
|
|
|
|
uint y;
|
|
|
|
PcxHeader pcx;
|
2006-06-13 17:13:46 +00:00
|
|
|
bool success;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-06-12 20:24:12 +00:00
|
|
|
if (pixelformat == 32) {
|
|
|
|
DEBUG(misc, 0, "Can't convert a 32bpp screenshot to PCX format. Please pick an other format.");
|
|
|
|
return false;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
if (pixelformat != 8 || w == 0)
|
|
|
|
return false;
|
|
|
|
|
2006-11-28 14:42:31 +00:00
|
|
|
f = fopen(name, "wb");
|
2004-08-09 17:04:08 +00:00
|
|
|
if (f == NULL) return false;
|
|
|
|
|
|
|
|
memset(&pcx, 0, sizeof(pcx));
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-04-04 01:35:16 +00:00
|
|
|
/* setup pcx header */
|
2004-08-09 17:04:08 +00:00
|
|
|
pcx.manufacturer = 10;
|
|
|
|
pcx.version = 5;
|
|
|
|
pcx.rle = 1;
|
|
|
|
pcx.bpp = 8;
|
2004-11-15 19:25:59 +00:00
|
|
|
pcx.xmax = TO_LE16(w - 1);
|
|
|
|
pcx.ymax = TO_LE16(h - 1);
|
2004-08-09 17:04:08 +00:00
|
|
|
pcx.hdpi = TO_LE16(320);
|
|
|
|
pcx.vdpi = TO_LE16(320);
|
|
|
|
|
|
|
|
pcx.planes = 1;
|
|
|
|
pcx.cpal = TO_LE16(1);
|
|
|
|
pcx.width = pcx.pitch = TO_LE16(w);
|
|
|
|
pcx.height = TO_LE16(h);
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-04-04 01:35:16 +00:00
|
|
|
/* write pcx header */
|
2004-11-15 19:25:59 +00:00
|
|
|
if (fwrite(&pcx, sizeof(pcx), 1, f) != 1) {
|
|
|
|
fclose(f);
|
|
|
|
return false;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-04 01:35:16 +00:00
|
|
|
/* use by default 64k temp memory */
|
2007-11-19 18:38:10 +00:00
|
|
|
maxlines = Clamp(65536 / w, 16, 128);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-04 01:35:16 +00:00
|
|
|
/* now generate the bitmap bits */
|
2009-02-24 21:32:23 +00:00
|
|
|
uint8 *buff = CallocT<uint8>(w * maxlines); // by default generate 128 lines at a time.
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
y = 0;
|
|
|
|
do {
|
2007-04-04 01:35:16 +00:00
|
|
|
/* determine # lines to write */
|
2004-11-15 19:25:59 +00:00
|
|
|
uint n = min(h - y, maxlines);
|
|
|
|
uint i;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-04-04 01:35:16 +00:00
|
|
|
/* render the pixels into the buffer */
|
2004-08-09 17:04:08 +00:00
|
|
|
callb(userdata, buff, y, w, n);
|
|
|
|
y += n;
|
|
|
|
|
2007-04-04 01:35:16 +00:00
|
|
|
/* write them to pcx */
|
2004-11-15 19:25:59 +00:00
|
|
|
for (i = 0; i != n; i++) {
|
2007-06-12 20:24:12 +00:00
|
|
|
const uint8 *bufp = buff + i * w;
|
2005-07-03 10:22:20 +00:00
|
|
|
byte runchar = bufp[0];
|
|
|
|
uint runcount = 1;
|
|
|
|
uint j;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-04-04 01:35:16 +00:00
|
|
|
/* for each pixel... */
|
2005-07-03 10:22:20 +00:00
|
|
|
for (j = 1; j < w; j++) {
|
2007-06-12 20:24:12 +00:00
|
|
|
uint8 ch = bufp[j];
|
2005-07-03 10:22:20 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (ch != runchar || runcount >= 0x3f) {
|
2004-11-15 19:25:59 +00:00
|
|
|
if (runcount > 1 || (runchar & 0xC0) == 0xC0)
|
|
|
|
if (fputc(0xC0 | runcount, f) == EOF) {
|
|
|
|
free(buff);
|
|
|
|
fclose(f);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (fputc(runchar, f) == EOF) {
|
|
|
|
free(buff);
|
|
|
|
fclose(f);
|
|
|
|
return false;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
runcount = 0;
|
|
|
|
runchar = ch;
|
|
|
|
}
|
|
|
|
runcount++;
|
|
|
|
}
|
|
|
|
|
2007-04-04 01:35:16 +00:00
|
|
|
/* write remaining bytes.. */
|
2004-11-15 19:25:59 +00:00
|
|
|
if (runcount > 1 || (runchar & 0xC0) == 0xC0)
|
|
|
|
if (fputc(0xC0 | runcount, f) == EOF) {
|
|
|
|
free(buff);
|
|
|
|
fclose(f);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (fputc(runchar, f) == EOF) {
|
|
|
|
free(buff);
|
|
|
|
fclose(f);
|
|
|
|
return false;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
} while (y != h);
|
|
|
|
|
2004-11-15 19:25:59 +00:00
|
|
|
free(buff);
|
|
|
|
|
2009-02-09 02:57:15 +00:00
|
|
|
/* write 8-bit colour palette */
|
2004-11-15 19:25:59 +00:00
|
|
|
if (fputc(12, f) == EOF) {
|
|
|
|
fclose(f);
|
|
|
|
return false;
|
|
|
|
}
|
2005-06-30 05:27:32 +00:00
|
|
|
|
2008-06-18 21:19:04 +00:00
|
|
|
/* Palette is word-aligned, copy it to a temporary byte array */
|
|
|
|
byte tmp[256 * 3];
|
2006-06-28 05:47:55 +00:00
|
|
|
|
2008-06-18 21:19:04 +00:00
|
|
|
for (uint i = 0; i < 256; i++) {
|
|
|
|
tmp[i * 3 + 0] = palette[i].r;
|
|
|
|
tmp[i * 3 + 1] = palette[i].g;
|
|
|
|
tmp[i * 3 + 2] = palette[i].b;
|
2004-11-15 19:25:59 +00:00
|
|
|
}
|
2008-06-18 21:19:04 +00:00
|
|
|
success = fwrite(tmp, sizeof(tmp), 1, f) == 1;
|
2006-06-13 17:13:46 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
fclose(f);
|
|
|
|
|
2006-06-13 17:13:46 +00:00
|
|
|
return success;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-03-15 00:32:18 +00:00
|
|
|
/*************************************************
|
|
|
|
**** GENERIC SCREENSHOT CODE
|
|
|
|
*************************************************/
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
static const ScreenshotFormat _screenshot_formats[] = {
|
|
|
|
#if defined(WITH_PNG)
|
|
|
|
{"PNG", "png", &MakePNGImage},
|
|
|
|
#endif
|
2009-11-14 15:31:06 +00:00
|
|
|
{"BMP", "bmp", &MakeBMPImage},
|
2004-08-09 17:04:08 +00:00
|
|
|
{"PCX", "pcx", &MakePCXImage},
|
|
|
|
};
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void InitializeScreenshotFormats()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2004-11-15 19:25:59 +00:00
|
|
|
int i, j;
|
|
|
|
for (i = 0, j = 0; i != lengthof(_screenshot_formats); i++)
|
|
|
|
if (!strcmp(_screenshot_format_name, _screenshot_formats[i].extension)) {
|
|
|
|
j = i;
|
|
|
|
break;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
_cur_screenshot_format = j;
|
|
|
|
_num_screenshot_formats = lengthof(_screenshot_formats);
|
2009-11-01 18:08:58 +00:00
|
|
|
_screenshot_type = SC_NONE;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *GetScreenshotFormatDesc(int i)
|
|
|
|
{
|
|
|
|
return _screenshot_formats[i].name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetScreenshotFormat(int i)
|
|
|
|
{
|
|
|
|
_cur_screenshot_format = i;
|
2009-04-10 20:37:05 +00:00
|
|
|
strecpy(_screenshot_format_name, _screenshot_formats[i].extension, lastof(_screenshot_format_name));
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-04-04 01:35:16 +00:00
|
|
|
/* screenshot generator that dumps the current video buffer */
|
2007-06-12 20:24:12 +00:00
|
|
|
static void CurrentScreenCallback(void *userdata, void *buf, uint y, uint pitch, uint n)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-06-17 20:30:28 +00:00
|
|
|
Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
|
|
|
|
void *src = blitter->MoveTo(_screen.dst_ptr, 0, y);
|
2007-06-21 12:36:46 +00:00
|
|
|
blitter->CopyImageToBuffer(src, buf, _screen.width, n, pitch);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-01-11 17:12:41 +00:00
|
|
|
/** generate a large piece of the world
|
|
|
|
* @param userdata Viewport area to draw
|
|
|
|
* @param buf Videobuffer with same bitdepth as current blitter
|
|
|
|
* @param y First line to render
|
|
|
|
* @param pitch Pitch of the videobuffer
|
|
|
|
* @param n Number of lines to render
|
|
|
|
*/
|
2007-06-12 20:24:12 +00:00
|
|
|
static void LargeWorldCallback(void *userdata, void *buf, uint y, uint pitch, uint n)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-09-02 20:34:33 +00:00
|
|
|
ViewPort *vp = (ViewPort *)userdata;
|
2004-08-09 17:04:08 +00:00
|
|
|
DrawPixelInfo dpi, *old_dpi;
|
|
|
|
int wx, left;
|
|
|
|
|
2008-01-11 17:12:41 +00:00
|
|
|
/* We are no longer rendering to the screen */
|
|
|
|
DrawPixelInfo old_screen = _screen;
|
|
|
|
bool old_disable_anim = _screen_disable_anim;
|
|
|
|
|
|
|
|
_screen.dst_ptr = buf;
|
|
|
|
_screen.width = pitch;
|
|
|
|
_screen.height = n;
|
|
|
|
_screen.pitch = pitch;
|
|
|
|
_screen_disable_anim = true;
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
old_dpi = _cur_dpi;
|
|
|
|
_cur_dpi = &dpi;
|
|
|
|
|
|
|
|
dpi.dst_ptr = buf;
|
|
|
|
dpi.height = n;
|
|
|
|
dpi.width = vp->width;
|
|
|
|
dpi.pitch = pitch;
|
2007-05-15 14:08:39 +00:00
|
|
|
dpi.zoom = ZOOM_LVL_WORLD_SCREENSHOT;
|
2004-08-09 17:04:08 +00:00
|
|
|
dpi.left = 0;
|
|
|
|
dpi.top = y;
|
|
|
|
|
2008-01-11 17:12:41 +00:00
|
|
|
/* Render viewport in blocks of 1600 pixels width */
|
2004-08-09 17:04:08 +00:00
|
|
|
left = 0;
|
|
|
|
while (vp->width - left != 0) {
|
|
|
|
wx = min(vp->width - left, 1600);
|
|
|
|
left += wx;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
|
|
|
ViewportDoDraw(vp,
|
2007-05-15 16:08:46 +00:00
|
|
|
ScaleByZoom(left - wx - vp->left, vp->zoom) + vp->virtual_left,
|
|
|
|
ScaleByZoom(y - vp->top, vp->zoom) + vp->virtual_top,
|
|
|
|
ScaleByZoom(left - vp->left, vp->zoom) + vp->virtual_left,
|
|
|
|
ScaleByZoom((y + n) - vp->top, vp->zoom) + vp->virtual_top
|
2004-08-09 17:04:08 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
_cur_dpi = old_dpi;
|
2008-01-11 17:12:41 +00:00
|
|
|
|
|
|
|
/* Switch back to rendering to the screen */
|
|
|
|
_screen = old_screen;
|
|
|
|
_screen_disable_anim = old_disable_anim;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2009-11-14 15:28:57 +00:00
|
|
|
static const char *MakeScreenshotName(const char *ext)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2009-11-14 15:50:38 +00:00
|
|
|
bool generate = StrEmpty(_screenshot_name);
|
|
|
|
|
|
|
|
if (generate) {
|
2009-11-01 18:15:35 +00:00
|
|
|
if (_game_mode == GM_EDITOR || _game_mode == GM_MENU || _local_company == COMPANY_SPECTATOR) {
|
|
|
|
strecpy(_screenshot_name, "screenshot", lastof(_screenshot_name));
|
|
|
|
} else {
|
|
|
|
GenerateDefaultSaveName(_screenshot_name, lastof(_screenshot_name));
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-06-17 15:48:57 +00:00
|
|
|
/* Add extension to screenshot file */
|
2009-11-01 18:15:35 +00:00
|
|
|
size_t len = strlen(_screenshot_name);
|
2007-06-17 15:48:57 +00:00
|
|
|
snprintf(&_screenshot_name[len], lengthof(_screenshot_name) - len, ".%s", ext);
|
|
|
|
|
2009-11-01 18:17:01 +00:00
|
|
|
static char filename[MAX_PATH];
|
2009-11-01 18:15:35 +00:00
|
|
|
for (uint serial = 1;; serial++) {
|
2009-11-01 17:49:23 +00:00
|
|
|
if (snprintf(filename, lengthof(filename), "%s%s", _personal_dir, _screenshot_name) >= (int)lengthof(filename)) {
|
|
|
|
/* We need more characters than MAX_PATH -> end with error */
|
|
|
|
filename[0] = '\0';
|
|
|
|
break;
|
|
|
|
}
|
2009-11-14 15:50:38 +00:00
|
|
|
if (!generate) break; // allow overwriting of non-automatic filenames
|
2007-06-17 15:48:57 +00:00
|
|
|
if (!FileExists(filename)) break;
|
|
|
|
/* If file exists try another one with same name, but just with a higher index */
|
2009-11-01 18:15:35 +00:00
|
|
|
snprintf(&_screenshot_name[len], lengthof(_screenshot_name) - len, "#%u.%s", serial, ext);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
2009-11-01 18:15:35 +00:00
|
|
|
void RequestScreenshot(ScreenshotType t, const char *name)
|
2006-07-28 21:51:00 +00:00
|
|
|
{
|
2009-11-01 18:08:58 +00:00
|
|
|
_screenshot_type = t;
|
2009-11-01 18:15:35 +00:00
|
|
|
_screenshot_name[0] = '\0';
|
|
|
|
if (name != NULL) strecpy(_screenshot_name, name, lastof(_screenshot_name));
|
2006-07-28 21:51:00 +00:00
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
bool IsScreenshotRequested()
|
2006-07-28 21:51:00 +00:00
|
|
|
{
|
2009-11-01 18:08:58 +00:00
|
|
|
return (_screenshot_type != SC_NONE);
|
2006-07-28 21:51:00 +00:00
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static bool MakeSmallScreenshot()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
const ScreenshotFormat *sf = _screenshot_formats + _cur_screenshot_format;
|
2007-06-12 20:24:12 +00:00
|
|
|
return sf->proc(MakeScreenshotName(sf->extension), CurrentScreenCallback, NULL, _screen.width, _screen.height, BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth(), _cur_palette);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static bool MakeWorldScreenshot()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
ViewPort vp;
|
|
|
|
const ScreenshotFormat *sf;
|
|
|
|
|
2007-05-15 14:08:39 +00:00
|
|
|
vp.zoom = ZOOM_LVL_WORLD_SCREENSHOT;
|
2004-08-09 17:04:08 +00:00
|
|
|
vp.left = 0;
|
|
|
|
vp.top = 0;
|
2006-07-28 21:51:00 +00:00
|
|
|
vp.virtual_left = -(int)MapMaxX() * TILE_PIXELS;
|
|
|
|
vp.virtual_top = 0;
|
|
|
|
vp.virtual_width = (MapMaxX() + MapMaxY()) * TILE_PIXELS;
|
|
|
|
vp.width = vp.virtual_width;
|
|
|
|
vp.virtual_height = (MapMaxX() + MapMaxY()) * TILE_PIXELS >> 1;
|
|
|
|
vp.height = vp.virtual_height;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
sf = _screenshot_formats + _cur_screenshot_format;
|
2007-06-12 20:24:12 +00:00
|
|
|
return sf->proc(MakeScreenshotName(sf->extension), LargeWorldCallback, &vp, vp.width, vp.height, BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth(), _cur_palette);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2006-07-28 21:51:00 +00:00
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
bool MakeScreenshot()
|
2006-07-28 21:51:00 +00:00
|
|
|
{
|
2009-11-01 18:08:58 +00:00
|
|
|
switch (_screenshot_type) {
|
2006-07-28 21:51:00 +00:00
|
|
|
case SC_VIEWPORT:
|
|
|
|
UndrawMouseCursor();
|
2006-12-25 01:40:33 +00:00
|
|
|
DrawDirtyBlocks();
|
2009-11-01 18:08:58 +00:00
|
|
|
_screenshot_type = SC_NONE;
|
2006-07-28 21:51:00 +00:00
|
|
|
return MakeSmallScreenshot();
|
|
|
|
case SC_WORLD:
|
2009-11-01 18:08:58 +00:00
|
|
|
_screenshot_type = SC_NONE;
|
2006-07-28 21:51:00 +00:00
|
|
|
return MakeWorldScreenshot();
|
|
|
|
default: return false;
|
|
|
|
}
|
|
|
|
}
|