testutils.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. SDL_RWops *rw;
  29. size_t len = SDL_strlen(base) + SDL_strlen(pathsep) + SDL_strlen(file) + 1;
  30. path = SDL_malloc(len);
  31. if (path == NULL) {
  32. SDL_free(base);
  33. SDL_OutOfMemory();
  34. return NULL;
  35. }
  36. SDL_snprintf(path, len, "%s%s%s", base, pathsep, file);
  37. SDL_free(base);
  38. rw = SDL_RWFromFile(path, "rb");
  39. if (rw) {
  40. SDL_RWclose(rw);
  41. return path;
  42. }
  43. /* Couldn't find the file in the base path */
  44. SDL_free(path);
  45. }
  46. path = SDL_strdup(file);
  47. if (path == NULL) {
  48. SDL_OutOfMemory();
  49. }
  50. return path;
  51. }
  52. /*
  53. * If user_specified is non-NULL, return a copy of it. Free with SDL_free.
  54. *
  55. * Otherwise, return the absolute path to def in the SDL_GetBasePath() if
  56. * possible, or the relative path to def on platforms that don't have a
  57. * working SDL_GetBasePath(). Free the result with SDL_free.
  58. *
  59. * Fails and returns NULL if out of memory.
  60. */
  61. char *
  62. GetResourceFilename(const char *user_specified, const char *def)
  63. {
  64. if (user_specified != NULL) {
  65. char *ret = SDL_strdup(user_specified);
  66. if (ret == NULL) {
  67. SDL_OutOfMemory();
  68. }
  69. return ret;
  70. } else {
  71. return GetNearbyFilename(def);
  72. }
  73. }
  74. /*
  75. * Load the .bmp file whose name is file, from the SDL_GetBasePath() if
  76. * possible or the current working directory if not.
  77. *
  78. * If transparent is true, set the transparent colour from the top left pixel.
  79. *
  80. * If width_out is non-NULL, set it to the texture width.
  81. *
  82. * If height_out is non-NULL, set it to the texture height.
  83. */
  84. SDL_Texture *
  85. LoadTexture(SDL_Renderer *renderer, const char *file, SDL_bool transparent,
  86. int *width_out, int *height_out)
  87. {
  88. SDL_Surface *temp = NULL;
  89. SDL_Texture *texture = NULL;
  90. char *path;
  91. path = GetNearbyFilename(file);
  92. if (path != NULL) {
  93. file = path;
  94. }
  95. temp = SDL_LoadBMP(file);
  96. if (temp == NULL) {
  97. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
  98. } else {
  99. /* Set transparent pixel as the pixel at (0,0) */
  100. if (transparent) {
  101. if (temp->format->palette) {
  102. SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *)temp->pixels);
  103. } else {
  104. switch (temp->format->BitsPerPixel) {
  105. case 15:
  106. SDL_SetColorKey(temp, SDL_TRUE,
  107. (*(Uint16 *) temp->pixels) & 0x00007FFF);
  108. break;
  109. case 16:
  110. SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
  111. break;
  112. case 24:
  113. SDL_SetColorKey(temp, SDL_TRUE,
  114. (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
  115. break;
  116. case 32:
  117. SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
  118. break;
  119. }
  120. }
  121. }
  122. if (width_out != NULL) {
  123. *width_out = temp->w;
  124. }
  125. if (height_out != NULL) {
  126. *height_out = temp->h;
  127. }
  128. texture = SDL_CreateTextureFromSurface(renderer, temp);
  129. if (!texture) {
  130. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
  131. }
  132. }
  133. if (temp) {
  134. SDL_FreeSurface(temp);
  135. }
  136. if (path) {
  137. SDL_free(path);
  138. }
  139. return texture;
  140. }