From 15ceaffcb576c04f184cc89c9d419f2c28ecd949 Mon Sep 17 00:00:00 2001 From: Cameron Cawley Date: Wed, 14 Jan 2026 20:03:20 +0000 Subject: [PATCH] examples: Added misc/03-locale --- .../examples/misc/03-locale/03-locale.vcxproj | 13 +++ examples/CMakeLists.txt | 1 + examples/misc/03-locale/README.txt | 1 + examples/misc/03-locale/locale.c | 94 +++++++++++++++++++ 4 files changed, 109 insertions(+) create mode 100644 VisualC/examples/misc/03-locale/03-locale.vcxproj create mode 100644 examples/misc/03-locale/README.txt create mode 100644 examples/misc/03-locale/locale.c diff --git a/VisualC/examples/misc/03-locale/03-locale.vcxproj b/VisualC/examples/misc/03-locale/03-locale.vcxproj new file mode 100644 index 0000000000..5565fe006d --- /dev/null +++ b/VisualC/examples/misc/03-locale/03-locale.vcxproj @@ -0,0 +1,13 @@ + + + + {6381F9D3-BA5F-4E5C-80FF-9013964777EE} + + + + + + + + + \ No newline at end of file diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 195545536d..ca88933009 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -156,6 +156,7 @@ add_sdl_example_executable(pen-drawing-lines SOURCES pen/01-drawing-lines/drawin add_sdl_example_executable(asyncio-load-bitmaps SOURCES asyncio/01-load-bitmaps/load-bitmaps.c DATAFILES ${CMAKE_CURRENT_SOURCE_DIR}/../test/sample.png ${CMAKE_CURRENT_SOURCE_DIR}/../test/gamepad_front.png ${CMAKE_CURRENT_SOURCE_DIR}/../test/speaker.png ${CMAKE_CURRENT_SOURCE_DIR}/../test/icon2x.png) 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(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/03-locale/README.txt b/examples/misc/03-locale/README.txt new file mode 100644 index 0000000000..3222fa0d66 --- /dev/null +++ b/examples/misc/03-locale/README.txt @@ -0,0 +1 @@ +This example code reports the currently selected locales. diff --git a/examples/misc/03-locale/locale.c b/examples/misc/03-locale/locale.c new file mode 100644 index 0000000000..1445297d8c --- /dev/null +++ b/examples/misc/03-locale/locale.c @@ -0,0 +1,94 @@ +/* + * This example code reports the currently selected locales. + * + * 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 + +/* We will use this renderer to draw into this window every frame. */ +static SDL_Window *window = NULL; +static SDL_Renderer *renderer = NULL; + +/* This function runs once at startup. */ +SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) +{ + SDL_SetAppMetadata("Example Misc Locale", "1.0", "com.example.misc-locale"); + + if (!SDL_Init(SDL_INIT_VIDEO)) { + SDL_Log("Couldn't initialize SDL: %s", SDL_GetError()); + return SDL_APP_FAILURE; + } + + if (!SDL_CreateWindowAndRenderer("examples/misc/locale", 640, 480, SDL_WINDOW_RESIZABLE, &window, &renderer)) { + SDL_Log("Couldn't create window/renderer: %s", SDL_GetError()); + return SDL_APP_FAILURE; + } + SDL_SetRenderLogicalPresentation(renderer, 640, 480, SDL_LOGICAL_PRESENTATION_LETTERBOX); + + 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. */ + } + 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) +{ + const SDL_FRect frame = { 0, 0, 640, 480 }; + SDL_Locale **locales; + char msg[128]; + int count, i; + float x, y; + + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); + SDL_RenderClear(renderer); + + locales = SDL_GetPreferredLocales(&count); + if (!locales) { + x = frame.x + ((frame.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(msg))) / 2.0f); + y = frame.y; + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + SDL_RenderDebugText(renderer, x, y, msg); + } else { + SDL_snprintf(msg, sizeof (msg), "Locales, in order of preference (%d total):", count); + + x = frame.x + ((frame.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(msg))) / 2.0f); + y = frame.y; + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + SDL_RenderDebugText(renderer, x, y, msg); + + for (i = 0; locales[i]; ++i) { + const SDL_Locale *l = locales[i]; + const char *c = l->country; + + SDL_snprintf(msg, sizeof (msg), " - %s%s%s", l->language, c ? "_" : "", c ? c : ""); + + x = frame.x + ((frame.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(msg))) / 2.0f); + y = frame.y + ((SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 2) * (i + 1)); + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + SDL_RenderDebugText(renderer, x, y, msg); + } + SDL_free(locales); + } + + /* put the new rendering on the screen. */ + 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 will clean up the window/renderer for us. */ +} +