Skip to content

Instantly share code, notes, and snippets.

@aqiank
Last active March 9, 2019 15:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aqiank/efa6eefcc81157982acc to your computer and use it in GitHub Desktop.
Save aqiank/efa6eefcc81157982acc to your computer and use it in GitHub Desktop.
SDL2 DropEvent minimal example
#include <SDL2/SDL.h>
int main() {
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window *window = SDL_CreateWindow("Drag-and-Drop", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_SHOWN);
int running = 1;
while (running) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_DROPFILE:
printf("Dropped file: %s\n", event.drop.file);
break;
case SDL_QUIT:
running = 0;
break;
}
}
}
SDL_DestroyWindow(window);
SDL_Quit();
}
@lgblgblgb
Copy link

Got this by random, and I would like to note, that you should free event.drop.file (after using it for something, here just printf() it), otherwise you have some memory leak every time you have a file drop event. If I am correct here ... But hmm, I guess this is more like a demonstration where it's not so much an issue :)

@20kdc
Copy link

20kdc commented Oct 15, 2018

fun fact: if said free requirement is correct, the majority of SDL2 applications have in-built memory leaks regarding dropped files
(EDIT: Note: I'm agreeing. The API implies this is most likely the case. It's quite horrifying.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment