diff --git a/VisualC/examples/misc/04-dialog/04-dialog.vcxproj b/VisualC/examples/misc/04-dialog/04-dialog.vcxproj new file mode 100644 index 0000000000..0b8bf98c6b --- /dev/null +++ b/VisualC/examples/misc/04-dialog/04-dialog.vcxproj @@ -0,0 +1,12 @@ + + + + {66F45BA5-3C6E-473E-AE30-4E2A16C45985} + + + + + + + + \ No newline at end of file diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index b6de25f126..682367197b 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -160,6 +160,7 @@ add_sdl_example_executable(storage-user SOURCES storage/01-user/user.c) add_sdl_example_executable(misc-power SOURCES misc/01-power/power.c) add_sdl_example_executable(misc-clipboard SOURCES misc/02-clipboard/clipboard.c) add_sdl_example_executable(misc-locale SOURCES misc/03-locale/locale.c) +add_sdl_example_executable(misc-dialog SOURCES misc/04-dialog/dialog.c) add_sdl_example_executable(demo-snake SOURCES demo/01-snake/snake.c) add_sdl_example_executable(demo-woodeneye-008 SOURCES demo/02-woodeneye-008/woodeneye-008.c) add_sdl_example_executable(demo-infinite-monkeys SOURCES demo/03-infinite-monkeys/infinite-monkeys.c) diff --git a/examples/misc/04-dialog/README.txt b/examples/misc/04-dialog/README.txt new file mode 100644 index 0000000000..8306661d83 --- /dev/null +++ b/examples/misc/04-dialog/README.txt @@ -0,0 +1 @@ +This example code shows usage of file dialog. diff --git a/examples/misc/04-dialog/dialog.c b/examples/misc/04-dialog/dialog.c new file mode 100644 index 0000000000..2402c553cf --- /dev/null +++ b/examples/misc/04-dialog/dialog.c @@ -0,0 +1,133 @@ +/* + * This example code shows usage of file dialog. + * + * This code is public domain. Feel free to use it for any purpose! + */ + +#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */ +#include +#include +#include +#include + +/* We will use this renderer to draw into this window every frame. */ +static SDL_Window *window = NULL; +static SDL_Renderer *renderer = NULL; +static SDL_Texture *texture = NULL; +static const char *initial_path = NULL; + +static const SDL_FRect OPEN_FILE_RECT = { 50, 50, 220, 140 }; +static const SDL_Rect IMAGE_RECT = { 370, 290, 220, 140 }; + +static const SDL_DialogFileFilter filters[] = { + { "JPG images", "jpg;jpeg" }, + { "PNG images", "png" } +}; + +/* This function runs when files are chosen (or dialog is canceled, or error happens). */ +static void SDLCALL callback(void *userdata, const char *const *files, int filter) +{ + if (files) { + if (*files) { + SDL_Surface *surface = SDL_LoadSurface(*files); + if (surface) { + SDL_Surface *scaled = SDL_ScaleSurface(surface, IMAGE_RECT.w, IMAGE_RECT.h, SDL_SCALEMODE_LINEAR); + SDL_DestroySurface(surface); + if (scaled) { + SDL_Surface *converted = SDL_ConvertSurface(scaled, SDL_PIXELFORMAT_RGBA8888); + SDL_DestroySurface(scaled); + if (converted) { + SDL_UpdateTexture(texture, NULL, converted->pixels, converted->pitch); + SDL_DestroySurface(converted); + } + } + } + } + + while (*files) { + SDL_Log("'%s'", *files); + files++; + } + } else { + SDL_Log("Error: %s", SDL_GetError()); + } +} + +/* This function runs once at startup. */ +SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) +{ + if (!SDL_Init(SDL_INIT_VIDEO)) { + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't initialize SDL!", SDL_GetError(), NULL); + return SDL_APP_FAILURE; + } + + if (!SDL_CreateWindowAndRenderer("examples/asyncio/load-bitmaps", 640, 480, SDL_WINDOW_RESIZABLE, &window, &renderer)) { + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create window/renderer!", SDL_GetError(), NULL); + return SDL_APP_FAILURE; + } + SDL_SetRenderLogicalPresentation(renderer, 640, 480, SDL_LOGICAL_PRESENTATION_LETTERBOX); + + initial_path = SDL_GetUserFolder(SDL_FOLDER_HOME); + if (!initial_path) { + SDL_Log("Will not use an initial path, couldn't get the home directory path: %s", SDL_GetError()); + } + + texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STATIC, IMAGE_RECT.w, IMAGE_RECT.h); + if (!texture) { + SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create texture!", SDL_GetError(), NULL); + return SDL_APP_FAILURE; + } else { + static const Uint32 blank[220 * 140]; + const SDL_Rect rect = { 0, 0, IMAGE_RECT.w, IMAGE_RECT.h }; + SDL_UpdateTexture(texture, &rect, blank, IMAGE_RECT.w * sizeof(Uint32)); + } + + return SDL_APP_CONTINUE; /* carry on with the program! */ +} + +/* This function runs when a new event (mouse input, keypresses, etc) occurs. */ +SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) +{ + if (event->type == SDL_EVENT_QUIT) { + return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */ + } + + if (event->type == SDL_EVENT_MOUSE_BUTTON_UP) { + const SDL_FPoint p = { event->button.x, event->button.y }; + SDL_Window *w = SDL_GetWindowFromID(event->button.windowID); + if (SDL_PointInRectFloat(&p, &OPEN_FILE_RECT)) { + SDL_ShowOpenFileDialog(callback, NULL, w, filters, SDL_arraysize(filters), initial_path, 1); + } + } + + return SDL_APP_CONTINUE; /* carry on with the program! */ +} + +/* This function runs once per frame, and is the heart of the program. */ +SDL_AppResult SDL_AppIterate(void *appstate) +{ + SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE); + SDL_RenderClear(renderer); + + SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE); + SDL_RenderFillRect(renderer, &OPEN_FILE_RECT); + + SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE); + SDL_RenderDebugText(renderer, OPEN_FILE_RECT.x + 5, OPEN_FILE_RECT.y + OPEN_FILE_RECT.h / 2, "Open File..."); + + SDL_FRect rect; + SDL_RectToFRect(&IMAGE_RECT, &rect); + SDL_RenderTexture(renderer, texture, NULL, &rect); + + SDL_RenderPresent(renderer); + + return SDL_APP_CONTINUE; /* carry on with the program! */ +} + +/* This function runs once at shutdown. */ +void SDL_AppQuit(void *appstate, SDL_AppResult result) +{ + SDL_DestroyTexture(texture); + + /* SDL will clean up the window/renderer for us. */ +} diff --git a/examples/misc/04-dialog/thumbnail.png b/examples/misc/04-dialog/thumbnail.png new file mode 100644 index 0000000000..bc3e6d9d76 Binary files /dev/null and b/examples/misc/04-dialog/thumbnail.png differ