locale.c 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
  7. #include <SDL3/SDL.h>
  8. #include <SDL3/SDL_main.h>
  9. /* We will use this renderer to draw into this window every frame. */
  10. static SDL_Window *window = NULL;
  11. static SDL_Renderer *renderer = NULL;
  12. /* This function runs once at startup. */
  13. SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
  14. {
  15. SDL_SetAppMetadata("Example Misc Locale", "1.0", "com.example.misc-locale");
  16. if (!SDL_Init(SDL_INIT_VIDEO)) {
  17. SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
  18. return SDL_APP_FAILURE;
  19. }
  20. if (!SDL_CreateWindowAndRenderer("examples/misc/locale", 640, 480, SDL_WINDOW_RESIZABLE, &window, &renderer)) {
  21. SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
  22. return SDL_APP_FAILURE;
  23. }
  24. SDL_SetRenderLogicalPresentation(renderer, 640, 480, SDL_LOGICAL_PRESENTATION_LETTERBOX);
  25. return SDL_APP_CONTINUE; /* carry on with the program! */
  26. }
  27. /* This function runs when a new event (mouse input, keypresses, etc) occurs. */
  28. SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
  29. {
  30. if (event->type == SDL_EVENT_QUIT) {
  31. return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
  32. }
  33. return SDL_APP_CONTINUE; /* carry on with the program! */
  34. }
  35. /* This function runs once per frame, and is the heart of the program. */
  36. SDL_AppResult SDL_AppIterate(void *appstate)
  37. {
  38. const SDL_FRect frame = { 0, 0, 640, 480 };
  39. SDL_Locale **locales;
  40. char msg[128];
  41. int count, i;
  42. float x, y;
  43. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  44. SDL_RenderClear(renderer);
  45. locales = SDL_GetPreferredLocales(&count);
  46. if (!locales) {
  47. x = frame.x + ((frame.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(msg))) / 2.0f);
  48. y = frame.y;
  49. SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  50. SDL_RenderDebugText(renderer, x, y, msg);
  51. } else {
  52. SDL_snprintf(msg, sizeof (msg), "Locales, in order of preference (%d total):", count);
  53. x = frame.x + ((frame.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(msg))) / 2.0f);
  54. y = frame.y;
  55. SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  56. SDL_RenderDebugText(renderer, x, y, msg);
  57. for (i = 0; locales[i]; ++i) {
  58. const SDL_Locale *l = locales[i];
  59. const char *c = l->country;
  60. SDL_snprintf(msg, sizeof (msg), " - %s%s%s", l->language, c ? "_" : "", c ? c : "");
  61. x = frame.x + ((frame.w - (SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(msg))) / 2.0f);
  62. y = frame.y + ((SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * 2) * (i + 1));
  63. SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  64. SDL_RenderDebugText(renderer, x, y, msg);
  65. }
  66. SDL_free(locales);
  67. }
  68. /* put the new rendering on the screen. */
  69. SDL_RenderPresent(renderer);
  70. return SDL_APP_CONTINUE; /* carry on with the program! */
  71. }
  72. /* This function runs once at shutdown. */
  73. void SDL_AppQuit(void *appstate, SDL_AppResult result)
  74. {
  75. /* SDL will clean up the window/renderer for us. */
  76. }