From 116604d341cca612723e5831d75ba838e6ce4fd6 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Thu, 30 Jan 2025 19:02:02 -0800
Subject: [PATCH] Added support for dragging and dropping files onto showimage
---
examples/showimage.c | 63 +++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 62 insertions(+), 1 deletion(-)
diff --git a/examples/showimage.c b/examples/showimage.c
index 1ff98bfc..b156c18f 100644
--- a/examples/showimage.c
+++ b/examples/showimage.c
@@ -67,12 +67,14 @@ int main(int argc, char *argv[])
(void)argc;
+#if 0 /* We now allow drag and drop onto the window */
/* Check command line usage */
- if ( ! argv[1] ) {
+ if ( !argv[1] ) {
SDL_Log("Usage: %s [-fullscreen] [-tonemap X] [-save file.png] <image_file> ...\n", argv[0]);
result = 1;
goto done;
}
+#endif
flags = SDL_WINDOW_HIDDEN;
for ( i=1; argv[i]; ++i ) {
@@ -250,8 +252,67 @@ int main(int argc, char *argv[])
SDL_Delay(100);
}
SDL_DestroyTexture(texture);
+ texture = NULL;
}
+ /* Show the window if needed */
+ SDL_SetWindowTitle(window, "showimage");
+ w = 640.0f;
+ h = 480.0f;
+ SDL_SetWindowSize(window, (int)w, (int)h);
+ SDL_ShowWindow(window);
+
+ done = quit;
+ while ( !done ) {
+ while ( SDL_PollEvent(&event) ) {
+ switch (event.type) {
+ case SDL_EVENT_DROP_FILE:
+ {
+ const char *file = event.drop.data;
+
+ SDL_DestroyTexture(texture);
+
+ SDL_Log("Loading %s\n", file);
+ texture = IMG_LoadTexture(renderer, file);
+ if (!texture) {
+ SDL_Log("Couldn't load %s: %s\n", file, SDL_GetError());
+ break;
+ }
+ SDL_SetWindowTitle(window, file);
+ SDL_GetTextureSize(texture, &w, &h);
+ SDL_SetWindowSize(window, (int)w, (int)h);
+ }
+ break;
+ case SDL_EVENT_KEY_UP:
+ switch (event.key.key) {
+ case SDLK_ESCAPE:
+ case SDLK_Q:
+ done = 1;
+ break;
+ }
+ break;
+ case SDL_EVENT_MOUSE_BUTTON_DOWN:
+ done = 1;
+ break;
+ case SDL_EVENT_QUIT:
+ done = 1;
+ break;
+ default:
+ break;
+ }
+ }
+
+ /* Draw a background pattern in case the image has transparency */
+ draw_background(renderer, (int)w, (int)h);
+
+ /* Display the image */
+ SDL_RenderTexture(renderer, texture, NULL, NULL);
+ SDL_RenderPresent(renderer);
+
+ SDL_Delay(100);
+ }
+ SDL_DestroyTexture(texture);
+
/* We're done! */
done:
SDL_Quit();