Skip to content

Commit

Permalink
Support PNG screenshots. PNG and ZLIB are an optional dependency, and…
Browse files Browse the repository at this point in the history
… can be enabled with USE_PNG=1 in cmake.
  • Loading branch information
katajakasa committed Jan 23, 2014
1 parent c2c31ee commit c228c6c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
14 changes: 12 additions & 2 deletions src/engine.c
Expand Up @@ -238,8 +238,18 @@ void engine_run(int net_mode) {
if(take_screenshot) {
image img;
video_screenshot(&img);
sprintf(screenshot_filename, "screenshot_%u.tga", SDL_GetTicks());
image_write_tga(&img, screenshot_filename);
int scr_ret = 0;
if(image_supports_png()) {
sprintf(screenshot_filename, "screenshot_%u.png", SDL_GetTicks());
scr_ret = image_write_png(&img, screenshot_filename);
} else {
sprintf(screenshot_filename, "screenshot_%u.tga", SDL_GetTicks());
scr_ret= image_write_tga(&img, screenshot_filename);
}
if(scr_ret) {
PERROR("Screenshot write operation failed (%s)", screenshot_filename);
}

image_free(&img);
take_screenshot = 0;
}
Expand Down
14 changes: 2 additions & 12 deletions src/video/image.c
Expand Up @@ -165,12 +165,7 @@ int image_write_png(image *img, const char *filename) {
// Get row pointers
char *rows[img->h];
for(int y = 0; y < img->h; y++) {
rows[y] = malloc(img->w * 3);
for(int x = 0; x < img->w; x++) {
rows[y][x * 3 + 0] = img->data[y * img->w + x * 4 + 0];
rows[y][x * 3 + 1] = img->data[y * img->w + x * 4 + 1];
rows[y][x * 3 + 2] = img->data[y * img->w + x * 4 + 2];
}
rows[y] = img->data + (y * img->w * 4);
}

// Init
Expand All @@ -186,7 +181,7 @@ int image_write_png(image *img, const char *filename) {
img->w,
img->h,
8,
PNG_COLOR_TYPE_RGB,
PNG_COLOR_TYPE_RGBA,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE,
PNG_FILTER_TYPE_BASE);
Expand All @@ -200,11 +195,6 @@ int image_write_png(image *img, const char *filename) {
setjmp(png_jmpbuf(png_ptr));
png_write_end(png_ptr, NULL);

// Free memory
for(int i = 0; i < img->h; i++) {
free(rows[i]);
}

// Free file
fclose(fp);
return 0; // Success
Expand Down

0 comments on commit c228c6c

Please sign in to comment.