Skip to content

Instantly share code, notes, and snippets.

@igormorgado
Created July 10, 2020 05:07
Show Gist options
  • Save igormorgado/caefc8610e07b4dfd9f0e2209527fd4a to your computer and use it in GitHub Desktop.
Save igormorgado/caefc8610e07b4dfd9f0e2209527fd4a to your computer and use it in GitHub Desktop.
void dump_texture_to_file(SDL_Renderer *renderer, SDL_Texture *texture, const char *filename)
{
SDL_Log("%s: ENTERING\n", __func__);
Uint32 format;
int access;
int width;
int height;
SDL_QueryTexture(texture, &format, &access, &width, &height);
SDL_Log("%s: Texture info: Format: %s - Access: %d - Width: %d - Height: %d\n",
__func__, SDL_GetPixelFormatName(format), access, width, height);
Uint32 *pixels = malloc(width * height * sizeof(*pixels));
SDL_RenderReadPixels(renderer, NULL, format, pixels, width);
SDL_Surface *surface = SDL_CreateRGBSurfaceWithFormatFrom(pixels, width, height, 32, width, format);
if(!surface)
SDL_Log("%s: Could not create surface %s\n", __func__, SDL_GetError());
SDL_Log("%s: Saving texture to file %s\n", __func__, filename);
SDL_SaveBMP(surface, filename);
free(pixels);
SDL_FreeSurface(surface);
SDL_Log("%s: LEAVING\n", __func__);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment