mirror of
https://github.com/libsdl-org/SDL.git
synced 2026-11-30 16:48:41 +01:00
Added example for OpenFile dialog
This commit is contained in:
committed by
Ryan C. Gordon
parent
42390c9617
commit
9935a329b7
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>{66F45BA5-3C6E-473E-AE30-4E2A16C45985}</ProjectGuid>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(SolutionDir)\examples\Examples.props" />
|
||||
<ItemGroup>
|
||||
<None Include="$(SolutionDir)\..\examples\misc\04-dialog\README.txt" />
|
||||
<ClCompile Include="$(SolutionDir)\..\examples\misc\04-dialog\dialog.c" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
</Project>
|
||||
@@ -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)
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
This example code shows usage of file dialog.
|
||||
@@ -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 <SDL3/SDL.h>
|
||||
#include <SDL3/SDL_iostream.h>
|
||||
#include <SDL3/SDL_main.h>
|
||||
#include <SDL3/SDL_test.h>
|
||||
|
||||
/* 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. */
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 3.3 KiB |
Reference in New Issue
Block a user