1 (edited by William Tee Riker 2009-10-18 09:58:17)

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);
}

2

Re: [PATCH] Faster screenshots and without alpha channel

Nice! Send it on the maillist (http://www.freelists.org/list/teeworlds) so it can be added to future teeworlds versions, much more useful this way.

Official Teeworlds map developer and community moderator
Administrator for the Teeworlds community Teesites

3

Re: [PATCH] Faster screenshots and without alpha channel

OK, I did that.

4

Re: [PATCH] Faster screenshots and without alpha channel

@William Tee Riker
A small note:
- you should include <time.h> on top
- remove the printf-message

Remember the 80s - good times smile

5

Re: [PATCH] Faster screenshots and without alpha channel

Yes, that was just for debugging, I noticed it myself wink
And I added the #include line.