From fbebd6527b9abfdd897abf93a48311d977711a3b Mon Sep 17 00:00:00 2001 From: zsilencer Date: Sun, 7 Jun 2015 12:27:13 -0600 Subject: [PATCH] fix whizzed screenshots --- src/interface/screenshot.c | 27 ++++++++++++++++++++++++--- src/platform/shared.c | 21 ++++++++++++++------- 2 files changed, 38 insertions(+), 10 deletions(-) diff --git a/src/interface/screenshot.c b/src/interface/screenshot.c index d401ce35b1..9ca6da7881 100644 --- a/src/interface/screenshot.c +++ b/src/interface/screenshot.c @@ -236,7 +236,7 @@ int screenshot_dump_png() { rct_drawpixelinfo *dpi = RCT2_ADDRESS(RCT2_ADDRESS_SCREEN_DPI, rct_drawpixelinfo); - int i, index, width, height, stride; + int i, index, width, height, padding; char path[MAX_PATH] = ""; unsigned int error; unsigned char r, g, b, a = 255; @@ -256,7 +256,8 @@ int screenshot_dump_png() // Get image size width = RCT2_GLOBAL(RCT2_ADDRESS_SCREEN_WIDTH, uint16); height = RCT2_GLOBAL(RCT2_ADDRESS_SCREEN_HEIGHT, uint16); - stride = (width + 3) & ~3; + + padding = dpi->pitch; for (i = 0; i < 256; i++) { b = RCT2_ADDRESS(0x01424680, uint8)[i * 4 + 0]; @@ -266,7 +267,24 @@ int screenshot_dump_png() lodepng_palette_add(&state.info_raw, r, g, b, a); } - error = lodepng_encode(&png, &pngSize, dpi->bits, width, height, &state); + uint8* pixels = dpi->bits; + + if (padding > 0) { + pixels = malloc(width * height); + if (!pixels) { + return -1; + } + uint8* src = dpi->bits; + uint8* dst = pixels; + for (int y = height; y > 0; y--) { + for (int x = width; x > 0; x--) { + *dst++ = *src++; + } + src += padding; + } + } + + error = lodepng_encode(&png, &pngSize, pixels, width, height, &state); if (error) { log_error("Unable to save screenshot, %u: %s", lodepng_error_text(error)); index = -1; @@ -275,6 +293,9 @@ int screenshot_dump_png() } free(png); + if (pixels != dpi->bits) { + free(pixels); + } return index; } diff --git a/src/platform/shared.c b/src/platform/shared.c index aafed93a35..3dfe15195d 100644 --- a/src/platform/shared.c +++ b/src/platform/shared.c @@ -183,17 +183,24 @@ void platform_draw() int pitch; if (SDL_LockTexture(gBufferTexture, NULL, &pixels, &pitch) == 0) { uint8 *src = (uint8*)_screenBuffer; + int padding = pitch - (width * 4); if (pitch == width * 4) { uint32 *dst = pixels; - for (int i = width * height; i > 0; i--) *(dst++) = *(uint32 *)(&gPaletteHWMapped[*src++]); + for (int i = width * height; i > 0; i--) { *dst++ = *(uint32 *)(&gPaletteHWMapped[*src++]); } } else - if (pitch == (((width * 2) + 3) & ~3)) { + if (pitch == (width * 2) + padding) { uint16 *dst = pixels; - for (int i = width * height; i > 0; i--) *(dst++) = *(uint16 *)(&gPaletteHWMapped[*src++]); + for (int y = height; y > 0; y++) { + for (int x = width; x > 0; x--) { *dst++ = *(uint16 *)(&gPaletteHWMapped[*src++]); } + dst = (uint16*)(((uint8 *)dst) + padding); + } } else - if (pitch == (((width) + 3) & ~3)) { + if (pitch == width + padding) { uint8 *dst = pixels; - for (int i = width * height; i > 0; i--) *(dst++) = *(uint8 *)(&gPaletteHWMapped[*src++]); + for (int y = height; y > 0; y++) { + for (int x = width; x > 0; x--) { *dst++ = *(uint8 *)(&gPaletteHWMapped[*src++]); } + dst += padding; + } } SDL_UnlockTexture(gBufferTexture); } @@ -797,8 +804,8 @@ static void platform_refresh_screenbuffer(int width, int height, int pitch) dst += pitch; } } - if (newScreenBufferSize - _screenBufferSize > 0) - memset((uint8*)newScreenBuffer + _screenBufferSize, 0, newScreenBufferSize - _screenBufferSize); + //if (newScreenBufferSize - _screenBufferSize > 0) + // memset((uint8*)newScreenBuffer + _screenBufferSize, 0, newScreenBufferSize - _screenBufferSize); free(_screenBuffer); }