testclipboard: load any image format supported by SDL

This commit is contained in:
Sam Lantinga
2025-12-04 08:04:16 -08:00
parent 09304831f6
commit 03c932b9a7
+22 -31
View File
@@ -128,41 +128,32 @@ static float PrintPrimarySelectionText(float x, float y)
static float PrintClipboardImage(float x, float y, const char *mime_type) static float PrintClipboardImage(float x, float y, const char *mime_type)
{ {
/* We don't actually need to read this data each frame, but this is a simple example */ /* We don't actually need to read this data each frame, but this is a simple example */
bool isBMP = (SDL_strcmp(mime_type, "image/bmp") == 0); size_t size;
bool isPNG = (SDL_strcmp(mime_type, "image/png") == 0); void *data = SDL_GetClipboardData(mime_type, &size);
if (isBMP || isPNG) { if (data) {
size_t size; float w = 0.0f, h = 0.0f;
void *data = SDL_GetClipboardData(mime_type, &size); bool rendered = false;
if (data) { SDL_IOStream *stream = SDL_IOFromConstMem(data, size);
float w = 0.0f, h = 0.0f; if (stream) {
bool rendered = false; SDL_Surface *surface = SDL_LoadSurface_IO(stream, false);
SDL_IOStream *stream = SDL_IOFromConstMem(data, size); if (surface) {
if (stream) { SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_Surface *surface; if (texture) {
if (isBMP) { SDL_GetTextureSize(texture, &w, &h);
surface = SDL_LoadBMP_IO(stream, false);
} else {
surface = SDL_LoadPNG_IO(stream, false);
}
if (surface) {
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
if (texture) {
SDL_GetTextureSize(texture, &w, &h);
SDL_FRect dst = { x, y, w, h }; SDL_FRect dst = { x, y, w, h };
rendered = SDL_RenderTexture(renderer, texture, NULL, &dst); rendered = SDL_RenderTexture(renderer, texture, NULL, &dst);
SDL_DestroyTexture(texture); SDL_DestroyTexture(texture);
}
SDL_DestroySurface(surface);
} }
SDL_CloseIO(stream); SDL_DestroySurface(surface);
} }
if (!rendered) { SDL_CloseIO(stream);
SDL_RenderDebugText(renderer, x, y, SDL_GetError());
}
SDL_free(data);
return h + 2.0f;
} }
if (!rendered) {
SDL_RenderDebugText(renderer, x, y, SDL_GetError());
}
SDL_free(data);
return h + 2.0f;
} }
return 0.0f; return 0.0f;
} }