void TCOD_sys_write_png(const SDL_Surface *surf, const char *filename) { unsigned char *image, *dest=(unsigned char *)malloc(surf->h*surf->w*3*sizeof(char)); int x,y; unsigned char *buf; size_t size; int error; /* SDL uses 32bits format without alpha layer for screen. convert it to 24 bits */ image=dest; for (y=0; y< surf->h; y++ ) { for (x=0; x < surf->w; x++ ) { Uint8 *pixel=(Uint8 *)(surf->pixels) + y * surf->pitch + x * surf->format->BytesPerPixel; *dest++=*((pixel)+surf->format->Rshift/8); *dest++=*((pixel)+surf->format->Gshift/8); *dest++=*((pixel)+surf->format->Bshift/8); } } error=lodepng_encode_memory(&buf,&size,image,surf->w,surf->h,LCT_RGB,8); free(image); if ( ! error ) { TCOD_sys_write_file(filename,buf,size); free(buf); } else { printf("error %u: %s\n", error, lodepng_error_text(error)); } }
bool TCODSystem::writeFile(const char *filename, unsigned char *buf, uint32 size) { return TCOD_sys_write_file(filename,buf,size) != 0; }