Atlas - locale.c

Home / ext / SDL / examples / misc / 03-locale Lines: 1 | Size: 3322 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 * This example code reports the currently selected locales. 3 * 4 * This code is public domain. Feel free to use it for any purpose! 5 */ 6 7#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */ 8#include <SDL3/SDL.h> 9#include <SDL3/SDL_main.h> 10 11/* We will use this renderer to draw into this window every frame. */ 12static SDL_Window *window = NULL; 13static SDL_Renderer *renderer = NULL; 14 15/* This function runs once at startup. */ 16SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) 17{ 18 SDL_SetAppMetadata("Example Misc Locale", "1.0", "com.example.misc-locale"); 19 20 if (!SDL_Init(SDL_INIT_VIDEO)) { 21 SDL_Log("Couldn't initialize SDL: %s", SDL_GetError()); 22 return SDL_APP_FAILURE; 23 } 24 25 if (!SDL_CreateWindowAndRenderer("examples/misc/locale", 640, 480, SDL_WINDOW_RESIZABLE, &window, &renderer)) { 26 SDL_Log("Couldn't create window/renderer: %s", SDL_GetError()); 27 return SDL_APP_FAILURE; 28 } 29 SDL_SetRenderLogicalPresentation(renderer, 640, 480, SDL_LOGICAL_PRESENTATION_LETTERBOX); 30 31 return SDL_APP_CONTINUE; /* carry on with the program! */ 32} 33 34/* This function runs when a new event (mouse input, keypresses, etc) occurs. */ 35SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) 36{ 37 if (event->type == SDL_EVENT_QUIT) { 38 return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */ 39 } 40 return SDL_APP_CONTINUE; /* carry on with the program! */ 41} 42 43/* This function runs once per frame, and is the heart of the program. */ 44SDL_AppResult SDL_AppIterate(void *appstate) 45{ 46 const SDL_FRect frame = { 0, 0, 640, 480 }; 47 SDL_Locale **locales; 48 char msg[128]; 49 int count, i; 50 float x, y; 51 52 SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); 53 SDL_RenderClear(renderer); 54 55 locales = SDL_GetPreferredLocales(&count); 56 if (!locales) { 57 x = frame.x + ((frame.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(msg))) / 2.0f); 58 y = frame.y; 59 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); 60 SDL_RenderDebugText(renderer, x, y, msg); 61 } else { 62 SDL_snprintf(msg, sizeof (msg), "Locales, in order of preference (%d total):", count); 63 64 x = frame.x + ((frame.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(msg))) / 2.0f); 65 y = frame.y; 66 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); 67 SDL_RenderDebugText(renderer, x, y, msg); 68 69 for (i = 0; locales[i]; ++i) { 70 const SDL_Locale *l = locales[i]; 71 const char *c = l->country; 72 73 SDL_snprintf(msg, sizeof (msg), " - %s%s%s", l->language, c ? "_" : "", c ? c : ""); 74 75 x = frame.x + ((frame.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(msg))) / 2.0f); 76 y = frame.y + ((SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 2) * (i + 1)); 77 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); 78 SDL_RenderDebugText(renderer, x, y, msg); 79 } 80 SDL_free(locales); 81 } 82 83 /* put the new rendering on the screen. */ 84 SDL_RenderPresent(renderer); 85 86 return SDL_APP_CONTINUE; /* carry on with the program! */ 87} 88 89/* This function runs once at shutdown. */ 90void SDL_AppQuit(void *appstate, SDL_AppResult result) 91{ 92 /* SDL will clean up the window/renderer for us. */ 93} 94 95
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.