(svn r631) Merge r440, r444, r485, r630 to trunk:

r440
Move screenshot function declarations to new file screenshot.h
Clean up screenshot.c a bit, mostly whitespace, alloca() -> malloc() and checking return values
r485
Remove unused field from struct ScreenshotFormat
This commit is contained in:
tron 2004-11-15 19:25:59 +00:00
parent fb4b5fa841
commit 450c669eb2
8 changed files with 111 additions and 53 deletions

View File

@ -257,17 +257,12 @@ void ttd_strlcpy(char *dst, const char *src, size_t len);
// callback from drivers that is called if the game size changes dynamically // callback from drivers that is called if the game size changes dynamically
void GameSizeChanged(); void GameSizeChanged();
bool MakeScreenshot();
bool MakeWorldScreenshot(int left, int top, int width, int height, int zoom);
bool FileExists(const char *filename); bool FileExists(const char *filename);
bool ReadLanguagePack(int index); bool ReadLanguagePack(int index);
void InitializeLanguagePacks(); void InitializeLanguagePacks();
byte *ReadFileToMem(const char *filename, size_t *lenp, size_t maxsize); byte *ReadFileToMem(const char *filename, size_t *lenp, size_t maxsize);
int GetLanguageList(char **languages, int max); int GetLanguageList(char **languages, int max);
const char *GetScreenshotFormatDesc(int i);
void InitializeScreenshotFormats();
void SetScreenshotFormat(int i);
void CheckSwitchToEuro(); void CheckSwitchToEuro();
void LoadFromConfig(); void LoadFromConfig();

View File

