// example // loads a PNG file an into SDL surface, and then writes it back to a PNG file. int main(int argc, char **argv) { SDL_Surface *input_surf; char *input, *output, *str_ptr; int namelen; /* Parsing shell parameters */ if (argc == 3) { input = argv[1]; output = argv[2]; } else if (argc == 2) { input = argv[1]; str_ptr = strstr(argv[1], "."); if (str_ptr == NULL) namelen = strlen(argv[1]); else namelen = str_ptr - argv[1]; output = (char *)malloc(namelen + 5); strncpy(output, argv[1], namelen); strcat(output, ".png"); } else { printf("The correct syntax is: %s input [output]\n", argv[0]); exit(-1); } /* Initializing video subsystem */ if (SDL_Init(SDL_INIT_VIDEO) < 0) { printf("SDL_Init error: %s\n", SDL_GetError()); exit (-1); } /* Calling SDL_Quit at exit */ atexit(SDL_Quit); printf("input file: %s\n", input); printf("output file: %s\n", output); /* Opening input and output files */ input_surf = IMG_Load(input); if (input_surf == NULL) { printf("IMG_Load error: %s\n", IMG_GetError()); exit(-1); } if(png_save_surface(output, input_surf) < 0); exit(-1); /* Quitting */ if (argc == 2) free(output); SDL_Quit(); return 0; }
void SaveScreenshot() { char Filename[52]; SDL_Rect viewport; SDL_Surface* surface; Screenshot::GenerateName( Filename ); if( Filename[0] == '\0' ){ stat("Can not get screenshot name. Too many screenshots in folder"); return; } SDL_RenderGetViewport(renderer, &viewport); surface = SDL_CreateRGBSurface(0, viewport.w, viewport.h, 24, #if SDL_BYTEORDER == SDL_BIG_ENDIAN 0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff #else 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 #endif ); if (!surface) { stat("Couldn't create surface: %s\n", SDL_GetError()); return; } if ( SDL_RenderReadPixels(renderer, NULL, surface->format->format, surface->pixels, surface->pitch) < 0 ) { stat("Couldn't read screen: %s\n", SDL_GetError()); return; } if( png_save_surface(Filename, surface) < 0 ){ SDL_FreeSurface( surface ); return; } SDL_FreeSurface( surface ); return; }