Topic: [PATCH] Faster screenshots and without alpha channel
Hi,
when you have a large number of screenshots, saving a new one takes so long that you get disconnected if you do it in game. This is because the game checks for existing files beginning from #1 up to #1000. I changed it to name the screenshot according to the current date and time.
Also, since 0.5.1, the screenshots contained an alpha channel. This is not really useful, since it looks strange when you view it with certain programs. I changed it back to 24-bit.
Here are the new functions in file engine\client\ec_gfx.c:
void gfx_swap()
{
if(do_screenshot)
{
/* generate filename */
time_t rawtime;
struct tm *timeinfo;
char filename[128];
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(filename, sizeof(filename), "screenshots/%Y-%m-%d_%H-%M-%S.png", timeinfo);
printf("%s\n", filename);
gfx_screenshot_direct(filename);
do_screenshot = 0;
}
{
static PERFORMACE_INFO pscope = {"glfwSwapBuffers", 0};
perf_start(&pscope);
SDL_GL_SwapBuffers();
perf_end();
}
if(render_enable && config.gfx_finish)
glFinish();
}
void gfx_screenshot_direct(const char *filename)
{
/* fetch image data */
int y;
int w = screen_width;
int h = screen_height;
unsigned char *pixel_data = (unsigned char *)mem_alloc(w*(h+1)*3, 1);
unsigned char *temp_row = pixel_data+w*h*3;
glReadPixels(0,0, w, h, GL_RGB, GL_UNSIGNED_BYTE, pixel_data);
/* flip the pixel because opengl works from bottom left corner */
for(y = 0; y < h/2; y++)
{
mem_copy(temp_row, pixel_data+y*w*3, w*3);
mem_copy(pixel_data+y*w*3, pixel_data+(h-y-1)*w*3, w*3);
mem_copy(pixel_data+(h-y-1)*w*3, temp_row,w*3);
}
/* find filename */
{
char wholepath[1024];
png_t png;
engine_savepath(filename, wholepath, sizeof(wholepath));
/* save png */
dbg_msg("client", "saved screenshot to '%s'", wholepath);
png_open_file_write(&png, wholepath);
png_set_data(&png, w, h, 8, PNG_TRUECOLOR, (unsigned char *)pixel_data);
png_close_file(&png);
}
/* clean up */
mem_free(pixel_data);
}