@ -4,6 +4,7 @@
#include "viewport.h" #include "viewport.h"
#include "player.h" #include "player.h"
#include "gui.h" #include "gui.h"
#include "screenshot.h"
// called by the ScreenShot proc to generate screenshot lines. // called by the ScreenShot proc to generate screenshot lines.
typedef void ScreenshotCallback(void *userdata, byte *buf, uint y, uint pitch, uint n); typedef void ScreenshotCallback(void *userdata, byte *buf, uint y, uint pitch, uint n);
@ -13,7 +14,6 @@ typedef struct {
const char *name; const char *name;
const char *extension; const char *extension;
ScreenshotHandlerProc *proc; ScreenshotHandlerProc *proc;
byte id;
} ScreenshotFormat; } ScreenshotFormat;
//************************************************ //************************************************
@ -89,7 +89,7 @@ static bool MakeBmpImage(const char *name, ScreenshotCallback *callb, void *user
bih.clrimp = 0; bih.clrimp = 0;
// convert the palette to the windows format // convert the palette to the windows format
for(i=0; i!=256; i++) { for (i = 0; i != 256; i++) {
rq[i].red = *palette++; rq[i].red = *palette++;
rq[i].green = *palette++; rq[i].green = *palette++;
rq[i].blue = *palette++; rq[i].blue = *palette++;
@ -97,15 +97,19 @@ static bool MakeBmpImage(const char *name, ScreenshotCallback *callb, void *user
} }
// write file header and info header and palette // write file header and info header and palette
fwrite(&bfh, 1, sizeof(bfh), f); if (fwrite(&bfh, sizeof(bfh), 1, f) != 1) return false;
fwrite(&bih, 1, sizeof(bih), f); if (fwrite(&bih, sizeof(bih), 1, f) != 1) return false;
fwrite(rq, 1, sizeof(rq), f); if (fwrite(rq, sizeof(rq), 1, f) != 1) return false;
// use by default 64k temp memory // use by default 64k temp memory
maxlines = clamp(65536 / padw, 16, 128); maxlines = clamp(65536 / padw, 16, 128);
// now generate the bitmap bits // now generate the bitmap bits
buff = (byte*)alloca(padw * maxlines); // by default generate 128 lines at a time. buff = malloc(padw * maxlines); // by default generate 128 lines at a time.
if (buff == NULL) {
fclose(f);
return false;
}
memset(buff, 0, padw * maxlines); // zero the buffer to have the padding bytes set to 0 memset(buff, 0, padw * maxlines); // zero the buffer to have the padding bytes set to 0
// start at the bottom, since bitmaps are stored bottom up. // start at the bottom, since bitmaps are stored bottom up.
@ -119,9 +123,14 @@ static bool MakeBmpImage(const char *name, ScreenshotCallback *callb, void *user
// write each line // write each line
while (n) while (n)
fwrite(buff + (--n) * padw, 1, padw, f); if (fwrite(buff + (--n) * padw, padw, 1, f) != 1) {
} while (h); free(buff);
fclose(f);
return false;
}
} while (h != 0);
free(buff);
fclose(f); fclose(f);
return true; return true;
@ -135,7 +144,7 @@ static bool MakeBmpImage(const char *name, ScreenshotCallback *callb, void *user
static void PNGAPI png_my_error(png_structp png_ptr, png_const_charp message) static void PNGAPI png_my_error(png_structp png_ptr, png_const_charp message)
{ {
DEBUG(misc, 0) ("ERROR(libpng): %s - %s\n", message,(char *)png_get_error_ptr(png_ptr)); DEBUG(misc, 0) ("ERROR(libpng): %s - %s\n", message, (char *)png_get_error_ptr(png_ptr));
longjmp(png_ptr->jmpbuf, 1); longjmp(png_ptr->jmpbuf, 1);
} }
@ -161,15 +170,15 @@ static bool MakePNGImage(const char *name, ScreenshotCallback *callb, void *user
f = fopen(name, "wb"); f = fopen(name, "wb");
if (f == NULL) return false; if (f == NULL) return false;
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (char *) name, png_my_error, png_my_warning); png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (char *)name, png_my_error, png_my_warning);
if (!png_ptr) { if (png_ptr == NULL) {
fclose(f); fclose(f);
return false; return false;
} }
info_ptr = png_create_info_struct(png_ptr); info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) { if (info_ptr == NULL) {
png_destroy_write_struct(&png_ptr, (png_infopp)NULL); png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
fclose(f); fclose(f);
return false; return false;
@ -185,15 +194,15 @@ static bool MakePNGImage(const char *name, ScreenshotCallback *callb, void *user
png_set_filter(png_ptr, 0, PNG_FILTER_NONE); png_set_filter(png_ptr, 0, PNG_FILTER_NONE);
png_set_IHDR(png_ptr, info_ptr, w, h, pixelformat, PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE, png_set_IHDR(png_ptr, info_ptr, w, h, pixelformat, PNG_COLOR_TYPE_PALETTE,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
// convert the palette to the .PNG format. // convert the palette to the .PNG format.
{ {
// avoids "might be clobbered" warning of argument "palette" // avoids "might be clobbered" warning of argument "palette"
const byte *pal = palette; const byte *pal = palette;
for(i=0; i!=256; i++) { for (i = 0; i != 256; i++) {
rq[i].red = *pal++; rq[i].red = *pal++;
rq[i].green = *pal++; rq[i].green = *pal++;
rq[i].blue = *pal++; rq[i].blue = *pal++;
@ -208,7 +217,12 @@ static bool MakePNGImage(const char *name, ScreenshotCallback *callb, void *user
maxlines = clamp(65536 / w, 16, 128); maxlines = clamp(65536 / w, 16, 128);
// now generate the bitmap bits // now generate the bitmap bits
buff = (byte*)alloca(w * maxlines); // by default generate 128 lines at a time. buff = malloc(w * maxlines); // by default generate 128 lines at a time.
if (buff == NULL) {
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(f);
return false;
}
memset(buff, 0, w * maxlines); // zero the buffer to have the padding bytes set to 0 memset(buff, 0, w * maxlines); // zero the buffer to have the padding bytes set to 0
y = 0; y = 0;
@ -221,13 +235,14 @@ static bool MakePNGImage(const char *name, ScreenshotCallback *callb, void *user
y += n; y += n;
// write them to png // write them to png
for(i=0; i!=n; i++) for (i = 0; i != n; i++)
png_write_row(png_ptr, buff + i * w); png_write_row(png_ptr, buff + i * w);
} while (y != h); } while (y != h);
png_write_end(png_ptr, info_ptr); png_write_end(png_ptr, info_ptr);
png_destroy_write_struct(&png_ptr, &info_ptr); png_destroy_write_struct(&png_ptr, &info_ptr);
free(buff);
fclose(f); fclose(f);
return true; return true;
} }
@ -278,8 +293,8 @@ static bool MakePCXImage(const char *name, ScreenshotCallback *callb, void *user
pcx.version = 5; pcx.version = 5;
pcx.rle = 1; pcx.rle = 1;
pcx.bpp = 8; pcx.bpp = 8;
pcx.xmax = TO_LE16(w-1); pcx.xmax = TO_LE16(w - 1);
pcx.ymax = TO_LE16(h-1); pcx.ymax = TO_LE16(h - 1);
pcx.hdpi = TO_LE16(320); pcx.hdpi = TO_LE16(320);
pcx.vdpi = TO_LE16(320); pcx.vdpi = TO_LE16(320);
@ -289,53 +304,86 @@ static bool MakePCXImage(const char *name, ScreenshotCallback *callb, void *user
pcx.height = TO_LE16(h); pcx.height = TO_LE16(h);
// write pcx header // write pcx header
fwrite(&pcx, sizeof(pcx), 1, f); if (fwrite(&pcx, sizeof(pcx), 1, f) != 1) {
fclose(f);
return false;
}
// use by default 64k temp memory // use by default 64k temp memory
maxlines = clamp(65536 / w, 16, 128); maxlines = clamp(65536 / w, 16, 128);
// now generate the bitmap bits // now generate the bitmap bits
buff = (byte*)alloca(w * maxlines); // by default generate 128 lines at a time. buff = malloc(w * maxlines); // by default generate 128 lines at a time.
memset(buff, 0, w * maxlines); // zero the buffer to have the padding bytes set to 0 if (buff == NULL) {
fclose(f);
return false;
}
memset(buff, 0, w * maxlines); // zero the buffer to have the padding bytes set to 0
y = 0; y = 0;
do { do {
// determine # lines to write // determine # lines to write
uint n = min(h - y, maxlines), i; uint n = min(h - y, maxlines);
uint i;
// render the pixels into the buffer // render the pixels into the buffer
callb(userdata, buff, y, w, n); callb(userdata, buff, y, w, n);
y += n; y += n;
// write them to pcx // write them to pcx
for(i=0; i!=n; i++) { for (i = 0; i != n; i++) {
int runcount = 1; int runcount = 1;
byte *bufp = buff + i * w; byte *bufp = buff + i * w;
byte runchar = buff[0]; byte runchar = buff[0];
uint left = w - 1; uint left;
// for each pixel... // for each pixel...
while (left) { for (left = w - 1; left > 0; --left) {
byte ch = *bufp++; byte ch = *bufp++;
if (ch != runchar || runcount >= 0x3f) { if (ch != runchar || runcount >= 0x3f) {
if (runcount > 1 || (runchar & 0xC0) == 0xC0) fputc(0xC0 | runcount, f); if (runcount > 1 || (runchar & 0xC0) == 0xC0)
fputc(runchar,f); if (fputc(0xC0 | runcount, f) == EOF) {
free(buff);
fclose(f);
return false;
}
if (fputc(runchar, f) == EOF) {
free(buff);
fclose(f);
return false;
}
runcount = 0; runcount = 0;
runchar = ch; runchar = ch;
} }
runcount++; runcount++;
left--;
} }
// write remaining bytes.. // write remaining bytes..
if (runcount > 1 || (runchar & 0xC0) == 0xC0) fputc(0xC0 | runcount, f); if (runcount > 1 || (runchar & 0xC0) == 0xC0)
fputc(runchar,f); if (fputc(0xC0 | runcount, f) == EOF) {
free(buff);
fclose(f);
return false;
}
if (fputc(runchar, f) == EOF) {
free(buff);
fclose(f);
return false;
}
} }
} while (y != h); } while (y != h);
free(buff);
// write 8-bit color palette // write 8-bit color palette
fputc(12, f); if (fputc(12, f) == EOF) {
fwrite(palette, 256*3, 1, f); fclose(f);
return false;
}
if (fwrite(palette, 256 * 3, 1, f) != 1) {
fclose(f);
return false;
}
fclose(f); fclose(f);
return true; return true;
@ -353,11 +401,14 @@ static const ScreenshotFormat _screenshot_formats[] = {
{"PCX", "pcx", &MakePCXImage}, {"PCX", "pcx", &MakePCXImage},
}; };
void InitializeScreenshotFormats() void InitializeScreenshotFormats(void)
{ {
int i,j; int i, j;
for (i=0,j=0; i!=lengthof(_screenshot_formats); i++) for (i = 0, j = 0; i != lengthof(_screenshot_formats); i++)
if (!strcmp(_screenshot_format_name, _screenshot_formats[i].extension)) { j=i; break; } if (!strcmp(_screenshot_format_name, _screenshot_formats[i].extension)) {
j = i;
break;
}
_cur_screenshot_format = j; _cur_screenshot_format = j;
_num_screenshot_formats = lengthof(_screenshot_formats); _num_screenshot_formats = lengthof(_screenshot_formats);
} }
@ -376,16 +427,13 @@ void SetScreenshotFormat(int i)
// screenshot generator that dumps the current video buffer // screenshot generator that dumps the current video buffer
static void CurrentScreenCallback(void *userdata, byte *buf, uint y, uint pitch, uint n) static void CurrentScreenCallback(void *userdata, byte *buf, uint y, uint pitch, uint n)
{ {
for (; n > 0; --n) for (; n > 0; --n) {
{
memcpy(buf, _screen.dst_ptr + y * _screen.pitch, _screen.width); memcpy(buf, _screen.dst_ptr + y * _screen.pitch, _screen.width);
++y; ++y;
buf += pitch; buf += pitch;
} }
} }
extern void ViewportDoDraw(ViewPort *vp, int left, int top, int right, int bottom);
// generate a large piece of the world // generate a large piece of the world
static void LargeWorldCallback(void *userdata, byte *buf, uint y, uint pitch, uint n) static void LargeWorldCallback(void *userdata, byte *buf, uint y, uint pitch, uint n)
{ {
@ -413,7 +461,7 @@ static void LargeWorldCallback(void *userdata, byte *buf, uint y, uint pitch, ui
((left - wx - vp->left) << vp->zoom) + vp->virtual_left, ((left - wx - vp->left) << vp->zoom) + vp->virtual_left,
((y - vp->top) << vp->zoom) + vp->virtual_top, ((y - vp->top) << vp->zoom) + vp->virtual_top,
((left - vp->left) << vp->zoom) + vp->virtual_left, ((left - vp->left) << vp->zoom) + vp->virtual_left,
(((y+n) - vp->top) << vp->zoom) + vp->virtual_top (((y + n) - vp->top) << vp->zoom) + vp->virtual_top
); );
} }
@ -440,7 +488,7 @@ static char *MakeScreenshotName(const char *ext)
base[0] = '.'; strcpy(base + 1, ext); base[0] = '.'; strcpy(base + 1, ext);
serial = 0; serial = 0;
for(;;) { for (;;) {
snprintf(filename, sizeof(filename), "%s%s", _path.personal_dir, _screenshot_name); snprintf(filename, sizeof(filename), "%s%s", _path.personal_dir, _screenshot_name);
if (!FileExists(filename)) if (!FileExists(filename))
break; break;
@ -450,9 +498,7 @@ static char *MakeScreenshotName(const char *ext)
return filename; return filename;
} }
extern byte _cur_palette[768]; bool MakeScreenshot(void)
bool MakeScreenshot()
{ {
const ScreenshotFormat *sf = _screenshot_formats + _cur_screenshot_format; const ScreenshotFormat *sf = _screenshot_formats + _cur_screenshot_format;
return sf->proc(MakeScreenshotName(sf->extension), CurrentScreenCallback, NULL, _screen.width, _screen.height, 8, _cur_palette); return sf->proc(MakeScreenshotName(sf->extension), CurrentScreenCallback, NULL, _screen.width, _screen.height, 8, _cur_palette);

12
screenshot.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef SCREENSHOT_H
#define SCREENSHOT_H
void InitializeScreenshotFormats(void);
const char *GetScreenshotFormatDesc(int i);
void SetScreenshotFormat(int i);
bool MakeScreenshot(void);
bool MakeWorldScreenshot(int left, int top, int width, int height, int zoom);
#endif

View File

@ -6,6 +6,7 @@
#include "gfx.h" #include "gfx.h"
#include "command.h" #include "command.h"
#include "engine.h" #include "engine.h"
#include "screenshot.h"
static uint32 _difficulty_click_a; static uint32 _difficulty_click_a;
static uint32 _difficulty_click_b; static uint32 _difficulty_click_b;

View File

@ -4,6 +4,7 @@
#include "town.h" #include "town.h"
#include "vehicle.h" #include "vehicle.h"
#include "news.h" #include "news.h"
#include "screenshot.h"
#define USE_TABLE(x) { assert(index < lengthof(x)); str = x[index]; break; } #define USE_TABLE(x) { assert(index < lengthof(x)); str = x[index]; break; }

1
ttd.c
View File

@ -22,6 +22,7 @@
#include "saveload.h" #include "saveload.h"
#include "ai.h" #include "ai.h"
#include "console.h" #include "console.h"
#include "screenshot.h"
#include <stdarg.h> #include <stdarg.h>

View File

@ -1132,7 +1132,7 @@ static void ViewportDrawStrings(DrawPixelInfo *dpi, StringSpriteToDraw *ss)
_cur_dpi = dpi; _cur_dpi = dpi;
} }
void ViewportDoDraw(ViewPort *vp, int left, int top, int right, int bottom) void ViewportDoDraw(const ViewPort *vp, int left, int top, int right, int bottom)
{ {
ViewportDrawer vd; ViewportDrawer vd;
int mask; int mask;

View File

@ -116,4 +116,6 @@ VARDEF Point _tile_fract_coords;
extern TileHighlightData * const _thd_ptr; extern TileHighlightData * const _thd_ptr;
#endif void ViewportDoDraw(const ViewPort *vp, int left, int top, int right, int bottom);
#endif /* VIEWPORT_H */