testutils.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. Copyright 1997-2022 Sam Lantinga
  3. Copyright 2022 Collabora Ltd.
  4. SPDX-License-Identifier: Zlib
  5. */
  6. #include "testutils.h"
  7. #if defined(SDL_FILESYSTEM_OS2) || defined(SDL_FILESYSTEM_WINDOWS)
  8. static const char pathsep[] = "\\";
  9. #elif defined(SDL_FILESYSTEM_RISCOS)
  10. static const char pathsep[] = ".";
  11. #else
  12. static const char pathsep[] = "/";
  13. #endif
  14. /*
  15. * Return the absolute path to def in the SDL_GetBasePath() if possible, or
  16. * the relative path to def on platforms that don't have a working
  17. * SDL_GetBasePath(). Free the result with SDL_free.
  18. *
  19. * Fails and returns NULL if out of memory.
  20. */
  21. char *
  22. GetNearbyFilename(const char *file)
  23. {
  24. char *base;
  25. char *path;
  26. base = SDL_GetBasePath();
  27. if (base != NULL) {
  28. size_t len = SDL_strlen(base) + SDL_strlen(pathsep) + SDL_strlen(file) + 1;
  29. path = SDL_malloc(len);
  30. if (path == NULL) {
  31. SDL_OutOfMemory();
  32. return NULL;
  33. }
  34. SDL_snprintf(path, len, "%s%s%s", base, pathsep, file);
  35. }
  36. if (base) {
  37. SDL_free(base);
  38. }
  39. return path;
  40. }
  41. /*
  42. * If user_specified is non-NULL, return a copy of it. Free with SDL_free.
  43. *
  44. * Otherwise, return the absolute path to def in the SDL_GetBasePath() if
  45. * possible, or the relative path to def on platforms that don't have a
  46. * working SDL_GetBasePath(). Free the result with SDL_free.
  47. *
  48. * Fails and returns NULL if out of memory.
  49. */
  50. char *
  51. GetResourceFilename(const char *user_specified, const char *def)
  52. {
  53. if (user_specified != NULL) {
  54. char *ret = SDL_strdup(user_specified);
  55. if (ret == NULL) {
  56. SDL_OutOfMemory();
  57. }
  58. return ret;
  59. } else {
  60. return GetNearbyFilename(def);
  61. }
  62. }
  63. /*
  64. * Load the .bmp file whose name is file, from the SDL_GetBasePath() if
  65. * possible or the current working directory if not.
  66. *
  67. * If transparent is true, set the transparent colour from the top left pixel.
  68. *
  69. * If width_out is non-NULL, set it to the texture width.
  70. *
  71. * If height_out is non-NULL, set it to the texture height.
  72. */
  73. SDL_Texture *
  74. LoadTexture(SDL_Renderer *renderer, const char *file, SDL_bool transparent,
  75. int *width_out, int *height_out)
  76. {
  77. SDL_Surface *temp = NULL;
  78. SDL_Texture *texture = NULL;
  79. char *path;
  80. path = GetNearbyFilename(file);
  81. if (path != NULL) {
  82. file = path;
  83. }
  84. temp = SDL_LoadBMP(file);
  85. if (temp == NULL) {
  86. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
  87. } else {
  88. /* Set transparent pixel as the pixel at (0,0) */
  89. if (transparent) {
  90. if (temp->format->palette) {
  91. SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *)temp->pixels);
  92. } else {
  93. switch (temp->format->BitsPerPixel) {
  94. case 15:
  95. SDL_SetColorKey(temp, SDL_TRUE,
  96. (*(Uint16 *) temp->pixels) & 0x00007FFF);
  97. break;
  98. case 16:
  99. SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
  100. break;
  101. case 24:
  102. SDL_SetColorKey(temp, SDL_TRUE,
  103. (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
  104. break;
  105. case 32:
  106. SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
  107. break;
  108. }
  109. }
  110. }
  111. if (width_out != NULL) {
  112. *width_out = temp->w;
  113. }
  114. if (height_out != NULL) {
  115. *height_out = temp->h;
  116. }
  117. texture = SDL_CreateTextureFromSurface(renderer, temp);
  118. if (!texture) {
  119. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
  120. }
  121. }
  122. if (temp) {
  123. SDL_FreeSurface(temp);
  124. }
  125. if (path) {
  126. SDL_free(path);
  127. }
  128. return texture;
  129. }